Example #1
0
        public async Task SendEventAsync(string connectionId, EventBody eventBody)
        {
            Console.WriteLine("sendEventAsync received for {0} with body {1}", connectionId, eventBody.Body.ToString());
            var client = objectMap[connectionId];
            await client.SendEventAsync(new Microsoft.Azure.Devices.Client.Message(GlueUtils.ObjectToBytes(eventBody.Body))).ConfigureAwait(false);

            Console.WriteLine("sendEventAsync complete");
        }
Example #2
0
        public async Task <object> RoundtripMethodCallAsync(string connectionId, string methodName, MethodRequestAndResponse requestAndResponse)
        {
            Console.WriteLine("RoundtripMethodCallAsync received for {0} and methodName {1}", connectionId, methodName);
            Console.WriteLine(JsonConvert.SerializeObject(requestAndResponse));
            var client = objectMap[connectionId];
            var mutex  = new System.Threading.SemaphoreSlim(1);
            await mutex.WaitAsync().ConfigureAwait(false);  // Grab the mutex. The handler will release it later

            MethodCallback callback = async(methodRequest, userContext) =>
            {
                Console.WriteLine("Method invocation received");

                object request  = JsonConvert.DeserializeObject(methodRequest.DataAsJson);
                string received = JsonConvert.SerializeObject(new JRaw(request));
                string expected = ((Newtonsoft.Json.Linq.JToken)requestAndResponse.RequestPayload)["payload"].ToString();
                Console.WriteLine("request expected: " + expected);
                Console.WriteLine("request received: " + received);
                if (expected != received)
                {
                    Console.WriteLine("request did not match expectations");
                    Console.WriteLine("Releasing the method mutex");
                    mutex.Release();
                    return(new MethodResponse(500));
                }
                else
                {
                    int status = 200;
                    if (requestAndResponse.StatusCode != null)
                    {
                        status = (int)requestAndResponse.StatusCode;
                    }

                    byte[] responseBytes = GlueUtils.ObjectToBytes(requestAndResponse.ResponsePayload);

                    Console.WriteLine("Releasing the method mutex");
                    mutex.Release();

                    Console.WriteLine("Returning the result");
                    return(new MethodResponse(responseBytes, status));
                }
            };

            Console.WriteLine("Setting the handler");
            await client.SetMethodHandlerAsync(methodName, callback, null).ConfigureAwait(false);

            Console.WriteLine("Waiting on the method mutex");
            await mutex.WaitAsync().ConfigureAwait(false);

            Console.WriteLine("Method mutex released.  Waiting for a tiny bit.");  // Otherwise, the connection might close before the response is actually sent
            await Task.Delay(100).ConfigureAwait(false);

            Console.WriteLine("Nulling the handler");
            await client.SetMethodHandlerAsync(methodName, null, null).ConfigureAwait(false);

            Console.WriteLine("RoundtripMethodCallAsync is complete");
            return(new object());
        }
Example #3
0
        public async Task <ConnectResponse> ConnectFromEnvironmentAsync(string transport)
        {
            var client = await ModuleClient.CreateFromEnvironmentAsync(GlueUtils.TransportNameToType(transport)).ConfigureAwait(false);

            await client.OpenAsync().ConfigureAwait(false);

            var connectionId = modulePrefix + Convert.ToString(++objectCount);

            objectMap[connectionId] = client;
            return(new ConnectResponse
            {
                ConnectionId = connectionId
            });
        }
Example #4
0
        public async Task <ConnectResponse> ConnectAsync(string transport, string connectionString, Certificate caCertificate)
        {
            Console.WriteLine("ConnectAsync for " + transport);
            var client = ModuleClient.CreateFromConnectionString(connectionString, GlueUtils.TransportNameToType(transport));
            await client.OpenAsync().ConfigureAwait(false);

            var connectionId = modulePrefix + Convert.ToString(++objectCount);

            Console.WriteLine("Connected successfully.  Connection Id = " + connectionId);
            objectMap[connectionId] = client;
            return(new ConnectResponse
            {
                ConnectionId = connectionId
            });
        }
Example #5
0
        public async Task <object> InvokeDeviceMethodAsync(string connectionId, string deviceId, MethodInvoke methodInvokeParameters)
        {
            Console.WriteLine("InvokeDeviceMethodAsync received for {0} with deviceId {1} ", connectionId, deviceId);
            Console.WriteLine(methodInvokeParameters.ToString());
            var client  = objectMap[connectionId];
            var request = GlueUtils.CreateMethodRequest(methodInvokeParameters);

            Console.WriteLine("Invoking");
            var response = await client.InvokeMethodAsync(deviceId, request, CancellationToken.None).ConfigureAwait(false);

            Console.WriteLine("Response received:");
            Console.WriteLine(JsonConvert.SerializeObject(response));
            return(new JObject(
                       new JProperty("status", response.Status),
                       new JProperty("payload", response.ResultAsJson)
                       ));
        }
Example #6
0
        public async Task SendOutputEventAsync(string connectionId, string outputName, EventBody eventBody)
        {
            Console.WriteLine("sendEventAsync received for {0} with output {1} and body {2}", connectionId, outputName, eventBody.Body.ToString());
            var    client = objectMap[connectionId];
            string toSend = JsonConvert.SerializeObject(eventBody);
            await client.SendEventAsync(outputName, new Microsoft.Azure.Devices.Client.Message(GlueUtils.ObjectToBytes(eventBody.Body))).ConfigureAwait(false);

            Console.WriteLine("sendOutputEventAsync complete");
        }
Example #7
0
 internal static MethodRequest CreateMethodRequest(MethodInvoke methodInvokeParameters)
 {
     return(new MethodRequest(methodInvokeParameters.MethodName, GlueUtils.ObjectToBytes(methodInvokeParameters.Payload), TimeSpan.FromSeconds((double)methodInvokeParameters.ResponseTimeoutInSeconds), TimeSpan.FromSeconds((double)methodInvokeParameters.ConnectTimeoutInSeconds)));
 }