Esempio n. 1
0
        public ItemsLogLatestAIOViewModel GeItemsLogLatest(int itemId)
        {
            try
            {
                ItemsLogLatestAIOViewModel result = null;

                var            Entities = new IndustrialMonitoringEntities();
                ItemsLogLatest current  = Entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == itemId);

                if (current == null)
                {
                    return(null);
                }

                result = new ItemsLogLatestAIOViewModel(current);

                return(result);
            }
            catch (Exception ex)
            {
                Logger.LogMonitoringServiceLibrary(ex);
                return(null);
            }
        }
        public async Task ReadValue()
        {
            try
            {
                Entities = new IndustrialMonitoringEntities();

                if (this.DefinationType == ItemDefinationType.SqlDefined)
                {
                    if (this.Type == ItemType.Digital)
                    {
                        SubscriberBool = new NetworkVariableBufferedSubscriber <bool>(this.Location);
                        SubscriberBool.Connect();
                    }
                    else if (this.Type == ItemType.Analog)
                    {
                        SubscriberInt = new NetworkVariableBufferedSubscriber <dynamic>(this.Location);
                        SubscriberInt.Connect();
                    }
                }
                else if (this.DefinationType == ItemDefinationType.BACnet)
                {
                    if (BACnetDevice == null)
                    {
                        this.BACnetDevice = BACnetDevice.Instance;
                    }

                    string     ip       = ItemObj.BACnetIP;
                    int        port     = ItemObj.BACnetPort.Value;
                    IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
                    uint       instance = (uint)ItemObj.BACnetControllerInstance.Value;

                    DeviceBaCnet = new Device("Device", 0, 0, endPoint, 0, instance);
                }


                string value = "-1000";

                if (this.DefinationType == ItemDefinationType.SqlDefined)
                {
                    if (this.Type == ItemType.Digital)
                    {
                        var data = SubscriberBool.ReadData();
                        value = Convert.ToInt32(data.GetValue()).ToString();
                    }
                    else if (this.Type == ItemType.Analog)
                    {
                        var data = SubscriberInt.ReadData();
                        value = Math.Round(data.GetValue(), 2).ToString();
                    }
                }
                else if (this.DefinationType == ItemDefinationType.CustomDefiend)
                {
                    switch (this.ItemId)
                    {
                    case 10:
                        value = (BSProcessDataServiceClient.GetPreHeatingZoneTemperature() / 10).ToString();
                        break;

                    case 13:
                        value = BSProcessDataServiceClient.GetSterilizerZoneTemperature().ToString();
                        break;

                    case 14:
                        value = (BSProcessDataServiceClient.GetCoolingZoneTemperature() / 10).ToString();
                        break;
                    }
                }
                else if (this.DefinationType == ItemDefinationType.BACnet)
                {
                    if (this.Type == ItemType.Digital)
                    {
                        BACnetEnums.BACNET_OBJECT_TYPE bacnetType = (BACnetEnums.BACNET_OBJECT_TYPE)ItemObj.BACnetItemType.Value;
                        uint itemInstance = (uint)ItemObj.BACnetItemInstance.Value;
                        value = BACnetDevice.ReadValue(DeviceBaCnet, bacnetType, itemInstance).ToString();
                    }
                    else if (Type == ItemType.Analog)
                    {
                        BACnetEnums.BACNET_OBJECT_TYPE bacnetType = (BACnetEnums.BACNET_OBJECT_TYPE)ItemObj.BACnetItemType.Value;
                        uint itemInstance = (uint)ItemObj.BACnetItemInstance.Value;

                        string preValue       = BACnetDevice.ReadValue(DeviceBaCnet, bacnetType, itemInstance).ToString();
                        double preValueDouble = double.Parse(preValue);
                        value = Math.Round(preValueDouble, 2).ToString();
                    }
                }

                lock (padlock)
                {
                    if (value == null)
                    {
                        return;
                    }

                    if (value == "-1000")
                    {
                        return;
                    }

                    double valueDouble = double.Parse(value);

                    bool condition = !string.IsNullOrEmpty(ItemObj.MinRange) && !string.IsNullOrEmpty(ItemObj.MaxRange);
                    if (condition)
                    {
                        double minRange = double.Parse(ItemObj.MinRange);
                        double maxRange = double.Parse(ItemObj.MaxRange);

                        bool shouldNormalize = false;

                        if (ItemObj.NormalizeWhenOutOfRange != null)
                        {
                            shouldNormalize = ItemObj.NormalizeWhenOutOfRange.Value;
                        }

                        if (valueDouble < minRange)
                        {
                            if (shouldNormalize)
                            {
                                value = ItemObj.MinRange;
                            }
                            else
                            {
                                return;
                            }
                        }

                        if (valueDouble > maxRange)
                        {
                            if (shouldNormalize)
                            {
                                value = ItemObj.MaxRange;
                            }
                            else
                            {
                                return;
                            }
                        }
                    }

                    // detect outliers

                    bool isOutlier = false;
                    int  outlierId = 0;

                    int numberOfDataForBoxplot = 0;

                    if (ItemObj.NumberOfDataForBoxplot != null)
                    {
                        numberOfDataForBoxplot = ItemObj.NumberOfDataForBoxplot.Value;
                    }

                    if (numberOfDataForBoxplot > 2)
                    {
                        if (this.Type == ItemType.Analog)
                        {
                            var lastData = Entities.ItemsLogRawDatas.Where(x => x.ItemId == ItemId).OrderByDescending(x => x.ItemLogRawDataId).Take(numberOfDataForBoxplot).ToList();

                            if (lastData.Count > 3)
                            {
                                List <double> lastDataInDouble = new List <double>();

                                foreach (var itemsLog in lastData)
                                {
                                    double currentValue = double.Parse(itemsLog.Value);

                                    lastDataInDouble.Add(currentValue);
                                }

                                var iqr = Statistics.InterquartileRange(lastDataInDouble);
                                var lqr = Statistics.LowerQuartile(lastDataInDouble);
                                var uqr = Statistics.UpperQuartile(lastDataInDouble);


                                if (valueDouble > 3 * iqr + uqr)
                                {
                                    isOutlier = true;
                                }

                                if (valueDouble < lqr - 3 * iqr)
                                {
                                    isOutlier = true;
                                }

                                if (isOutlier)
                                {
                                    LogOutlier logOutlier = new LogOutlier();
                                    logOutlier.ItemId = this.ItemId;
                                    logOutlier.IQR    = iqr.ToString();
                                    logOutlier.LQR    = lqr.ToString();
                                    logOutlier.UQR    = uqr.ToString();
                                    logOutlier.Value  = valueDouble.ToString();
                                    logOutlier.Time   = DateTime.Now;

                                    Entities.LogOutliers.Add(logOutlier);
                                    Entities.SaveChanges();

                                    outlierId = logOutlier.OutlierId;
                                }
                            }
                        }
                    }

                    //


                    // Save in ItemsLogRawData

                    var lastItemLogRaw = Entities.ItemsLogRawDatas.OrderByDescending(x => x.ItemLogRawDataId).FirstOrDefault(x => x.ItemId == ItemId);

                    if (lastItemLogRaw == null)
                    {
                        ItemsLogRawData rawData = new ItemsLogRawData();
                        rawData.ItemId = ItemId;
                        rawData.Value  = value;
                        rawData.Time   = DateTime.Now;

                        if (outlierId > 0)
                        {
                            rawData.OutlierId = outlierId;
                        }

                        Entities.ItemsLogRawDatas.Add(rawData);
                        Entities.SaveChanges();

                        lastItemLogRaw = rawData;
                    }

                    if (SaveInItemsLogWhen == WhenToLog.OnTimerElapsed)
                    {
                        TimeSpan timeSpan = DateTime.Now - lastItemLogRaw.Time;

                        if (timeSpan.TotalSeconds >= SaveInItemsLogTimeInterval)
                        {
                            ItemsLogRawData rawData = new ItemsLogRawData();
                            rawData.ItemId = ItemId;
                            rawData.Value  = value;
                            rawData.Time   = DateTime.Now;

                            if (outlierId > 0)
                            {
                                rawData.OutlierId = outlierId;
                            }

                            Entities.ItemsLogRawDatas.Add(rawData);
                            Entities.SaveChanges();
                        }
                    }
                    else if (SaveInItemsLogWhen == WhenToLog.OnChange)
                    {
                        if (lastItemLogRaw.Value != value)
                        {
                            ItemsLogRawData rawData = new ItemsLogRawData();
                            rawData.ItemId = ItemId;
                            rawData.Value  = value;
                            rawData.Time   = DateTime.Now;

                            if (outlierId > 0)
                            {
                                rawData.OutlierId = outlierId;
                            }

                            Entities.ItemsLogRawDatas.Add(rawData);
                            Entities.SaveChanges();
                        }
                        else
                        {
                            if (DateTime.Now - lastItemLogRaw.Time > new TimeSpan(0, 0, 1, 0))
                            {
                                ItemsLogRawData rawData = new ItemsLogRawData();
                                rawData.ItemId = ItemId;
                                rawData.Value  = value;
                                rawData.Time   = DateTime.Now;

                                if (outlierId > 0)
                                {
                                    rawData.OutlierId = outlierId;
                                }

                                Entities.ItemsLogRawDatas.Add(rawData);
                                Entities.SaveChanges();
                            }
                        }
                    }
                    //

                    if (!isOutlier)
                    {
                        var lastItemLog = Entities.ItemsLogs.OrderByDescending(x => x.ItemLogId).FirstOrDefault(x => x.ItemId == ItemId);

                        if (lastItemLog == null)
                        {
                            ItemsLog itemsLog = new ItemsLog();
                            itemsLog.ItemId = this.ItemId;
                            itemsLog.Time   = DateTime.Now;
                            itemsLog.Value  = value;
                            Entities.ItemsLogs.Add(itemsLog);

                            Entities.SaveChanges();

                            lastItemLog = itemsLog;
                        }

                        if (SaveInItemsLogWhen == WhenToLog.OnTimerElapsed)
                        {
                            TimeSpan timeSpan = DateTime.Now - lastItemLog.Time;

                            if (timeSpan.TotalSeconds >= SaveInItemsLogTimeInterval)
                            {
                                ItemsLog itemsLog = new ItemsLog();
                                itemsLog.ItemId = this.ItemId;
                                itemsLog.Time   = DateTime.Now;
                                itemsLog.Value  = value;
                                Entities.ItemsLogs.Add(itemsLog);

                                Entities.SaveChanges();
                            }
                        }
                        else if (SaveInItemsLogWhen == WhenToLog.OnChange)
                        {
                            if (lastItemLog.Value != value)
                            {
                                ItemsLog itemsLog = new ItemsLog();
                                itemsLog.ItemId = this.ItemId;
                                itemsLog.Time   = DateTime.Now;
                                itemsLog.Value  = value;
                                Entities.ItemsLogs.Add(itemsLog);

                                Entities.SaveChanges();
                            }
                            else
                            {
                                if (DateTime.Now - lastItemLog.Time > new TimeSpan(0, 0, 1, 0))
                                {
                                    ItemsLog itemsLog = new ItemsLog();
                                    itemsLog.ItemId = this.ItemId;
                                    itemsLog.Time   = DateTime.Now;
                                    itemsLog.Value  = value;
                                    Entities.ItemsLogs.Add(itemsLog);

                                    Entities.SaveChanges();
                                }
                            }
                        }


                        var lastItemLogLatest = Entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == ItemId);

                        if (lastItemLogLatest == null)
                        {
                            ItemsLogLatest itemsLogLatest = null;
                            if (Entities.ItemsLogLatests.Any(x => x.ItemId == this.ItemId))
                            {
                                itemsLogLatest       = Entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == this.ItemId);
                                itemsLogLatest.Time  = DateTime.Now;
                                itemsLogLatest.Value = value;
                            }
                            else
                            {
                                itemsLogLatest        = new ItemsLogLatest();
                                itemsLogLatest.ItemId = this.ItemId;
                                itemsLogLatest.Time   = DateTime.Now;
                                itemsLogLatest.Value  = value;
                                Entities.ItemsLogLatests.Add(itemsLogLatest);
                            }

                            Entities.SaveChanges();

                            lastItemLogLatest = itemsLogLatest;
                        }

                        if (SaveInItemsLogLastWhen == WhenToLog.OnTimerElapsed)
                        {
                            TimeSpan timeSpan = DateTime.Now - lastItemLogLatest.Time;

                            if (timeSpan.TotalSeconds >= SaveInItemsLogLastesTimeInterval)
                            {
                                ItemsLogLatest itemsLogLatest = null;
                                if (Entities.ItemsLogLatests.Any(x => x.ItemId == this.ItemId))
                                {
                                    itemsLogLatest       = Entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == this.ItemId);
                                    itemsLogLatest.Time  = DateTime.Now;
                                    itemsLogLatest.Value = value;
                                }
                                else
                                {
                                    itemsLogLatest        = new ItemsLogLatest();
                                    itemsLogLatest.ItemId = this.ItemId;
                                    itemsLogLatest.Time   = DateTime.Now;
                                    itemsLogLatest.Value  = value;
                                    Entities.ItemsLogLatests.Add(itemsLogLatest);
                                }

                                Entities.SaveChanges();
                            }
                        }
                        else if (SaveInItemsLogLastWhen == WhenToLog.OnChange)
                        {
                            if (lastItemLogLatest.Value != value)
                            {
                                ItemsLogLatest itemsLogLatest = null;
                                if (Entities.ItemsLogLatests.Any(x => x.ItemId == this.ItemId))
                                {
                                    itemsLogLatest       = Entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == this.ItemId);
                                    itemsLogLatest.Time  = DateTime.Now;
                                    itemsLogLatest.Value = value;
                                }
                                else
                                {
                                    itemsLogLatest        = new ItemsLogLatest();
                                    itemsLogLatest.ItemId = this.ItemId;
                                    itemsLogLatest.Time   = DateTime.Now;
                                    itemsLogLatest.Value  = value;
                                    Entities.ItemsLogLatests.Add(itemsLogLatest);
                                }

                                Entities.SaveChanges();
                            }
                            else
                            {
                                if (DateTime.Now - lastItemLogLatest.Time > new TimeSpan(0, 0, 1, 0))
                                {
                                    ItemsLogLatest itemsLogLatest = null;
                                    if (Entities.ItemsLogLatests.Any(x => x.ItemId == this.ItemId))
                                    {
                                        itemsLogLatest       = Entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == this.ItemId);
                                        itemsLogLatest.Time  = DateTime.Now;
                                        itemsLogLatest.Value = value;
                                    }
                                    else
                                    {
                                        itemsLogLatest        = new ItemsLogLatest();
                                        itemsLogLatest.ItemId = this.ItemId;
                                        itemsLogLatest.Time   = DateTime.Now;
                                        itemsLogLatest.Value  = value;
                                        Entities.ItemsLogLatests.Add(itemsLogLatest);
                                    }

                                    Entities.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogMonitoringServiceLibrary(ex);
            }
        }
