|
- using System.Collections.Concurrent;
- using HKHelper;
- using S7.Net;
-
- namespace HKControl
- {
- public class Main
- {
-
- private volatile static Main _Instance;
- public static Main GetInstance => _Instance ?? (_Instance = new Main());
- private Main() { }
-
- ConcurrentDictionary<int, Siemens> SiemensDicitonary = new ConcurrentDictionary<int, Siemens>();
- ConcurrentDictionary<int, DataModel> DataModels = new ConcurrentDictionary<int, DataModel>();
-
-
- public bool GetIsSwipe(int CarNum)
- {
- var left = DataModels.Values.FirstOrDefault(p => p.LeftWindowData.CarNum == CarNum.ToString());
- var right = DataModels.Values.FirstOrDefault(p => p.RightWindowData.CarNum == CarNum.ToString());
- return false;
- }
-
- public void Init()
- {
- DataInit();
- Json<CommunicationPar>.Data.CommunicationModels.ToList()?.ForEach(item =>
- {
- if (!DataModels.ContainsKey(item.DeviceNum)) { DataModels.TryAdd(item.DeviceNum, new DataModel()); };
- if (!SiemensDicitonary.ContainsKey(item.DeviceNum)) { SiemensDicitonary.TryAdd(item.DeviceNum, new Siemens()); }
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- SiemensDicitonary[item.DeviceNum].Connect(CpuType.S71200, item.IpAddress);
- SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() =>
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- var vattable = DataModels[item.DeviceNum];
- DataModels[item.DeviceNum] = SiemensDicitonary[item.DeviceNum].ReadClass<DataModel>(0, 1);
- Thread.Sleep(100);
- }), $"{item.DeviceNum} 号设备监听");
- });
- }), $"{item.DeviceNum} 号设备连接初始化");
-
- });
- }
-
- private void DataInit()
- {
- Json<CommunicationPar>.Read();
- if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
- {
- Json<CommunicationPar>.Data.CommunicationModels.Clear();
-
- Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
- {
- IpAddress = "192.168.0.1",
- DeviceNum = 1
- });
-
- Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
- {
- IpAddress = "192.168.0.2",
- DeviceNum = 2
- });
-
- Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
- {
- IpAddress = "192.168.0.3",
- DeviceNum = 3
- });
-
- Json<CommunicationPar>.Save();
- }
- }
- }
- }
|