MessageCutterV105.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Edge.Core.Processor;
  2. using Edge.Core.IndustryStandardInterface.Pump;
  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 Edge.Core.Processor.Communicator;
  9. namespace Censtar_31064V105OrV106_Pump
  10. {
  11. public class MessageCutterV105 : IMessageCutter<byte[]>
  12. {
  13. private readonly SizableWindow<byte> window;
  14. private State nextState = State.Uninitialized;
  15. private readonly List<byte> buffer = new List<byte>();
  16. /// <summary>
  17. /// cmd:bodyLength
  18. /// </summary>
  19. private static Dictionary<byte, byte> CmdCharAndBodyLengthMappers
  20. = new Dictionary<byte, byte>() {
  21. {0,0 },
  22. {2,1 },
  23. {3,0 },
  24. {4,0},
  25. {5,0},
  26. {6,0 },
  27. {7,0 },
  28. {8,12 },
  29. {9,1 },
  30. {0x0E,3 },
  31. {0x10,1 },
  32. {0x12,0 },
  33. {0x13,0 },
  34. {0x14,0 },
  35. {0x15,3 },
  36. {0x17,0 },
  37. {0x31,0 },
  38. {0x32,0 },
  39. {0x33,0 },
  40. {0x34,4 },
  41. {0x35,4 },
  42. {0x36,20 },
  43. {0x38,22 },
  44. };
  45. public byte[] Message { get; private set; }
  46. public event EventHandler OnMessageCut;
  47. public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
  48. private enum State
  49. {
  50. Uninitialized,
  51. SyncHeader_Cmd_NozzleNumber_Ready,
  52. BodyAndCrcReady,
  53. }
  54. public MessageCutterV105()
  55. {
  56. this.window = new SizableWindow<byte>(3);
  57. this.window.OnWindowFull += (data) =>
  58. {
  59. switch (nextState)
  60. {
  61. case State.Uninitialized:
  62. if (data[0] == data[1] && data[0] == data[2] && data[0] == 0xFC)
  63. {
  64. this.window.NewSize = 5;
  65. this.nextState = State.SyncHeader_Cmd_NozzleNumber_Ready;
  66. }
  67. else
  68. {
  69. this.window.Clear();
  70. this.OnInvalidMessageRead?.Invoke(this, new MessageCutterInvalidMessageReadEventArg()
  71. {
  72. Message = "First 3 bytes: 0x" + this.window.ToHexLogString() + " are not the expecting 0xFC, will clear window and start over"
  73. });
  74. }
  75. break;
  76. case State.SyncHeader_Cmd_NozzleNumber_Ready:
  77. var cmd = this.window[3];
  78. if (!CmdCharAndBodyLengthMappers.TryGetValue(cmd, out byte bodyLength))
  79. {
  80. this.nextState = State.Uninitialized;
  81. this.OnInvalidMessageRead?.Invoke(this, new MessageCutterInvalidMessageReadEventArg()
  82. {
  83. Message = "Cmd byte: 0x" + cmd.ToString("X2") + " is not defined, will clear window and start over"
  84. });
  85. }
  86. else
  87. {
  88. this.window.NewSize = 3 + 1 + 1 + bodyLength + 1;
  89. this.nextState = State.BodyAndCrcReady;
  90. }
  91. break;
  92. case State.BodyAndCrcReady:
  93. //innerLogger.Debug(this.loggerAppendix + " Fire OnMessageConstructed with innerQueue: " + this.buffer.ToHexLogString());
  94. this.Message = this.window.ToArray();
  95. var safe = this.OnMessageCut;
  96. safe?.Invoke(this, null);
  97. this.nextState = State.Uninitialized;
  98. this.window.Clear();
  99. //this.window.NewSize = this.initWindowSize;
  100. break;
  101. default:
  102. throw new ArgumentOutOfRangeException();
  103. }
  104. };
  105. }
  106. public void Feed(byte[] data)
  107. {
  108. for (int i = 0; i < data.Length; i++)
  109. this.window.Add(data[i]);
  110. }
  111. }
  112. }