private void ProcessMgosResponse(MgosRpcResponse response) { if (_pendingRequests.TryGetValue(response.Id.GetValueOrDefault(), out var tcs) == false) { throw new InvalidOperationException($"Unable to find a pending request for id '{response.Id}'"); } _pendingRequests.Remove(response.Id.GetValueOrDefault()); if (response.Error != null) { tcs.TrySetException(new MgosRpcException(response.Error)); } else { tcs.TrySetResult(response.Result); } }
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 }); }); } }