/// <summary> /// Traces the specified scope request. /// </summary> /// <param name="scope">The scope.</param> /// <param name="context">The context.</param> /// <param name="eventHandler">The event handler.</param> /// <returns></returns> public static ILifetimeScope Trace(this ILifetimeScope scope, HttpContext context, TraceEventHandler?eventHandler) { if (scope.Resolver.Resolve <ISimplifyWebSettings>().ConsoleTracing) { TraceToConsole(context); } eventHandler?.Invoke(context); return(scope); }
public void Open() { Close(); mqttClient = factory.CreateMqttClient(); if (string.IsNullOrEmpty(userClientId)) { clientId = Guid.NewGuid().ToString(); } else { clientId = userClientId; } var options = new MqttClientOptionsBuilder() .WithTcpServer(serverAddress, port).WithClientId(clientId) .Build(); mqttClient.UseApplicationMessageReceivedHandler(t => { string str = ASCIIEncoding.ASCII.GetString(t.ApplicationMessage.Payload); GXJsonParser parser = new GXJsonParser(); GXMessage msg = parser.Deserialize <GXMessage>(str); if (msg.id == messageId || (MesssageType)msg.type == MesssageType.Close || (MesssageType)msg.type == MesssageType.Exception) { switch ((MesssageType)msg.type) { case MesssageType.Open: m_OnMediaStateChange?.Invoke(this, new MediaStateEventArgs(MediaState.Open)); replyReceivedEvent.Set(); break; case MesssageType.Send: break; case MesssageType.Receive: byte[] bytes = Gurux.Common.GXCommon.HexToBytes(msg.frame); replyReceivedEvent.Set(); if (bytes.Length != 0) { HandleReceivedData(bytes.Length, bytes, t.ClientId); } break; case MesssageType.Close: m_OnMediaStateChange?.Invoke(this, new MediaStateEventArgs(MediaState.Closed)); replyReceivedEvent.Set(); break; case MesssageType.Exception: lastException = msg.exception; replyReceivedEvent.Set(); break; } } else { m_OnTrace?.Invoke(this, new TraceEventArgs(TraceTypes.Info, "Unknown reply. " + msg, msg.sender)); } }); mqttClient.UseConnectedHandler(t => { // Subscribe to a topic mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(clientId).WithExactlyOnceQoS().Build()).Wait(); m_OnMediaStateChange?.Invoke(this, new MediaStateEventArgs(MediaState.Opening)); GXMessage msg = new GXMessage() { id = MessageId, type = (int)MesssageType.Open, sender = clientId }; PublishMessage(msg); }); mqttClient.UseDisconnectedHandler(t => { m_OnMediaStateChange?.Invoke(this, new MediaStateEventArgs(MediaState.Closed)); replyReceivedEvent.Set(); }); try { replyReceivedEvent.Reset(); if (AsyncWaitTime == 0) { mqttClient.ConnectAsync(options).Wait(); } else { mqttClient.ConnectAsync(options).Wait((int)AsyncWaitTime * 1000); } } catch (AggregateException ex) { if (mqttClient != null) { mqttClient.DisconnectAsync().Wait(10000); } mqttClient = null; throw ex.InnerException; } if (AsyncWaitTime == 0) { replyReceivedEvent.WaitOne(); } else { if (!replyReceivedEvent.WaitOne((int)AsyncWaitTime * 1000)) { throw new TimeoutException("Invalid topic '" + topic + "'."); } } if (lastException != null) { throw new Exception(lastException); } }