using BPA.Communication.Interfaces; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Collections.Concurrent; using System.Diagnostics; using BPA.Helper; using System.Collections.Specialized; using System.Configuration; using BPASmartClient.CustomResource.UserControls.MessageShow; namespace BPASmartClient.Academy._50L { public class PlcControl : AddData { private volatile static PlcControl _Instance; public static PlcControl GetInstance => _Instance ?? (_Instance = new PlcControl()); private PlcControl() { } public bool IsConnect => comm != null && comm.IsConnected(); public DeviceStatus DeviceStatus { get; set; } = new DeviceStatus(); public ConcurrentDictionary AlarmInfo { get; private set; } = new ConcurrentDictionary(); private ICommSiemens comm = CommHelper.Siemens; public string id { get; set; } = ""; public void Init() { AlarmInfoGet(); Task.Factory.StartNew(() => { CommHelper.CreateSiemens(CpuType.S7200Smart, Json.Data.IpAddress).OnSuccess(s => { comm = s.Content; WriteHeartBeat(); Read(); }); }); } private volatile bool _isReadSuccess; public void AlarmInfoGet() { var x = (NameValueCollection)ConfigurationManager.GetSection("Reactor_50L_Alarm/Alarm"); if (x != null) { x.AllKeys.ToList().ForEach(o => { if (!AlarmInfo.ContainsKey(o)) { AlarmInfo.TryAdd(o, x.Get(o)); } }); } } public void AlarmRead() { if (this.IsConnect) { AlarmInfo.Keys.ToList().ForEach(o => { comm.Read(o).OnSuccess(p => { if (RTrig.GetInstance($"{o}").Start(p.Content)) { App.Current.Dispatcher.Invoke(() => { NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "报警", this.AlarmInfo[o]); MessageNotify.GetInstance.ShowAlarmLog(this.AlarmInfo[o]); }); } }); }); } Thread.Sleep(2000); } private void Read() { TaskManage.GetInstance.StartLong(() => { comm.Read("DB1.DBX1000.0", 32).OnSuccess(s => { #region V1000 DeviceStatus.IsSetReactTurnMotor = s.Content[0]; DeviceStatus.IsSetFans = s.Content[1]; DeviceStatus.IsSetBrinePumpState = s.Content[2]; DeviceStatus.IsSetReactHomeBit = s.Content[3]; DeviceStatus.IsSetReactOut = s.Content[4]; DeviceStatus.IsSetReactTurnMotorJogging = s.Content[5]; DeviceStatus.IsSetReactHeat = s.Content[6]; DeviceStatus.IsSetReactCool = s.Content[7]; #endregion #region V1001 DeviceStatus.IsSetCWTCool = s.Content[8]; DeviceStatus.IsSetReactPressureReliefValve = s.Content[9]; DeviceStatus.IsSetReactOutHotGasValve = s.Content[10]; DeviceStatus.IsSetHotGasEmptyValve = s.Content[11]; DeviceStatus.IsSetColdWaterTankInAirValve = s.Content[12]; DeviceStatus.IsSetReactDrainValve = s.Content[13]; DeviceStatus.IsSetReactInCleanWaterValve = s.Content[14]; DeviceStatus.IsSetReactPressureAirInValve = s.Content[15]; #endregion #region V1002 DeviceStatus.IsSetReactInBrineWaterValve = s.Content[16]; DeviceStatus.IsSetColdWaterTankVacuumValve = s.Content[17]; DeviceStatus.IsSetCompressorRunning = s.Content[18]; DeviceStatus.IsSetReactEnter = s.Content[19]; DeviceStatus.IsSetReactReverse = s.Content[20]; DeviceStatus.IsSetStartEnterBrine = s.Content[21]; DeviceStatus.IsSetStartEmptyWater = s.Content[22]; DeviceStatus.IsSetReactOutColdValve = s.Content[23]; #endregion #region V1003 DeviceStatus.IsSetColdWaterTankInWaterValve = s.Content[24]; DeviceStatus.IsSetStartClean = s.Content[25]; DeviceStatus.IsSetVacuumDistill = s.Content[26]; DeviceStatus.IsSetGoAddPosition = s.Content[27]; DeviceStatus.IsSetColdWaterTankZero = s.Content[28]; DeviceStatus.IsSetBrineTankZero = s.Content[29]; DeviceStatus.IsSetSystemStart = s.Content[30]; DeviceStatus.IsSetHeartBeat = s.Content[31]; #endregion }); comm.Read("DB1.DBX2000.0", 31).OnSuccess(s => { //Data[FeedbackData.旋转电机启停反馈] = s.Content[0]; //Data[FeedbackData.负压风机启动反馈] = s.Content[1]; //Data[FeedbackData.压缩空气进气阀反馈] = s.Content[2]; //Data[FeedbackData.排热空气阀反馈] = s.Content[3]; //Data[FeedbackData.泄压阀反馈] = s.Content[4]; //Data[FeedbackData.进冷却水阀反馈] = s.Content[5]; //Data[FeedbackData.出冷却水阀反馈] = s.Content[6]; //Data[FeedbackData.蒸汽梳水阀反馈] = s.Content[7]; //Data[FeedbackData.冷凝水罐进水阀反馈] = s.Content[8]; //Data[FeedbackData.称重水罐负压阀反馈] = s.Content[9]; //Data[FeedbackData.反应釜原点位置反馈] = s.Content[10]; #region V2000 DeviceStatus.ReactTurnMotor = s.Content[0]; DeviceStatus.Fans = s.Content[1]; DeviceStatus.BrinePumpState = s.Content[2]; DeviceStatus.ReactInSteamValue = s.Content[3]; DeviceStatus.ReactInColdValve = s.Content[4]; DeviceStatus.ReactOutColdValve = s.Content[5]; DeviceStatus.ReactDrainValve = s.Content[6]; DeviceStatus.ReactInCleanWaterValve = s.Content[7]; #endregion #region V2001 DeviceStatus.ReactPressureAirInValve = s.Content[8]; DeviceStatus.ReactInBrineWaterValve = s.Content[9]; DeviceStatus.ReactOutHotGasValve = s.Content[10]; DeviceStatus.HotGasEmptyValve = s.Content[11]; DeviceStatus.ColdWaterTankInAirValve = s.Content[12]; DeviceStatus.ColdWaterTankInWaterValve = s.Content[13]; DeviceStatus.ReactHomeBit = s.Content[14]; DeviceStatus.ReactPressureReliefValve = s.Content[15]; #endregion #region V2002 DeviceStatus.ColdWaterTankVacuumValve = s.Content[16]; DeviceStatus.CompressorRunning = s.Content[17]; #endregion }); comm.Read("DB1.DBD2006", 11).OnSuccess(s => { //Data[FeedbackData.反应釜温度] = s.Content[0]; //Data[FeedbackData.反应釜蒸汽压力] = s.Content[1]; //Data[FeedbackData.反应釜蒸汽流量] = s.Content[2]; //Data[FeedbackData.冷凝水罐温度] = s.Content[3]; //Data[FeedbackData.冷凝水罐湿度] = s.Content[4]; //Data[FeedbackData.负压流量] = s.Content[5]; //Data[FeedbackData.称重水罐重量] = s.Content[6]; //Data[FeedbackData.反应釜编码器值] = s.Content[7]; //Data[FeedbackData.蒸汽比例阀实际开度] = s.Content[8]; DeviceStatus.ReactTemp = s.Content[0]; DeviceStatus.ReactSteamPressure = s.Content[1]; DeviceStatus.ReactSteamFlow = s.Content[2]; DeviceStatus.ColdWaterTankTemp = s.Content[3]; DeviceStatus.ColdWaterTankHumidity = s.Content[4]; DeviceStatus.VacuumFlow = s.Content[5]; DeviceStatus.WaterTankWeight = s.Content[6]; DeviceStatus.ReactEncoderValue = s.Content[7]; DeviceStatus.SteamProportValveRealOpening = s.Content[8]; DeviceStatus.BrineTankWeight = s.Content[9]; DeviceStatus.ReactPressure = s.Content[10]; SamplingData sampling = new SamplingData() { Temperature = s.Content[0], SteamPressure = s.Content[1], SteamFlowRate = s.Content[2], CondensateWaterTemperature = s.Content[3], CondensateWaterHumidity = s.Content[4], NegativePressureFlowRate = s.Content[5], WeighingWaterTankWeight = s.Content[6], ReactEncoderValue = s.Content[7], ProportionalValveOpening = s.Content[8], BrineTankWeight = s.Content[9], ReactPressure = s.Content[10] }; if (Delay.GetInstance("趋势数据").Start(_isReadSuccess, 2)) { _isReadSuccess = false; } else if (!_isReadSuccess) { SqliteOperate.GetInstance.RealtimeChart(sampling); if (!string.IsNullOrWhiteSpace(id)) { sampling.ProductNumberId = id; SqliteOperate.GetInstance.Add(sampling); } _isReadSuccess = true; } }); comm.Read("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content ); //comm.Read("DB1.DBX2004.0", 16).OnSuccess(s => //{ // AlarmHelper.GetInstance.Get("50L反应釜").EStop = s.Content[0]; // AlarmHelper.GetInstance.Get("50L反应釜").RotatingMachinesError = s.Content[1]; // AlarmHelper.GetInstance.Get("50L反应釜").AbnormalNegativePressureFan = s.Content[2]; // AlarmHelper.GetInstance.Get("50L反应釜").BrinePumpError = s.Content[3]; // AlarmHelper.GetInstance.Get("50L反应釜").CompressorError = s.Content[4]; // AlarmHelper.GetInstance.Get("50L反应釜").ReactorPressureLimit = s.Content[5]; //}); AlarmRead(); Thread.Sleep(100); }, "数据获取", true); } private void WriteHeartBeat() { Task.Factory.StartNew( async() => { while (true) { if (this.IsConnect) { //var tokenSource = new CancellationTokenSource(); //tokenSource.CancelAfter(2000); //await WriteAsync(BoolAddEnum.心跳, true,tokenSource.Token); Write(BoolAddEnum.心跳, true); } //IsConnectChanged?.Invoke(this.IsConnect); await Task.Delay(2500); } },TaskCreationOptions.LongRunning); } public OperateResult Write(BoolAddEnum add, bool value) { if (boolAdds.ContainsKey(add)) { return comm.Write(boolAdds[add], value); } else { return OperateResult.Failed($"未查找到该地址{add.ToString()}"); } } public OperateResult Write(FloatAddEnum add, float value) { if (floatAdds.ContainsKey(add)) { return comm.Write(floatAdds[add], value); } else { return OperateResult.Failed($"未查找到该地址{add.ToString()}"); } } public async Task WriteAsync(BoolAddEnum add, bool value,CancellationToken cancellationToken=default) { if (boolAdds.ContainsKey(add)) { try { var result = await Task.Run(() => { return comm.Write(boolAdds[add], value); }, cancellationToken); return result; } catch (OperationCanceledException) { return OperateResult.Failed($"操作被取消"); } } else { return OperateResult.Failed($"未查找到该地址{add.ToString()}"); } } } }