public void Start() { 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(); } Timer = new Timer(CheckValue, new object(), 0, ScanCycle); }
public bool GetMuteHorn() { try { var subscriberBool = new NetworkVariableBufferedSubscriber <bool>(@"\\localhost\Simulation\OPC\Channel1\Device1\MuteHorn"); subscriberBool.Connect(); var variable = subscriberBool.ReadData(); bool result = variable.GetValue(); subscriberBool.Disconnect(); return(result); } catch (Exception ex) { Logger.LogMonitoringServiceLibrary(ex); return(false); } }
public PLCInt(string location) { this.Location = location; Subscriber = new NetworkVariableBufferedSubscriber <UInt16>(this.Location); BufferedWriter = new NetworkVariableBufferedWriter <UInt16>(this.Location); }
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); } }
public Dictionary <int, int> GetStatus2() { try { string motor1Speed1Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor1Speed1_Q"; string motor1Speed2Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor1Speed2_Q"; string motor1Speed3Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor1Speed3_Q"; string motor2Speed1Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor2Speed1_Q"; string motor2Speed2Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor2Speed2_Q"; string motor2Speed3Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor2Speed3_Q"; bool motor1Speed1; bool motor1Speed2; bool motor1Speed3; bool motor2Speed1; bool motor2Speed2; bool motor2Speed3; var subscriberBool1 = new NetworkVariableBufferedSubscriber <bool>(motor1Speed1Path); subscriberBool1.Connect(); var variable1 = subscriberBool1.ReadData(); motor1Speed1 = variable1.GetValue(); subscriberBool1.Disconnect(); var subscriberBool2 = new NetworkVariableBufferedSubscriber <bool>(motor1Speed2Path); subscriberBool2.Connect(); var variable2 = subscriberBool2.ReadData(); motor1Speed2 = variable2.GetValue(); subscriberBool2.Disconnect(); var subscriberBool3 = new NetworkVariableBufferedSubscriber <bool>(motor1Speed3Path); subscriberBool3.Connect(); var variable3 = subscriberBool3.ReadData(); motor1Speed3 = variable3.GetValue(); subscriberBool3.Disconnect(); var subscriberBool4 = new NetworkVariableBufferedSubscriber <bool>(motor2Speed1Path); subscriberBool4.Connect(); var variable4 = subscriberBool4.ReadData(); motor2Speed1 = variable4.GetValue(); subscriberBool4.Disconnect(); var subscriberBool5 = new NetworkVariableBufferedSubscriber <bool>(motor2Speed2Path); subscriberBool5.Connect(); var variable5 = subscriberBool5.ReadData(); motor2Speed2 = variable5.GetValue(); subscriberBool5.Disconnect(); var subscriberBool6 = new NetworkVariableBufferedSubscriber <bool>(motor2Speed3Path); subscriberBool6.Connect(); var variable6 = subscriberBool6.ReadData(); motor2Speed3 = variable6.GetValue(); subscriberBool6.Disconnect(); Dictionary <int, int> result = new Dictionary <int, int>(); if (motor1Speed1 == false && motor1Speed2 == false && motor1Speed3 == false) { result.Add(1, 0); } else if (motor1Speed1) { result.Add(1, 1); } else if (motor1Speed2) { result.Add(1, 2); } else if (motor1Speed3) { result.Add(1, 3); } if (motor2Speed1 == false && motor2Speed2 == false && motor2Speed3 == false) { result.Add(2, 0); } else if (motor2Speed1) { result.Add(2, 1); } else if (motor2Speed2) { result.Add(2, 2); } else if (motor2Speed3) { result.Add(2, 3); } return(result); } catch (Exception ex) { Logger.LogMonitoringServiceLibrary(ex); } return(null); }
public string GetStatus() { try { string motor1Speed1Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor1Speed1_Q"; string motor1Speed2Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor1Speed2_Q"; string motor1Speed3Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor1Speed3_Q"; string motor2Speed1Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor2Speed1_Q"; string motor2Speed2Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor2Speed2_Q"; string motor2Speed3Path = @"\\localhost\Simulation\OPC\Channel1\Device1\TechnicalFanCoilMotor2Speed3_Q"; bool motor1Speed1; bool motor1Speed2; bool motor1Speed3; bool motor2Speed1; bool motor2Speed2; bool motor2Speed3; var subscriberBool1 = new NetworkVariableBufferedSubscriber <bool>(motor1Speed1Path); subscriberBool1.Connect(); var variable1 = subscriberBool1.ReadData(); motor1Speed1 = variable1.GetValue(); subscriberBool1.Disconnect(); var subscriberBool2 = new NetworkVariableBufferedSubscriber <bool>(motor1Speed2Path); subscriberBool2.Connect(); var variable2 = subscriberBool2.ReadData(); motor1Speed2 = variable2.GetValue(); subscriberBool2.Disconnect(); var subscriberBool3 = new NetworkVariableBufferedSubscriber <bool>(motor1Speed3Path); subscriberBool3.Connect(); var variable3 = subscriberBool3.ReadData(); motor1Speed3 = variable3.GetValue(); subscriberBool3.Disconnect(); var subscriberBool4 = new NetworkVariableBufferedSubscriber <bool>(motor2Speed1Path); subscriberBool4.Connect(); var variable4 = subscriberBool4.ReadData(); motor2Speed1 = variable4.GetValue(); subscriberBool4.Disconnect(); var subscriberBool5 = new NetworkVariableBufferedSubscriber <bool>(motor2Speed2Path); subscriberBool5.Connect(); var variable5 = subscriberBool5.ReadData(); motor2Speed2 = variable5.GetValue(); subscriberBool5.Disconnect(); var subscriberBool6 = new NetworkVariableBufferedSubscriber <bool>(motor2Speed3Path); subscriberBool6.Connect(); var variable6 = subscriberBool6.ReadData(); motor2Speed3 = variable6.GetValue(); subscriberBool6.Disconnect(); string result1 = ""; string result2 = ""; if (motor1Speed1 == false && motor1Speed2 == false && motor1Speed3 == false) { result1 = "Motor 1 is Off"; } else if (motor1Speed1) { result1 = "Motor 1 is On and Speed is 1"; } else if (motor1Speed2) { result1 = "Motor 1 is On and Speed is 2"; } else if (motor1Speed3) { result1 = "Motor 1 is On and Speed is 3"; } if (motor2Speed1 == false && motor2Speed2 == false && motor2Speed3 == false) { result2 = "Motor 2 is Off"; } else if (motor2Speed1) { result2 = "Motor 2 is On and Speed is 1"; } else if (motor2Speed2) { result2 = "Motor 2 is On and Speed is 2"; } else if (motor2Speed3) { result2 = "Motor 2 is On and Speed is 3"; } string result = string.Format("{0}\r\n{1}", result1, result2); return(result); } catch (Exception ex) { Logger.LogMonitoringServiceLibrary(ex); return("Failed"); } }
public PLCBool(string location) { this.Location = location; SubscriberBool = new NetworkVariableBufferedSubscriber <bool>(this.Location); BufferedWriterBool = new NetworkVariableBufferedWriter <bool>(this.Location); }