private void ParseResponse(NikoMessage message) { List <object> data; var parser = PayloadParserFactory.GetParser(message.Method); try { data = parser.Parse((JsonElement)message.Params); } catch (Exception e) { var error = new ResponseError(Constants.Errors.MessageParsingError, e.Message, message.Method); _messageObservable.MessageReceived(new Message(message.Method, error)); return; } _messageObservable.MessageReceived(new Message(message.Method, data)); Logger.WriteInfo($"{data?.Count ?? 0} item(s) received."); if (Logger.IsEnabled && data != null) { Logger.WriteReceived(JsonSerializer.Serialize(data)); } }
private void ParseError(NikoMessage message) { var parser = PayloadParserFactory.GetParser(Constants.Messages.Error); var errorraw = parser.Parse((JsonElement)message.Params).Cast <IError>().ToList(); var error = new ResponseError(errorraw.First().ErrCode, errorraw.First().ErrMessage, message.Method); _messageObservable.MessageReceived(new Message(message.Method, error)); }
public Task <ConnectionResult> Connect(string hostname, string token, CancellationToken canceltoken = default) { var tcs = new TaskCompletionSource <ConnectionResult>(); var factory = new MqttFactory(); _mqttClient = factory.CreateMqttClient(); var options = new MqttClientOptionsBuilder() .WithClientId(Guid.NewGuid().ToString()) .WithTcpServer(hostname, 8884) .WithCredentials("hobby", token) .WithTls(new MqttClientOptionsBuilderTlsParameters { UseTls = true, SslProtocol = SslProtocols.Tls12, CertificateValidationCallback = (X509Certificate x, X509Chain y, SslPolicyErrors z, IMqttClientOptions o) => true }) .WithCleanSession() .Build(); _mqttClient.UseConnectedHandler(d => { try { tcs.TrySetResult( new ConnectionResult((ConnectResultCode)d.AuthenticateResult.ResultCode)); } catch (Exception e) { tcs.TrySetException(e); } }); _mqttClient.UseApplicationMessageReceivedHandler(d => { Logger.WriteInfo($"Message received {d.ApplicationMessage.Topic}"); var payload = System.Text.Encoding.Default.GetString(d.ApplicationMessage.Payload); Logger.WriteReceivedRaw($"Message received { payload }"); try { var msgtype = d.ApplicationMessage.Topic.Split('/').Last(); INikoMessage response = null; var doc = JsonDocument.Parse(payload); switch (msgtype) { case "cmd": break; case "evt": var evtparams = JsonDocument.Parse(doc.RootElement.GetProperty("Params").GetRawText()).RootElement; response = new NikoMessage { MessageType = NikoMessageType.Evt, Method = doc.RootElement.GetProperty("Method").GetString(), Params = evtparams, CorrelationData = Guid.Empty }; break; case "rsp": var rspparams = JsonDocument.Parse(doc.RootElement.GetProperty("Params").GetRawText()).RootElement; response = new NikoMessage { MessageType = NikoMessageType.Rsp, Method = doc.RootElement.GetProperty("Method").GetString(), Params = rspparams, CorrelationData = Guid.Empty }; break; case "err": response = new NikoMessage { MessageType = NikoMessageType.Err, Method = doc.RootElement.GetProperty("Method").GetString(), Params = doc }; break; } ResponseObservable.MessageReceived(response); } catch (Exception e) { Console.WriteLine(e); throw; } }); _mqttClient.UseDisconnectedHandler(d => { Disconnected?.Invoke(this, EventArgs.Empty); }); _mqttClient.ConnectAsync(options, canceltoken); return(tcs.Task); }