public async Task <bool> SendAsync(string topic, byte[] payload, bool retain = false) { const string rpcCall = "$upstream/rpc/"; if (topic.StartsWith(rpcCall)) { var guid = topic.Substring(rpcCall.Length); await dispatcher.HandleAsync(new MqttPublishInfo("$downstream/rpc/ack/" + guid, new byte[0])); var packet = default(RpcPacket); using (var reader = new BsonDataReader(new MemoryStream(payload))) { var serializer = new JsonSerializer { ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() } }; packet = serializer.Deserialize <RpcPacket>(reader); } this.spy.ForEach(s => s(packet)); } return(true); }
public async Task <string> DirectMethodCall(DirectMethodResponse response) { var connector = new Mock <IMqttBrokerConnector>(); var edgeHub = new Mock <IEdgeHub>(); var lastPayload = default(byte[]); var milestone = new SemaphoreSlim(0, 1); connector.Setup(c => c.SendAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <bool>())) .Callback <string, byte[], bool>( (_, p, __) => { lastPayload = p; milestone.Release(); }) .Returns(() => Task.FromResult(true)); edgeHub.Setup(e => e.InvokeMethodAsync(It.IsAny <string>(), It.IsAny <DirectMethodRequest>())) .Returns(() => Task.FromResult(response)); var sut = new BrokeredCloudProxyDispatcher(); sut.BindEdgeHub(edgeHub.Object); sut.SetConnector(connector.Object); await sut.HandleAsync(new MqttPublishInfo("$downstream/dev_a/mod_1/methods/post/test/?$rid=123", Encoding.UTF8.GetBytes("{ \"test\":\"data\"}"))); await milestone.WaitAsync(); var packet = GetRpcPacket(lastPayload); return(packet.Topic); }
async Task SignalConnectionEvent(BrokeredCloudProxyDispatcher brokeredCloudProxyDispatcher, string status) { var packet = new MqttPublishInfo(ConnectivityTopic, Encoding.UTF8.GetBytes("{\"status\":\"" + status + "\"}")); await brokeredCloudProxyDispatcher.HandleAsync(packet); }