DMFactory.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Windows.Interop;
  7. using InABox.Core;
  8. using NPOI.HPSF;
  9. using PRSServer;
  10. namespace InABox.DigitalMatter
  11. {
  12. public static class DMFactory
  13. {
  14. private static readonly Dictionary<byte, string> _devices = new();
  15. private static readonly Dictionary<byte, Type> _messages = new();
  16. private static readonly Dictionary<byte, Tuple<Type, string>> _fields = new();
  17. private static readonly Dictionary<byte, Type> _bluetoothtags = new();
  18. private static DumpFormat _dumpFormat;
  19. private static string _dumpFile;
  20. public static void Initialise(DumpFormat dumpFormat, string dumpFile)
  21. {
  22. _dumpFormat = dumpFormat;
  23. _dumpFile = dumpFile;
  24. RegisterMessage<DMHelloRequest>(0x00);
  25. RegisterMessage<DMHelloResponse>(0x01);
  26. RegisterMessage<DMDataRequest>(0x04);
  27. RegisterMessage<DMConfirmRequest>(0x05);
  28. RegisterMessage<DMConfirmResponse>(0x06);
  29. RegisterField<DMGPSField>(0x00, "GPS Data");
  30. RegisterField<DMDigitalDataField>(0x02, "Digital Data");
  31. RegisterField<DMOperatorField>(0x03, "Operator Info");
  32. RegisterField<DMAnalogueDataField16>(0x06, "Analogue Data");
  33. RegisterField<DMTripDataField>(0x0F, "Trip Data");
  34. RegisterField<DMBluetoothTagList>(0x1D, "BT Tag List");
  35. RegisterField<DMBluetoothTagData>(0x1E, "BT Tag Data");
  36. // Current DMT Devices
  37. RegisterDevice(0x4B, "Bolt");
  38. RegisterDevice(0x44, "Dart2");
  39. RegisterDevice(0x4E, "Eagle");
  40. RegisterDevice(0x4A, "Falcon");
  41. RegisterDevice(0x43, "G62 Cellular");
  42. RegisterDevice(0x4F, "G120");
  43. RegisterDevice(0x4D, "Oyster2 Cellular");
  44. RegisterDevice(0x3E, "Remora2");
  45. RegisterDevice(0x49, "Yabby GPS");
  46. RegisterDevice(0x48, "Yabby WiFi Cellular");
  47. //Obsolete DMT Devices
  48. RegisterDevice(0x22, "Dart");
  49. RegisterDevice(0x1E, "Flexi1");
  50. RegisterDevice(0x11, "G52 Solar");
  51. RegisterDevice(0x17, "G60");
  52. RegisterDevice(0x1C, "G100");
  53. RegisterDevice(0x21, "Remora");
  54. RegisterDevice(0x3A, "Oyster Cellular(v1)");
  55. RegisterDevice(0x34, "Sting");
  56. RegisterBluetoothTag<DMGuppyBluetoothTag>(0x00);
  57. RegisterBluetoothTag<DMIBeaconBluetoothTag>(0x01);
  58. RegisterBluetoothTag<DMEddystoneBluetoothTag>(0x02);
  59. RegisterBluetoothTag<DMIngicsiBS01BasicBluetoothTag>(0x03);
  60. RegisterBluetoothTag<DMIngicsiBS01TemperatureBluetoothTag>(0x04);
  61. RegisterBluetoothTag<DMSensorNodeBluetoothTag>(0x05);
  62. RegisterBluetoothTag<DMEddystoneTLMBluetoothTag>(0x06);
  63. RegisterBluetoothTag<DMTechnotonES7BluetoothTag>(0x07);
  64. RegisterBluetoothTag<DMGeoboxTPMSBluetoothTag>(0x08);
  65. RegisterBluetoothTag<DMEscortFuelBluetoothTag>(0x09);
  66. RegisterBluetoothTag<DMIngicsiBS04BluetoothTag>(0x0A);
  67. RegisterBluetoothTag<DMGenericBluetoothTag>(0xFF);
  68. }
  69. public static void RegisterDevice(byte identifier, string name)
  70. {
  71. _devices[identifier] = name;
  72. }
  73. public static string GetDeviceName(byte identifier)
  74. {
  75. if (_devices.ContainsKey(identifier))
  76. return _devices[identifier];
  77. return string.Format("Unknown Device (0x{0})", BitConverter.ToString(new[] { identifier }));
  78. }
  79. public static void RegisterMessage<TMessage>(byte identifier) where TMessage : DMMessage
  80. {
  81. _messages[identifier] = typeof(TMessage);
  82. }
  83. public static DMMessage ParseMessage(byte[] data)
  84. {
  85. if (data.Length < 3)
  86. throw new Exception("Invalid Packet Length");
  87. var buffer = new DMBuffer { Buffer = data };
  88. var magicnumber = buffer.PeekBytes(0, 2);
  89. if (magicnumber[0] != 0x02 || magicnumber[1] != 0x55)
  90. throw new Exception("Invalid Header");
  91. var messagetype = buffer.PeekByte(2);
  92. if (!_messages.ContainsKey(messagetype))
  93. throw new Exception(string.Format("Invalid Message Type({0})", messagetype));
  94. var payloadlength = buffer.PeekUInt16(3);
  95. if (buffer.BufferSize - payloadlength < 5)
  96. throw new Exception(string.Format("Payload Length Mismatch - Expected: {0} Actual {1}", payloadlength, buffer.BufferSize - 5));
  97. var message = (Activator.CreateInstance(_messages[messagetype]) as DMMessage)!;
  98. try
  99. {
  100. message.Decode(data);
  101. }
  102. catch (Exception e)
  103. {
  104. Logger.Send(
  105. LogType.Error,
  106. Thread.CurrentThread.ManagedThreadId.ToString(),
  107. string.Format("Unable to Parse Message: {0} Magic={1} Type={2} Payload={3} Data=({1})", e.Message,
  108. BitConverter.ToString(buffer.PeekBytes(0, payloadlength + 3)))
  109. );
  110. }
  111. switch (_dumpFormat)
  112. {
  113. case DumpFormat.Raw:
  114. using (var stream = DumpStream())
  115. {
  116. stream.Write(data); // Write data
  117. stream.WriteByte(0x0a); // Newline
  118. }
  119. break;
  120. case DumpFormat.SemiParsed:
  121. using(var stream = DumpStream())
  122. {
  123. stream.Write(message.Dump()); // Write message dump
  124. stream.WriteByte(0x0a); // Newline
  125. }
  126. break;
  127. }
  128. return message;
  129. }
  130. public static void RegisterField<TField>(byte identifier, string name) where TField : DMField
  131. {
  132. _fields[identifier] = new Tuple<Type, string>(typeof(TField), name);
  133. }
  134. public static string GetFieldName(byte identifier)
  135. {
  136. if (_fields.ContainsKey(identifier))
  137. return _fields[identifier].Item2;
  138. return string.Format("Unknown Field (0x{0})", BitConverter.ToString(new[] { identifier }));
  139. }
  140. public static DMField ParseField(byte[] data)
  141. {
  142. if (data.Length < 2)
  143. throw new Exception("Invalid Field Length");
  144. var buffer = new DMBuffer { Buffer = data };
  145. var type = buffer.PeekByte(0);
  146. if (!_fields.ContainsKey(type))
  147. throw new Exception(string.Format("Invalid Field Type ({0})", type));
  148. var field = Activator.CreateInstance(_fields[type].Item1) as DMField;
  149. field.Decode(data);
  150. return field;
  151. }
  152. public static void RegisterBluetoothTag<TTagType>(byte identifier) where TTagType : DMBluetoothTag
  153. {
  154. _bluetoothtags[identifier] = typeof(TTagType);
  155. }
  156. public static DMBluetoothTag ParseBluetoothTag(byte type, byte[] data)
  157. {
  158. if (!_bluetoothtags.ContainsKey(type))
  159. throw new Exception(string.Format("Invalid Bluetooth Tag Type ({0})", type));
  160. var tag = Activator.CreateInstance(_bluetoothtags[type]) as DMBluetoothTag;
  161. tag.DataLength = (byte)data.Length;
  162. tag.Decode(data);
  163. return tag;
  164. }
  165. private static Stream DumpStream()
  166. {
  167. return new FileStream(_dumpFile, FileMode.Append);
  168. }
  169. private static void DumpData(byte[] data)
  170. {
  171. try
  172. {
  173. using (var stream = new FileStream(_dumpFile, FileMode.Append))
  174. {
  175. stream.Write(data);
  176. }
  177. }
  178. catch (Exception e)
  179. {
  180. Logger.Send(LogType.Error, "", $"Error while dumping data: {CoreUtils.FormatException(e)}");
  181. }
  182. }
  183. }
  184. }