Exemple #1
0
        private void ProcessMgosRequest(string deviceId, MgosRpcRequest request)
        {
            if (_handlers.TryGetValue(request.Method, out var handler))
            {
                Task.Run(async() =>
                {
                    var response = new MgosRpcResponse
                    {
                        Id  = request.Id,
                        Src = deviceId,
                        Dst = request.Src,
                    };

                    try
                    {
                        var responseArgs = await handler.ProcessAsync(deviceId, request.Args);

                        response.Result = responseArgs;
                    }
                    catch (MgosRpcException e)
                    {
                        response.Error = e.Error;
                    }

                    var payload = JsonConvert.SerializeObject(response, _serializerSettings);

                    await _mqttClient.PublishAsync(new MqttApplicationMessage
                    {
                        Topic   = $"{request.Src}/rpc",
                        Payload = Encoding.UTF8.GetBytes(payload),
                        QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
                        Retain = false
                    });
                });
            }
        }