123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using Edge.Core.Parser.BinaryParser.Attributes;
- using Edge.Core.Parser.BinaryParser;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Edge.Core.Parser.BinaryParser.MessageEntity;
- using static VeederRoot_ATG_Console.MessageEntity.MessageBase;
- namespace VeederRoot_ATG_Console.MessageEntity
- {
- public static class FunctionCodeWrapper
- {
- private static List<Tuple<string, FuncCode, MessageFormat>> map
- = new List<Tuple<string, FuncCode, MessageFormat>>()
- {
- new Tuple<string, FuncCode, MessageFormat>("i201",
- FuncCode.QueryInTankInventoryReport, MessageFormat.Computer ),
- new Tuple<string, FuncCode, MessageFormat>("I201",
- FuncCode.QueryInTankInventoryReport, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i501",
- FuncCode.QueryTimeOfDay, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I501",
- FuncCode.QueryTimeOfDay, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("s501",
- FuncCode.SetTimeOfDay, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("S501",
- FuncCode.SetTimeOfDay, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i205",
- FuncCode.QueryInTankStatusReport, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I205",
- FuncCode.QueryInTankStatusReport, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i20C",
- FuncCode.QueryInTankMostRecentDeliveryReport, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I20C",
- FuncCode.QueryInTankMostRecentDeliveryReport, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i505",
- FuncCode.QuerySystemTypeAndLanguageFlags, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I505",
- FuncCode.QuerySystemTypeAndLanguageFlags, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i517",
- FuncCode.QuerySystemTypeAndLanguageFlags_Extended, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I517",
- FuncCode.QuerySystemTypeAndLanguageFlags_Extended, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("s505",
- FuncCode.SetSystemTypeAndLanguageFlags, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("S505",
- FuncCode.SetSystemTypeAndLanguageFlags, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i602",
- FuncCode.QueryTankProductLabel, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I602",
- FuncCode.QueryTankProductLabel, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("s602",
- FuncCode.SetTankProductLabel, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("S602",
- FuncCode.SetTankProductLabel, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i607",
- FuncCode.QueryTankDiameter, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I607",
- FuncCode.QueryTankDiameter, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("s607",
- FuncCode.SetTankDiameter, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("S607",
- FuncCode.SetTankDiameter, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("i101",
- FuncCode.QuerySystemStatusReport, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("I101",
- FuncCode.QuerySystemStatusReport, MessageFormat.Display),
- new Tuple<string, FuncCode, MessageFormat>("iA01",
- FuncCode.QueryInTankDiagnosticReport, MessageFormat.Computer),
- new Tuple<string, FuncCode, MessageFormat>("IA01",
- FuncCode.QueryInTankDiagnosticReport, MessageFormat.Display)
- };
- /// <summary>
- /// Convert 6 digits raw code into a pre-defined function code and its format.
- /// </summary>
- /// <param name="rawCode">Length is 6, started with an upper or lower case alphabet, like i00100, I00100, s00200, S00200</param>
- /// <returns>null indicates could not find correlated pre-defined function code</returns>
- public static Tuple<MessageFormat, FuncCode> FromRawCode(string rawCode)
- {
- return map.Where(c => rawCode.StartsWith(c.Item1))
- .Select(s => new Tuple<MessageFormat, FuncCode>(s.Item3, s.Item2)).FirstOrDefault();
- }
- /// <summary>
- /// Get a length 4 raw code string.
- /// </summary>
- /// <param name="messageFormat"></param>
- /// <param name="functionCode"></param>
- /// <returns>length 4 string with an 1 digit alphabet + 3 digits numbers</returns>
- public static string ToRawCode(MessageFormat messageFormat, FuncCode functionCode)
- {
- return map.Where(c => c.Item2 == functionCode && c.Item3 == messageFormat)
- .Select(s => s.Item1).FirstOrDefault();
- }
- }
- public abstract class MessageBase : MessageBaseGeneric
- {
- public enum MessageFormat
- {
- /// <summary>
- /// started with a lower case alphabet.
- /// indicates the message is a stream of numbers without any formatting characters, no carriage return,
- /// line feed, spaces, labels and etc.
- ///
- /// SOH + FunctionCode + DataField + && + Checksum + ETX
- /// </summary>
- Computer,
- /// <summary>
- /// started with a upper case alphabet.
- /// indicates the message is intended for display to a CRT or printer, it includes all the necessary
- /// formatting characters such as carriage returns, line feeds, nulls, spaces, labels and etc.
- ///
- /// SOH + FunctionCode + DataField + ETX
- /// </summary>
- Display,
- }
- public enum FuncCode
- {
- /// <summary>
- /// i|I50500
- /// </summary>
- QuerySystemTypeAndLanguageFlags,
- /// <summary>
- /// i|I51700
- /// </summary>
- QuerySystemTypeAndLanguageFlags_Extended,
- /// <summary>
- /// s|S50500UL, U is system units, L is system language.
- /// </summary>
- SetSystemTypeAndLanguageFlags,
- /// <summary>
- /// Append i|I, response function code is i|I201.
- /// </summary>
- QueryInTankInventoryReport,
- /// <summary>
- /// Append i|I, response function code is i|I501.
- /// </summary>
- QueryTimeOfDay,
- /// <summary>
- /// Append s|S, response function code is s|S501.
- /// </summary>
- SetTimeOfDay,
- /// <summary>
- /// Append i|I, response function code is i|I205.
- /// </summary>
- QueryInTankStatusReport,
- /// <summary>
- /// Append i|I, response functin code is i|I20C.
- /// </summary>
- QueryInTankMostRecentDeliveryReport,
- /// <summary>
- /// Append i|I, response functin code is i|I602.
- /// </summary>
- QueryTankProductLabel,
- /// <summary>
- /// Append s|S, response functin code is i|I602.
- /// </summary>
- SetTankProductLabel,
- /// <summary>
- /// Append i|I, response functin code is i|I607.
- /// </summary>
- QueryTankDiameter,
- /// <summary>
- /// Append s|S, response functin code is i|I607.
- /// </summary>
- SetTankDiameter,
- /// <summary>
- /// Append i|I, response functin code is i|I10100.
- /// </summary>
- QuerySystemStatusReport,
- /// <summary>
- /// Append i|I, response functin code is i|IA01.
- /// Probe Type and Serial Number
- /// </summary>
- QueryInTankDiagnosticReport,
- Undefined,
- }
- [Format(1, EncodingType.BIN, -9999)]
- public byte SOH { get; set; } = 0x01;
- /// <summary>
- /// Gets or sets the function code raw value for the message, it's a 6 digits string, like S60201
- /// In most case you should not use this property directly, try use more friendly property `FunctionCode`
- /// </summary>
- [Format(6, EncodingType.ASCII, -9000)]
- public string FunctionCodeRaw { get; set; }
- /// <summary>
- /// Gets or sets the function code, it include 3 parts:
- /// MessageFormat(1st digits) + functionCode(3 digits) + appendix(2 digits)
- /// </summary>
- public Tuple<MessageFormat, FuncCode, string> FunctionCode
- {
- get
- {
- var c = FunctionCodeWrapper.FromRawCode(this.FunctionCodeRaw);
- return new Tuple<MessageFormat,
- FuncCode,
- string>(
- c.Item1,
- c.Item2,
- this.FunctionCodeRaw.Substring(4, 2));
- }
- set
- {
- var raw = FunctionCodeWrapper.ToRawCode(value.Item1, value.Item2);
- if (string.IsNullOrEmpty(raw))
- throw new ArgumentException("Can't find raw function code for: " + value.Item1 + " - " + value.Item2);
- if (!string.IsNullOrEmpty(value.Item3))
- raw += value.Item3.PadLeft(2, '0');
- else
- raw = raw.PadRight(6, '0');
- if (raw.Length != 6) throw new ArgumentException("Function code must have length 6, but now is " + raw.Length);
- this.FunctionCodeRaw = raw;
- }
- }
- }
- }
|