MessageBase.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Edge.Core.Parser.BinaryParser.Attributes;
  2. using Edge.Core.Parser.BinaryParser.MessageEntity;
  3. using System;
  4. namespace PetroChinaOnlineWatchPlugin.MessageEntity
  5. {
  6. public enum MessageCode
  7. {
  8. IFSF_MESSAGE_CODE_APPLICATION = 0,
  9. IFSF_MESSAGE_CODE_HEARTBEAT = 1,
  10. IFSF_MESSAGE_CODE_COMMUNICATION = 2
  11. }
  12. public enum MessageType
  13. {
  14. IFSF_MESSAGE_TYPE_READ = 0x00,
  15. IFSF_MESSAGE_TYPE_ANSWER = 0x20,
  16. IFSF_MESSAGE_TYPE_WRITE = 0x40,
  17. /// <summary>
  18. /// message type is an event, and need Ack.
  19. /// </summary>
  20. IFSF_MESSAGE_TYPE_UNSOLICITED_ACK = 0x60,
  21. IFSF_MESSAGE_TYPE_UNSOLICITED_WITHOUT_ACK = 0x80,
  22. IFSF_MESSAGE_TYPE_ACK = 0xE0
  23. }
  24. public abstract class MessageBase : MessageTemplateBase
  25. {
  26. [Format(1, EncodingType.BIN, -9990)]
  27. [Range(0, 255, "Recipient subnet must range in 1 to 255")]
  28. public byte RecipientSubnet { get; set; }
  29. [Format(1, EncodingType.BIN, -9980)]
  30. [Range(0, 127, "Recipient node must range in 1 to 127, but now is {0}")]
  31. public byte RecipientNode { get; set; }
  32. [Format(1, EncodingType.BIN, -9970)]
  33. [Range(0, 255, "Originator subnet must range in 1 to 255")]
  34. public byte OriginatorSubnet { get; set; }
  35. [Format(1, EncodingType.BIN, -9960)]
  36. [Range(0, 127, "Originator node must range in 1 to 127, but now is {0}")]
  37. public byte OriginatorNode { get; set; }
  38. [Format(1, EncodingType.BIN, -9950)]
  39. public MessageCode MessageCode { get; set; }
  40. /// <summary>
  41. /// 000xxxxx (bit map)
  42. /// 000 indicates the message type is READ
  43. /// xxxxx is the token created by the originator of the message
  44. /// 010xxxxx
  45. /// 010 indicates message type is WRITE
  46. /// xxxxx is the token
  47. /// 111xxxxx
  48. /// 111 indicates type ACKNOWLEDGE
  49. /// xxxxx is the token
  50. /// </summary>
  51. [Format(1, EncodingType.BIN, -9940)]
  52. public byte RawMessageStatusAndToken { get; protected set; }
  53. public MessageType MessageType
  54. {
  55. get { return (MessageType)(this.RawMessageStatusAndToken & 0xE0); }
  56. set
  57. {
  58. //1F = 0001 1111
  59. this.RawMessageStatusAndToken = (byte)((this.RawMessageStatusAndToken & 0x1F) + ((byte)value));
  60. }
  61. }
  62. public byte MessageToken
  63. {
  64. get { return (byte)(this.RawMessageStatusAndToken & 0x1F); }
  65. set
  66. {
  67. if (value > 0x1F) throw new ArgumentOutOfRangeException("Message Token max value is 0x1F");
  68. //E0 = 1110 0000
  69. this.RawMessageStatusAndToken = (byte)((this.RawMessageStatusAndToken & 0xE0) + value);
  70. }
  71. }
  72. public abstract override string ToString();
  73. }
  74. }