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 FuRen_Sinopec_IcCardReader { 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 MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes) { // from protocol definition, the msg body started at 7th byte, and the 7th bytes is always the msg type code. var msgHandleCode = bytes.Skip(6).First(); switch (msgHandleCode) { //case 0x30: // return new PumpGenericInquiryCommand(); case 0x31: return new PumpStateChangeCommand(); case 0x32: return new PumpNotifyTransactionDoneCommand(); case 0x3A: return new FuRen_PcActivelyReadPumpConfigurationInfoResponse(); } 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)); } } }