123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Configuration;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using Timer = System.Timers.Timer;
- using System.Collections;
- using Edge.Core.Processor;
- using Edge.Core.IndustryStandardInterface.Pump;
- using Wayne.FDCPOSLibrary;
- using System.Xml;
- using Edge.Core.Database.Models;
- using System.Threading.Tasks;
- using Edge.Core.Processor.Communicator;
- using Edge.Core.Processor.Dispatcher.Attributes;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Logging.Abstractions;
- using Stateless;
- using Microsoft.Extensions.DependencyInjection;
- namespace Gilbarco_Pump
- {
- public class StatePumpHandler : IFdcPumpController//, IDeviceHandler<byte[], MessageTemplateBase>
- {
- private IServiceProvider services;
- private IContext<byte[], MessageTemplateBase> context;
- private ILogger logger = NullLogger.Instance;
- private PumpGroupHandler parent;
- private LogicalDeviceState currentState = LogicalDeviceState.FDC_OFFLINE;
- public event EventHandler<FdcPumpControllerOnStateChangeEventArg> OnStateChange;
- /// <summary>
- /// fired on fueling process is on going, the fuel amount should keep changing.
- /// </summary>
- public event EventHandler<FdcTransactionDoneEventArg> OnCurrentFuellingStatusChange;
- StateMachine<LogicalDeviceState, Trigger> stateless
- = new StateMachine<LogicalDeviceState, Trigger>(LogicalDeviceState.FDC_OFFLINE);
- protected List<LogicalNozzle> nozzles;
- public IEnumerable<LogicalNozzle> Nozzles => this.nozzles;
- protected enum Trigger
- {
- //AnyPumpMsgReceived,
- AnyPumpMsgHaveNotReceivedForWhile,
- NozzleLifted_And_开机,
- NozzleLifted_And_停机,
- NozzleReplaced_And_开机,
- NozzleReplaced_And_停机,
- NozzleFuelNumbersIsRunning,
- //PumpAuthorizedByFC,
- }
- public int AmountDecimalDigits => 2;
- public int VolumeDecimalDigits => 2;
- public int PriceDecimalDigits => 2;
- public int VolumeTotalizerDecimalDigits => 2;
- /// <summary>
- /// </summary>
- public int PumpId { get; private set; }
- /// <summary>
- /// will set it to 油机端的配置值,枪号为以整个加油站为基础的油枪顺序编号, 就是全站枪号
- /// </summary>
- public int PumpPhysicalId { get; private set; }
- public string Name => "ZhongSheng_NonIC_Pump";
- /// <summary>
- /// 每把枪就是一个Pump即一个加油点
- /// </summary>
- /// <param name="pumpId"></param>
- /// <param name="dispenserSideNozzleId">油机端的配置值,枪号为以整个加油站为基础的油枪顺序编号, 就是全站枪号?</param>
- public StatePumpHandler(PumpGroupHandler parent, byte pumpId,
- byte dispenserSideNozzleId, IServiceProvider services)
- {
- this.parent = parent;
- this.services = services;
- var loggerFactory = services.GetRequiredService<ILoggerFactory>();
- this.logger = loggerFactory.CreateLogger("PumpHandler");
- this.PumpId = pumpId;
- this.PumpPhysicalId = dispenserSideNozzleId;
- //每把枪就是一个Pump即一个加油点
- this.nozzles = new List<LogicalNozzle>() { new LogicalNozzle(pumpId, dispenserSideNozzleId, 1, null) };
- }
- internal void TriggerPumpOffline()
- {
- if (this.currentState != LogicalDeviceState.FDC_OFFLINE)
- {
- this.currentState = LogicalDeviceState.FDC_OFFLINE;
- this.OnStateChange?.Invoke(this, new FdcPumpControllerOnStateChangeEventArg(LogicalDeviceState.FDC_OFFLINE));
- }
- }
- public void Init(IContext<byte[], MessageTemplateBase> context)
- {
- this.context = context;
- }
- public async Task Process(IContext<byte[], MessageTemplateBase> context)
- {
- this.context = context;
- }
- public virtual async Task<LogicalDeviceState> QueryStatusAsync()
- {
- return this.currentState;
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns>MoneyTotalizer:VolumnTotalizer</returns>
- public async Task<Tuple<int, int>> QueryTotalizerAsync(byte logicalNozzleId)
- {
- var result = new System.Tuple<int, int>(-1, -1);
- logger.LogDebug("Pump: " + this.PumpId + ", " + "Start QueryTotalizer pump with logical nozzle: " + logicalNozzleId);
- return result;
- }
- public virtual async Task<bool> ChangeFuelPriceAsync(int newPriceWithoutDecimalPoint, byte logicalNozzleId)
- {
- logger.LogDebug("Pump: " + this.PumpId + ", " + "start ChangeFuelPriceAsync pump with nozzle: " + logicalNozzleId);
- return false;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="logicalNozzleId">useless for this type of pump, it always one pump one nozzle</param>
- /// <returns></returns>
- public virtual async Task<bool> AuthorizeAsync(byte logicalNozzleId)
- {
- logger.LogDebug("Pump: " + this.PumpId + ", " + "start AuthorizeAsync pump with nozzle: " + logicalNozzleId);
- return false;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="moneyAmount"></param>
- /// <param name="logicalNozzleId">useless for this type of pump, it always one pump one nozzle</param>
- /// <returns></returns>
- public virtual async Task<bool> AuthorizeWithAmountAsync(int moneyAmountWithoutDecimalPoint, byte logicalNozzleId)
- {
- logger.LogDebug("Pump: " + this.PumpId + ", " + "start AuthorizeWithAmountAsync pump with amount: " + moneyAmountWithoutDecimalPoint + ", nozzle: " + logicalNozzleId);
- return false;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="volumnWithoutDecimalPoint"></param>
- /// <param name="logicalNozzleId">useless for this type of pump, it always one pump one nozzle</param>
- /// <returns></returns>
- public virtual async Task<bool> AuthorizeWithVolumeAsync(int volumnWithoutDecimalPoint, byte logicalNozzleId)
- {
- logger.LogDebug("Pump: " + this.PumpId + ", " + "start AuthorizeWithVolumeAsync pump with volume: " + volumnWithoutDecimalPoint + ", nozzle: " + logicalNozzleId);
- return false;
- }
- public virtual async Task<bool> FuelingRoundUpByAmountAsync(int amount)
- {
- throw new NotImplementedException();
- }
- #region not implemented
- public async Task<bool> UnAuthorizeAsync(byte logicalNozzleId)
- {
- logger.LogDebug("Pump: " + this.PumpId + ", " + "start UnAuthorizeAsync pump with nozzle: " + logicalNozzleId);
- return false;
- }
- public async Task<bool> SuspendFuellingAsync()
- {
- throw new NotImplementedException();
- }
- public async Task<bool> ResumeFuellingAsync()
- {
- throw new NotImplementedException();
- }
- public async Task<bool> FuelingRoundUpByVolumeAsync(int volume)
- { throw new NotImplementedException(); }
- #endregion
- /// <summary>
- /// </summary>
- protected Dictionary<byte, FuelSaleTransaction> logicalNozzleIdToLastFuelSaleTrxMapping = new Dictionary<byte, FuelSaleTransaction>();
- public void OnFdcServerInit(Dictionary<string, object> parameters)
- {
- if (parameters.ContainsKey("LastPriceChange"))
- {
- }
- /* Load Last sale(from db) for void the case of FC accidently disconnect from Pump in fueling,
- and may cause a fueling trx gone from FC control */
- if (parameters.ContainsKey("LastFuelSaleTrx"))
- {
- // nozzle logical id:lastSale
- //var lastFuelSaleTrxes = parameters["LastFuelSaleTrx"] as Dictionary<byte, FuelSaleTransaction>;
- //foreach (var lastFuelSaleTrx in lastFuelSaleTrxes)
- //{
- // logger.Info("Pump: " + this.pumpId + ", OnFdcServerInit, load last fuel sale " +
- // "on logical nozzle: " + lastFuelSaleTrx.Key + " with value: " + lastFuelSaleTrx.Value);
- // this.logicalNozzleIdToLastFuelSaleTrxMapping.Remove(lastFuelSaleTrx.Key);
- // this.logicalNozzleIdToLastFuelSaleTrxMapping.Add(lastFuelSaleTrx.Key, lastFuelSaleTrx.Value);
- //}
- }
- }
- public async Task<bool> LockNozzleAsync(byte logicalNozzleId)
- {
- return false;
- }
- public async Task<bool> UnlockNozzleAsync(byte logicalNozzleId)
- {
- return false;
- }
- }
- }
|