using System.Collections.Concurrent; using System.Diagnostics; using BPA.Helper; using BPASmartClient.CustomResource.Pages.Model; using S7.Net; namespace HKControl { public class Main { private volatile static Main _Instance; public static Main GetInstance => _Instance ?? (_Instance = new Main()); private Main() { } ConcurrentDictionary SiemensDicitonary = new ConcurrentDictionary(); ConcurrentDictionary DataModels = new ConcurrentDictionary(); ConcurrentQueue CarQueue = new ConcurrentQueue(); public Action[] CompleteNotify { get; set; } = new Action[6]; /// /// 判断指定出餐窗口是否允许刷卡(1-6) /// /// /// public bool GetIsSwipe(int CarNum) { switch (CarNum) { case 1: if (!DataModels.ContainsKey(1)) return false; return DataModels[1].LeftWindowData.IsSwipe; case 2: if (!DataModels.ContainsKey(1)) return false; return DataModels[1].RightWindowData.IsSwipe; case 3: if (!DataModels.ContainsKey(2)) return false; return DataModels[2].LeftWindowData.IsSwipe; case 4: if (!DataModels.ContainsKey(2)) return false; return DataModels[2].RightWindowData.IsSwipe; case 5: if (!DataModels.ContainsKey(3)) return false; return DataModels[3].LeftWindowData.IsSwipe; case 6: if (!DataModels.ContainsKey(3)) return false; return DataModels[3].RightWindowData.IsSwipe; default: break; } return false; } public bool GetIsConnect(int CarNum) { switch (CarNum) { case 1: if (!SiemensDicitonary.ContainsKey(1)) return false; return SiemensDicitonary[1].IsConnected; case 2: if (!SiemensDicitonary.ContainsKey(1)) return false; return SiemensDicitonary[1].IsConnected; case 3: if (!SiemensDicitonary.ContainsKey(2)) return false; return SiemensDicitonary[2].IsConnected; case 4: if (!SiemensDicitonary.ContainsKey(2)) return false; return SiemensDicitonary[2].IsConnected; case 5: if (!SiemensDicitonary.ContainsKey(3)) return false; return SiemensDicitonary[3].IsConnected; case 6: if (!SiemensDicitonary.ContainsKey(3)) return false; return SiemensDicitonary[3].IsConnected; default: break; } return false; } /// /// 获取指定档口是否配餐完成 /// /// /// public bool GetIsComplete(int CarNum) { if (!DataModels.ContainsKey(CarNum)) return false; switch (CarNum) { case 1: return DataModels[1].LeftWindowData.Complete; case 2: return DataModels[1].RightWindowData.Complete; case 3: return DataModels[2].LeftWindowData.Complete; case 4: return DataModels[2].RightWindowData.Complete; case 5: return DataModels[3].LeftWindowData.Complete; case 6: return DataModels[3].RightWindowData.Complete; default: break; } return false; } /// /// 设置指定窗口开始出餐(1--6) /// /// public void Start(int CarNum) { CarQueue.Enqueue(CarNum); } /// /// 初始化 /// public void Init() { DataInit(); Json.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].ConnectOk = new Action(() => { AlarmHelper.Init(); //AlarmHelper.GetInstance($"{item.DeviceNum}号档口"); HKLog.HKLogImport.WriteInfo($"{item.DeviceNum}:连接成功"); ThreadManage.GetInstance().StartLong(new Action(() => { try { var md = AlarmHelper.GetInstance($"{item.DeviceNum}"); var MB7 = SiemensDicitonary[item.DeviceNum].Read("MB7"); DataModels[item.DeviceNum].LeftWindowData.IsSwipe = Get8bitValue(MB7, 1); DataModels[item.DeviceNum].LeftWindowData.Complete = Get8bitValue(MB7, 2); DataModels[item.DeviceNum].RightWindowData.IsSwipe = Get8bitValue(MB7, 3); DataModels[item.DeviceNum].RightWindowData.Complete = Get8bitValue(MB7, 4); var MB10 = SiemensDicitonary[item.DeviceNum].Read("MB10"); var MB11 = SiemensDicitonary[item.DeviceNum].Read("MB11"); md.LeftWindowData.L2 = Get8bitValue(MB10, 2); md.LeftWindowData.L3 = Get8bitValue(MB10, 3); md.LeftWindowData.L4 = Get8bitValue(MB10, 4); md.LeftWindowData.L5 = Get8bitValue(MB10, 5); md.LeftWindowData.L6 = Get8bitValue(MB10, 6); md.LeftWindowData.L7 = Get8bitValue(MB10, 7); md.LeftWindowData.L8 = Get8bitValue(MB10, 8); md.LeftWindowData.L9 = Get8bitValue(MB11, 1); var MB12 = SiemensDicitonary[item.DeviceNum].Read("MB12"); var MB13 = SiemensDicitonary[item.DeviceNum].Read("MB13"); md.RightWindowData.R2 = Get8bitValue(MB12, 2); md.RightWindowData.R3 = Get8bitValue(MB12, 3); md.RightWindowData.R4 = Get8bitValue(MB12, 4); md.RightWindowData.R5 = Get8bitValue(MB12, 5); md.RightWindowData.R6 = Get8bitValue(MB12, 6); md.RightWindowData.R7 = Get8bitValue(MB12, 7); md.RightWindowData.R8 = Get8bitValue(MB12, 8); md.RightWindowData.R9 = Get8bitValue(MB13, 1); var MB14 = SiemensDicitonary[item.DeviceNum].Read("MB14"); md.WaterTemperatureLow = Get8bitValue(MB14, 5); md.FrequencyConverterFault = Get8bitValue(MB14, 6); var MB15 = SiemensDicitonary[item.DeviceNum].Read("MB15"); md.RobotCommFault = Get8bitValue(MB15, 6); bool TempFault = Get8bitValue(MB15, 5); if (TempFault) { md.RBStatus = RobotStatus.故障; } else { if (Get8bitValue(MB15, 1)) md.RBStatus = RobotStatus.运行; else if (Get8bitValue(MB15, 2)) md.RBStatus = RobotStatus.停止; else if (Get8bitValue(MB15, 3)) md.RBStatus = RobotStatus.暂停; else if (Get8bitValue(MB15, 4)) md.RBStatus = RobotStatus.准备; } } catch (Exception ex) { HKLog.HKLogImport.WriteInfo(ex.ToString()); } Thread.Sleep(100); //}), $"号设备监听", true); }), $"{item.DeviceNum} 号设备监听", true); //ThreadManage.GetInstance().StartLong(new Action(() => //{ // for (int i = 0; i < 6; i++) // { // if (RTrig.GetInstance($"窗口{i + 1}").Start(GetIsComplete(i + 1))) // { // CompleteNotify[i]?.Invoke(); // } // } // Thread.Sleep(100); //}), "完成通知"); }); SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress); }), $"{item.DeviceNum} 号设备连接初始化"); }); StartRun(); } private void StartRun() { ThreadManage.GetInstance().StartLong(new Action(() => { while (CarQueue.Count > 0) { if (CarQueue.TryDequeue(out int carNum)) { switch (carNum) { case 1: Control(1, "M6.1"); break; case 2: Control(1, "M6.0"); break; case 3: Control(2, "M6.1"); break; case 4: Control(2, "M6.0"); break; case 5: Control(3, "M6.1"); break; case 6: Control(3, "M6.0"); break; default: break; } } } Thread.Sleep(100); }), "开始运行设备"); } private void Control(int num, string add) { HKLog.HKLogImport.WriteInfo($"地址:{add} {num} 号出餐口控制"); SiemensDicitonary[num].Write(add, true); Thread.Sleep(500); SiemensDicitonary[num].Write(add, false); } private void DataInit() { Json.Read(); if (Json.Data.CommunicationModels.Count < 3) { Json.Data.CommunicationModels.Clear(); Json.Data.CommunicationModels.Add(new CommunicationModel() { IpAddress = "192.168.0.1", DeviceNum = 3 }); Json.Data.CommunicationModels.Add(new CommunicationModel() { IpAddress = "192.168.0.2", DeviceNum = 2 }); Json.Data.CommunicationModels.Add(new CommunicationModel() { IpAddress = "192.168.0.3", DeviceNum = 1 }); Json.Save(); } } /// /// 获取字节中指定位的值 /// /// 要获取的整数 /// 偏移量 范围(1-8) /// public bool Get8bitValue(byte data, byte offset) { if (offset > 8 || offset < 1) return false; return (data & 1 << offset - 1) == 0 ? false : true; } } }