123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
- using Edge.Core.Parser.BinaryParser.Util;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Edge.Core.Processor.Communicator;
- namespace Wayne_Pump_Dart
- {
- public class StateMachineMessageCutter : IMessageCutter<byte[]>
- {
- public byte[] Message { get; private set; }
- public event EventHandler OnMessageCut;
- public event EventHandler<MessageCutterInvalidMessageReadEventArg> OnInvalidMessageRead;
-
- static NLog.Logger innerLogger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("Communicator");
- private string loggerAppendix = "Wayne_Pump_Dart msgCutter ";
- private readonly List<byte> buffer = new List<byte>();
-
-
-
-
-
-
- private int Get0xFAPairCountInWindow(IList<byte> target)
- {
- return (int)Math.Round(((double)(target.Count(w => w == 0xFA)) / 2), MidpointRounding.AwayFromZero);
- }
-
-
-
-
- private int Reduce0xFAPair(IList<byte> target, int from)
- {
- var faAppearedPositions = new List<int>();
- for (int i = from; i < target.Count - 1; i++)
- {
- if (target[i] == 0x10 && target[i + 1] == 0xFA)
- {
- faAppearedPositions.Add(i);
- i++;
- }
- }
- for (int i = 0; i < faAppearedPositions.Count; i++)
- {
- target.RemoveAt(faAppearedPositions[i] - i);
- }
- return faAppearedPositions.Count;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public StateMachineMessageCutter()
- {
- }
- public void Feed(byte[] next)
- {
- try
- {
-
- for (int i = 0; i < next.Length; i++)
- {
- this.buffer.Add(next[i]);
- if (this.buffer[0] < 0x50 || this.buffer[0] > 0x6F)
- {
- this.buffer.Clear();
- }
-
- if (this.buffer.Count >= 2 &&
- ((this.buffer[1] & 0xF0) != 0x30)
-
- && ((this.buffer[1] & 0xF0) != 0xC0)
-
- && ((this.buffer[1] & 0xF0) != 0x50)
-
- && ((this.buffer[1] & 0xF0) != 0x70)
-
- && ((this.buffer[1] & 0xF0) != 0xe0))
- {
- this.OnInvalidMessageRead?.Invoke(this, new MessageCutterInvalidMessageReadEventArg()
- {
- Message = "invalid byte[1]: 0x" + this.buffer[1].ToString("X2") + ", clear buf(valid byte[0]: 0x" + this.buffer[0].ToString("X2") + ") anyway"
- });
- this.buffer.Clear();
- }
- if (this.buffer.Count >= 3
- && this.buffer[this.buffer.Count - 2] != 0x10
- && this.buffer[this.buffer.Count - 1] == 0xFA)
- {
- Reduce0xFAPair(this.buffer, 0);
- if (this.buffer.Count >= 44)
- innerLogger.Info("Long length(len: " + this.buffer.Count + ") message was cut from MsgCutter: 0x" + this.buffer.ToHexLogString());
- this.Message = this.buffer.ToArray();
- var safe = this.OnMessageCut;
- safe?.Invoke(this, null);
- this.buffer.Clear();
- }
- if (this.buffer.Count >= 45)
- innerLogger.Info("Long length(len: " + this.buffer.Count + ") message is still constructing in MsgCutter: 0x" + this.buffer.ToHexLogString());
- }
- }
- catch (Exception exx)
- {
- innerLogger.Error($"Wayne_Pump_Dart.StateMachineMessageCutter, " +
- $"next: 0x{next?.ToHexLogString() ?? "null"}, " +
- $"this.buffer: 0x{this.buffer?.ToHexLogString() ?? "null"}" +
- $"{Environment.NewLine}exception detail: {exx}");
- throw;
- }
- }
- }
- }
|