Beispiel #1
0
        public void Save(IOTMessage message)
        {
            switch (message.Type)
            {
            case "SensorData":
                sensorDBContext.SensorData.Add((SensorData)message.Data);
                break;

            default: break;
            }
            sensorDBContext.SaveChanges();
        }
Beispiel #2
0
 private static void ThreshholdDetect(DeviceClient s_deviceClient, IOTMessage msg, string upper, string lower, List <IOTAlarmData> lstIOTAlarmMessage)
 {
     if (!upper.Equals(null) && !upper.Equals("NA"))
     {
         if (msg.parameterValue > Convert.ToDouble(upper))
         {
             var alarmData = new IOTAlarmData()
             {
                 id           = System.Guid.NewGuid().ToString(),
                 alarmTypeId  = "1309",
                 alarmStateId = "0",
                 eventTime    = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                 source       = msg.metricCategory,
                 note         = "System Alarm",
                 userId       = "System",
                 siteId       = msg.siteId
             };
             lstIOTAlarmMessage.Add(alarmData);
         }
     }
     if (!lower.Equals(null) && !lower.Equals("NA"))
     {
         if (msg.parameterValue < Convert.ToDouble(lower))
         {
             var alarmData = new IOTAlarmData()
             {
                 id           = System.Guid.NewGuid().ToString(),
                 alarmTypeId  = "1310",
                 alarmStateId = "0",
                 eventTime    = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                 source       = msg.metricCategory,
                 note         = "System Alarm",
                 userId       = "System",
                 siteId       = msg.siteId
             };
             lstIOTAlarmMessage.Add(alarmData);
         }
     }
 }
Beispiel #3
0
        // Async method to send tags telemetry
        private static async void SendDeviceToCloudMessagesAsync()
        {
            while (true)
            {
                OPCServerMock srv         = new OPCServerMock();
                var           isConnected = srv.Connect(oPCDAServerUrl);
                Console.WriteLine("Connected to OPCDA Server:" + srv.IsConnected);

                if (!isConnected)
                {
                    await Task.Delay(Convert.ToInt32(connectionTimeout) * 60000);

                    continue;
                }
                // OPCServerClient opcClient = new OPCServerClient(srv);
                // Create JSON message
                try
                {
                    var results = srv.ReadTagVal(tags.Keys);
                    Console.WriteLine("Number of tags data:" + results.Length);

                    List <IOTMessage>   lstIOTMessage      = new List <IOTMessage>();
                    List <IOTAlarmData> lstIOTAlarmMessage = new List <IOTAlarmData>();

                    foreach (var item in results)
                    {
                        var tagparts      = item.ItemName.Split('.');
                        var siteidValue   = tagparts[2].Substring(0, 10);
                        var floorNumber   = siteidValue.Substring(8);
                        var device        = tagparts[2].Substring(11).Split('#');
                        var lineNumber    = "0";
                        var parameterName = "";
                        var opcTagObj     = tags[item.ItemName];
                        if (tagparts[3].ToLower().Contains("line"))
                        {
                            lineNumber    = tagparts[3].Split('-')[0].Trim();
                            parameterName = tagparts[3].Split('-')[1].Trim();
                        }
                        else
                        {
                            parameterName = tagparts[3].Trim();
                        }
                        var telemetryDataPoint = new IOTMessage
                        {
                            //OPCTag = item.ItemName,
                            id             = System.Guid.NewGuid().ToString(),
                            metricCategory = opcTagObj.Description,
                            siteId         = siteidValue,
                            FloorNumber    = floorNumber,
                            DeviceName     = device[0],
                            DeviceAddress  = string.Concat("#", device[1]),
                            //ParameterName = parameterName,
                            //parameterValue = Convert.ToDouble(item.Value),
                            parameterValue = opcTagObj.Index * Convert.ToDouble(item.Value),
                            unit           = opcTagObj.Unit,
                            LineNum        = lineNumber,
                            ReceivedTime   = item.Timestamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"),
                            messageType    = "Metric"
                        };
                        lstIOTMessage.Add(telemetryDataPoint);
                        ThreshholdDetect(s_deviceClient, telemetryDataPoint, opcTagObj.ThresholdUpper, opcTagObj.ThresholdLower, lstIOTAlarmMessage);
                    }
                    if (lstIOTAlarmMessage.Count != 0)
                    {
                        var alarmMessage = new IOTAlarmMessage()
                        {
                            time        = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                            data        = lstIOTAlarmMessage,
                            type        = "Threshold",
                            messageType = "Alarm"
                        };
                        var alarmMsgString = JsonConvert.SerializeObject(alarmMessage);
                        var alarmMsg       = new Message(Encoding.UTF8.GetBytes(alarmMsgString));
                        await s_deviceClient.SendEventAsync(alarmMsg);

                        Console.WriteLine("{0} > Sending alarm message: {1}", DateTime.Now, alarmMsgString);
                        lstIOTAlarmMessage.Clear();
                    }


                    var messageString = JsonConvert.SerializeObject(lstIOTMessage);
                    var message       = new Message(Encoding.UTF8.GetBytes(messageString));

                    // Add a custom application property to the message.
                    // An IoT hub can filter on these properties without access to the message body.
                    // message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");

                    // Send the telemetry message
                    await s_deviceClient.SendEventAsync(message);

                    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

                    await Task.Delay(Convert.ToInt32(streamInterval));
                }
                catch (Exception ex)
                {
                    if (ex.Message == "Not connected to OPC server")
                    {
                        continue;
                    }
                    throw ex;
                }
                finally
                {
                    srv.DisConnect();
                }
            }
        }