private void LoadAndShowFiles() { // user never did the config, so create one based on default values. if (!File.Exists(priceFileName)) { MessageBox.Show("未发现人工配置价格文件,将使用默认值生成,请人工修改后保存!"); var mySerializer = new XmlSerializer(typeof(FuelPriceList)); // To write to a file, create a StreamWriter object. using (var myWriter = new StreamWriter(priceFileName)) { mySerializer.Serialize(myWriter, LogicalPump.GetDefaultFuelPriceList()); myWriter.Close(); } } this.textBoxPriceConfig.Text = File.ReadAllText(priceFileName, Encoding.UTF8); // user never did the config, so create one based on default values. if (!File.Exists(pumpConfigFileName)) { MessageBox.Show("未发现人工配置过的油机配置文件,将使用默认值生成,请人工修改后保存!"); var mySerializer = new XmlSerializer(typeof(PumpStationInfo)); // To write to a file, create a StreamWriter object. using (var myWriter = new StreamWriter(pumpConfigFileName)) { mySerializer.Serialize(myWriter, LogicalPump.GetDefaultPumpStationInfo()); myWriter.Close(); } } this.textBoxPumpConfig.Text = File.ReadAllText(pumpConfigFileName, Encoding.UTF8); }
public PumpSettings(LogicalPump pump) { InitializeComponent(); this.pump = pump; this.pumpIcon = pump.PumpIcon; this.priceFileName = @"Xml\" + this.pump.ComPortName + "_PriceConfig.xml"; this.pumpConfigFileName = @"Xml\" + this.pump.ComPortName + "_PumpConfig.xml"; this.Text = "连接在 " + this.pump.ComPortName + " 上的加油机"; this.pump.PropertyChanged += (a, b) => this.Invoke(new Action(this.UpdateUI)); UpdateUI(); }
public void Start() { port.DataReceived += Port_DataReceived; this.buffer = new List <byte>(); port.Open(); logger.Debug(port.PortName + " is Opened? : " + port.IsOpen); if (this.ActiveMode == CommActiveMode.PcActive) { timelyCheck = new Timer(10000); timelyCheck.Elapsed += (_, __) => { // first time to attempt connect to a pump which may not existed in other peer if (this.Pump == null) { // here all send with 0 for avoid downloading the incorrect data to an unknown configuration pump. does it work? var outgoing = new PcGenericInquiryWithRichInfoResponse(); outgoing.SetMessageSequenceNumber(0); outgoing.SetPcTime(DateTime.Now); outgoing.BL_VER = 0; //LogicalPump.GetFundamentalBlackList().Version; outgoing.ADD_BL_VER = 0; //(byte)LogicalPump.GetIncrementalBlackList().Version; outgoing.DEL_BL_VER = 0; //(byte)LogicalPump.GetDeletionBlackList().Version; outgoing.WH_VER = 0; //(byte)LogicalPump.GetWhiteList().Version; outgoing.PRC_VER = 0; //LogicalPump.GetDefaultFuelPriceList().VER_版本; outgoing.Sta_VER = 0; //LogicalPump.GetDefaultPumpStationInfo().Ver; outgoing.SELF_D_VER = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; outgoing.SOFT_FLAG = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; } else { // wait for manual confirm the download files content. if (!this.Pump.AllowCommWithRealPhsicalPump) { return; } 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; outgoing.PRC_VER = this.Pump.FulePriceList.VER_版本; outgoing.Sta_VER = this.Pump.PumpStationInfo.Ver; outgoing.SELF_D_VER = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; outgoing.SOFT_FLAG = PcGenericInquiryWithRichInfoResponse.更新标志.无数据; } }; timelyCheck.Start(); } }
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); }