public static Header HeaderParse(IByteBuffer buffer) { // start sign var startSign = buffer.ReadByte(); // task sn var taskId = buffer.ReadByte(); // msg sn var msgSn = buffer.ReadByte(); // ttl var ttl = buffer.ReadByte(); // 通信方式 // 0x81 - 以太网, 0x82 - GPRS , 0x83 - NBIOT var transportCode = buffer.ReadByte(); var transportType = (TransportType)transportCode; // 时间戳 var timestamp = buffer.ReadInt(); var date = TimestampUtil.ConvertSeconds2DateTime(timestamp).AddHours(8); // 控制命令 心跳 - 0x4064 var funCode = buffer.ReadShort(); var funType = (FunType)funCode; // src mac - 6B var srcMacBytes = new byte[6]; buffer.ReadBytes(srcMacBytes); // dst mac var dstMacBytes = new byte[6]; buffer.ReadBytes(dstMacBytes); var srcMac = BytesUtil.BytesToHex(srcMacBytes); var dstMac = BytesUtil.BytesToHex(dstMacBytes); // content length var length = buffer.ReadShort(); byte[] content = null; if (length > 0) { content = new byte[length]; buffer.ReadBytes(content); } var crc = new byte[2]; buffer.ReadBytes(crc); var header = new Header() { TaskId = taskId, MsgSn = msgSn, Ttl = ttl, TransportType = transportType, Time = date, FunType = funType, SrcMac = srcMac, DstMac = dstMac, Length = length, Content = content, crc = crc }; return(header); }
public static byte[] BuildSensorMsg(string mac, int port, SensorType sensorType, byte[] coreData) { var buffer = Unpooled.Buffer(1024); try { var settingCoreSize = 1; var detectInterval = 5; var coreSize = coreData.Length / 21; buffer.WriteByte(settingCoreSize); buffer.WriteShort(detectInterval); buffer.WriteByte(coreSize); var macBytes = BytesUtil.Hex2Bytes(mac); buffer.WriteBytes(macBytes); buffer.WriteByte(port); buffer.WriteShort((int)sensorType); buffer.WriteInt(TimestampUtil.ConvertDateTime2Seconds(DateTime.Now)); buffer.WriteBytes(coreData); var msg = new byte[buffer.ReadableBytes]; buffer.ReadBytes(msg); return(msg); } finally { buffer.Release(); } }
public static byte[] BuildMessage(int taskId, TransportType transportType, FunType funType, string mac, byte[] content) { var buffer = Unpooled.Buffer(100); try { // start sign buffer.WriteByte(LoraConst.StartSign); // task sn buffer.WriteByte(taskId); // msg sn buffer.WriteByte(SearialUtil.GetSerial(mac)); // ttl buffer.WriteByte(0); // 通信方式 // 0x81 - 以太网, 0x82 - GPRS , 0x83 - NBIOT buffer.WriteByte((int)transportType); // 时间戳 buffer.WriteInt(TimestampUtil.GetUtcSecondsStamp()); // 控制命令 心跳 - 0x4064 buffer.WriteShort((int)funType); // src mac - 6B var macBytes = BytesUtil.Hex2Bytes(mac); buffer.WriteBytes(macBytes); // dst mac buffer.WriteBytes(macBytes); // content length if (content != null) { buffer.WriteShort(content.Length); buffer.WriteBytes(content); } else { buffer.WriteShort(0); } var validatingData = new byte[buffer.ReadableBytes]; buffer.GetBytes(buffer.ReaderIndex, validatingData); // crc var crc = CRC16.calcCrc16(validatingData); buffer.WriteShort(crc); var msg = new byte[buffer.ReadableBytes]; buffer.ReadBytes(msg); return(msg); } finally { buffer.Release(); } }
public ExerciseResultVO(int score, float accuracy, int bonusScore, IList <FullRoundVO> history, int currentDifficulty) { Accuracy = accuracy; Score = score; BonusScore = bonusScore; _history = history; ReactionTimeGoodRunAverage = 0; ReactionTimeBadRunAverage = 0; if (_history != null && history.Any()) { GoodRuns = _history.Count(x => !x.RoundResult.Any(y => !y.AnswerCorrect)); BadRuns = _history.Count(x => x.RoundResult.Any(y => !y.AnswerCorrect)); CurrentDifficulty = currentDifficulty; StartDifficulty = _history[0].RoundDataVO.Difficulty; MeanDifficulty = (float)_history.Sum(x => x.RoundDataVO.Difficulty) / _history.Count; MaxDifficulty = Math.Max(_history.Max(x => x.RoundDataVO.Difficulty), currentDifficulty); MinDifficulty = Math.Min(_history.Min(x => x.RoundDataVO.Difficulty), currentDifficulty); FirstErrorDifficulty = _history.Any(x => x.RoundResult.Any(y => !y.AnswerCorrect)) ? _history.First(x => x.RoundResult.Any(y => !y.AnswerCorrect)).RoundDataVO.Difficulty : -1; ReactionTimeAverage = (int)(_history.Any(x => x.RoundResult != null) ? _history.Sum(x => x.RoundResult.Sum(y => y.ReactionTime.TotalMilliseconds)) / _history.Count : 0); if (_history.Any(x => x.RoundResult != null)) { var sum = (_history.Sum(x => x.RoundResult.Any(y => y.AnswerCorrect) ? x.RoundResult.Sum(y => y.ReactionTime.TotalMilliseconds) : 0)); if (sum > 0) { ReactionTimeGoodRunAverage = (int)(sum / _history.Count(x => x.RoundResult.Any(y => y.AnswerCorrect))); } sum = (_history.Sum(x => x.RoundResult.Any(y => !y.AnswerCorrect) ? x.RoundResult.Sum(y => y.ReactionTime.TotalMilliseconds) : 0)); if (sum > 0) { ReactionTimeBadRunAverage = (int)(sum / _history.Count(x => x.RoundResult.Any(y => !y.AnswerCorrect))); } } var builder = new StringBuilder(); foreach (var round in _history) { builder.Append(round.RoundResult.Any(y => !y.AnswerCorrect) ? "1" : "0"); } GameHistory = builder.ToString().EncryptZeroOne(); } else { GameHistory = ""; } FinishedTimestamp = TimestampUtil.NowUnixTimestamp(); }
public void Serialize(BinaryWriter writer) { writer.Write(0); writer.Write(TimestampUtil.UnixTimestamp()); writer.Write((byte)1); writer.Write(new byte[10]); writer.Write((short)1); for (int i = 0; i < 1; ++i) { writer.Write((short)1); writer.Write((short)1); writer.Write((short)1); writer.Write((short)0); writer.Write((short)4); writer.WriteString("Olvia", 62); writer.WriteString("Black Desert", 62); writer.Write((byte)0); writer.WriteStringAscii("127.0.0.1", 62); writer.Write(new byte[39]); writer.Write((short)8889); writer.Write((byte)2); writer.Write((byte)1); writer.Write((byte)1); writer.Write((byte)1); writer.Write((byte)0); writer.Write((short)0); writer.Write((long)0); writer.Write(TimestampUtil.UnixTimestamp()); writer.Write((byte)0); writer.Write((byte)0); writer.Write((short)0); writer.Write(2000000); writer.WriteString("", 62); writer.Write(new byte[] { 0x40, 0x42, 0x0F, 0x00, 0x40, 0x42, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03 }); } }