示例#1
0
        private void InsertSensorData(Equipment ex, Sensor sx, DateTime dt, Alarm al)
        {
            try
            {
                decimal        value  = 0;
                List <decimal> allVal = new List <decimal>();

                List <SensorData> dr = new List <SensorData>();

                DateTime dstart = new DateTime(dt.Year, dt.Month, dt.Day);

                DateTime dend = dt.AddHours(24);
                Random   rx   = new Random();
                while (dstart < dend)
                {
                    double v = 0;
                    if (sx.Sensor_Category.Equals("Efficiency"))
                    {
                        v = rx.NextDouble() * 100;
                        if (v < 50)
                        {
                            v += 40;
                        }
                        if (v > 90)
                        {
                            v = 90;
                        }
                    }
                    else if (sx.Sensor_Category.Equals("Up Time"))
                    {
                        v = rx.NextDouble() * 3;
                        if (v < 1)
                        {
                            v += 1;
                        }
                        if (v > 3)
                        {
                            v = 3;
                        }
                    }
                    else if (sx.Sensor_Category.Equals("Power Consumed"))
                    {
                        v = (0.3 + rx.NextDouble()) * 15;
                    }
                    else if (sx.Sensor_Category.Equals("Cost"))
                    {
                        v = rx.NextDouble() * 10;
                    }
                    else
                    {
                        v = (0.3 + rx.NextDouble()) * 20;
                    }
                    SensorData sxx = new SensorData();
                    sxx.Related_Equipment  = ex.Record_ID.ToString();
                    sxx.Related_Sensor     = sx.Record_ID.ToString();
                    sxx.TimeStamp          = dstart.ToString("s") + "Z";
                    sxx.TimeStampDt        = DateTime.SpecifyKind(dstart, DateTimeKind.Utc);
                    sxx.Sensor_Name_Lookup = sx.Tag.ToLower().Replace("/", "_");
                    sxx.Value    = Math.Round(v, 4).ToString();
                    sxx.DecValue = Math.Round(v, 4);

                    sxx.Site   = ex.Site_Name_Lookup;
                    sxx.SiteId = ex.Related_Site;

                    sxx.EquipmentName = ex.Equipment_Name;

                    allVal.Add((decimal)sxx.DecValue);
                    dr.Add(sxx);
                    dstart = dstart.AddHours(3);
                }

                InfluxConnector conn = new InfluxConnector(envconfiguration.GetSection("InfluxServer").Value,
                                                           envconfiguration.GetSection("InfluxToken").Value,
                                                           envconfiguration.GetSection("InfluxBucket").Value,
                                                           envconfiguration.GetSection("InfluxOrg").Value);
                conn.WriteData(dr);

                SensorData s = new SensorData();
                s.Related_Equipment  = ex.Record_ID.ToString();
                s.Related_Sensor     = sx.Record_ID.ToString();
                s.TimeStamp          = dt.ToString("s") + "Z";
                s.TimeStampDt        = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
                s.Sensor_Name_Lookup = sx.Tag.ToLower().Replace("/", "_");


                if (sx.Sensor_Category.Equals("Efficiency"))
                {
                    value = allVal.Average();
                }
                else
                {
                    value = allVal.Sum();
                }

                s.Value = value.ToString();


                List <SensorData> dx = UpsertData <SensorData>("SensorData", s, SensorData.fieldIds);
                if (dx.Count > 0)
                {
                    // Alarm
                    if (al != null)
                    {
                        bool    isAlarm = false;
                        decimal alVal   = Decimal.Parse(al.Formula_Value);
                        if (al.Formula_Operator.Equals(">"))
                        {
                            if (value > alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals("<"))
                        {
                            if (value < alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals(">="))
                        {
                            if (value >= alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals("<="))
                        {
                            if (value <= alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals("=="))
                        {
                            if (value == alVal)
                            {
                                isAlarm = true;
                            }
                        }

                        if (isAlarm)
                        {
                            AlarmLog a = new AlarmLog();
                            a.Alarm_Text           = al.Alarm_Name + " Raised - " + al.Priority + " for " + ex.Site_Name_Lookup + "-" + ex.Equipment_Name;
                            a.Related_Equipment    = ex.Record_ID.ToString();
                            a.Related_Sensor_Datum = dx[0].Record_ID.ToString();
                            a.Related_Alarm        = al.Record_ID.ToString();

                            List <AlarmLog> da = UpsertData <AlarmLog>("AlarmLog", a, AlarmLog.fieldIds);
                        }
                    }
                }
            }
            catch (Exception exx)
            { }
        }
示例#2
0
        private void InsertMQQTSensorData(Equipment ex, Sensor sx, DateTime dt, Alarm al, String value)
        {
            try
            {
                List <decimal> allVal = new List <decimal>();

                List <SensorData> dr = new List <SensorData>();


                double v = 0;
                Double.TryParse(value, out v);

                SensorData sxx = new SensorData();
                sxx.Related_Equipment  = ex.Record_ID.ToString();
                sxx.Related_Sensor     = sx.Record_ID.ToString();
                sxx.TimeStamp          = dt.ToString("s") + "Z";
                sxx.TimeStampDt        = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
                sxx.Sensor_Name_Lookup = sx.Tag.ToLower().Replace("/", "_");
                sxx.Value    = Math.Round(v, 4).ToString();
                sxx.DecValue = Math.Round(v, 4);

                sxx.Site   = ex.Site_Name_Lookup;
                sxx.SiteId = ex.Related_Site;

                sxx.EquipmentName = ex.Equipment_Name;

                allVal.Add((decimal)sxx.DecValue);
                dr.Add(sxx);

                //Write to Influx
                InfluxConnector conn = new InfluxConnector(envconfiguration.GetSection("InfluxServer").Value,
                                                           envconfiguration.GetSection("InfluxToken").Value,
                                                           envconfiguration.GetSection("InfluxBucket").Value,
                                                           envconfiguration.GetSection("InfluxOrg").Value);

                conn.WriteData(dr);

                String measure = sx.Tag.Replace("/", "_");

                // Get data for current day from influx
                List <FluxTable> tab = conn.ReadData(ex.Record_ID.ToString(), dt, measure).Result;

                //Get data for current day from QB
                if (tab != null)
                {
                    if (tab.Count > 0)
                    {
                        FluxTable ft = tab[0];
                        if (ft.Records.Count > 0)
                        {
                            foreach (var rec in ft.Records)
                            {
                                String  cx  = rec.GetValue().ToString();
                                decimal dxm = 0;
                                if (Decimal.TryParse(cx, out dxm))
                                {
                                    allVal.Add(dxm);
                                }
                            }
                        }
                    }
                }
                DateTime   start = new DateTime(dt.Year, dt.Month, dt.Day);
                SensorData s     = new SensorData();
                s.Related_Equipment  = ex.Record_ID.ToString();
                s.Related_Sensor     = sx.Record_ID.ToString();
                s.TimeStamp          = start.ToString("s") + "Z";
                s.TimeStampDt        = DateTime.SpecifyKind(start, DateTimeKind.Utc);
                s.Sensor_Name_Lookup = sx.Tag.ToLower().Replace("/", "_");

                decimal sensorValue = 0;

                if (sx.Sensor_Category.Equals("Efficiency"))
                {
                    sensorValue = allVal.Average();
                }
                else
                {
                    sensorValue = allVal.Sum();
                }

                s.Value = sensorValue.ToString();

                List <SensorData> sensorData = GetData <SensorData>("SensorData", SensorData.fieldIds, "{15.EX.'" + ex.Record_ID + "'}AND{8.EX." +
                                                                    sx.Record_ID + "}AND{11.OBF.'" + dt.ToString("yyyy-MM-dd") + "'}AND{11.AF.'" + dt.AddDays(-2).ToString("yyyy-MM-dd") + "'}");
                var ext = sensorData.Where(u => u.TimeStamp.Equals(dt.ToString("MM/dd/yyyy") + " 12:00:00 AM")).FirstOrDefault();

                if (ext != null)
                {
                    s.Record_ID = ext.Record_ID;
                }

                List <SensorData> dx = UpsertData <SensorData>("SensorData", s, SensorData.fieldIds);
                if (dx.Count > 0)
                {
                    // Alarm
                    if (al != null)
                    {
                        bool    isAlarm = false;
                        decimal alVal   = Decimal.Parse(al.Formula_Value);
                        if (al.Formula_Operator.Equals(">"))
                        {
                            if (sensorValue > alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals("<"))
                        {
                            if (sensorValue < alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals(">="))
                        {
                            if (sensorValue >= alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals("<="))
                        {
                            if (sensorValue <= alVal)
                            {
                                isAlarm = true;
                            }
                        }
                        else if (al.Formula_Operator.Equals("=="))
                        {
                            if (sensorValue == alVal)
                            {
                                isAlarm = true;
                            }
                        }

                        if (isAlarm)
                        {
                            AlarmLog a = new AlarmLog();
                            a.Alarm_Text           = al.Alarm_Name + " Raised - " + al.Priority + " for " + ex.Site_Name_Lookup + "-" + ex.Equipment_Name;
                            a.Related_Equipment    = ex.Record_ID.ToString();
                            a.Related_Sensor_Datum = dx[0].Record_ID.ToString();
                            a.Related_Alarm        = al.Record_ID.ToString();

                            List <AlarmLog> da = UpsertData <AlarmLog>("AlarmLog", a, AlarmLog.fieldIds);
                        }
                    }
                }
            }
            catch (Exception exx)
            { }
        }