DMGPSField.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections.Generic;
  2. namespace InABox.DigitalMatter
  3. {
  4. public enum GPSStatus
  5. {
  6. Valid,
  7. ThreeDimensional,
  8. NoSignal
  9. }
  10. public class DMGPSField : DMField
  11. {
  12. public override byte Type => 0x00;
  13. public uint TimeStamp { get; set; }
  14. public int Latitude { get; set; }
  15. public int Longitude { get; set; }
  16. public short Altitude { get; set; }
  17. public ushort Speed { get; set; }
  18. public byte SpeedAccuracy { get; set; }
  19. public byte Heading { get; set; }
  20. public byte PDOP { get; set; }
  21. public byte GPSAccuracy { get; set; }
  22. public byte Status { get; set; }
  23. public GPSStatus[] StatusFlags()
  24. {
  25. var result = new List<GPSStatus>();
  26. var flags = IDMBuffer.DecodeByte(Status, 1, 8);
  27. if (flags[0])
  28. result.Add(GPSStatus.Valid);
  29. if (flags[1])
  30. result.Add(GPSStatus.ThreeDimensional);
  31. if (flags[2])
  32. result.Add(GPSStatus.NoSignal);
  33. return result.ToArray();
  34. }
  35. public override bool IsValid()
  36. {
  37. return Status != 0;
  38. }
  39. public override string ToString()
  40. {
  41. return string.Format("{0:dd MMM yy hh-mm-ss} Lat: {1:F6} Lng: {2:F6} Status: {3}", TimeStampToDateTime(TimeStamp),
  42. (double)Latitude / 10000000.0F, (double)Longitude / 10000000.0F, string.Join(",", StatusFlags()));
  43. }
  44. protected override void DoDecode(IDMReadBuffer buffer)
  45. {
  46. TimeStamp = buffer.TakeUInt32();
  47. Latitude = buffer.TakeInt32();
  48. Longitude = buffer.TakeInt32();
  49. Altitude = buffer.TakeInt16();
  50. Speed = buffer.TakeByte();
  51. SpeedAccuracy = buffer.TakeByte();
  52. Heading = buffer.TakeByte();
  53. PDOP = buffer.TakeByte();
  54. GPSAccuracy = buffer.TakeByte();
  55. Status = buffer.TakeByte();
  56. }
  57. protected override void DoEncode(IDMWriteBuffer buffer)
  58. {
  59. buffer.AddUInt32(TimeStamp);
  60. buffer.AddInt32(Latitude);
  61. buffer.AddInt32(Longitude);
  62. buffer.AddInt16(Altitude);
  63. buffer.AddUInt16(Speed);
  64. buffer.AddByte(SpeedAccuracy);
  65. buffer.AddByte(Heading);
  66. buffer.AddByte(PDOP);
  67. buffer.AddByte(GPSAccuracy);
  68. buffer.AddByte(Status);
  69. }
  70. }
  71. }