Пример #1
0
        static public void SendMessageToCloud(MqttMessage message)
        {
            try
            {
                if (message.MqttMessageType != MqttMessageType.Unknwon)
                {
                    double value      = message.PayloadDoubleValue;
                    bool   isCritical = false;

                    if (message.MqttMessageType == MqttMessageType.Temperature)
                    {
                        Console.WriteLine($"Received temperature {message.payload}");
                        if (value < TempThresholdLower || value > TempThresholdUpper)
                        {
                            isCritical = true;
                        }
                    }

                    if (message.MqttMessageType == MqttMessageType.Humidity)
                    {
                        Console.WriteLine($"Received humidity {message.payload}");
                        if (value < HumThresholdLower || value > HumThresholdUpper)
                        {
                            isCritical = true;
                        }
                    }

                    if (IsDebug)
                    {
                        Console.Write($"DEBUG: Current configuration: ");
                        Console.Write($"TempMean: {TempMean}, ");
                        Console.Write($"HumMean: {HumMean}, ");
                        Console.Write($"TempStdDev: {TempStdDev}, ");
                        Console.Write($"HumStdDev: {HumStdDev}, ");
                        Console.Write($"LocalWebserver: {LocalWebServerUrl}, ");
                        Console.Write($"TempThresholdUpper: {TempThresholdUpper}, ");
                        Console.Write($"TempThresholdLower: {TempThresholdLower}, ");
                        Console.Write($"HumThresholdUpper: {HumThresholdUpper}, ");
                        Console.Write($"HumThresholdLower: {HumThresholdLower}, ");
                        Console.Write($"SendDataToCloud: {SendDataToCloud}, ");
                        Console.WriteLine("");
                    }



                    if (IsDebug)
                    {
                        Console.Write($"IsCritical: {isCritical}");
                        Console.WriteLine("");
                    }

                    DeviceToCloudMessage d2cMessage = null;
                    if (isCritical)
                    {
                        d2cMessage = new DeviceToCloudMessage()
                        {
                            Sensor       = message.sensor,
                            Value        = message.PayloadDoubleValue,
                            MessageLevel = (int)(isCritical ? DeviceToCloudMessageLevel.Critical : DeviceToCloudMessageLevel.Info),
                            DeviceId     = DeviceId
                        };

                        var level = isCritical ? InteractionCommandLevel.Critical : InteractionCommandLevel.Warning;
                        machineConnector.InteractWithMachine(new MachineInteractionCommand()
                        {
                            CommandLevel = level
                        });
                    }
                    else if (SendDataToCloud)
                    {
                        d2cMessage = new DeviceToCloudMessage()
                        {
                            Sensor       = message.sensor,
                            Value        = message.PayloadDoubleValue,
                            MessageLevel = (int)DeviceToCloudMessageLevel.Info,
                            DeviceId     = DeviceId
                        };

                        machineConnector.SendText(new MachineSendTextCommand()
                        {
                            CommandLevel = InteractionCommandLevel.Info,
                            Text         = message.payload
                        });
                    }

                    if (d2cMessage != null)
                    {
                        SendCloudMessage(d2cMessage);
                    }
                }
                else
                {
                    Console.WriteLine("Error: Cannot read data from DHT sensor");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception occured during message sending: {ex.Message}");
                Console.WriteLine(ex);
            }
        }
Пример #2
0
        static public void StartMessageSending()
        {
            while (true)
            {
                try
                {
                    var data = dataLoader.GetDHTData();
                    if (data != null)
                    {
                        Console.WriteLine($"Read data temp: {data.Temperature}, hum: {data.Humidity}");
                        if (IsDebug)
                        {
                            Console.Write($"DEBUG: Current configuration: ");
                            Console.Write($"TempMean: {TempMean}, ");
                            Console.Write($"HumMean: {HumMean}, ");
                            Console.Write($"TempStdDev: {TempStdDev}, ");
                            Console.Write($"HumStdDev: {HumStdDev}, ");
                            Console.Write($"LocalWebserver: {LocalWebServerUrl}, ");
                            Console.Write($"SleepInterval: {SleepInterval}, ");
                            Console.Write($"TempThresholdUpper: {TempThresholdUpper}, ");
                            Console.Write($"TempThresholdLower: {TempThresholdLower}, ");
                            Console.Write($"HumThresholdUpper: {HumThresholdUpper}, ");
                            Console.Write($"HumThresholdLower: {HumThresholdLower}, ");
                            Console.Write($"SendDataToCloud: {SendDataToCloud}, ");
                            Console.WriteLine("");
                        }

                        var isAnomaly  = anomalyDetector.IsAnomaly(data.Temperature, data.Humidity);
                        var isCritical = data.Temperature <TempThresholdLower ||
                                                           data.Temperature> TempThresholdUpper ||
                                         data.Humidity <HumThresholdLower ||
                                                        data.Humidity> HumThresholdUpper;

                        if (IsDebug)
                        {
                            Console.Write($"IsAnomaly: {isAnomaly}, IsCritical: {isCritical}");
                            Console.WriteLine("");
                        }

                        DeviceToCloudMessage d2cMessage = null;
                        if (isAnomaly || isCritical)
                        {
                            d2cMessage = new DeviceToCloudMessage()
                            {
                                Temperature  = data.Temperature,
                                Humidity     = data.Humidity,
                                MessageLevel = (int)(isCritical ? DeviceToCloudMessageLevel.Critical : DeviceToCloudMessageLevel.Warning),
                                DeviceId     = DeviceId
                            };

                            var level = isCritical ? InteractionCommandLevel.Critical : InteractionCommandLevel.Warning;
                            machineConnector.InteractWithMachine(new MachineInteractionCommand()
                            {
                                CommandLevel = level,
                                Temperature  = data.Temperature,
                                Humidity     = data.Humidity
                            });
                        }
                        else if (SendDataToCloud)
                        {
                            d2cMessage = new DeviceToCloudMessage()
                            {
                                Temperature  = data.Temperature,
                                Humidity     = data.Humidity,
                                MessageLevel = (int)DeviceToCloudMessageLevel.Info,
                                DeviceId     = DeviceId
                            };
                        }

                        if (d2cMessage != null)
                        {
                            SendCloudMessage(d2cMessage);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error: Cannot read data from DHT sensor");
                    }

                    Thread.Sleep(SleepInterval);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Exception occured during message sending: {ex.Message}");
                    Console.WriteLine(ex);
                }
            }
        }