using Edge.Core.Database.Configuration.Models; using Edge.Core.IndustryStandardInterface.Pump; using Edge.Core.Processor; using Edge.Core.Processor.Communicator; using Edge.Core.Processor.Dispatcher; using Edge.Core.Processor.Dispatcher.Attributes; using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Wayne_Pump_Dart; namespace Edge.Core.Test { [TestClass] public class ProcessorInstanceCreatorUnitTest { public class PumpGroupHandler_WithoutMetaPartAttribute : PumpGroupHandler { public PumpGroupHandler_WithoutMetaPartAttribute(int amountDecimalDigits, int volumeDecimalDigits, int priceDecimalDigits, int volumeTotalizerDecimalDigits, string pumpGroupXmlConfiguration, IServiceProvider services) : base(amountDecimalDigits, volumeDecimalDigits, priceDecimalDigits, volumeTotalizerDecimalDigits, pumpGroupXmlConfiguration) { } } [MetaPartsRequired(typeof(StateMachineMessageCutter))] [MetaPartsRequired(typeof(Wayne_Pump_Dart.Parser))] [MetaPartsRequired(typeof(HalfDuplexActivePollingDeviceProcessor<,>), DefaultCtorParamsJsonStrings = new string[] { "300" })] public class PumpGroupHandler_WithMetaPartAttribute : PumpGroupHandler { public PumpGroupHandler_WithMetaPartAttribute(int amountDecimalDigits, int volumeDecimalDigits, int priceDecimalDigits, int volumeTotalizerDecimalDigits, string pumpGroupXmlConfiguration, IServiceProvider services) : base(amountDecimalDigits, volumeDecimalDigits, priceDecimalDigits, volumeTotalizerDecimalDigits, pumpGroupXmlConfiguration) { } } [TestMethod] public async Task DeviceProcessor_TestMethod1() { var parserFullTypeStr = typeof(Wayne_Pump_Dart.Parser).FullName; var messageCutterFullTypeStr = typeof(Wayne_Pump_Dart.StateMachineMessageCutter).FullName; var messageBaseFullTypeStr = typeof(Wayne_Pump_Dart.MessageEntity.MessageBase).FullName; var deviceHandlerFullTypeStr = typeof(PumpGroupHandler_WithoutMetaPartAttribute).FullName; var communicatorFullTypeStr = typeof(ComPortCommunicator<>).FullName; var processorFullTypeStr = typeof(HalfDuplexActivePollingDeviceProcessor<,>).FullName; ProcessorMetaConfig processorMetaConfig = new ProcessorMetaConfig() { Activated = true, Name = "testWayneDartPumpOnComportConfig", Type = ProcessorTypeEnum.DeviceProcessor, Parts = new List() { new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.Parser, FullTypeString = parserFullTypeStr, }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.MessageCutter, FullTypeString = messageCutterFullTypeStr, }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.RawMessageTypeStr, FullTypeString = typeof(byte[]).FullName, }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.MessageTypeStr, FullTypeString = messageBaseFullTypeStr, }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.DeviceHandler, FullTypeString = deviceHandlerFullTypeStr, ParametersJsonArrayStr = "[2,2,3,2,\"\"]", }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.Communicator, FullTypeString = communicatorFullTypeStr, ParametersJsonArrayStr = "[\"COM8\",9600,1,8,1]", }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.DeviceProcessor, FullTypeString = processorFullTypeStr, ParametersJsonArrayStr = "[400]", }, } }; var services = new ServiceCollection().AddLogging().BuildServiceProvider(); var processorInstance = ObjectInstanceCreator.CreateProcessor(processorMetaConfig, new object[] { services }); Assert.AreEqual(true, processorInstance != null); Assert.AreEqual(true, processorInstance.MetaConfigName == processorMetaConfig.Name); var deviceProcessor = processorInstance as GenericDeviceProcessor; //deviceProcessor.Communicator.sendMessage??? var WayneDartPumpGroupHandler = deviceProcessor.ProcessorDescriptor().DeviceHandlerOrApp as IEnumerable; WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnFdcServerInit(new Dictionary() { {"test","1" } })); bool pumpCallingStateReceived = false; bool pumpTrxReceived = false; WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnStateChange += async (s, a) => { if (a.NewPumpState == Wayne.FDCPOSLibrary.LogicalDeviceState.FDC_CALLING) { pumpCallingStateReceived = true; await ((IFdcPumpController)s).AuthorizeAsync(1); } }); WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnCurrentFuellingStatusChange += async (s, a) => { if (a.Transaction.Finished) { pumpTrxReceived = true; } }); await deviceProcessor.Start(); //await Task.Delay(25000); } [TestMethod] public async Task DeviceProcessor_WithAllMetaParts_TestMethod1() { var deviceHandlerFullTypeStr = typeof(PumpGroupHandler_WithMetaPartAttribute).FullName; var communicatorFullTypeStr = typeof(ComPortCommunicator<>).FullName; ProcessorMetaConfig processorMetaConfig = new ProcessorMetaConfig() { Activated = true, Name = "testWayneDartPumpOnComportConfig", Type = ProcessorTypeEnum.DeviceProcessor, Parts = new List() { //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.Parser, // FullTypeString = parserFullTypeStr, //}, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.MessageCutter, // FullTypeString = messageCutterFullTypeStr, //}, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.RawMessageTypeStr, // FullTypeString = typeof(byte[]).FullName, //}, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.MessageTypeStr, // FullTypeString = messageBaseFullTypeStr, //}, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.DeviceHandler, FullTypeString = deviceHandlerFullTypeStr, ParametersJsonArrayStr = "[2,2,3,2,\"\"]", }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.Communicator, FullTypeString = communicatorFullTypeStr, ParametersJsonArrayStr = "[\"COM99\",9600,1,8,1]", }, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.Processor, // FullTypeString = processorFullTypeStr, // ParametersJsonArrayStr = "[400]", //}, } }; var services = new ServiceCollection().AddLogging().BuildServiceProvider(); var processorInstance = ObjectInstanceCreator.CreateProcessor(processorMetaConfig, new object[] { services }); Assert.AreEqual(true, processorInstance != null); Assert.AreEqual(true, processorInstance.MetaConfigName == processorMetaConfig.Name); var deviceProcessor = processorInstance as GenericDeviceProcessor; //deviceProcessor.Communicator.sendMessage??? var WayneDartPumpGroupHandler = deviceProcessor.ProcessorDescriptor().DeviceHandlerOrApp as IEnumerable; WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnFdcServerInit(new Dictionary() { {"test","1" } })); bool pumpCallingStateReceived = false; bool pumpTrxReceived = false; WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnStateChange += async (s, a) => { if (a.NewPumpState == Wayne.FDCPOSLibrary.LogicalDeviceState.FDC_CALLING) { pumpCallingStateReceived = true; await ((IFdcPumpController)s).AuthorizeAsync(1); } }); WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnCurrentFuellingStatusChange += async (s, a) => { if (a.Transaction.Finished) { pumpTrxReceived = true; } }); var isStarted = await deviceProcessor.Start(); } [TestMethod] public async Task Integration_DeviceProcessor_WithAllMetaParts_TestMethod1() { /* need turn on WayneDartPump simualtor, and create a comport pair, processor listen COM8, pump sim listen COM5*/ var deviceHandlerFullTypeStr = typeof(PumpGroupHandler_WithMetaPartAttribute).FullName; var communicatorFullTypeStr = typeof(ComPortCommunicator<>).FullName; ProcessorMetaConfig processorMetaConfig = new ProcessorMetaConfig() { Activated = true, Name = "testWayneDartPumpOnComportConfig", Type = ProcessorTypeEnum.DeviceProcessor, Parts = new List() { //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.Parser, // FullTypeString = parserFullTypeStr, //}, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.MessageCutter, // FullTypeString = messageCutterFullTypeStr, //}, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.RawMessageTypeStr, // FullTypeString = typeof(byte[]).FullName, //}, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.MessageTypeStr, // FullTypeString = messageBaseFullTypeStr, //}, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.DeviceHandler, FullTypeString = deviceHandlerFullTypeStr, ParametersJsonArrayStr = "[2,2,3,2,\"\"]", }, new ProcessorMetaPartsConfig() { Type = ProcessorMetaPartsTypeEnum.Communicator, FullTypeString = communicatorFullTypeStr, ParametersJsonArrayStr = "[\"COM8\",9600,1,8,1]", }, //new ProcessorPartsMetaConfig() //{ // Type = ProcessorPartsTypeEnum.Processor, // FullTypeString = processorFullTypeStr, // ParametersJsonArrayStr = "[400]", //}, } }; var services = new ServiceCollection().AddLogging().BuildServiceProvider(); var processorInstance = ObjectInstanceCreator.CreateProcessor(processorMetaConfig, new object[] { services }); Assert.AreEqual(true, processorInstance != null); Assert.AreEqual(true, processorInstance.MetaConfigName == processorMetaConfig.Name); var deviceProcessor = processorInstance as GenericDeviceProcessor; //deviceProcessor.Communicator.sendMessage??? var WayneDartPumpGroupHandler = deviceProcessor.ProcessorDescriptor().DeviceHandlerOrApp as IEnumerable; WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnFdcServerInit(new Dictionary() { {"test","1" } })); bool pumpCallingStateReceived = false; bool pumpTrxReceived = false; WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnStateChange += async (s, a) => { if (a.NewPumpState == Wayne.FDCPOSLibrary.LogicalDeviceState.FDC_CALLING) { pumpCallingStateReceived = true; await ((IFdcPumpController)s).AuthorizeAsync(1); } }); WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnCurrentFuellingStatusChange += async (s, a) => { if (a.Transaction.Finished) { pumpTrxReceived = true; } }); var isStarted = await deviceProcessor.Start(); Assert.AreEqual(true, isStarted); int maxWaitSec = 30; int wait = 0; while (true) { if (wait > maxWaitSec) break; if (pumpCallingStateReceived && pumpTrxReceived) break; await Task.Delay(1000); wait++; } Assert.AreEqual(true, pumpCallingStateReceived); Assert.AreEqual(true, pumpTrxReceived); } } }