|
- using BPA.Helper;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.CustomResource.UserControls;
- using LiveCharts;
- using Microsoft.Windows.Input;
- using Newtonsoft.Json.Linq;
- using SqlSugar.Extensions;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static Org.BouncyCastle.Asn1.Cmp.Challenge;
- using Org.BouncyCastle.Asn1.Crmf;
-
- namespace BPASmartClient.Academy.Model
- {
- public class Simens_PLC
- {
- private static Simens_PLC _instance;
- public static Simens_PLC GetInstance { get; set; } = _instance ??= new Simens_PLC();
- private Simens_PLC() { }
-
- public DataFeedback dataFeedback { get; set; } = new DataFeedback();
- public PipeLineMode PipeLine { get; set; } = new PipeLineMode();
- public string id { get; set; } = "";
- //public SiemensHelper siemens { get; set; } = new SiemensHelper();
- public ICommSiemens siemens = CommHelper.Siemens;
- public DeviceCtrl deviceCtrl { get; set; } = new DeviceCtrl();
- public Action<bool> connect { get; set; }
- public ConcurrentDictionary<string, string> AlarmInfo { get; set; } = new ConcurrentDictionary<string, string>();
- public bool IsConnected { get; set; } = false;
- bool heart = false;
- public void Connect()
- {
- Task.Factory.StartNew(() =>
- {
- CommHelper.CreateSiemens(CpuType.S7200Smart, Json<DevicePar>.Data.IpAddress).OnSuccess(o =>
- {
- siemens = o.Content;
- IsConnected = true;
- connect?.Invoke(true);
- ThreadManage.GetInstance.StartLong(() =>
- {
- ReadStatus();
- }, "状态读取");
- ThreadManage.GetInstance.StartLong(() =>
- {
- Heartbeat();
- connect?.Invoke(IsConnected);
- }, "心跳监听");
- ThreadManage.GetInstance.StartLong(() =>
- {
- ChartDataRead();
- }, "曲线数据");
- ThreadManage.GetInstance.StartLong(() =>
- {
- AlarmRead();
- }, "报警监控");
- }).OnFailure(o =>
- {
- IsConnected = false;
- });
- });
- }
- public void Init()
- {
- AlarmInfoGet();
- }
- public void AlarmInfoGet()
- {
- var x = (NameValueCollection)ConfigurationManager.GetSection("FlexBatchSystem/Alarm");
- if (x != null)
- {
- x.AllKeys.ToList().ForEach(o =>
- {
- if (!AlarmInfo.ContainsKey(o))
- {
- AlarmInfo.TryAdd(o, x.Get(o));
-
- }
- });
- }
- }
- public void AlarmRead()
- {
- if (IsConnected)
- {
- AlarmInfo.Keys.ToList().ForEach(o =>
- {
- siemens.Read<bool>(o).OnSuccess(p =>
- {
- //if (o == "DB1.DBX2004.4" && p.Content)
- //{
- // IsConnected = false;
- //}
- if (RTrig.GetInstance($"{o}").Start(p.Content))
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", Simens_PLC.GetInstance.AlarmInfo[o], 2, 3);
- MessageNotify.GetInstance.ShowAlarmLog(Simens_PLC.GetInstance.AlarmInfo[o]);
- });
-
- }
- });
- });
-
- }
- Thread.Sleep(2000);
- }
- public void ReadStatus()
- {
- if (IsConnected)
- {
- siemens.Read<bool>("DB1.DBX1003.7").OnSuccess(o =>
- {
- heart = o.Content;
- });
- /* siemens.Read<bool[]>("DB1.DBX2000.0", 20).OnSuccess(o =>
- {
- deviceCtrl.M101 = o.Content[0];
- deviceCtrl.M102 = o.Content[1];
- deviceCtrl.M103 = o.Content[2];
- deviceCtrl.VX100 = o.Content[3];
- deviceCtrl.VX101 = o.Content[4];
- deviceCtrl.VX102 = o.Content[5];
- deviceCtrl.VX103 = o.Content[6];
- deviceCtrl.VX104 = o.Content[7];
- deviceCtrl.VX105 = o.Content[8];
- deviceCtrl.VX106 = o.Content[9];
- deviceCtrl.VX107 = o.Content[10];
- deviceCtrl.VX108 = o.Content[11];
- deviceCtrl.VX109 = o.Content[12];
- deviceCtrl.VX110 = o.Content[13];
- deviceCtrl.VX111 = o.Content[14];
- deviceCtrl.VX112 = o.Content[15];
- deviceCtrl.VX113 = o.Content[16];
- deviceCtrl.VX114 = o.Content[17];
- deviceCtrl.VX115 = o.Content[18];
- });*/
- siemens.Read<bool>("DB1.DBX2000.0").OnSuccess(o =>
- {
- deviceCtrl.M101 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2000.1").OnSuccess(o =>
- {
- deviceCtrl.M102 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2000.2").OnSuccess(o =>
- {
- deviceCtrl.M103 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2000.3").OnSuccess(o =>
- {
- deviceCtrl.VX100 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2000.4").OnSuccess(o =>
- {
- deviceCtrl.VX101 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2000.5").OnSuccess(o =>
- {
- deviceCtrl.VX102 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2000.6").OnSuccess(o =>
- {
- deviceCtrl.VX103 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2000.7").OnSuccess(o =>
- {
- deviceCtrl.VX104 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.0").OnSuccess(o =>
- {
- deviceCtrl.VX105 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.1").OnSuccess(o =>
- {
- deviceCtrl.VX106 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.2").OnSuccess(o =>
- {
- deviceCtrl.VX107 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.3").OnSuccess(o =>
- {
- deviceCtrl.VX108 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.4").OnSuccess(o =>
- {
- deviceCtrl.VX109 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.5").OnSuccess(o =>
- {
- deviceCtrl.VX110 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.6").OnSuccess(o =>
- {
- deviceCtrl.VX111 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2001.7").OnSuccess(o =>
- {
- deviceCtrl.VX112 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2002.0").OnSuccess(o =>
- {
- deviceCtrl.VX113 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2002.1").OnSuccess(o =>
- {
- deviceCtrl.VX114 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2002.2").OnSuccess(o =>
- {
- deviceCtrl.VX115 = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2002.3").OnSuccess(o =>
- {
- deviceCtrl.Compressor = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2002.4").OnSuccess(o =>
- {
- deviceCtrl.ByPassMaterial = o.Content;
- });
- siemens.Read<bool>("DB1.DBX2002.5").OnSuccess(o =>
- {
- deviceCtrl.ByPassReact = o.Content;
- });
- siemens.Read<int>("DB1.DBD2022").OnSuccess(o =>
- {
- deviceCtrl.TempWok = o.Content;
- });
- siemens.Read<int>("DB1.DBD2026").OnSuccess(o =>
- {
- deviceCtrl.TempMaterial = o.Content;
- });
- siemens.Read<float>("DB1.DBD2018").OnSuccess(o =>
- {
- deviceCtrl.WeightWok = o.Content;
- });
- siemens.Read<int>("DB1.DBD2030").OnSuccess(o =>
- {
- deviceCtrl.TempVent = o.Content;
- });
- siemens.Read<float>("DB1.DBD2034").OnSuccess((o) =>
- {
- deviceCtrl.PressureWok = o.Content;
- });
- siemens.Read<float>("DB1.DBD2038").OnSuccess((o) =>
- {
- deviceCtrl.PressureWeek = o.Content;
- });
- siemens.Read<float>("DB1.DBD2014").OnSuccess((o) =>
- {
- deviceCtrl.OpenValue = o.Content;
- });
- PipeDetect();
-
- siemens.Read<bool[]>("DB1.DBX1000.0", 32).OnSuccess((s) =>
- {
- #region V1000
- deviceCtrl.配料罐搅拌电机 = s.Content[0];
- deviceCtrl.反应釜搅拌电机 = s.Content[1];
- deviceCtrl.真空泵 = s.Content[2];
- deviceCtrl.进料阀 = s.Content[3];
- deviceCtrl.蒸馏阀 = s.Content[4];
- deviceCtrl.回油阀 = s.Content[5];
- deviceCtrl.回水阀 = s.Content[6];
- deviceCtrl.汇馏阀 = s.Content[7];
- #endregion
-
- #region V1001
- deviceCtrl.排馏阀 = s.Content[8];
- deviceCtrl.冷凝器冷却阀 = s.Content[9];
- deviceCtrl.配料釜蒸汽阀 = s.Content[10];
- deviceCtrl.反应釜蒸汽阀 = s.Content[11];
- deviceCtrl.反应釜蒸汽疏水阀 = s.Content[12];
- deviceCtrl.反应釜冷却水阀 = s.Content[13];
- deviceCtrl.配料罐自动升温 = s.Content[14];
- deviceCtrl.反应釜自动升温 = s.Content[15];
- #endregion
-
- #region V1002
- deviceCtrl.系统启动 = s.Content[16];
- deviceCtrl.手自动 = s.Content[17];
- deviceCtrl.反应釜冷却水排水阀 = s.Content[18];
- deviceCtrl.分馏真空阀 = s.Content[19];
- deviceCtrl.蒸馏真空阀 = s.Content[20];
- deviceCtrl.空压机启动 = s.Content[21];
- //deviceCtrl.配料罐搅拌电机 = s.Content[22];
- deviceCtrl.水罐重量清零 = s.Content[23];
- #endregion
-
- #region V1003
- deviceCtrl.反应釜自动降温 = s.Content[24];
- deviceCtrl.反应釜出料阀 = s.Content[25];
- deviceCtrl.反应釜排空阀 = s.Content[26];
- deviceCtrl.配料釜蒸汽输水旁通阀 = s.Content[27];
- deviceCtrl.反应釜蒸汽输水旁通阀 = s.Content[28];
- //deviceCtrl.配料罐搅拌电机 = s.Content[29];
- //deviceCtrl.配料罐搅拌电机 = s.Content[30];
- deviceCtrl.心跳 = s.Content[31];
- #endregion
- });
- siemens.Read<bool[]>("DB1.DBX3000.0", 5).OnSuccess((s) =>
- {
- deviceCtrl.闪蒸 = s.Content[0];
- deviceCtrl.真空蒸馏 = s.Content[1];
- deviceCtrl.真空下料 = s.Content[2];
- deviceCtrl.二次闪蒸 = s.Content[3];
- deviceCtrl.排空阀 = s.Content[4];
- });
- Thread.Sleep(100);
- }
- }
- public void PipeDetect()
- {
- //阀门3-4连接
- if (deviceCtrl.VX102 || deviceCtrl.VX103)
- {
- PipeLine.PipeH_1.Fill = true;
- PipeLine.PipeH_1.Direction = 2;
- }
- else
- {
- PipeLine.PipeH_1.Fill = false;
- PipeLine.PipeH_1.Direction = 0;
- }
- //阀门10-11连接
- if (!deviceCtrl.VX110 && !deviceCtrl.VX109)
- {
- PipeLine.PipeH_2.Fill = false;
- PipeLine.PipeH_2.Direction = 0;
- }
- else
- {
- if (deviceCtrl.VX109)
- {
- PipeLine.PipeH_2.Fill = true;
- PipeLine.PipeH_2.Direction = 1;
- }
- if (deviceCtrl.VX110)
- {
- PipeLine.PipeH_2.Fill = true;
- PipeLine.PipeH_2.Direction = 2;
- }
- }
- //阀门9-12连接
- if (!deviceCtrl.VX108 && !deviceCtrl.VX111)
- {
- PipeLine.PipeH_3.Fill = false;
- PipeLine.PipeH_3.Direction = 0;
- }
- else
- {
- if (deviceCtrl.VX108)
- {
- PipeLine.PipeH_3.Fill = true;
- PipeLine.PipeH_3.Direction = 1;
- }
- if (deviceCtrl.VX111)
- {
- PipeLine.PipeH_3.Fill = true;
- PipeLine.PipeH_3.Direction = 2;
- }
- }
-
- //阀门13-14连接
- if (deviceCtrl.VX112 || deviceCtrl.VX113)
- {
- PipeLine.PipeH_4.Fill = true;
- PipeLine.PipeH_4.Direction = 1;
- }
- else
- {
- PipeLine.PipeH_4.Fill = false;
- PipeLine.PipeH_4.Direction = 0;
- }
- }
-
-
-
- public void ChartDataRead()
- {
- //DateTime date = DateTime.Now;
- //Random random = new Random();
- //SaveData saveData = new SaveData()
- //{
- // Id = id,
- // Name = dataFeedback.Name,
- // Date = DateTime.Now,
- // M101_Speed = random.Next(0, 2000),
- // M102_Speed = random.Next(0, 2000),
- // M103_Speed = random.Next(0, 2000),
- // OpenValve = random.Next(0, 5),
- // TempWok = random.Next(-10, 150),
- // TempMaterial = random.Next(-10, 150),
- // TempVent = random.Next(-10, 150),
- // PressureWok = random.NextDouble(),
- // WeightWok = random.Next(0, 50),
- //};
- //Sqlite.GetInstance.AddData(saveData);
- //DataView(dataFeedback.M101_Speed, new DataValue() { DateTime = date, Value = saveData.M101_Speed });
- //DataView(dataFeedback.M102_Speed, new DataValue() { DateTime = date, Value = saveData.M102_Speed });
- //DataView(dataFeedback.M103_Speed, new DataValue() { DateTime = date, Value = saveData.M103_Speed });
- //DataView(dataFeedback.TempWok, new DataValue() { DateTime = date, Value = saveData.TempWok });
- //DataView(dataFeedback.TempMaterial, new DataValue() { DateTime = date, Value = saveData.TempMaterial });
- //DataView(dataFeedback.TempVent, new DataValue() { DateTime = date, Value = saveData.TempVent });
- //DataView(dataFeedback.OpenValve, new DataValue() { DateTime = date, Value = saveData.OpenValve });
- //DataView(dataFeedback.PressureWok, new DataValue() { DateTime = date, Value = saveData.PressureWok });
- //DataView(dataFeedback.WeightWok, new DataValue() { DateTime = date, Value = saveData.WeightWok });
- if (siemens != null && id != "")
- {
-
- SaveData saveData = new SaveData()
- {
- ProductNumberId = Simens_PLC.GetInstance.id,
- Name = dataFeedback.Name,
- //ProductNumberId = dataFeedback.ProductNumberId,
- Date = DateTime.Now,
- M101_Speed = siemens.Read<int>("DB1.DBD2006").Content,
- M102_Speed = siemens.Read<int>("DB1.DBD2010").Content,
- TempWok = siemens.Read<int>("DB1.DBD2022").Content,
- TempMaterial = siemens.Read<int>("DB1.DBD2026").Content,
- TempVent = siemens.Read<int>("DB1.DBD2030").Content,
- WeightWok = siemens.Read<float>("DB1.DBD2018").Content,
- PressureWok = siemens.Read<float>("DB1.DBD2034").Content,
- PressureWok_Week = siemens.Read<float>("DB1.DBD2038").Content,
- OpenValve = siemens.Read<float>("DB1.DBD2014").Content,
- };
- Sqlite.GetInstance.AddData(saveData);
- DataView(dataFeedback.M101_Speed, new DataValue() { DateTime = saveData.Date, Value = saveData.M101_Speed });
- DataView(dataFeedback.M102_Speed, new DataValue() { DateTime = saveData.Date, Value = saveData.M102_Speed });
- DataView(dataFeedback.TempWok, new DataValue() { DateTime = saveData.Date, Value = saveData.TempWok });
- DataView(dataFeedback.TempMaterial, new DataValue() { DateTime = saveData.Date, Value = saveData.TempMaterial });
- DataView(dataFeedback.TempVent, new DataValue() { DateTime = saveData.Date, Value = saveData.TempVent });
- DataView(dataFeedback.WeightWok, new DataValue() { DateTime = saveData.Date, Value = saveData.WeightWok });
- DataView(dataFeedback.OpenValve, new DataValue() { DateTime = saveData.Date, Value = saveData.OpenValve });
- DataView(dataFeedback.PressureWok, new DataValue() { DateTime = saveData.Date, Value = saveData.PressureWok });
- DataView(dataFeedback.PressureWok_Week, new DataValue() { DateTime = saveData.Date, Value = saveData.PressureWok_Week });
- }
-
- Thread.Sleep(1000);
- }
- public void DataView<T>(ChartValues<T> values, T value)
- {
- if (values.Count >= 20)
- {
- values.RemoveAt(0);
- values.Add(value);
- }
- else
- {
- values.Add(value);
- }
- }
- public void Heartbeat()
- {
- if (!heart && siemens != null)
- {
- siemens.Write<bool>("DB1.DBX1003.7", true).OnFailure(o =>
- {
- IsConnected = false;
- GetInstance.Connect();
- }).OnSuccess(o =>
- {
- IsConnected = true;
- });
- }
-
- Thread.Sleep(3000);
- }
-
- }
- }
|