private static void ActionTemperatureAlert(AlarmMessage alarmMessage)
        {
            WriteHighlightedMessage(
                GetDeviceIdHint(alarmMessage.ioTHubDeviceID) +
                "[" + alarmMessage.createdAt + "]" +
                " TempAlert! Temp=" + alarmMessage.reading +
                ", Threshold=" + alarmMessage.threshold,
                ConsoleColor.Yellow);

            /* Action 1: Send Cloud-to-Device command */
            C2DCommand c2dCommand = new C2DCommand();

            c2dCommand.command = C2DCommand.COMMAND_TEMPERATURE_ALERT;
            c2dCommand.value   = alarmMessage.reading;
            c2dCommand.time    = alarmMessage.createdAt;

            SendCloudToDeviceCommand(
                _serviceClient,
                alarmMessage.ioTHubDeviceID,
                c2dCommand).Wait();

            /* Action 2: Send to Web dashboard */
            //string webSCResult = _webSC.PostTelemetryAlarm(alarmMessage);
            //Console.WriteLine(webSCResult);
        }
        private static void ProcessAlarmMessage(AlarmMessage alarmMessage)
        {
            switch (alarmMessage.alarmType)
            {
            case "CutOutSpeed":
                ActionCutOutSpeed(alarmMessage);
                break;

            case "Repair":
                ActionRepair(alarmMessage);
                break;

            case "EnableWindTurbine":
                ActionEnableWindTurbine(alarmMessage.ioTHubDeviceID, alarmMessage.reading, alarmMessage.createdAt);
                break;

            case "ResetDepreciation":
                ActionResetDepreciation(alarmMessage.ioTHubDeviceID, alarmMessage.createdAt);
                break;

            default:
                Console.WriteLine("AlarmType is Not accpeted!");
                break;
            }
        }
        public string PostTelemetryAlarm(AlarmMessage alarmMessage)
        {
            try
            {
                string postData = "ioTHubDeviceID=" + alarmMessage.ioTHubDeviceID + "&" +
                                  "alarmType=" + alarmMessage.alarmType + "&" +
                                  "reading=" + alarmMessage.reading + "&" +
                                  "threshold=" + alarmMessage.threshold + "&" +
                                  "createdAt=" + alarmMessage.createdAt;

                var data = Encoding.UTF8.GetBytes(postData);

                var request = (HttpWebRequest)WebRequest.Create(_webServerUrl);
                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();
                return(new StreamReader(response.GetResponseStream()).ReadToEnd());
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
 private static void ActionCutOutSpeed(AlarmMessage alarmMessage)
 {
     if (alarmMessage.ioTHubDeviceID.Equals(DEVICEID_WINDOWS_TURBINE))
     {
         ActionCutOutSpeedWindows(alarmMessage);
     }
     else if (alarmMessage.ioTHubDeviceID.Equals(DEVICEID_LINUX_TURBINE))
     {
         ActionCutOutSpeedLinux(alarmMessage);
     }
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Console App for Alert Service Bus...");

            /* Load the settings from App.config */
            string serviceBusConnectionString = ConfigurationManager.AppSettings["ServiceBus.ConnectionString"];

            Console.WriteLine("serviceBusConnectionString={0}\n", serviceBusConnectionString);

            string iotHubConnectionString = ConfigurationManager.AppSettings["IoTHub.ConnectionString"];

            Console.WriteLine("iotHubConnectionString={0}\n", iotHubConnectionString);

            // Retrieve Web Server URL
            string webServerUrl;
            string isProduction = ConfigurationManager.AppSettings["WebServer.isProduction"];

            if (isProduction.Equals("1"))
            {
                webServerUrl = ConfigurationManager.AppSettings["WebServer.Production"];
            }
            else
            {
                webServerUrl = ConfigurationManager.AppSettings["WebServer.Localhost"];
            }

            _webSC = new WebServerConnector(webServerUrl);

            // Retrieve a Queue Client
            QueueClient queueClient = QueueClient.CreateFromConnectionString(serviceBusConnectionString, QueueName);

            // Retrieve a Service Client of IoT Hub
            _serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);

            queueClient.OnMessage(message =>
            {
                Console.WriteLine("\n*******************************************************");
                string msg = message.GetBody <String>();
                try
                {
                    AlarmMessage alarmMessage = JsonConvert.DeserializeObject <AlarmMessage>(msg);

                    ProcessAlarmMessage(alarmMessage);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("****  Exception=" + ex.Message);
                }
            });

            Console.ReadLine();
        }
        private static void ProcessAlarmMessage(AlarmMessage alarmMessage)
        {
            switch (alarmMessage.alarmType)
            {
            case "TempAlert":
                ActionTemperatureAlert(alarmMessage);
                break;

            case "EnableDevice":
                ActionEnableDevice(alarmMessage.ioTHubDeviceID, alarmMessage.reading, alarmMessage.createdAt);
                break;

            default:
                Console.WriteLine("AlarmType is Not accpeted!");
                break;
            }
        }
        private static void ActionRepairLinux(AlarmMessage alarmMessage)
        {
            WriteHighlightedMessage(
                GetDeviceIdHint(alarmMessage.ioTHubDeviceID) +
                " Repair! Depreciation=" + alarmMessage.reading +
                ", MessageID=" + alarmMessage.messageID +
                ", Threshold=" + alarmMessage.threshold,
                ConsoleColor.DarkRed);

            C2DCommandLinux c2dCommand = new C2DCommandLinux();

            c2dCommand.Name       = C2DCommandLinux.COMMAND_REPAIR_WARNING;
            c2dCommand.Parameters = new JObject();

            SendCloudToDeviceLinuxCommand(
                _serviceClient,
                alarmMessage.ioTHubDeviceID,
                c2dCommand).Wait();
        }
        private static void ActionCutOutSpeedLinux(AlarmMessage alarmMessage)
        {
            WriteHighlightedMessage(
                GetDeviceIdHint(alarmMessage.ioTHubDeviceID) +
                " CutOutSpeed! Speed=" + alarmMessage.reading +
                ", MessageID=" + alarmMessage.messageID +
                ", Threshold=" + alarmMessage.threshold,
                ConsoleColor.DarkYellow);

            C2DCommandLinux c2dCommand = new C2DCommandLinux();

            c2dCommand.Name       = C2DCommandLinux.COMMAND_CUTOUT_SPEED_WARNING;
            c2dCommand.Parameters = new JObject();

            SendCloudToDeviceLinuxCommand(
                _serviceClient,
                alarmMessage.ioTHubDeviceID,
                c2dCommand).Wait();
        }
Example #9
0
        private static void ProcessAlarmMessage(AlarmMessage alarmMessage)
        {
            WriteHighlightedMessage(
                alarmMessage.IoTHubDeviceID +
                " - AvgLight=" + alarmMessage.AvgLight +
                ", WinStartTime=" + alarmMessage.WinStartTime +
                ", WinEndTime=" + alarmMessage.WinEndTime,
                ConsoleColor.Yellow);

            C2DCommand c2dCommand = new C2DCommand();

            c2dCommand.command = C2DCommand.COMMAND_LIGHT_WARNING;
            c2dCommand.value   = alarmMessage.AvgLight;
            c2dCommand.time    = alarmMessage.WinEndTime;

            SendCloudToDeviceCommand(
                _serviceClient,
                alarmMessage.IoTHubDeviceID,
                c2dCommand).Wait();
        }
        private static void ActionCutOutSpeedWindows(AlarmMessage alarmMessage)
        {
            WriteHighlightedMessage(
                GetDeviceIdHint(alarmMessage.ioTHubDeviceID) +
                " CutOutSpeed! Speed=" + alarmMessage.reading +
                ", MessageID=" + alarmMessage.messageID +
                ", Threshold=" + alarmMessage.threshold,
                ConsoleColor.Yellow);

            C2DCommand c2dCommand = new C2DCommand();

            c2dCommand.command = C2DCommand.COMMAND_CUTOUT_SPEED_WARNING;
            c2dCommand.value   = alarmMessage.messageID;
            c2dCommand.time    = alarmMessage.createdAt;

            SendCloudToDeviceCommand(
                _serviceClient,
                alarmMessage.ioTHubDeviceID,
                c2dCommand).Wait();
        }
        private static void ActionRepairWindows(AlarmMessage alarmMessage)
        {
            WriteHighlightedMessage(
                GetDeviceIdHint(alarmMessage.ioTHubDeviceID) +
                " Repair! Depreciation=" + alarmMessage.reading +
                ", MessageID=" + alarmMessage.messageID +
                ", Threshold=" + alarmMessage.threshold,
                ConsoleColor.Red);

            C2DCommand c2dCommand = new C2DCommand();

            c2dCommand.command = C2DCommand.COMMAND_REPAIR_WARNING;
            c2dCommand.value   = alarmMessage.messageID;
            c2dCommand.time    = alarmMessage.createdAt;

            SendCloudToDeviceCommand(
                _serviceClient,
                alarmMessage.ioTHubDeviceID,
                c2dCommand).Wait();
        }
Example #12
0
        static void Main(string[] args)
        {
            Console.WriteLine("Console App for Alarm Service Bus...");

            /* Load the settings from App.config */
            string serviceBusConnectionString = ConfigurationManager.AppSettings["ServiceBus.ConnectionString"];

            Console.WriteLine("serviceBusConnectionString={0}\n", serviceBusConnectionString);
            string iotHubConnectionString = ConfigurationManager.AppSettings["IoTHub.ConnectionString"];

            Console.WriteLine("iotHubConnectionString={0}\n", iotHubConnectionString);

            // Retrieve a Queue Client
            QueueClient queueClient = QueueClient.CreateFromConnectionString(serviceBusConnectionString, QueueName);

            // Retrieve a Service Client of IoT Hub
            _serviceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString);

            queueClient.OnMessage(message =>
            {
                Console.WriteLine("\n*******************************************************");
                string msg = message.GetBody <String>();
                try
                {
                    AlarmMessage alarmMessage = JsonConvert.DeserializeObject <AlarmMessage>(msg);

                    ProcessAlarmMessage(alarmMessage);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("****  Exception=" + ex.Message);
                }
            });

            Console.ReadLine();
        }