DMAnalogueDataField.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace InABox.DigitalMatter
  4. {
  5. public class DMAnalogueDataField16 : DMField
  6. {
  7. public DMAnalogueDataField16()
  8. {
  9. Data = new Dictionary<byte, short>();
  10. }
  11. public override byte Type => 0x06;
  12. public Dictionary<byte, short> Data { get; }
  13. public double InternalVoltage => Data.ContainsKey(1) ? (double)Data[1] / 100.0F : 0.0F;
  14. public double ExternalVoltage => Data.ContainsKey(2) ? (double)Data[2] / 100.0F : 0.0F;
  15. public double InternalTemperature => Data.ContainsKey(3) ? (double)Data[3] / 100.0F : 0.0F;
  16. public int GSMStrength => Data.ContainsKey(4) ? Data[4] : 0;
  17. public double LoadedVoltage => Data.ContainsKey(5) ? (double)Data[5] / 100.0F : 0.0F;
  18. public double? BatteryStrength => Data.ContainsKey(6) ? (double)Data[6] / 100.0F : new double?();
  19. protected override void DoDecode(IDMReadBuffer buffer)
  20. {
  21. for (var i = 0; i < Length; i += 3)
  22. Data[buffer.TakeByte()] = buffer.TakeInt16();
  23. }
  24. protected override void DoEncode(IDMWriteBuffer buffer)
  25. {
  26. foreach (var key in Data.Keys)
  27. {
  28. buffer.AddByte(key);
  29. buffer.AddInt16(Data[key]);
  30. }
  31. }
  32. public override string ToString()
  33. {
  34. return string.Join(" ", Data.Select(x => string.Format("{0:X}={1}", x.Key, x.Value)));
  35. }
  36. public override bool IsValid()
  37. {
  38. return Data.Any();
  39. }
  40. }
  41. }