123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- 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<FuelingPoint>
- {
- int screen = 0;
- private int idleRequestId;
- private int trxOpRequestId;
- private List<EpsTransactionModel> trxList;
- private byte? sqNo = null;
- public Idle()
- {
- #region testing code
- /*
- trxList = new List<EpsTransactionModel>();
- 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<NozzleLiftedEventArgs>;
- Main.CurrentNozzleId = nlEvent.EventArgs.NozzleId;
- transition = new Transition(this, TransitionType.NozzleLifted);
- stateEngineEvent.Handled = true;
- break;
- case EventType.CarPlateScanned:
- var genericEvent = stateEngineEvent as GenericEvent<CarPlateTrxRequest>;
- //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<DisplayResponseEventArgs>;
- 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<TransactionOperationEventArgs> trxOpEvent = stateEngineEvent as GenericEvent<TransactionOperationEventArgs>;
- 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<CardReaderStateEventArgs>;
- 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<FuelingDoneEventArgs>;
- 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
- }
- }
|