123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- 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<ProcessorMetaPartsConfig>()
- {
- 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,\"<PumpGroup><Pump pumpId='1' physicalId='1'><Nozzles><Nozzle logicalId='1' physicalId='1' defaultNoDecimalPointPriceIfNoHistoryPriceReadFromDb='1234'/></Nozzles></Pump></PumpGroup>\"]",
- },
- 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<IProcessor>(processorMetaConfig, new object[] { services });
- Assert.AreEqual(true, processorInstance != null);
- Assert.AreEqual(true, processorInstance.MetaConfigName == processorMetaConfig.Name);
- var deviceProcessor = processorInstance as GenericDeviceProcessor<byte[], Wayne_Pump_Dart.MessageEntity.MessageBase>;
- //deviceProcessor.Communicator.sendMessage???
- var WayneDartPumpGroupHandler = deviceProcessor.ProcessorDescriptor().DeviceHandlerOrApp as IEnumerable<IFdcPumpController>;
- WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnFdcServerInit(new Dictionary<string, object>() {
- {"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<ProcessorMetaPartsConfig>()
- {
- //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,\"<PumpGroup><Pump pumpId='1' physicalId='1'><Nozzles><Nozzle logicalId='1' physicalId='1' defaultNoDecimalPointPriceIfNoHistoryPriceReadFromDb='1234'/></Nozzles></Pump></PumpGroup>\"]",
- },
- 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<IProcessor>(processorMetaConfig, new object[] { services });
- Assert.AreEqual(true, processorInstance != null);
- Assert.AreEqual(true, processorInstance.MetaConfigName == processorMetaConfig.Name);
- var deviceProcessor = processorInstance as GenericDeviceProcessor<byte[], Wayne_Pump_Dart.MessageEntity.MessageBase>;
- //deviceProcessor.Communicator.sendMessage???
- var WayneDartPumpGroupHandler = deviceProcessor.ProcessorDescriptor().DeviceHandlerOrApp as IEnumerable<IFdcPumpController>;
- WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnFdcServerInit(new Dictionary<string, object>() {
- {"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<ProcessorMetaPartsConfig>()
- {
- //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,\"<PumpGroup><Pump pumpId='1' physicalId='1'><Nozzles><Nozzle logicalId='1' physicalId='1' defaultNoDecimalPointPriceIfNoHistoryPriceReadFromDb='1234'/></Nozzles></Pump></PumpGroup>\"]",
- },
- 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<IProcessor>(processorMetaConfig, new object[] { services });
- Assert.AreEqual(true, processorInstance != null);
- Assert.AreEqual(true, processorInstance.MetaConfigName == processorMetaConfig.Name);
- var deviceProcessor = processorInstance as GenericDeviceProcessor<byte[], Wayne_Pump_Dart.MessageEntity.MessageBase>;
- //deviceProcessor.Communicator.sendMessage???
- var WayneDartPumpGroupHandler = deviceProcessor.ProcessorDescriptor().DeviceHandlerOrApp as IEnumerable<IFdcPumpController>;
- WayneDartPumpGroupHandler.ToList().ForEach(ph => ph.OnFdcServerInit(new Dictionary<string, object>() {
- {"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);
- }
- }
- }
|