You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.9 KiB

  1. using System.Collections.Concurrent;
  2. using HKHelper;
  3. using S7.Net;
  4. namespace HKControl
  5. {
  6. public class Main
  7. {
  8. private volatile static Main _Instance;
  9. public static Main GetInstance => _Instance ?? (_Instance = new Main());
  10. private Main() { }
  11. ConcurrentDictionary<int, Siemens> SiemensDicitonary = new ConcurrentDictionary<int, Siemens>();
  12. ConcurrentDictionary<int, DataModel> DataModels = new ConcurrentDictionary<int, DataModel>();
  13. public bool GetIsSwipe(int CarNum)
  14. {
  15. var left = DataModels.Values.FirstOrDefault(p => p.LeftWindowData.CarNum == CarNum.ToString());
  16. var right = DataModels.Values.FirstOrDefault(p => p.RightWindowData.CarNum == CarNum.ToString());
  17. return false;
  18. }
  19. public void Init()
  20. {
  21. DataInit();
  22. Json<CommunicationPar>.Data.CommunicationModels.ToList()?.ForEach(item =>
  23. {
  24. if (!DataModels.ContainsKey(item.DeviceNum)) { DataModels.TryAdd(item.DeviceNum, new DataModel()); };
  25. if (!SiemensDicitonary.ContainsKey(item.DeviceNum)) { SiemensDicitonary.TryAdd(item.DeviceNum, new Siemens()); }
  26. ThreadManage.GetInstance().Start(new Action(() =>
  27. {
  28. SiemensDicitonary[item.DeviceNum].Connect(CpuType.S71200, item.IpAddress);
  29. SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() =>
  30. {
  31. ThreadManage.GetInstance().StartLong(new Action(() =>
  32. {
  33. var vattable = DataModels[item.DeviceNum];
  34. DataModels[item.DeviceNum] = SiemensDicitonary[item.DeviceNum].ReadClass<DataModel>(0, 1);
  35. Thread.Sleep(100);
  36. }), $"{item.DeviceNum} 号设备监听");
  37. });
  38. }), $"{item.DeviceNum} 号设备连接初始化");
  39. });
  40. }
  41. private void DataInit()
  42. {
  43. Json<CommunicationPar>.Read();
  44. if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
  45. {
  46. Json<CommunicationPar>.Data.CommunicationModels.Clear();
  47. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  48. {
  49. IpAddress = "192.168.0.1",
  50. DeviceNum = 1
  51. });
  52. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  53. {
  54. IpAddress = "192.168.0.2",
  55. DeviceNum = 2
  56. });
  57. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  58. {
  59. IpAddress = "192.168.0.3",
  60. DeviceNum = 3
  61. });
  62. Json<CommunicationPar>.Save();
  63. }
  64. }
  65. }
  66. }