StateMachineMessageCutter.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Edge.Core.Database;
  2. using Edge.Core.Processor;
  3. using Edge.Core.IndustryStandardInterface.Pump;
  4. using Edge.Core.Processor.Communicator;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Logging.Abstractions;
  13. using Microsoft.Extensions.DependencyInjection;
  14. namespace Wayne_VaporRecoveryDataCollectorBoard
  15. {
  16. public class StateMachineMessageCutter : IMessageCutter<byte[]>
  17. {
  18. public byte[] Message { get; private set; }
  19. public event EventHandler OnMessageCut;
  20. public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
  21. static ILogger innerLogger = NullLogger.Instance;
  22. private string loggerAppendix = "Wayne_VaporRecoveryDataCollectorBoard msgCutter ";
  23. private readonly SizableWindow<byte> window;
  24. private State nextState = State.Uninitialized;
  25. public StateMachineMessageCutter() : this(null)
  26. {
  27. }
  28. /// <summary>
  29. /// 包起始标志(1B)+采集器地址Addr(1B)+命令代码(1B)+数据域长度(1B)+数据域(由数据域长度指定)+校验CS(1B)+包结束标志(1B)。
  30. ///包起始标志:68;
  31. ///包结束标志:0x16;
  32. ///校验:从采集器地址开始(包括采集器地址)到校验字节前所有数据累加求和取低八位数据。
  33. /// </summary>
  34. public StateMachineMessageCutter(IServiceProvider services)
  35. {
  36. var loggerFactory = services?.GetRequiredService<ILoggerFactory>();
  37. innerLogger = loggerFactory?.CreateLogger("StateMachineMessageCutter") ?? NullLogger.Instance;
  38. //this.initWindowSize = initWindowSize;
  39. this.window = new SizableWindow<byte>();
  40. this.window.OnWindowFull += (data) =>
  41. {
  42. switch (nextState)
  43. {
  44. case State.Uninitialized:
  45. if (data.First() == 0x68)
  46. {
  47. this.window.NewSize = 4;
  48. this.nextState = State.LengthReady;
  49. if (innerLogger.IsEnabled(LogLevel.Trace))
  50. innerLogger.LogTrace(this.loggerAppendix + " in State.Uninitialized read header 0x68, switch to LengthReady");
  51. }
  52. else
  53. this.window.Clear();
  54. break;
  55. case State.LengthReady:
  56. if (this.window.Skip(3).First() >= 40)
  57. {
  58. innerLogger.LogInformation($"{this.loggerAppendix } MsgBodyLen value: 0x{this.window.Skip(3).First().ToString("X").PadLeft(2, '0')} is too large, treat as error msg.");
  59. this.OnInvalidMessageRead?.Invoke(this, new MessageCutterInvalidMessageReadEventArg()
  60. {
  61. Message = $"{this.loggerAppendix } MsgBodyLen value: 0x{this.window.Skip(3).First().ToString("X").PadLeft(2, '0')} is too large, treat as error msg."
  62. });
  63. this.nextState = State.Uninitialized;
  64. this.window.Clear();
  65. return;
  66. }
  67. if (this.window.Skip(2).First() == 0 && this.window.Skip(3).First() == 0)
  68. {
  69. /* handle a special case of 'device is in busy' command, it is like: 0x68 06 00 00 16*/
  70. this.window.NewSize = 5;
  71. }
  72. else
  73. this.window.NewSize = this.window.Skip(3).First() + 4 + 1 + 1;
  74. this.nextState = State.BodyReady;
  75. if (innerLogger.IsEnabled(LogLevel.Trace))
  76. innerLogger.LogTrace(this.loggerAppendix + " MsgBodyLen caculated with: " + this.window.NewSize);
  77. break;
  78. case State.BodyReady:
  79. if (this.window.Last() != 0x16)
  80. {
  81. innerLogger.LogInformation($"{this.loggerAppendix } last byte must be 0x16 but now is 0x{this.window.Last().ToString("X").PadLeft(2, '0')}, treat as error msg.");
  82. this.OnInvalidMessageRead?.Invoke(this, new MessageCutterInvalidMessageReadEventArg()
  83. {
  84. Message = $"{this.loggerAppendix } last byte must be 0x16 but now is 0x{this.window.Last().ToString("X").PadLeft(2, '0')}, treat as error msg."
  85. });
  86. this.nextState = State.Uninitialized;
  87. this.window.Clear();
  88. return;
  89. }
  90. this.Message = this.window.ToArray();
  91. /* handle a special case of 'device is in busy' command, it is like: 0x68 06 00 00 16*/
  92. //make a hack for `device is in busy command` as the parser lib must have a concreate byte data to fill all properties.
  93. if (this.Message.Length == 5)
  94. this.Message = this.Message.Concat(new byte[] { 0x00 }).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. private enum State
  107. {
  108. Uninitialized,
  109. //HeaderSeeking,
  110. HeaderReady,
  111. LengthReady,
  112. BodyReady,
  113. }
  114. public void Feed(byte[] next)
  115. {
  116. //innerLogger.Debug(this.loggerAppendix + " " + next.ToHexLogString() + " is feed in Window in state: " + nextState);
  117. for (int i = 0; i < next.Length; i++)
  118. this.window.Add(next[i]);
  119. }
  120. }
  121. }