|
- 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;
-
- 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();
- private ICommSiemens comm = CommHelper.Siemens;
- public string id { get; set; } = "";
- public void Init()
- {
- Task.Factory.StartNew(() =>
- {
- CommHelper.CreateSiemens(CpuType.S7200Smart, Json<DevicePar>.Data.IpAddress).OnSuccess(s =>
- {
- comm = s.Content;
- WriteHeartBeat();
- Read();
- });
- });
-
- }
-
-
- private volatile bool _isReadSuccess;
- private void Read()
- {
- TaskManage.GetInstance.StartLong(() =>
- {
- comm.Read<bool[]>("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<float[]>("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<bool>("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content );
-
- comm.Read<bool[]>("DB1.DBX2004.0", 16).OnSuccess(s =>
- {
- AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").EStop = s.Content[0];
- AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").RotatingMachinesError = s.Content[1];
- AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").AbnormalNegativePressureFan = s.Content[2];
- AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").BrinePumpError = s.Content[3];
- AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").CompressorError = s.Content[4];
- });
-
- 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<OperateResult> 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()}");
- }
- }
-
- }
- }
|