Esempio n. 3
0
        private void CheckNotifications()
        {
            IsThreadRunning = true;

            while (!StopThread)
            {
                try
                {
                    IndustrialMonitoringEntities entities          = new IndustrialMonitoringEntities();
                    List <NotificationItem>      notificationItems = entities.NotificationItems.ToList();

                    foreach (var notificationItem in notificationItems)
                    {
                        entities = new IndustrialMonitoringEntities();
                        int notificationId = notificationItem.NotificationId;

                        if (notificationItem.DisableNotification)
                        {
                            NotificationItemsLog prev =
                                entities.NotificationItemsLogs.FirstOrDefault(x => x.NotificationId == notificationId);

                            if (!prev.Value)
                            {
                                NotificationItemsLog notificationItemsLog = new NotificationItemsLog();
                                notificationItemsLog.NotificationId = notificationId;
                                notificationItemsLog.Value          = true;
                                notificationItemsLog.Time           = DateTime.Now;

                                entities.NotificationItemsLogs.Add(notificationItemsLog);
                            }

                            NotificationItemsLogLatest latest =
                                entities.NotificationItemsLogLatests.FirstOrDefault(
                                    x => x.NotificationId == notificationId);

                            if (!latest.Value)
                            {
                                latest.Value = true;
                                latest.Time  = DateTime.Now;

                                entities.SaveChanges();
                            }


                            continue;
                        }

                        NotificationItemsLogLatest notificationItemsLogLatest = entities.NotificationItemsLogLatests.FirstOrDefault(x => x.NotificationId == notificationId);

                        if (notificationItemsLogLatest == null)
                        {
                            notificationItemsLogLatest = new NotificationItemsLogLatest();
                            notificationItemsLogLatest.NotificationId = notificationId;
                            notificationItemsLogLatest.Value          = false;
                            notificationItemsLogLatest.Time           = DateTime.Now;

                            entities.NotificationItemsLogLatests.Add(notificationItemsLogLatest);
                            entities.SaveChanges();
                        }
                        else
                        {
                            if (!notificationItem.DisableNotification)
                            {
                                ItemsLogLatest itemLogLatest =
                                    entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == notificationItem.ItemId);

                                double currentValue = double.Parse(itemLogLatest.Value);

                                bool withoutNotification = false;

                                if (notificationItem.NotificationType == (int)NotificationType.Lower)
                                {
                                    if (currentValue < notificationItem.High)
                                    {
                                        withoutNotification = true;
                                    }
                                }
                                else if (notificationItem.NotificationType == (int)NotificationType.Between)
                                {
                                    if (currentValue > notificationItem.Low & currentValue < notificationItem.High)
                                    {
                                        withoutNotification = true;
                                    }
                                }
                                else if (notificationItem.NotificationType == (int)NotificationType.Higher)
                                {
                                    if (currentValue > notificationItem.Low)
                                    {
                                        withoutNotification = true;
                                    }
                                }

                                if (withoutNotification)
                                {
                                    if (notificationItemsLogLatest.Value == false)
                                    {
                                        NotificationItemsLog notificationItemsLog = new NotificationItemsLog();
                                        notificationItemsLog.NotificationId = notificationId;
                                        notificationItemsLog.Value          = true;
                                        notificationItemsLog.Time           = DateTime.Now;

                                        entities.NotificationItemsLogs.Add(notificationItemsLog);
                                        entities.SaveChanges();

                                        //var bot = NotificationsBotInvoker.Instance;
                                        //bot.SendNotification(notificationItemsLog.NotificationLogId);

                                        NotificationsBotInvoker.RegisterNewRecord(notificationId, notificationItemsLog.NotificationLogId);
                                    }
                                }
                                else
                                {
                                    // we have notification

                                    if (notificationItemsLogLatest.Value)
                                    {
                                        // we have a change in notification state

                                        NotificationItemsLog notificationItemsLog = new NotificationItemsLog();
                                        notificationItemsLog.NotificationId = notificationId;
                                        notificationItemsLog.Value          = false;
                                        notificationItemsLog.Time           = DateTime.Now;

                                        entities.NotificationItemsLogs.Add(notificationItemsLog);
                                        entities.SaveChanges();

                                        //var bot = NotificationsBotInvoker.Instance;
                                        //bot.SendNotification(notificationItemsLog.NotificationLogId);

                                        NotificationsBotInvoker.RegisterNewRecord(notificationId, notificationItemsLog.NotificationLogId);
                                    }
                                }


                                notificationItemsLogLatest.Value = withoutNotification;
                                notificationItemsLogLatest.Time  = DateTime.Now;
                                entities.SaveChanges();
                            }
                            else
                            {
                                if (notificationItemsLogLatest.Value == false)
                                {
                                    NotificationItemsLog notificationItemsLog = new NotificationItemsLog();
                                    notificationItemsLog.NotificationId = notificationId;
                                    notificationItemsLog.Value          = true;
                                    notificationItemsLog.Time           = DateTime.Now;

                                    entities.NotificationItemsLogs.Add(notificationItemsLog);
                                    entities.SaveChanges();

                                    NotificationsBotInvoker.RegisterNewRecord(notificationId, notificationItemsLog.NotificationLogId);
                                }
                            }
                        }

                        Thread.Sleep(10);
                    }

                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Logger.LogMonitoringServiceLibrary(ex);
                }
            }

            IsThreadRunning = false;
        }
        public async void CheckNotificationBot()
        {
            while (true)
            {
                IndustrialMonitoringEntities entities         = null;
                List <NotificationBot>       notificationBots = new List <NotificationBot>();

                try
                {
                    entities = new IndustrialMonitoringEntities();

                    notificationBots = entities.NotificationBots.Where(x => x.IsCompleted == false).OrderBy(x => x.NotificationBotId).ToList();
                }
                catch (Exception ex)
                {
                    Logger.LogMonitoringServiceLibrary(ex);
                }

                foreach (NotificationBot n in notificationBots)
                {
                    try
                    {
                        if (n.Delay == 0)
                        {
                            await this.SendNotification(n.NotificationLogId, NotificationDelayType.Normal);

                            n.IsSent      = true;
                            n.IsCompleted = true;
                            n.SentTime    = DateTime.Now;

                            entities.SaveChanges();
                        }

                        bool timerElapsed = DateTime.Now - n.RegisterTime > TimeSpan.FromSeconds(n.Delay);

                        if (n.Delay > 0)
                        {
                            if (!n.NotificationItemsLog.Value)
                            {
                                // with alarm
                                // if still has alarm

                                if (timerElapsed)
                                {
                                    ItemsLogLatest itemLogLatest =
                                        entities.ItemsLogLatests.FirstOrDefault(x => x.ItemId == n.NotificationItem.ItemId);

                                    double currentValue = double.Parse(itemLogLatest.Value);

                                    bool withoutNotification = false;

                                    if (n.NotificationItem.NotificationType == (int)NotificationType.Lower)
                                    {
                                        if (currentValue < n.NotificationItem.High)
                                        {
                                            withoutNotification = true;
                                        }
                                    }
                                    else if (n.NotificationItem.NotificationType == (int)NotificationType.Between)
                                    {
                                        if (currentValue > n.NotificationItem.Low & currentValue < n.NotificationItem.High)
                                        {
                                            withoutNotification = true;
                                        }
                                    }
                                    else if (n.NotificationItem.NotificationType == (int)NotificationType.Higher)
                                    {
                                        if (currentValue > n.NotificationItem.Low)
                                        {
                                            withoutNotification = true;
                                        }
                                    }

                                    if (!withoutNotification)
                                    {
                                        await this.SendNotification(n.NotificationLogId, NotificationDelayType.Delayed);

                                        n.IsSent      = true;
                                        n.IsCompleted = true;
                                        n.SentTime    = DateTime.Now;

                                        entities.SaveChanges();
                                    }
                                    else
                                    {
                                        n.IsSent      = false;
                                        n.IsCompleted = true;

                                        entities.SaveChanges();
                                    }
                                }
                            }
                            else
                            {
                                // without alarm

                                if (timerElapsed)
                                {
                                    IndustrialMonitoringEntities entities2 = new IndustrialMonitoringEntities();
                                    var lastNotificationBot =
                                        entities2.NotificationBots.Where(x => x.NotificationId == n.NotificationId & x.WithoutAlarm == false &
                                                                         x.Delay > 0 & x.IsCompleted == true).
                                        OrderByDescending(x => x.NotificationBotId).FirstOrDefault();

                                    if (lastNotificationBot == null)
                                    {
                                        continue;
                                    }

                                    if (lastNotificationBot.IsSent)
                                    {
                                        await this.SendNotification(n.NotificationLogId, NotificationDelayType.Delayed);

                                        n.IsSent      = true;
                                        n.IsCompleted = true;
                                        n.SentTime    = DateTime.Now;

                                        entities.SaveChanges();
                                    }
                                    else
                                    {
                                        n.IsSent      = false;
                                        n.IsCompleted = true;

                                        entities.SaveChanges();
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogMonitoringServiceLibrary(ex);
                    }
                }

                await Task.Delay(1000);
            }
        }