123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- using Edge.Core.Processor;
- using Edge.Core.IndustryStandardInterface.Pump;
- using Dfs.WayneChina.CardTrxMonitor.Models;
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Text;
- using Wayne.FDCPOSLibrary;
- using System.Threading.Tasks;
- using System.Threading;
- using Dfs.WayneChina.CardTrxManager;
- using Dfs.WayneChina.CardTrxManager.TrxSubmitter;
- namespace Dfs.WayneChina.CardTrxMonitor
- {
-
-
-
- public class CardTrxMonitorApp : IAppProcessor, IFdcPumpController
- {
- #region Properties
- public string Name => "CardTrxMonitor";
- public int PumpId { get; private set; }
- public byte NozzleId { get; private set; }
-
- public int PumpPhysicalId => 0;
- private List<LogicalNozzle> nozzles = new List<LogicalNozzle>();
- public IEnumerable<LogicalNozzle> Nozzles => nozzles;
-
- public int AmountDecimalDigits => 2;
- public int VolumeDecimalDigits => 2;
- public int PriceDecimalDigits => 2;
- public int VolumeTotalizerDecimalDigits => 2;
- public string MetaConfigName { get; set; }
- #endregion
- #region Fields
- private System.Timers.Timer _timer;
- private int scanInterval;
- private int siteNozzleNo;
- private int bindingBarcode;
- private CloudCredential cloudCredential;
- private TrxSubmitter submitter;
- #endregion
- #region Logger
- NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("CardTrxMonitor");
- #endregion
- #region Constructor
- public CardTrxMonitorApp(int pumpId, int nozzleId, int siteNozzleNo, int barcode, int scanInterval,
- string username, string password, string authServiceBaseUrl, string transactionServiceBaseUrl, string deviceSN)
- {
- PumpId = pumpId;
- NozzleId = Convert.ToByte(nozzleId);
- this.siteNozzleNo = siteNozzleNo;
- bindingBarcode = barcode;
- nozzles.Add(new LogicalNozzle(pumpId, 1, NozzleId, null));
- this.scanInterval = scanInterval;
- cloudCredential = new CloudCredential
- {
- UserName = username,
- Password = password,
- AuthServiceBaseUrl = authServiceBaseUrl,
- TransactionServiceBaseUrl = transactionServiceBaseUrl,
- DeviceSN = deviceSN
- };
- submitter = new TrxSubmitter(PumpId, cloudCredential);
- }
- #endregion
- #region IApplication implementation
- public void Init(IEnumerable<IProcessor> processors)
- {
- }
- public Task<bool> Start()
- {
- _timer = new System.Timers.Timer();
- _timer.Interval = scanInterval * 1000;
- _timer.Elapsed += TimeElapsed;
- _timer.Start();
- return Task.FromResult(true);
- }
- private void TimeElapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- Log("Timer reset...");
- try
- {
- FindTransaction();
- }
- catch (Exception ex)
- {
- Log($"{ex}");
- }
- }
- private void Log(string message)
- {
- logger.Info($"Monitor{PumpId}, {message}");
- }
- private void FindTransaction()
- {
- Log("start to check db...");
- try
- {
- using (var _context = new SpsDbContext())
- {
- var cardTrx = _context.TCardtrx.FirstOrDefault(t => t.PumpNo == PumpId);
- if (cardTrx != null && IsTrdTypeAllowed(cardTrx.TrdType) && cardTrx.Mon != 0)
- {
-
-
- _timer.Stop();
- if (cardTrx.TrdType == 6)
- {
- Log("Non-card fuelling");
- }
- else if (cardTrx.TrdType == 1)
- {
- Log("Gray card transaction, remove it otherwise it will block further transactions");
- RemoveTransaction(cardTrx, _context);
- }
-
- if (cardTrx.PaymodeId == 103)
- {
- Log($"Found finished IC card transaction: Pump: {cardTrx.PumpNo}, Volume: {cardTrx.Vol}, Amount: {cardTrx.Mon}");
- FdcTransaction fdcTransaction = new FdcTransaction();
- fdcTransaction.Amount = cardTrx.Mon.Value;
- fdcTransaction.Volumn = Convert.ToInt32(cardTrx.Vol.Value);
- fdcTransaction.VolumeTotalizer = Convert.ToInt32(cardTrx.EndPumpId.Value);
- fdcTransaction.Price = Convert.ToInt32(cardTrx.Prc.Value);
- fdcTransaction.SequenceNumberGeneratedOnPhysicalPump = Convert.ToInt32(cardTrx.Gid);
- fdcTransaction.Nozzle = new LogicalNozzle(cardTrx.PumpNo, 0, Convert.ToByte(NozzleId), 0);
- fdcTransaction.Finished = true;
- OnCurrentFuellingStatusChange?.Invoke(this, new FdcTransactionDoneEventArg(fdcTransaction));
- RemoveTransaction(cardTrx, _context);
- }
- else if (cardTrx.PaymodeId == 100)
- {
- Log($"Found customer card transaction: Pump: {cardTrx.PumpNo}, Volume: {cardTrx.Vol}, Amount: {cardTrx.Mon}");
- var submitResult = Task.Run(async () =>
- {
- var result = await SubmitTrxAsync(cardTrx, bindingBarcode);
- return result;
- });
- Log($"submit transaction, result: {submitResult.Result}");
- RemoveTransaction(cardTrx, _context);
- }
- else
- {
-
-
- }
- }
- else if (cardTrx != null && cardTrx.Mon == 0)
- {
- Log($"Zero amount transaction, Pump No: {cardTrx.PumpNo}, SeqNo: {cardTrx.SeqNo}, delete it!");
- RemoveTransaction(cardTrx, _context);
- }
- else if (cardTrx != null && cardTrx.Gid != 0)
- {
- Log($"Trd Type: {cardTrx?.TrdType}");
- RemoveTransaction(cardTrx, _context);
- }
-
- _timer.Start();
- }
- }
- catch (Exception ex)
- {
- Log($"Database operation: {ex}");
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- private bool IsTrdTypeAllowed(byte trdType)
- {
- if (trdType == 0 || trdType == 2 || trdType == 6)
- return true;
- return false;
- }
- private void RemoveTransaction(TCardtrx cardTrx, SpsDbContext context)
- {
- try
- {
- context.Remove(cardTrx);
- context.SaveChanges();
- }
- catch (Exception ex)
- {
- Log($"Exception in removing trx: {ex}");
- }
- }
- private async Task<bool> SubmitTrxAsync(TCardtrx cardTrx, int barcode)
- {
- var clientTrxInfo = new ClientTrxInfo
- {
- CardNo = cardTrx.CardNo,
- CurrentCardBalance = Convert.ToDecimal(cardTrx.CardBal.Value) / 100,
- UnitPrice = Convert.ToDecimal(cardTrx.Prc) / 100,
- Amount = Convert.ToDecimal(cardTrx.Mon) / 100,
- PayAmount = Convert.ToDecimal(cardTrx.RealMon) / 100,
- Volume = Convert.ToDecimal(cardTrx.Vol) / 100,
- PumpId = cardTrx.PumpNo,
- NozzleId = Convert.ToByte(cardTrx.NozNo),
- SiteNozzleNo = siteNozzleNo,
- Barcode = barcode,
- FuelingStartTime = cardTrx.Ttctime,
- FuelingFinishedTime = cardTrx.TtctimeEnd.Value,
- SeqNo = Convert.ToInt32(cardTrx.SeqNo)
- };
- var result = await submitter.SubmitTrxAsync(clientTrxInfo);
- return result;
- }
- public Task<bool> Stop()
- {
- if (_timer != null)
- {
- _timer.Dispose();
- }
- return Task.FromResult(true);
- }
- #endregion
- #region Event handlers
- public event EventHandler<FdcPumpControllerOnStateChangeEventArg> OnStateChange;
- public event EventHandler<FdcTransactionDoneEventArg> OnCurrentFuellingStatusChange;
- #endregion
- #region Methods, not useful
- public bool Authorize(byte logicalNozzleId)
- {
- return false;
- }
- public bool AuthorizeWithAmount(int moneyAmountWithoutDecimalPoint, byte logicalNozzleId)
- {
- return false;
- }
- public bool AuthorizeWithVolumn(int volumnWithoutDecimalPoint, byte logicalNozzleId)
- {
- return false;
- }
- public bool ChangeFuelPrice(int newPriceWithoutDecimalPoint, byte logicalNozzleId)
- {
- return false;
- }
- public bool FuelingRoundUpByAmount(int amount)
- {
- return false;
- }
- public bool FuelingRoundUpByVolumn(int volume)
- {
- return false;
- }
- public void OnFdcServerInit(Dictionary<string, object> parameters)
- {
-
- }
- public LogicalDeviceState QueryStatus()
- {
- return LogicalDeviceState.FDC_READY;
- }
- public Tuple<int, int> QueryTotalizer(byte logicalNozzleId)
- {
- return new Tuple<int, int>(-1, -1);
- }
- public bool ResumeFuelling()
- {
- return false;
- }
- public bool SuspendFuelling()
- {
- return false;
- }
- public bool UnAuthorize(byte logicalNozzleId)
- {
- return false;
- }
- public Task<LogicalDeviceState> QueryStatusAsync()
- {
- throw new NotImplementedException();
- }
- public Task<Tuple<int, int>> QueryTotalizerAsync(byte logicalNozzleId)
- {
- return Task.FromResult(new Tuple<int, int>(-1, -1));
- }
- public Task<bool> SuspendFuellingAsync()
- {
- throw new NotImplementedException();
- }
- public Task<bool> ResumeFuellingAsync()
- {
- throw new NotImplementedException();
- }
- public Task<bool> ChangeFuelPriceAsync(int newPriceWithoutDecimalPoint, byte logicalNozzleId)
- {
- throw new NotImplementedException();
- }
- public Task<bool> AuthorizeAsync(byte logicalNozzleId)
- {
- throw new NotImplementedException();
- }
- public Task<bool> UnAuthorizeAsync(byte logicalNozzleId)
- {
- throw new NotImplementedException();
- }
- public Task<bool> AuthorizeWithAmountAsync(int moneyAmountWithoutDecimalPoint, byte logicalNozzleId)
- {
- throw new NotImplementedException();
- }
- public Task<bool> AuthorizeWithVolumeAsync(int volumnWithoutDecimalPoint, byte logicalNozzleId)
- {
- throw new NotImplementedException();
- }
- public Task<bool> FuelingRoundUpByAmountAsync(int amount)
- {
- throw new NotImplementedException();
- }
- public Task<bool> FuelingRoundUpByVolumeAsync(int volume)
- {
- throw new NotImplementedException();
- }
- public async Task<bool> LockNozzleAsync(byte logicalNozzleId)
- {
- return false;
- }
- public async Task<bool> UnlockNozzleAsync(byte logicalNozzleId)
- {
- return false;
- }
- #endregion
- }
- }
|