Exemple #1
0
        static async public void StartMessageReceiver()
        {
            while (true)
            {
                Message receivedMessage = await deviceClient.ReceiveAsync();

                if (receivedMessage == null)
                {
                    continue;
                }

                Console.ForegroundColor = ConsoleColor.Yellow;
                var messageString = Encoding.ASCII.GetString(receivedMessage.GetBytes());
                Console.WriteLine("Received message: {0}", messageString);
                Console.ResetColor();

                if (machineConnector.SendText(new MachineSendTextCommand()
                {
                    Text = messageString,
                    CommandLevel = InteractionCommandLevel.Info
                }))
                {
                    Console.WriteLine($"Successfully send text to local machine");
                }

                await deviceClient.CompleteAsync(receivedMessage);
            }
        }
Exemple #2
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);
            }
        }