using Edge.Core.Parser.BinaryParser; using Edge.Core.Parser.BinaryParser.MessageEntity; using Edge.Core.Parser.BinaryParser.Util; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ZhongSheng_NonIC_Pump { public class MessageTemplateLookup : IMessageTemplateLookup { /// /// Gets the default singleton instance of type MessageTemplateLookup. /// public static MessageTemplateLookup Default { get; } = new MessageTemplateLookup(); /// /// Create a message entity based on input whole message raw bytes which is: 2bytes Len + 1 byte AppId + 1 byte SSK /// + variable length message code + variable length message body /// /// whole message raw bytes /// new created message entity public Edge.Core.Parser.BinaryParser.MessageEntity.MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes) { var commandCode = bytes.Skip(5).First(); switch (commandCode) { case 0x31: var subCode = bytes.Skip(6).First(); if (subCode == 0) return new PumpInIdleResponse(); else return new PumpInOperationResponse(); case 0x32: return new PumpNotifyTransactionDoneEvent(); case 0x33: return new ReadPumpAccumulatorResponse(); case 0x34: return new AckChangePumpPriceResponse(); case 0x35: return new AckStartPumpResponse(); case 0x36: return new AckStopPumpResponse(); case 0x37: return new AckDisplayResponse(); case 0x52: { subCode = bytes.Skip(6).First(); if (subCode == 0x01) return new AckSetPumpConfigResponse(); else if (subCode == 0x02) return new ReadPumpConfigResponse(); } throw new ArgumentException("PC 发送参数信息字符串给油机,或者读取参数信息字符串从加油机, 但subcode即不是0x01也不是0x02"); } throw new ArgumentException("Can't find correlated message entity type for incoming raw message bytes: " + bytes.Select(s => s.ToString("X").PadLeft(2, '0')).Aggregate((n, acc) => n + " " + acc)); } } }