Example #1
0
        private static async Task <MessageResponse> MessageReceived(Message message, object userContext)
        {
            try{
                var deviceClient = userContext as DeviceClient;

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

                byte[] messageBytes  = message.GetBytes();
                string messageString = Encoding.UTF8.GetString(messageBytes);
                Console.WriteLine($"Received message {DateTime.UtcNow.ToString()}: {messageString}");

                DHTMessageBody messageBody = null;
                // Get message body
                try{
                    messageBody = JsonConvert.DeserializeObject <DHTMessageBody>(messageString);
                }
                catch (Exception ex) {
                    Console.WriteLine("Cannot deserialize message.");
                    Console.WriteLine(ex);
                    Console.WriteLine(ex.Message);
                }

                if (messageBody != null)
                {
                    //do the scoring
                    var prediction = AnomalyDetector.IsAnomaly(messageBody.temperature, messageBody.humidity);

                    var predictionMessage = new ScoredDHTMessageBody(messageBody, prediction);

                    var jsonMessage = JsonConvert.SerializeObject(predictionMessage);

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

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

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

                    Console.WriteLine($"Scored data sent {predictionMessage.timeCreated}: {predictionMessage.temperature} |  {predictionMessage.humidity} | Anomaly {predictionMessage.IsAnomaly}");
                }
            }
            catch (Exception ex) {
                Console.WriteLine($"Exception occured: {ex.Message}");
                Console.WriteLine(ex);
            }

            return(MessageResponse.Completed);
        }
Example #2
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["TemperatureMeanValue"] != null)
                {
                    TemperatureMeanValue = desiredProperties["TemperatureMeanValue"];

                    reportedProperties["TemperatureMeanValue"] = TemperatureMeanValue;
                }

                if (desiredProperties["TemperatureStdDeviation"] != null)
                {
                    TemperatureStdDeviation = desiredProperties["TemperatureStdDeviation"];

                    reportedProperties["TemperatureStdDeviation"] = TemperatureStdDeviation;
                }

                if (desiredProperties["HumidityMeanValue"] != null)
                {
                    HumidityMeanValue = desiredProperties["HumidityMeanValue"];

                    reportedProperties["HumidityMeanValue"] = HumidityMeanValue;
                }

                if (desiredProperties["HumidityStdDeviation"] != null)
                {
                    HumidityStdDeviation = desiredProperties["HumidityStdDeviation"];

                    reportedProperties["HumidityStdDeviation"] = HumidityStdDeviation;
                }

                AnomalyDetector = new AnomalyDetector(TemperatureMeanValue, TemperatureStdDeviation, HumidityMeanValue, HumidityStdDeviation);

                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);
        }