IDisposable SubscribeLocal(SecsEventSubscription subscription) { var handler = new Action <SecsMessage>(msg => { #region event action var filter = subscription.Filter; try { if (msg.IsMatch(filter)) { EapLogger.Info("event[" + filter.Name + "] >> EAP"); msg.Name = filter.Name; subscription.Handle(msg); } } catch (Exception ex) { EapLogger.Error("event[" + filter.Name + "] EAP process Error!", ex); } #endregion }); _eventHandlers.AddHandler(subscription.GetKey(), handler); EapLogger.Notice("EAP subscribe event " + subscription.Filter.Name); return(new LocalDisposable(() => { _eventHandlers.RemoveHandler(subscription.GetKey(), handler); EapLogger.Notice("EAP unsubscribe event " + subscription.Filter.Name); })); }
void IEAP.SetTxHandler <T>(Func <T, Task> handler) { _txHandlers[typeof(T).Name] = async txElm => { T tx = default(T); bool error = false; try { tx.Parse(txElm); await handler(tx); txElm.SetAttributeValue("SystemErrCode", 0); txElm.SetAttributeValue("AppErrCode", 0); txElm.SetAttributeValue("AppErrDescription", string.Empty); } catch (XmlException ex) { error = true; txElm.SetAttributeValue("SystemErrCode", 9); txElm.SetAttributeValue("AppErrCode", 9); txElm.SetAttributeValue("AppErrDescription", "XML parsing error:" + ex.Message); } catch (ScenarioException ex) { error = true; txElm.SetAttributeValue("SystemErrCode", 9); txElm.SetAttributeValue("AppErrCode", 0); txElm.SetAttributeValue("AppErrDescription", "Scenario exception:" + ex.Message); } catch (SecsException ex) { error = true; txElm.SetAttributeValue("SystemErrCode", 9); txElm.SetAttributeValue("AppErrCode", 1); txElm.SetAttributeValue("AppErrDescription", "Secs/Gem eror:" + ex.Message); } catch (Exception ex) { error = true; EapLogger.Error(ex); txElm.SetAttributeValue("SystemErrCode", 9); txElm.SetAttributeValue("AppErrCode", 1); txElm.SetAttributeValue("AppErrDescription", string.Format("Exception occurred in {0}: {1}", handler.GetType().Name, ex.Message)); } finally { var txtype = txElm.Attribute("Type"); if (txtype.Value == "Request") { txtype.Value = "Ack"; Send2TCS(txElm.Document, !error); } } }; }
private void PrimaryMsgHandler(object sender, PrimaryMessageWrapper e) { try { e.Reply(SecsMessages[e.Message.S, (byte)(e.Message.F + 1)].FirstOrDefault()); Action <SecsMessage> handler = null; if (_eventHandlers.TryGetValue(e.Message.GetKey(), out handler)) { Parallel.ForEach(handler.GetInvocationList().Cast <Action <SecsMessage> >(), h => h(e.Message)); } } catch (Exception ex) { EapLogger.Error("Handle Primary SECS message Error", ex); } }
async void btnSend_Click(object sender, EventArgs e) { if (_secsGem == null) { MessageBox.Show("SECS/GEM not enable!"); return; } try { EapLogger.Notice("Send by operator"); await _secsGem.SendAsync(txtMsg.Text.ToSecsMessage()); } catch (Exception ex) { EapLogger.Error(ex); } }
void Send2TCS(XDocument xmldoc, bool normal) { try { string msg = xmldoc.ToFormatedXml(); //axPostMan.Send(TcsPostmanId, "", msg); if (normal) { EapLogger.Info("EAP >> TCS : \r\n" + msg); } else { EapLogger.Error("EAP >> TCS : \r\n" + msg); } } catch (Exception ex) { EapLogger.Error("Send2TCS error:", ex); } }
public void Error(string msg, Exception ex = null) { EapLogger.Error("SECS/GEM Error: " + msg); }
IDisposable SubscribeRemote(SecsEventSubscription subscription) { var filter = subscription.Filter; var description = filter.Name; var handler = new Action <SecsMessage>(msg => { try { if (msg.IsMatch(filter)) { EapLogger.Info($"event[{description}] >> Z"); msg.Name = filter.Name; subscription.Handle(msg); } } catch (Exception ex) { EapLogger.Error($"event[{description}] Z process Error!", ex); } }); int key = subscription.GetKey(); string recoverQueuePath = $"FormatName:DIRECT=TCP:{subscription.ClientAddress} \\private$\\{subscription.Id}"; if (subscription.Recoverable) { return(new SerializableDisposable(subscription, new RemoteDisposable(subscription, subscribeAction: () => { #region recover complete _eventHandlers.AddHandler(key, handler); EapLogger.Notice("Z subscribe event " + description); Action <SecsMessage> recover = null; if (_recoverEventHandlers.TryRemove(recoverQueuePath, out recover)) { _eventHandlers.RemoveHandler(key, recover); EapLogger.Notice($"Z recover completely event[{description}]"); } #endregion }, disposeAction: () => { #region Dispose _eventHandlers.RemoveHandler(key, handler); EapLogger.Notice("Z unsubscribe event " + description); var queue = new MessageQueue(recoverQueuePath, QueueAccessMode.Send) { Formatter = new BinaryMessageFormatter() }; queue.DefaultPropertiesToSend.Recoverable = true; var recover = new Action <SecsMessage>(msg => { #region recover action try { if (msg.IsMatch(filter)) { EapLogger.Info("recoverable event[" + description + "]"); msg.Name = filter.Name; queue.Send(msg); } } catch (Exception ex) { EapLogger.Error(ex); } #endregion }); if (_recoverEventHandlers.TryAdd(recoverQueuePath, recover)) { EapLogger.Notice("Z subscribe event[" + description + "] for recovering."); _eventHandlers.AddHandler(key, recover); } #endregion }))); } _eventHandlers.AddHandler(key, handler); EapLogger.Notice("Z subscribe event " + description); return(new SerializableDisposable(subscription, new RemoteDisposable(subscription, null, () => { _eventHandlers.RemoveHandler(key, handler); EapLogger.Notice("Z unsubscribe event " + description); }))); }
static void Main() { Application.ThreadException += (sender, e) => { EapLogger.Error(e.Exception); }; AppDomain.CurrentDomain.UnhandledException += (sender, e) => { EapLogger.Error(e.ExceptionObject as Exception); if (e.IsTerminating) { MessageBox.Show(e.ExceptionObject.ToString(), "程式發生嚴重錯誤而終止"); } }; var fileAppender = new RollingLogFileAppender { Layout = new PatternLayout("%date{HH:mm:ss,fff} %-6level [%4thread] %message%newline") }; fileAppender.ActivateOptions(); var l = (Logger)LogManager.GetLogger("EAP").Logger; l.Level = Level.All; l.Repository.RendererMap.Put(typeof(SecsMessageLogInfo), new SecsMessageRenderer()); l.Repository.Threshold = Level.All; l.Repository.Configured = true; l.AddAppender(fileAppender); RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off; ChannelServices.RegisterChannel( new TcpChannel( new Hashtable { ["port"] = 0, ["bindTo"] = Settings.Default.TcpBindTo }, null, new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full }), false); bool newMutexCreated; using (new Mutex(true, "EAP" + EAPConfig.Instance.ToolId, out newMutexCreated)) { if (!newMutexCreated) { MessageBox.Show($"系統中已經執行了EAP:{EAPConfig.Instance.ToolId}啟動錯誤"); return; } EapLogger.Info("__________________ EAP Started __________________"); var form = new HostMainForm(); RemotingServices.Marshal(form, EAPConfig.Instance.ToolId, typeof(ISecsDevice)); PublishZService(); Application.Run(form); EapLogger.Info("___________________ EAP Stop ____________________"); } }