using System; using System.Linq; using Edge.Core.Processor.Communicator; namespace HengShan_Pump_NonIC_Plus { public class StateMachineMessageCutter : IMessageCutter { public byte[] Message { get; private set; } public event EventHandler OnMessageCut; public event EventHandler OnInvalidMessageRead; static NLog.Logger innerLogger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("StateMachineMessageCutter"); private string loggerAppendix = "HengShan_Pump_NonIC_Plus msgCutter "; private readonly SizableWindow window; private State nextState = State.Uninitialized; public StateMachineMessageCutter() { this.window = new SizableWindow(); this.window.OnWindowFull += (data) => { switch (nextState) { case State.Uninitialized: if (data.First() == 0x9F) { //前导码(09FH) + 枪号 + 长度(0-20H) this.window.NewSize = 3; this.nextState = State.LengthReady; innerLogger.Debug(this.loggerAppendix + " state is State.Uninitialized and next is 0x9F, switch to LengthReady"); } else ReadInvalidMessage(); break; case State.LengthReady: int len = this.window.Skip(2).First(); if (len > 0) { this.window.NewSize += len; this.nextState = State.BodyReady; innerLogger.Debug(this.loggerAppendix + " MsgBodyLen caculated with: " + this.window.NewSize); } else ReadInvalidMessage(); break; case State.BodyReady: //innerLogger.Debug(this.loggerAppendix + " Fire OnMessageConstructed with innerQueue: " + this.buffer.ToHexLogString()); this.Message = this.window.ToArray(); var safe = this.OnMessageCut; safe?.Invoke(this, null); this.nextState = State.Uninitialized; this.window.Clear(); break; default: throw new ArgumentOutOfRangeException(); } }; } private void ReadInvalidMessage() { this.nextState = State.Uninitialized; this.window.Clear(); } private enum State { Uninitialized, //HeaderSeeking, HeaderReady, LengthReady, BodyReady, } public void Feed(byte[] next) { //innerLogger.Debug(this.loggerAppendix + " " + next.ToHexLogString() + " is feed in Window in state: " + nextState); for (int i = 0; i < next.Length; i++) this.window.Add(next[i]); } } }