Example #1
0
        public virtual void Write(MessageTemplateBase outgoing)
        {
            var safe = this.OnComPortDataWriting;
            var arg  = new ComPortEventArg()
            {
                Message = outgoing, Continue = true
            };

            safe?.Invoke(this, arg);
            if (arg.Continue)
            {
                var parser = new Parser();
                var bytes  = parser.Serialize(outgoing);
                logger.Debug(port.PortName + " outgoing a msg raw(Hex): " + bytes.Select(s => s.ToString("X").PadLeft(2, '0'))
                             .Aggregate((acc, n) => acc + " " + n));
                this.port.Write(bytes, 0, bytes.Length);
            }
        }
Example #2
0
        public void ProcessMessage(MessageTemplateBase incomingMsg)
        {
            var safe = this.OnMessageReceiving;
            var arg  = new ComPortEventArg()
            {
                Continue = true, Message = incomingMsg
            };

            safe?.Invoke(this, arg);

            var parser = new Parser();

            // skip the logging for PumpGenericInquiryRequest msg since it's too much.
            if (!(incomingMsg is PumpGenericInquiryRequest))
            {
                logger.Debug(port.PortName + "      " + incomingMsg.ToLogString());
            }

            // Null LogicalPump object indicates comm is not ready yet.
            if (this.Pump == null)
            {
                return;
            }

            this.Pump.Pulse();
            if (!this.Pump.AllowCommWithRealPhsicalPump)
            {
                return;
            }
            if (incomingMsg is PumpGenericInquiryRequest)
            {
                if (this.tryReadPumpGenericInfoAtBeginning)
                {
                    /* here we send the same versions for all downloadable content for simplify */
                    var outgoing = new PcGenericInquiryWithRichInfoResponse();
                    outgoing.SetPcTime(DateTime.Now);
                    outgoing.BL_VER     = this.Pump.BL_VER;
                    outgoing.ADD_BL_VER = this.Pump.ADD_BL_VER;
                    outgoing.DEL_BL_VER = this.Pump.DEL_BL_VER;
                    outgoing.WH_VER     = this.Pump.WH_VER;
                    outgoing.PRC_VER    = this.Pump.PRC_VER;
                    outgoing.Sta_VER    = this.Pump.Sta_VER;
                    outgoing.SELF_D_VER = PcGenericInquiryWithRichInfoResponse.更新标志.无数据;
                    outgoing.SOFT_FLAG  = PcGenericInquiryWithRichInfoResponse.更新标志.无数据;
                    outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++);
                    this.Write(outgoing);
                }
                else
                {
                    /* here we send the confirmed (by cashier, since `AllowCommWithRealPhsicalPump` is true now) versions for all downloadable content*/
                    var outgoing = new PcGenericInquiryWithRichInfoResponse();
                    outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++);
                    outgoing.SetPcTime(DateTime.Now);
                    outgoing.BL_VER     = LogicalPump.GetFundamentalBlackList().Version;
                    outgoing.ADD_BL_VER = (byte)LogicalPump.GetIncrementalBlackList().Version;
                    outgoing.DEL_BL_VER = (byte)LogicalPump.GetDeletionBlackList().Version;
                    outgoing.WH_VER     = (byte)LogicalPump.GetWhiteList().Version;
                    // below use the latest one.
                    outgoing.PRC_VER    = this.Pump.FulePriceList.VER_版本;
                    outgoing.Sta_VER    = this.Pump.PumpStationInfo.Ver;
                    outgoing.SELF_D_VER = PcGenericInquiryWithRichInfoResponse.更新标志.无数据;
                    outgoing.SOFT_FLAG  = PcGenericInquiryWithRichInfoResponse.更新标志.无数据;
                    this.Write(outgoing);
                }
            }
            else if (incomingMsg is PcReadPumpGenericInfoResponse)
            {
                var incoming = incomingMsg as PcReadPumpGenericInfoResponse;
                this.Pump.ReloadFrom(incoming);
            }
            else if (incomingMsg is PumpNotifyTransactionDoneRequest)
            {
                var incoming = incomingMsg as PumpNotifyTransactionDoneRequest;

                this.Pump.StackTransaction(LogicalTransaction.LoadFrom(incoming));
                if (this.Pump.Transactions.Count() > this.Pump.KeepMaxTransactionNumber)
                {
                    this.Pump.ClearTransaction(0);
                }
                var outgoing = new PumpNotifyTransactionDoneResponse();
                outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++);
                outgoing.Result = PumpNotifyTransactionDoneResponse.PumpNotifyTransactionDoneResponseResult.正确;
                this.Write(outgoing);

                if (this.Pump.ReadPumpAccumualtorAfterTrxDone)
                {
                    var request = new PcAskReadPumpAccumulator();
                    request.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++);
                    this.Write(request);
                }
            }
            else if (incomingMsg is PumpAskDataDownloadRequest)
            {
                var incoming = incomingMsg as PumpAskDataDownloadRequest;
                var outgoing = new PumpAskDataDownloadResponse();
                outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++);
                int downloadingContentLength = 0;
                switch (incoming.Content_内容)
                {
                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.基础黑名单:
                    downloadingContentLength = parser.Serialize(LogicalPump.GetWhiteList()).Length;
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新增黑名单:
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新删黑名单:
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.白名单:
                    downloadingContentLength = parser.Serialize(LogicalPump.GetWhiteList()).Length;
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油品油价表:
                    downloadingContentLength = parser.Serialize(LogicalPump.GetDefaultFuelPriceList()).Length;
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油站通用信息:
                    downloadingContentLength = parser.Serialize(LogicalPump.GetDefaultPumpStationInfo()).Length;
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.私有数据:
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.载程序:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                // tell pump this length of content will start downloading
                outgoing.BL_LEN_长度  = downloadingContentLength;
                outgoing.Content_内容 = incoming.Content_内容;
                this.Write(outgoing);
            }
            else if (incomingMsg is PumpDataDownloadRequest)
            {
                var incoming = incomingMsg as PumpDataDownloadRequest;
                var outgoing = new PumpDataDownloadResponse();
                outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++);
                byte[] fullDownloadingBytes = null;
                switch (incoming.Content_内容)
                {
                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.基础黑名单:
                    fullDownloadingBytes = parser.Serialize(LogicalPump.GetWhiteList());
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新增黑名单:
                    fullDownloadingBytes = parser.Serialize(LogicalPump.GetIncrementalBlackList());
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.新删黑名单:
                    fullDownloadingBytes = parser.Serialize(LogicalPump.GetDeletionBlackList());
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.白名单:
                    fullDownloadingBytes = parser.Serialize(LogicalPump.GetWhiteList());
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油品油价表:
                    fullDownloadingBytes = parser.Serialize(LogicalPump.GetDefaultFuelPriceList());
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.油站通用信息:
                    fullDownloadingBytes = parser.Serialize(LogicalPump.GetDefaultPumpStationInfo());
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.私有数据:
                    break;

                case PumpAskDataDownloadRequest.PumpAskDataDownloadType.载程序:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                outgoing.Content_内容        = incoming.Content_内容;
                outgoing.S_OFFSET_段偏移      = incoming.S_OFFSET_段偏移;
                outgoing.SEG_段数_segs       = incoming.SEG_段数_segs;
                outgoing.DATA_Content_数据内容 =
                    fullDownloadingBytes.Skip(16 * incoming.S_OFFSET_段偏移).Take(incoming.SEG_段数_segs * 16).ToList();
                this.Write(outgoing);
            }
            else if (incomingMsg is PumpStateChangeRequest)
            {
                //var previousState = Pump.PumpState;
                var incoming = incomingMsg as PumpStateChangeRequest;
                Pump.UpdatePumpStateByMessage(incoming);
            }
            else if (incomingMsg is PcAskReadPumpAccumulatorResponse)
            {
                var incoming = incomingMsg as PcAskReadPumpAccumulatorResponse;
                foreach (var incomingNozzleInfo in incoming.MZN_单个油枪)
                {
                    if (Pump.Nozzles_油枪组.Any(p => p.NozzleNumber == incomingNozzleInfo.NZN_枪号))
                    {
                        Pump.Nozzles_油枪组.First(p => p.NozzleNumber == incomingNozzleInfo.NZN_枪号).VolumnAccumulator =
                            incomingNozzleInfo.V_TOT_升累计;
                    }
                    else
                    {
                        Pump.AddNozzle(new LogicalNozzle()
                        {
                            NozzleNumber = incomingNozzleInfo.NZN_枪号, VolumnAccumulator = incomingNozzleInfo.V_TOT_升累计
                        });
                    }
                }
            }
            else if (incomingMsg is PumpInquiryBlackAndWhiteListRequest)
            {
                var incoming = incomingMsg as PumpInquiryBlackAndWhiteListRequest;
                var outgoing = new PumpInquiryBlackAndWhiteListResponse();
                outgoing.SetMessageSequenceNumber(Pump.LastSendMsgSequenceNumber++);
                outgoing.ASN卡应用号     = incoming.ASN卡应用号;
                outgoing.M_FLAG_匹配标志 = PumpInquiryBlackAndWhiteListResponse.PumpInquiryBlackAndWhiteListResult.匹配;
                this.Write(outgoing);
            }

            var safe1 = this.OnMessageProcessed;
            var arg1  = new ComPortEventArg()
            {
                Continue = true, Message = incomingMsg
            };

            safe1?.Invoke(this, arg1);
        }