DMBluetoothTagData.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. namespace InABox.DigitalMatter
  2. {
  3. public class DMBluetoothTagData : DMField
  4. {
  5. public override byte Type => 0x1E;
  6. public byte DataLength { get; set; }
  7. public byte LogReason { get; set; }
  8. public byte TagType { get; set; }
  9. public sbyte RSSI { get; set; }
  10. public uint TimeStamp { get; set; }
  11. public int Latitude { get; set; }
  12. public int Longitude { get; set; }
  13. public byte GPSAccuracy { get; set; }
  14. public DMBluetoothTag Tag { get; set; }
  15. public override bool IsValid()
  16. {
  17. return true;
  18. }
  19. protected override void DoDecode(IDMReadBuffer buffer)
  20. {
  21. var header = buffer.TakeByte();
  22. DataLength = IDMBuffer.EncodeByte(IDMBuffer.DecodeByte(header, 0, 6));
  23. LogReason = IDMBuffer.EncodeByte(IDMBuffer.DecodeByte(header, 6, 2));
  24. TagType = buffer.TakeByte();
  25. RSSI = buffer.TakeInt8();
  26. TimeStamp = buffer.TakeUInt32();
  27. Latitude = buffer.TakeInt32();
  28. Longitude = buffer.TakeInt32();
  29. GPSAccuracy = buffer.TakeByte();
  30. var data = buffer.TakeBytes(DataLength);
  31. Tag = DMFactory.ParseBluetoothTag(TagType, data);
  32. }
  33. protected override void DoEncode(IDMWriteBuffer buffer)
  34. {
  35. buffer.AddByte(TagType);
  36. buffer.AddInt8(RSSI);
  37. buffer.AddUInt32(TimeStamp);
  38. buffer.AddInt32(Latitude);
  39. buffer.AddInt32(Longitude);
  40. buffer.AddByte(GPSAccuracy);
  41. var data = Tag.EncodeArray();
  42. buffer.AddBytes(data);
  43. DataLength = (byte)data.Length;
  44. var header = IDMBuffer.EncodeByte(IDMBuffer.DecodeByte(DataLength, 0, 6));
  45. header = IDMBuffer.UpdateByte(header, 6, IDMBuffer.DecodeByte(LogReason, 0, 2));
  46. buffer.InsertByte(0, header);
  47. }
  48. public override string ToString()
  49. {
  50. return string.Format("{0} {1} {2:dd MMM yy hh-mm-ss} Lat: {3:F6} Lng: {4:F6} RSSI: {5}", Tag,
  51. LogReason == 1 ? "Found" : LogReason == 2 ? "Lost" : "Updated", TimeStampToDateTime(TimeStamp), (double)Latitude / 10000000.0F,
  52. (double)Longitude / 10000000.0F, RSSI);
  53. }
  54. }
  55. }