Heartbeat.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Edge.Core.Parser.BinaryParser.Attributes;
  2. using Edge.Core.Parser.BinaryParser.MessageEntity;
  3. using Edge.Core.Parser.BinaryParser.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace HengShan_Pump_TQC_IFSF.MessageEntity
  10. {
  11. public enum HeartbeatDeviceStatus
  12. {
  13. EverythingIsFine,
  14. ConfigurationNeeded,
  15. ReservedForFutureUse,
  16. SoftwareRefreshRequired,
  17. }
  18. public class Heartbeat : IfsfMessageBase
  19. {
  20. public Heartbeat(byte originatorSubnet, byte originatorNode, byte ipPart0, byte ipPart1, byte ipPart2, byte ipPart3, int port)
  21. {
  22. base.OriginatorSubnet = originatorSubnet;
  23. base.OriginatorNode = originatorNode;
  24. this.MessageCode = MessageCode.IFSF_MESSAGE_CODE_HEARTBEAT;
  25. this.IpPart0 = ipPart0;
  26. this.IpPart1 = ipPart1;
  27. this.IpPart2 = ipPart2;
  28. this.IpPart3 = ipPart3;
  29. this.Port = port;
  30. }
  31. [Range(1, 254, "Ip address 1st part should between 1 to 254")]
  32. [Format(1, EncodingType.BIN, -999999)]
  33. public byte IpPart0 { get; set; }
  34. [Range(1, 254, "Ip address 2nd part should between 1 to 254")]
  35. [Format(1, EncodingType.BIN, -999998)]
  36. public byte IpPart1 { get; set; }
  37. [Range(1, 254, "Ip address 3rd part should between 1 to 254")]
  38. [Format(1, EncodingType.BIN, -999997)]
  39. public byte IpPart2 { get; set; }
  40. [Range(1, 254, "Ip address 4th part should between 1 to 254")]
  41. [Format(1, EncodingType.BIN, -999996)]
  42. public byte IpPart3 { get; set; }
  43. [Range(1, 65534, "Ip port part should between 1 to 65534")]
  44. [Format(2, EncodingType.BIN, -999995)]
  45. public int Port { get; set; }
  46. [Format(1, EncodingType.BIN, 3)]
  47. public byte RawDeviceState { get; set; }
  48. public IEnumerable<HeartbeatDeviceStatus> DeviceStates
  49. {
  50. get
  51. {
  52. var result = new List<HeartbeatDeviceStatus>();
  53. if (this.RawDeviceState == 0)
  54. result.Add(HeartbeatDeviceStatus.EverythingIsFine);
  55. if (this.RawDeviceState.GetBit(0) == 1)
  56. result.Add(HeartbeatDeviceStatus.ConfigurationNeeded);
  57. if (this.RawDeviceState.GetBit(7) == 1)
  58. result.Add(HeartbeatDeviceStatus.SoftwareRefreshRequired);
  59. return result;
  60. }
  61. set
  62. {
  63. foreach (var e in value)
  64. {
  65. if (e == HeartbeatDeviceStatus.ConfigurationNeeded)
  66. this.RawDeviceState = this.RawDeviceState.SetBit(0, 0, 1);
  67. if (e == HeartbeatDeviceStatus.SoftwareRefreshRequired)
  68. this.RawDeviceState = this.RawDeviceState.SetBit(7, 7, 1);
  69. }
  70. }
  71. }
  72. public override string ToLogString()
  73. {
  74. return base.ToLogString() + this.DeviceStates.Select(s => s.ToString()).Aggregate((acc, n) => acc + ", " + n);
  75. }
  76. }
  77. }