using SinochemCarplateService.Models; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml.Serialization; using Wayne.Lib; using Wayne.Lib.StateEngine; using Wayne.Lib.StateEngine.Generic; using WayneChina_IcCardReader_SinoChem.MessageEntity.Incoming; namespace SinochemInternetPlusApp.States { class Idle : TimeoutState { int screen = 0; private int idleRequestId; private int trxOpRequestId; private List trxList; private byte? sqNo = null; public Idle() { #region testing code /* trxList = new List(); trxList.Clear(); trxList.Add(new EpsTransactionModel { id = 120, jihao = 1, youpin = "92#", qty = 10, danjia = 6.78, amount = 67.8, xf_date = DateTime.Now.Date, xf_time = DateTime.Now.TimeOfDay.ToString(), liushuino = "12345", fzqty = "", fzamount = 0, trx_status = EpsTrxStatus.BeforeFueling, mop = 1, car_number = "京NS1695", card_no = "1234567", ttc = "IOT1234567890", token = "TestToken", tid = "1", mac = "", balance_before_trx = 250, real_pay_amount = 67.8, auth_time = DateTime.Now.ToString("yyMMdd HHmmSS"), bill_id = "1", created_time = DateTime.Now, shift_id = "1", business_date = DateTime.Now.ToString("yyMMddHHmmSS") }); trxList.Add(new EpsTransactionModel { id = 121, jihao = 1, youpin = "92#", qty = 10, danjia = 6.78, amount = 167.8, xf_date = DateTime.Now.Date, xf_time = DateTime.Now.TimeOfDay.ToString(), liushuino = "12345", fzqty = "", fzamount = 0, trx_status = EpsTrxStatus.PaymentOk, mop = 1, car_number = "京A88888", card_no = "1234567", ttc = "IOT1234567890", token = "TestToken", tid = "1", mac = "", balance_before_trx = 250, real_pay_amount = 67.8, auth_time = DateTime.Now.ToString("yyMMdd HHmmSS"), bill_id = "1", created_time = DateTime.Now, shift_id = "1", business_date = DateTime.Now.ToString("yyMMddHHmmSS") }); trxList.Add(new EpsTransactionModel { id = 122, jihao = 1, youpin = "92#", qty = 10, danjia = 6.78, amount = 200, xf_date = DateTime.Now.Date, xf_time = DateTime.Now.TimeOfDay.ToString(), liushuino = "12345", fzqty = "", fzamount = 0, trx_status = EpsTrxStatus.PaymentOk, mop = 1, car_number = "冀J25089", card_no = "1234567", ttc = "IOT1234567890", token = "TestToken", tid = "1", mac = "", balance_before_trx = 250, real_pay_amount = 67.8, auth_time = DateTime.Now.ToString("yyMMdd HHmmSS"), bill_id = "1", created_time = DateTime.Now, shift_id = "1", business_date = DateTime.Now.ToString("yyMMddHHmmSS") }); trxList.Add(new EpsTransactionModel { id = 123, jihao = 1, youpin = "92#", qty = 10, danjia = 6.78, amount = 366.5, xf_date = DateTime.Now.Date, xf_time = DateTime.Now.TimeOfDay.ToString(), liushuino = "12345", fzqty = "", fzamount = 0, trx_status = EpsTrxStatus.PaymentFailed, mop = 1, car_number = "沪B35L36", card_no = "1234567", ttc = "IOT1234567890", token = "TestToken", tid = "1", mac = "", balance_before_trx = 250, real_pay_amount = 67.8, auth_time = DateTime.Now.ToString("yyMMdd HHmmSS"), bill_id = "1", created_time = DateTime.Now, shift_id = "1", business_date = DateTime.Now.ToString("yyMMddHHmmSS") }); trxList.Add(new EpsTransactionModel { id = 124, jihao = 1, youpin = "95#", qty = 10, danjia = 7.33, amount = 73.3, xf_date = DateTime.Now.Date, xf_time = DateTime.Now.TimeOfDay.ToString(), liushuino = "12345", fzqty = "", fzamount = 0, trx_status = EpsTrxStatus.PaymentOk, mop = 1, car_number = "京QD8R01", card_no = "1234567", ttc = "IOT1234567890", token = "TestToken", tid = "1", mac = "", balance_before_trx = 250, real_pay_amount = 67.8, auth_time = DateTime.Now.ToString("yyMMdd HHmmSS"), bill_id = "1", created_time = DateTime.Now, shift_id = "1", business_date = DateTime.Now.ToString("yyMMddHHmmSS") }); */ #endregion } protected override void Enter(StateEntry stateEntry, ref Transition transition) { base.Enter(stateEntry, ref transition); if (Main.CardReaderDisabled) { Main.OpenCardReader(out sqNo); if (sqNo.HasValue) Main.IdleStateCardReaderSqNo = sqNo.Value; } if (Main.Eps.CarPlateHandler != null) { int remainder = screen % 5; if (remainder == 0) { var text = CreateDisplayIdleCommand(); DebugLog(text); Main.Eps.CarPlateHandler.BroadcastMessageViaFdc(text); if (!Main.ContainsRequestId(idleRequestId)) { DebugLog($"Adding RequestId: {idleRequestId} to the list"); Main.AddRequestId(idleRequestId); } } else if (remainder == 1) { var text = CreateDisplayWelcomeCommand(); DebugLog(text); Main.Eps.CarPlateHandler.BroadcastMessageViaFdc(text); } else { var text = CreateDisplayTrxListCommand(remainder); DebugLog(text); Main.Eps.CarPlateHandler.BroadcastMessageViaFdc(text); if (remainder == 4) { var text1 = CreateDisplayTrxResultCommand(screen); DebugLog(text1); Main.Eps.CarPlateHandler.BroadcastMessageViaFdc(text1); } } //used for 15" screen test //screen++; } } protected override void HandleNonTimeoutEvent(StateEngineEvent stateEngineEvent, ref Transition transition) { if (stateEngineEvent.Type is EventType) { switch ((EventType)stateEngineEvent.Type) { case EventType.NozzleLifted: var nlEvent = stateEngineEvent as GenericEvent; Main.CurrentNozzleId = nlEvent.EventArgs.NozzleId; transition = new Transition(this, TransitionType.NozzleLifted); stateEngineEvent.Handled = true; break; case EventType.CarPlateScanned: var genericEvent = stateEngineEvent as GenericEvent; //Current physical nozzle to be used Main.CurrentNozzleId = Convert.ToInt32(genericEvent.EventArgs.gun); Main.CreateEpsTransaction(TransactionMode.CarPlateMode); Main.CurrentEpsTrx.Model.car_number = genericEvent.EventArgs.car_Number; //Main.CurrentEpsTrx.Model.card_no = genericEvent.EventArgs.card_No; // will be set when payment ok Main.CurrentEpsTrx.Model.ttc = genericEvent.EventArgs.ttc; Main.CurrentEpsTrx.Model.balance_before_trx = genericEvent.EventArgs.amount; Main.CurrentEpsTrx.Model.token = genericEvent.EventArgs.token; Main.CurrentEpsTrx.Model.tid = genericEvent.EventArgs.tid; Main.CurrentEpsTrx.Model.youpin = genericEvent.EventArgs.oilName; Main.CurrentEpsTrx.Model.xf_date = DateTime.Parse(DateTime.Now.Date.ToString("yyyy-MM-dd")); Main.CurrentEpsTrx.SaveToDb(); Main.ClearAllRequestIds(); transition = new Transition(this, TransitionType.CarPlateScanned); stateEngineEvent.Handled = true; break; case EventType.ShutdownRequest: transition = new Transition(this, TransitionType.Shutdown); stateEngineEvent.Handled = true; break; case EventType.DisplayResponseReceived: { var e = stateEngineEvent as GenericEvent; if (e != null && e.EventArgs != null) { if (idleRequestId == e.EventArgs.DispResponse.RequestId || trxOpRequestId == e.EventArgs.DispResponse.RequestId) { e.Handled = true; var reqId = e.EventArgs.DispResponse.RequestId; if (Main.ContainsRequestId(reqId)) { Main.RemoveRequestId(reqId); DebugLog($"Removed RequestId : {reqId} from the list"); } } } } break; //Delete pending fueling trx when 15" screen is in Idle case EventType.TrxOpRequest: { GenericEvent trxOpEvent = stateEngineEvent as GenericEvent; if (trxOpEvent != null && trxOpEvent.EventArgs != null) { trxOpEvent.Handled = true; if (trxOpEvent.EventArgs.TrxOp.OpType == OpType.Delete) { var allTrx = EpsTransactionQuery.GetValidCarPlateEpsTrxModels( Main.GetAllNozzlesOnThisSide(), -1, // force all trxes Main.DebugLogger); //Delete the matching trx int trxId = trxOpEvent.EventArgs.TrxOp.TrxId; bool hasTrxDeleted = false; foreach (var trx in allTrx) { if ((trx.id == trxId) && Main.GetAllNozzlesOnThisSide().Contains(trx.jihao) && (trx.trx_status == EpsTrxStatus.BeforeFueling)) { hasTrxDeleted = true; DebugLog($"Deleting Trx, id = {trxId}"); EpsTransaction.RestroeEpsTrxFrom(trx).UpdateTrxStatusToDb(EpsTrxStatus.Removed); } } if (hasTrxDeleted) { var validTrx = EpsTransactionQuery.GetValidCarPlateEpsTrxModels( Main.GetAllNozzlesOnThisSide(), ConfigurationValues.AlreadyDoneEpsTrxCountPerDisplay, Main.DebugLogger); //Refresh 15" screen var text = Main.Eps.CreateDisplayTrxCommand(validTrx, out trxOpRequestId, 0); if (Main.Eps.CarPlateHandler != null) { DebugLog(text); Main.Eps.CarPlateHandler.BroadcastMessageViaFdc(text); if (!Main.ContainsRequestId(trxOpRequestId)) Main.AddRequestId(trxOpRequestId); } } } } } break; case EventType.ReaderStateChanged: { var e = stateEngineEvent as GenericEvent; if (e != null && e.EventArgs != null) { if (e.EventArgs.CardReaderState.State == CardReaderState.CardInserted) { e.Handled = true; transition = new Transition(this, TransitionType.CardInserted); } else if (e.EventArgs.CardReaderState.State == CardReaderState.Idle) { e.Handled = true; return; } } } break; case EventType.CardReaderAck: { if (sqNo.HasValue && sqNo.Value == sqNo.Value) { stateEngineEvent.Handled = true; sqNo = null; Main.CardReaderDisabled = false; } } break; case EventType.FuelingDone: var fdEvent = stateEngineEvent as GenericEvent; if (fdEvent != null && fdEvent.EventArgs != null) { var nozzleId = fdEvent.EventArgs.NozzleId; var liushuino = fdEvent.EventArgs.FuelingSqNo; var amount = fdEvent.EventArgs.Amount; var authId = fdEvent.EventArgs.AuthId; DebugLog(string.Format($"Floating fuel trx, liushuihao({liushuino}), amount({amount}), authId({authId})")); MultiFusionsSupport.CopyXiaofei2ToTargetFusion(nozzleId, Convert.ToString(liushuino), Main.DebugLogger); fdEvent.Handled = true; } break; default: DebugLog("ATTENTION unhandled event " + stateEngineEvent); stateEngineEvent.Handled = true; break; } } } protected override void Timeout(ref Transition transition) { transition = new Transition(this, TransitionType.Done); } protected override int TimeoutInterval => TimeoutValues.GetValueInMilliSec(TimeoutValues.FuelingPoint.Idle, 15); private string CreateDisplayIdleCommand() { Display display; string cmdText = ""; XmlSerializer serializer = new XmlSerializer(typeof(Display)); MemoryStream ms; StreamReader sr; display = new Display { ScreenType = ScreenType.Idle, RequestId = Main.Eps.GetNextRequestId(), TimeoutSpecified = true, Timeout = TimeoutValues.GetValueInSec(TimeoutValues.Misc.BigScreenBackToIdle, 300) }; idleRequestId = display.RequestId; display.CompanyContactInfo = new DisplayCompanyContactInfo { Tel = "010-59569575", Address = "世界500强企业\n中国第四大国家石油公司" }; display.StationInfo = new DisplayStationInfo { StationNo = Main.StationNo, StationName = Main.StationName }; display.PumpInfo = new DisplayPumpInfo { Id = 1, NozzleId = Main.AssociatedNozzles.First() }; ms = new MemoryStream(); serializer.Serialize(ms, display); ms.Position = 0; sr = new StreamReader(ms, true); cmdText = sr.ReadToEnd(); return cmdText; } #region Used for 15" screen debug private string CreateDisplayWelcomeCommand() { Display display; string cmdText = ""; XmlSerializer serializer = new XmlSerializer(typeof(Display)); MemoryStream ms; StreamReader sr; display = new Display(); display.ScreenType = ScreenType.Welcome; display.TimeoutSpecified = true; display.Timeout = 10; display.PumpInfo = new DisplayPumpInfo { Id = 1, NozzleId = 1 }; display.MemberInfo = new DisplayMemberInfo { LicensePlateNo = "京NS1695",//epsTrxModel.car_number, Id = "" //epsTrxModel.card_no }; ms = new MemoryStream(); serializer.Serialize(ms, display); ms.Position = 0; sr = new StreamReader(ms, true); cmdText = sr.ReadToEnd(); return cmdText; } private string CreateDisplayTrxListCommand(int screen) { Display display; string cmdText = ""; XmlSerializer serializer = new XmlSerializer(typeof(Display)); MemoryStream ms; StreamReader sr; display = new Display(); display.ScreenType = ScreenType.ShowTrxList; display.TimeoutSpecified = true; display.Timeout = 10; display.PumpInfo = new DisplayPumpInfo { Id = 1, NozzleId = 1 }; var state = TrxStatus.ReadyForFillingStart; if (screen == 2) { state = TrxStatus.ReadyForFillingStart; display.Timeout = 30; } else if (screen == 3) state = TrxStatus.PendingForPayment; else if (screen == 4) state = TrxStatus.Success; display.TrxList = new DisplayTrx[trxList.Count]; for (int i = 0; i < trxList.Count; i++) { display.TrxList[i] = Main.Eps.ConvertEpsTrxModelToDisplayTrx(trxList[i]); } display.TrxList[0].State = state; //display.TrxList = new DisplayTrx[] //{ // new DisplayTrx // { // Id = "200", // FillingInfo = new DisplayTrxFillingInfo // { // Amount = 200, // NozzleId = 1, // ProductType = "95#", // UnitPrice = 6.78m, // Volume = 35, // ProductDiscription = "汽油" // }, // MemberInfo = new DisplayTrxMemberInfo // { // LicensePlateNo = "京NS1695", // Id = "1234567" // }, // State = state, // TimeStamp = new DisplayTrxTimeStamp // { // StartTime = DateTime.Now, // FinishTime =DateTime.Now // } // }, // new DisplayTrx // { // Id = "101", // FillingInfo = new DisplayTrxFillingInfo // { // Amount = 211, // NozzleId = 1, // ProductType = "95#", // UnitPrice = 6.78m, // Volume = 35, // ProductDiscription = "汽油" // }, // MemberInfo = new DisplayTrxMemberInfo // { // LicensePlateNo = "京A22222", // Id = "1234567" // }, // State = TrxStatus.Success, // TimeStamp = new DisplayTrxTimeStamp // { // StartTime = DateTime.Now, // FinishTime =DateTime.Now // } // }, // new DisplayTrx // { // Id = "102", // FillingInfo = new DisplayTrxFillingInfo // { // Amount = 222, // NozzleId = 1, // ProductType = "95#", // UnitPrice = 6.78m, // Volume = 35, // ProductDiscription = "汽油" // }, // MemberInfo = new DisplayTrxMemberInfo // { // LicensePlateNo = "京A33333", // Id = "1234567" // }, // State = TrxStatus.Success, // TimeStamp = new DisplayTrxTimeStamp // { // StartTime = DateTime.Now, // FinishTime =DateTime.Now // } // }, // new DisplayTrx // { // Id = "103", // FillingInfo = new DisplayTrxFillingInfo // { // Amount = 233, // NozzleId = 1, // ProductType = "95#", // UnitPrice = 6.78m, // Volume = 35, // ProductDiscription = "汽油" // }, // MemberInfo = new DisplayTrxMemberInfo // { // LicensePlateNo = "京A44444", // Id = "1234567" // }, // State = TrxStatus.Success, // TimeStamp = new DisplayTrxTimeStamp // { // StartTime = DateTime.Now, // FinishTime =DateTime.Now // } // }, // new DisplayTrx // { // Id = "104", // FillingInfo = new DisplayTrxFillingInfo // { // Amount = 244, // NozzleId = 1, // ProductType = "95#", // UnitPrice = 6.78m, // Volume = 35, // ProductDiscription = "汽油" // }, // MemberInfo = new DisplayTrxMemberInfo // { // LicensePlateNo = "京A55555", // Id = "1234567" // }, // State = TrxStatus.Success, // TimeStamp = new DisplayTrxTimeStamp // { // StartTime = DateTime.Now, // FinishTime =DateTime.Now // } // } //}; ms = new MemoryStream(); serializer.Serialize(ms, display); ms.Position = 0; sr = new StreamReader(ms, true); cmdText = sr.ReadToEnd(); return cmdText; } private string CreateDisplayTrxResultCommand(int i) { Display display; string cmdText = ""; XmlSerializer serializer = new XmlSerializer(typeof(Display)); MemoryStream ms; StreamReader sr; display = new Display(); display.ScreenType = ScreenType.TrxResult; display.TimeoutSpecified = true; display.Timeout = 10; display.PumpInfo = new DisplayPumpInfo { Id = 1, NozzleId = 1 }; display.TrxList = new DisplayTrx[] { new DisplayTrx { Id = "200", State = TrxStatus.Success, MemberInfo = new DisplayTrxMemberInfo { LicensePlateNo = "京NS1695",//epsTrxModel.car_number, Id = "1234567"//epsTrxModel.card_no }, FillingInfo = new DisplayTrxFillingInfo { PumpId = 1, NozzleId = 1, ProductNo = 1, ProductDiscription = "92#", Amount = 105, Volume = 18.90m } } }; ms = new MemoryStream(); serializer.Serialize(ms, display); ms.Position = 0; sr = new StreamReader(ms, true); cmdText = sr.ReadToEnd(); ms.Close(); sr.Close(); return cmdText; } #endregion } }