using Edge.Core.Processor;
using Edge.Core.IndustryStandardInterface.Pump;
using HengShan_Pump_TQC_IFSF.MessageEntity;
using HengShan_Pump_TQC_IFSF.MessageEntity.Incoming;
using HengShan_Pump_TQC_IFSF.MessageEntity.Outgoing;
using Edge.Core.Parser.BinaryParser.Util;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Wayne.FDCPOSLibrary;
using Timer = System.Timers.Timer;
namespace HengShan_Pump_TQC_IFSF
{
public class PetroChinaSilentPumpHandler : PumpHandler
{
///
///
///
///
/// range from 0x21 to 0x24
/// used to locate the remote tqc pump, must a value different from FC
/// used to locate the remote tqc pump
///
public PetroChinaSilentPumpHandler(PumpGroupHandler parent, int pumpId, byte ifsfFuelPointId, byte recipientSubnet, byte recipientNode, string nozzlesXmlConfiguration, Func msgTokenGenerator)
: base(parent, pumpId, ifsfFuelPointId, recipientSubnet, recipientNode, nozzlesXmlConfiguration, msgTokenGenerator)
{
//sample of nozzlesXmlConfiguration
//
//
//
//
//
//
}
public override Task Process(IContext context)
{
this.context = context;
//if (context.Incoming.Message is IfsfMessage ifsfMsg
// && ifsfMsg.MessageType == MessageType.IFSF_MESSAGE_TYPE_UNSOLICITED_ACK)
//{
// //logger.Debug("PumpHandlerSilent with ifsfFpId 0x" + base.ifsfFuelPointId.ToHexLogString() + " will ack the Ack requried event!");
// context.Outgoing.Write(
// new AcknowledgeMessage(
// ifsfMsg.OriginatorSubnet,
// ifsfMsg.OriginatorNode,
// PumpGroupHandler.originatorSubnet, PumpGroupHandler.originatorNode,
// ifsfMsg.MessageToken,
// ifsfMsg.DatabaseId,
// MessageAcknowledgeStatus.ACK_PositiveAcknowledgeDataReceived
// ));
//}
switch (context.Incoming.Message)
{
case FuellingPointDb_FpRunningTransaction_Event fpRunningTrxEvent:
{
logger.Debug("Pump " + this.pumpId + ", fpRunningTrxEvent ifsf FpId: 0x" + fpRunningTrxEvent.TargetFuelPointId.ToHexLogString()
+ ", CurAmt and CurVol(no decimal points): " + fpRunningTrxEvent.CurrentAmount
+ " - " + fpRunningTrxEvent.CurrentVolume);
var operatingNozzle = this.nozzles.First(f => f.LogicalId == this.currentCallingNozzleLogicalId);
//fire fuelling progress.
base.FireOnCurrentFuellingStatusChangeEvent(new FdcTransaction()
{
Nozzle = operatingNozzle,
Amount = fpRunningTrxEvent.CurrentAmount,
Volumn = fpRunningTrxEvent.CurrentVolume,
Price = 0,
Finished = false,
});
break;
}
case FuellingTrxDb_TransactionBufferStatus_Answer trxBufferStatusAnswer:
{
//if (trxBufferStatusAnswer.State.HasValue
// && trxBufferStatusAnswer.State == FuellingTrxDb_TransactionBufferStatus_Event.TransactionState.PAYABLE)
//{
logger.Debug("Pump " + this.pumpId + ", received a Payable trx from TQC with Vol and Amount(without decimal points): "
+ trxBufferStatusAnswer.Volume + " - " + trxBufferStatusAnswer.Amount
+ ", sequenceNo.: " + trxBufferStatusAnswer.TransactionSeqNumber);
var operatingNozzle = this.nozzles.First(f => f.LogicalId == this.currentCallingNozzleLogicalId);
base.FireOnCurrentFuellingStatusChangeEvent(new FdcTransaction()
{
Nozzle = operatingNozzle,
Amount = trxBufferStatusAnswer.Amount,
Volumn = trxBufferStatusAnswer.Volume,
Price = int.Parse(trxBufferStatusAnswer.Price ?? "0"),
SequenceNumberGeneratedOnPhysicalPump = trxBufferStatusAnswer.TransactionSeqNumber,
VolumeTotalizer = Convert.ToInt32(trxBufferStatusAnswer.VolumeTotalizerAfterTrx ?? 0 * Math.Pow(10, this.VolumeTotalizerDecimalDigits)),
Finished = true,
});
//}
break;
}
case FuellingTrxDb_TransactionBufferStatus_Event trx_done_evt:
{
// mute this event to base which will popup a new fdc fuel trx by this event.
// for this project, FuellingTrxDb_TransactionBufferStatus_Answer is the one used for popup fdc fuel trx.
return Task.CompletedTask;
}
default:
base.Process(context);
break;
}
return Task.CompletedTask;
}
public override void HandleFpStatusChange(FuellingPointStatus newStatus, LogicalNozzle stateChangedNozzle)
{
switch (newStatus)
{
case FuellingPointStatus.Idle:
{
if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_READY)
{
this.lastLogicalDeviceState = LogicalDeviceState.FDC_READY;
this.FireOnStateChangeEvent(LogicalDeviceState.FDC_READY, stateChangedNozzle);
}
break;
}
case FuellingPointStatus.CALLING:
{
if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_CALLING)
{
this.lastLogicalDeviceState = LogicalDeviceState.FDC_CALLING;
this.FireOnStateChangeEvent(LogicalDeviceState.FDC_CALLING, stateChangedNozzle);
}
break;
}
case FuellingPointStatus.AUTHORISED:
{
if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_AUTHORISED)
{
this.lastLogicalDeviceState = LogicalDeviceState.FDC_AUTHORISED;
this.FireOnStateChangeEvent(LogicalDeviceState.FDC_AUTHORISED, stateChangedNozzle);
}
break;
}
case FuellingPointStatus.STARTED:
{
if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_STARTED)
{
this.lastLogicalDeviceState = LogicalDeviceState.FDC_STARTED;
this.FireOnStateChangeEvent(LogicalDeviceState.FDC_STARTED, stateChangedNozzle);
}
break;
}
case FuellingPointStatus.FUELLING:
{
if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_FUELLING)
{
this.lastLogicalDeviceState = LogicalDeviceState.FDC_FUELLING;
this.FireOnStateChangeEvent(LogicalDeviceState.FDC_FUELLING, stateChangedNozzle);
}
break;
}
case FuellingPointStatus.Inoperative:
{
if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_OFFLINE)
{
this.lastLogicalDeviceState = LogicalDeviceState.FDC_OFFLINE;
this.FireOnStateChangeEvent(LogicalDeviceState.FDC_OFFLINE, stateChangedNozzle);
}
break;
}
case FuellingPointStatus.Closed:
{
if (this.lastLogicalDeviceState != LogicalDeviceState.FDC_CLOSED)
{
this.lastLogicalDeviceState = LogicalDeviceState.FDC_CLOSED;
this.FireOnStateChangeEvent(LogicalDeviceState.FDC_CLOSED, stateChangedNozzle);
}
break;
}
}
}
public override async Task QueryStatusAsync()
{
return await Task.FromResult(base.lastLogicalDeviceState);
}
public override async Task AuthorizeAsync(byte logicalNozzleId)
{
return false;
}
public override async Task AuthorizeWithAmountAsync(int moneyAmountWithoutDecimalPoint, byte logicalNozzleId)
{
return false;
}
public override async Task AuthorizeWithVolumeAsync(int volumnWithoutDecimalPoint, byte logicalNozzleId)
{
return false;
}
public override async Task ChangeFuelPriceAsync(int newPriceWithoutDecimalPoint, byte logicalNozzleId)
{
return false;
}
public override async Task FuelingRoundUpByAmountAsync(int amount)
{
return false;
}
public override async Task FuelingRoundUpByVolumeAsync(int volume)
{
return false;
}
public override async Task> QueryTotalizerAsync(byte logicalNozzleId)
{
return new System.Tuple(-1, -1);
}
}
}