public void OpenHSMSPort() { try { string xmlConfigFile = AppDomain.CurrentDomain.BaseDirectory + "ConfigureFiles\\HSMS\\HostInfo.xml"; //_logWriter = logWriter; foreach (XElement var in XElement.Load(xmlConfigFile).Nodes()) { _hostInfo = new HostInfo(var); //JsonTextSerializer.DataSerializer.Deserialize<HostInfo>(var as XElement); hostInfoList.Add(_hostInfo.UnitID, _hostInfo); } if (hostInfoList.Count > 1) { foreach (var var in hostInfoList) { _secsPort = new SECSPort(var.Key); SetHSMSDriver(var.Value); _secsPort.OpenPort(); secsPortList.Add(var.Key, _secsPort); } } else { _secsPort = new SECSPort("CIS"); SetHSMSDriver(_hostInfo); _secsPort.OpenPort(); } } catch (Exception e) { //_logWriter.Write(e.Message, "ApplicationException", 0, 8000, System.Diagnostics.TraceEventType.Error, "HSMSDriver Open Error!"); } }
async public void SendAsync(string unitID, string transName, XElement xmlPrimaryMessage) { try { if (!string.IsNullOrEmpty(unitID) && secsPortList.ContainsKey(unitID)) { _secsPort = secsPortList[unitID]; } //else return; if (_secsPort.Connected) { await Task.Run(() => { try { //XElement trans = xmlPrimaryMessage.Element("Primary"); SECSTransaction tran = _secsPort.Library.FindTransaction(transName); //new SECSTransaction(stream, function); tran.Primary.Root = XMLToRootSecsItem(xmlPrimaryMessage); tran.Send(_secsPort); //LogInfo.HSMS(string.Format("[Send] {0} : {1}", unitID, tran.Primary)); logger.Info(string.Format("[Send] {0} : {1}", unitID, tran.Primary)); //_secsPort.SendAsync(transName, xmlPrimaryMessage); } catch (NullReferenceException nullR) { //_logWriter.Write(nullR.Message, "ApplicationException", 0, 8000, System.Diagnostics.TraceEventType.Error, transName + "CustomSendAsync: SECSPort NullReferenceException!"); } catch (ArgumentNullException a) { //_logWriter.Write(a.Message, "ApplicationException", 0, 8000, System.Diagnostics.TraceEventType.Error, transName + ":CustomSendAsync Unrecognized Message!"); } }); } else { DispatchSecondaryInGEMMessage("S99F1", xmlPrimaryMessage); //Connection Failure } } catch (Exception e) { //_logWriter.Write(e.Message, "ApplicationException", 0, 8000, System.Diagnostics.TraceEventType.Error, transName + "SendAsync: SecsWellDriver Exception Error!"); } }
async private void ReplyAsync(string unitID, SECSTransaction t, XElement xmlSecondaryMessage) { if (!string.IsNullOrEmpty(unitID) && secsPortList.ContainsKey(unitID)) { _secsPort = secsPortList[unitID]; } //if (_sECSIILibrary == null) throw new NullReferenceException("SECSLibrary is null!"); if (_secsPort.Connected == true) { await Task.Run(() => { try { t.Secondary.Root = XMLToRootSecsItem(xmlSecondaryMessage); t.Reply(_secsPort); //LogInfo.HSMS("[Reply] " + unitID + " : " + t?.Name + Environment.NewLine + t.Secondary); logger.Info("[Reply] " + unitID + " : " + t?.Name + Environment.NewLine + t.Secondary); } catch (ArgumentNullException a) { } }); } }
async private void OnSECSEventHandler(SECSPort secsPort, SECSEventType type, SECSTransaction trans, SECSErrors err, string errmsg) { try { XElement xmlMessage = secsPort.Library.TransToXElement(trans); switch (type) { case SECSEventType.PrimaryRcvd: if (err == SECSErrors.None) { if (secsPortList.Count < 1) { xmlMessage.Element("Primary").Add(new XElement("UnitName", _unitID)); } else { xmlMessage.Element("Primary").Add(new XElement("UnitName", secsPortList.First(j => j.Value.DeviceID == secsPort.DeviceID).Key)); } await Task.Factory.StartNew(() => DispatchPrimaryInGEMMessage(secsPort.Name, trans, xmlMessage)); } //else //_logWriter.Write(String.Format("Error Code: {0}. {1}", trans.Name, errmsg), "ApplicationException", 0, 8000, System.Diagnostics.TraceEventType.Error, "Primary Recvd Error!"); //BOE_CIS20EventSource.Log.ReceiveSECSIIMessage(trans.Name, xmlMessage.Element("Primary").ToString()); //_logWriter.Write(xmlMessage.Element("Primary"), "SECSII", 0, 7000, System.Diagnostics.TraceEventType.Information, "Rcvd " + trans.Name); break; case SECSEventType.SecondaryRcvd: string transName = ParseMessageName(trans.Primary.Stream, trans.Secondary.Function); //S9F* SecsDriver 会自动恢复,不用自己处理 //if (err == SECSErrors.T3TimeOut && trans.Name == "S1F14") //{ // _dispatchGEMMessage.CommunicationFailure("S1F14", null); //} await Task.Factory.StartNew(() => DispatchSecondaryInGEMMessage(transName, xmlMessage)); //_logWriter.Write(xmlMessage.Element("Secondary"), "SECSII", 0, 7000, System.Diagnostics.TraceEventType.Information, "Rcvd " + transName); break; case SECSEventType.PrimarySent: //_logWriter.Write(xmlMessage.Element("Primary"), "SECSII", 0, 7000, System.Diagnostics.TraceEventType.Information, "Sent " + trans.Name); break; case SECSEventType.SecondarySent: //_logWriter.Write(xmlMessage.Element("Secondary"), "SECSII", 0, 7000, System.Diagnostics.TraceEventType.Information, "Reply " + ParseMessageName(trans.Primary.Stream, trans.Secondary.Function)); break; case SECSEventType.HSMSDisconnected: DispatchSecondaryInGEMMessage("S99F1", null); //_logWriter.Write("HSMS is Disconnected", "SECSII"); break; case SECSEventType.HSMSConnected: DispatchSecondaryInGEMMessage("S1F13", null); //_logWriter.Write("HSMS is Connected", "SECSII"); break; case SECSEventType.Error: if (err == SECSErrors.T3TimeOut && trans.Name == "S1F13") { DispatchSecondaryInGEMMessage("S1F14", xmlMessage); } if (err == SECSErrors.ReadError || err == SECSErrors.WriteError) { DispatchSecondaryInGEMMessage("S99F2", null); } string name = ""; if (trans != null) { //if (err == SECSErrors.T3TimeOut) // name = ParseMessageName(trans.Primary.Stream, trans.Secondary.Function); //else name = trans.Name; } //_logWriter.Write(String.Format(" ErrorCode: {0} {1}", err, errmsg), "ApplicationException", 0, 8000, System.Diagnostics.TraceEventType.Error, name + ": SECSDriver Error!"); break; } } catch (NullReferenceException n) { } catch (Exception e) { } //Logging }