using HengShan_Pump_TQC_IFSF.MessageEntity; using HengShan_Pump_TQC_IFSF.MessageEntity.Incoming; using Edge.Core.Parser.BinaryParser; using Edge.Core.Parser.BinaryParser.MessageEntity; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HengShan_Pump_TQC_IFSF { public class MessageTemplateLookup : IMessageTemplateLookup { public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes) { if (bytes.Length == 10 && bytes[0] <= 254 && bytes[0] >= 1 && bytes[1] <= 254 && bytes[1] >= 1 && bytes[2] <= 254 && bytes[2] >= 1 && bytes[3] <= 254 && bytes[3] >= 1 && (((bytes[4] << 8) + bytes[5]) <= 65534)) return new Heartbeat(1, 1, 1, 1, 1, 1, 1); else if ((byte)(bytes[5] & 0xE0) == 0xE0) { return new AcknowledgeMessage(); } else if ((bytes[5] & 0xE0) == 0x80 && bytes[8] == 0x03 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x41) { return new ErrorCodeDb_Event(); } else if ( ((bytes[5] & 0xE0) == 0x80 || (bytes[5] & 0xE0) == 0x60) && bytes[8] == 0x01 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && (bytes[10] == 0x64 || bytes[10] == 0x14)) { return new FuellingPointDb_FpStatus_Event(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x01 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x14) { return new FuellingPointDb_FpStatus_Answer(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x01 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x04) { return new FuellingPointDb_Nb_Logical_Nozzle_Answer(); } else if ( ((bytes[5] & 0xE0) == 0x80) && bytes[8] == 0x01 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x66 && bytes[11] == 0x00 && bytes[12] == 0x22 && bytes[19] == 0x23) { return new FuellingPointDb_FpRunningTransaction_Event(); } else if ((bytes[5] & 0xE0) == 0x60 && bytes[8] == 0x01 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x22 && bytes[17] == 0x23 && bytes[24] == 0x24) { return new FuellingPointDb_FpRunningTransaction_Event_ACK(); } else if ( (bytes[5] & 0xE0) == 0x80 && bytes[8] == 0x04 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x21 && bytes[11] <= 0x99 && bytes[12] <= 0x99 && bytes[13] == 0x64) { return new FuellingTrxDb_TransactionBufferStatus_Event(); } else if ( (bytes[5] & 0xE0) == 0x60 && bytes[8] == 0x04 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x21 && bytes[11] <= 0x99 && bytes[12] <= 0x99 && bytes[13] == 0x01 && bytes[17] == 0x05) { return new FuellingTrxDb_TransactionBufferStatus_Event_ACK(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x04 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] == 0x21) { return new FuellingTrxDb_TransactionBufferStatus_Answer(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x02 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] >= 0x11 && bytes[10] <= 0x18 && bytes[11] == 0x14 && bytes[20] == 0x15) { return new LogicalNozzleDb_NozzleTotalizer_Answer(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x01 && bytes[9] >= 0x40 && bytes[9] <= 0x48 // dataid must 0x02 && bytes[10] == 0x02 // length must 4 && bytes[11] == 0x04) { return new ProductDb_ProductNo_Answer(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x06 && bytes[9] == 0x61 && bytes[14] >= 0x11 && bytes[14] <= 0x18 // dataid must 0x02 && bytes[15] == 0x02 // length must 4 && bytes[16] == 0x04) { return new ProductPerFuellingModeDb_ProductPrice_Answer(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x02 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] >= 0x11 && bytes[10] <= 0x18 // dataid must 0x01 && bytes[11] == 0x01 && bytes[14] == 0x05 /* the data_id 5, could has its var len to value 0, as I see a sample message from haosheng ifsf dispenser: 0x02 01 01 09 00 23 00 0B 02 21 11 01 01 42 05 00 07 01 00 * and 0 is a valid value from protocol as it said: PCD Comment: As this is an optional data field the PCD can NAK any write requests to this Data_Id with a Data_ACK code of 4 (Data does not exist in this device) or reply to any read request with an answer message with the respective Data_Id’s length set to 0. * so here adding a strict condition to detetecting it */ && ((bytes[15] == 0x00 && bytes[16] == 0x07) || (bytes[15] != 0x00 && bytes[17] == 0x07))) { return new LogicalNozzleDb_Nozzle_ProductInfo_PhyId_Answer(); } else if ((bytes[5] & 0xE0) == 0x60 && bytes[8] == 0x02 && bytes[9] >= 0x21 && bytes[9] <= 0x24 && bytes[10] >= 0x11 && bytes[10] <= 0x18 // dataid must 0x05 && bytes[11] == 0x05) { return new LogicalNozzleDb_Nozzle_PhyId_Event_ACK(); } else if ((bytes[5] & 0xE0) == 0x60 && bytes[8] == 0x01 && bytes[9] == 0x00 ) { return new CommunicationDb_Event_ACK(); } else if ((bytes[5] & 0xE0) == 0x20 && bytes[8] == 0x01 && bytes[9] == 0x00 && bytes[10] == 0x04 ) { return new CommunicationDb_HeartbeatInterval_Answer(); } else return new IfsfMessage(); } } }