Exemplo n.º 1
0
        public List <ItemsLogChartHistoryViewModel> GetItemLogs(int itemId, DateTime startDate, DateTime endDate)
        {
            var Entities = new IndustrialMonitoringEntities();
            List <ItemsLogChartHistoryViewModel> result = new List <ItemsLogChartHistoryViewModel>();

            var itemsLog1 = Entities.ItemsLogs.Where(x => x.ItemId == itemId & x.Time >= startDate & x.Time <= endDate).OrderBy(x => x.ItemLogId);
            var itemsLog2 = Entities.ItemsLogArchives.Where(x => x.ItemId == itemId & x.Time >= startDate & x.Time <= endDate).OrderBy(x => x.ItemLogId);

            List <ItemsLog> itemsLog = new List <ItemsLog>();

            itemsLog.AddRange(itemsLog1);

            foreach (ItemsLogArchive itemsLogArchive in itemsLog2)
            {
                ItemsLog log2 = new ItemsLog();
                log2.ItemId    = itemsLogArchive.ItemId;
                log2.Value     = itemsLogArchive.Value;
                log2.Time      = itemsLogArchive.Time;
                log2.ItemLogId = itemsLogArchive.ItemLogId;

                itemsLog.Add(log2);
            }

            var itemsLogAll = itemsLog.OrderBy(x => x.Time);

            foreach (var log in itemsLogAll)
            {
                result.Add(new ItemsLogChartHistoryViewModel(log));
            }

            return(result);
        }
Exemplo n.º 2
0
        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);
            }
        }