예제 #1
0
        private static Task onDesiredPropertiesUpdate(TwinCollection desiredProperties, object userContext)
        {
            if (desiredProperties.Count == 0)
            {
                return(Task.CompletedTask);
            }

            try
            {
                Console.WriteLine("Desired property change:");
                Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));

                var deviceClient = userContext as DeviceClient;

                if (deviceClient == null)
                {
                    throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
                }

                var reportedProperties = new TwinCollection();

                if (desiredProperties.Contains("interval") && desiredProperties["interval"] != null)
                {
                    Interval = desiredProperties["interval"];

                    reportedProperties["interval"] = Interval;
                }

                if (desiredProperties.Contains("localhusturl") && !string.IsNullOrEmpty(desiredProperties["localhosturl"]))
                {
                    LocalhostUrl = desiredProperties["localhosturl"];

                    reportedProperties["localhosturl"] = LocalhostUrl;
                    DataLoader = new DHTDataLoader(LocalhostUrl);
                }

                if (reportedProperties.Count > 0)
                {
                    deviceClient.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
                }
            }
            catch (AggregateException ex)
            {
                foreach (Exception exception in ex.InnerExceptions)
                {
                    Console.WriteLine();
                    Console.WriteLine("Error when receiving desired property: {0}", exception);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("Error when receiving desired property: {0}", ex.Message);
            }

            return(Task.CompletedTask);
        }
예제 #2
0
        private static async void ThreadBody(object userContext)
        {
            var url = LocalhostUrl;

            Console.WriteLine($"Connecting to DHT sensor via url {url}");
            DataLoader = new DHTDataLoader(url);
            while (true)
            {
                var deviceClient = userContext as DeviceClient;

                if (deviceClient == null)
                {
                    throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
                }

                var data = DataLoader.GetDHTData();

                DHTMessageBody dhttMessageBody;
                if (data == null)
                {
                    Console.WriteLine("Error: Cannot read DHT Data from local machine");
                }
                else
                {
                    dhttMessageBody = new DHTMessageBody
                    {
                        timeCreated = DateTime.Now.ToString("hh:mm:ss"),
                        humidity    = data.Humidity,
                        temperature = data.Temperature
                    };

                    var jsonMessage = JsonConvert.SerializeObject(dhttMessageBody);

                    var pipeMessage = new Message(Encoding.UTF8.GetBytes(jsonMessage));

                    pipeMessage.Properties.Add("content-type", "application/json");

                    await deviceClient.SendEventAsync("output1", pipeMessage);

                    Console.WriteLine($"DHT data sent {dhttMessageBody.timeCreated}: {dhttMessageBody.temperature} |  {dhttMessageBody.humidity}");
                }

                Thread.Sleep(Interval);
            }
        }