async Task ProcessTwinAsync(IProtocolGatewayMessage protocolGatewayMessage) { var properties = new Dictionary<StringSegment, StringSegment>(); if (TwinAddressHelper.TryParseOperation(protocolGatewayMessage.Address, properties, out TwinAddressHelper.Operation operation, out StringSegment subresource)) { bool hasCorrelationId = properties.TryGetValue(RequestId, out StringSegment correlationId); switch (operation) { case TwinAddressHelper.Operation.TwinGetState: EnsureNoSubresource(subresource); if (!hasCorrelationId || correlationId.Length == 0) { throw new InvalidOperationException("Correlation id is missing or empty."); } await this.deviceListener.SendGetTwinRequest(correlationId.ToString()); Events.GetTwin(this.deviceListener.Identity); break; case TwinAddressHelper.Operation.TwinPatchReportedState: EnsureNoSubresource(subresource); IMessage forwardMessage = new EdgeMessage.Builder(this.byteBufferConverter.ToByteArray(protocolGatewayMessage.Payload)) .Build(); await this.deviceListener.UpdateReportedPropertiesAsync(forwardMessage, hasCorrelationId ? correlationId.ToString() : string.Empty); Events.UpdateReportedProperties(this.deviceListener.Identity); break; default: throw new InvalidOperationException("Twin operation is not supported."); } }
public Task <DirectMethodResponse> InvokeMethodAsync(DirectMethodRequest request) { string address = TwinAddressHelper.FormatDeviceMethodRequestAddress(request.CorrelationId, request.Name); IProtocolGatewayMessage pgMessage = new ProtocolGatewayMessage.Builder(this.byteBufferConverter.ToByteBuffer(request.Data), address) .WithCreatedTimeUtc(DateTime.UtcNow) .Build(); this.channel.Handle(pgMessage); return(Task.FromResult(default(DirectMethodResponse))); }