|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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>();
- ConcurrentQueue<int> CarQueue = new ConcurrentQueue<int>();
-
- /// <summary>
- /// 判断指定出餐窗口是否允许刷卡(1-6)
- /// </summary>
- /// <param name="CarNum"></param>
- /// <returns></returns>
- public bool GetIsSwipe(int CarNum)
- {
- switch (CarNum)
- {
- case 1:
- return DataModels[1].LeftWindowData.IsSwipe;
- case 2:
- return DataModels[1].RightWindowData.IsSwipe;
- case 3:
- return DataModels[2].LeftWindowData.IsSwipe;
- case 4:
- return DataModels[2].RightWindowData.IsSwipe;
- case 5:
- return DataModels[3].LeftWindowData.IsSwipe;
- case 6:
- return DataModels[3].LeftWindowData.IsSwipe;
- default:
- break;
- }
- return false;
- }
-
- /// <summary>
- /// 设置指定窗口开始出餐(1--6)
- /// </summary>
- /// <param name="CarNum"></param>
- public void Start(int CarNum)
- {
- CarQueue.Enqueue(CarNum);
- }
-
- /// <summary>
- /// 初始化
- /// </summary>
- 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.S7200Smart, item.IpAddress);
- SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() =>
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- var res = SiemensDicitonary[item.DeviceNum].Read<byte>("MB7");
-
- DataModels[item.DeviceNum].LeftWindowData.IsSwipe = Get8bitValue(res, 1);
- DataModels[item.DeviceNum].LeftWindowData.Complete = Get8bitValue(res, 2);
-
- DataModels[item.DeviceNum].RightWindowData.IsSwipe = Get8bitValue(res, 3);
- DataModels[item.DeviceNum].RightWindowData.Complete = Get8bitValue(res, 4);
-
-
- //DataModels[item.DeviceNum].LeftWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.0");
- //DataModels[item.DeviceNum].LeftWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.1");
-
- //DataModels[item.DeviceNum].RightWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.2");
- //DataModels[item.DeviceNum].RightWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.3");
-
- Thread.Sleep(100);
- }), $"{item.DeviceNum} 号设备监听");
- });
- }), $"{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:
- SiemensDicitonary[1].Write("M6.0", true);
- break;
- case 2:
- SiemensDicitonary[1].Write("M6.1", true);
- break;
- case 3:
- SiemensDicitonary[2].Write("M6.0", true);
- break;
- case 4:
- SiemensDicitonary[2].Write("M6.1", true);
- break;
- case 5:
- SiemensDicitonary[3].Write("M6.0", true);
- break;
- case 6:
- SiemensDicitonary[3].Write("M6.1", true);
- break;
-
- default:
- break;
- }
- }
- }
- Thread.Sleep(100);
- }), "开始运行设备");
- }
-
- 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();
- }
- }
-
- /// <summary>
- /// 获取字节中指定位的值
- /// </summary>
- /// <param name="data">要获取的整数</param>
- /// <param name="offset">偏移量 范围(1-8)</param>
- /// <returns></returns>
- public bool Get8bitValue(byte data, byte offset)
- {
- if (offset > 8 || offset < 1) return false;
- return (data & 1 << offset - 1) == 0 ? false : true;
- }
- }
- }
|