using Common; using Communicator; using log4net; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HengShan_Pump_NonIC { public class StateMachineMessageCutter : IMessageCutter { public byte[] Message { get; private set; } public event EventHandler OnMessageCut; static ILog innerLogger = log4net.LogManager.GetLogger("StateMachine"); private string loggerAppendix = "HengShan_Pump_NonIC msgCutter "; private readonly SizableWindow window; private State nextState = State.Uninitialized; /// /// /// public StateMachineMessageCutter() { //this.initWindowSize = initWindowSize; this.window = new SizableWindow(); this.window.OnWindowFull += (data) => { switch (nextState) { case State.Uninitialized: if (data.First() == 0xFF) { //前导码(0FFH) +长度(0-20H) this.window.NewSize = 2; this.nextState = State.LengthReady; innerLogger.Debug(this.loggerAppendix + " state is State.Uninitialized and next is 0xFF, switch to LengthReady"); } else this.window.Clear(); break; case State.LengthReady: //this.DumpWindowToQueue(); this.window.NewSize = this.window.Skip(1).First() + 2; this.nextState = State.BodyReady; innerLogger.Debug(this.loggerAppendix + " MsgBodyLen caculated with: " + this.window.NewSize); 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(); //this.window.NewSize = this.initWindowSize; break; default: throw new ArgumentOutOfRangeException(); } }; } 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]); } } }