终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

406 rader
20 KiB

  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.DosingSystemSingle.ViewModel;
  3. using BPASmartClient.Modbus;
  4. using BPASmartClient.Model;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Net.NetworkInformation;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using BPA.Helper;
  14. using System.Text.RegularExpressions;
  15. namespace BPASmartClient.DosingSystemSingle
  16. {
  17. public class DeviceInquire
  18. {
  19. private volatile static DeviceInquire _Instance;
  20. public static DeviceInquire GetInstance => _Instance ?? (_Instance = new DeviceInquire());
  21. private DeviceInquire() { }
  22. string IPSegment = "192.168.0.";
  23. ConcurrentDictionary<string, DeviceStatus> DeviceLists = new ConcurrentDictionary<string, DeviceStatus>();
  24. List<string> InvalidIP = new List<string>();//无效 IP 集合
  25. List<string> IPLists = new List<string>();//启动 Ping 任务IP集合
  26. ConcurrentQueue<string> IPQueues = new ConcurrentQueue<string>();//pincomplete 完成队列
  27. public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  28. public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  29. public ObservableCollection<Devices> devices { get; set; } = new ObservableCollection<Devices>();
  30. private void DeviceDataInit()
  31. {
  32. TaskManage.GetInstance.StartLong(new Action(() =>
  33. {
  34. for (int i = 0; i < DeviceLists.Count; i++)
  35. {
  36. string deviceName = DeviceLists.ElementAt(i).Value.DeviceName;
  37. int TopIndex = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  38. int BottomIndex = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  39. if (TopIndex >= 0 && TopIndex < TopDeviceCurrentStatuses.Count)
  40. {
  41. TopDeviceCurrentStatuses.ElementAt(TopIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
  42. TopDeviceCurrentStatuses.ElementAt(TopIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  43. }
  44. if (BottomIndex >= 0 && BottomIndex < BottomDeviceCurrentStatuses.Count)
  45. {
  46. BottomDeviceCurrentStatuses.ElementAt(BottomIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
  47. BottomDeviceCurrentStatuses.ElementAt(BottomIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  48. }
  49. int deviceIndex = Array.FindIndex(devices.ToArray(), p => p.IpAddress == DeviceLists.ElementAt(i).Key/* && p.DeviceName != DeviceLists.ElementAt(i).Value.DeviceName && p.DeviceNum != DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum*/);
  50. if (deviceIndex >= 0 && deviceIndex < devices.Count)
  51. {
  52. devices.ElementAt(deviceIndex).DeviceName = DeviceLists.ElementAt(i).Value.DeviceName;
  53. devices.ElementAt(deviceIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  54. }
  55. }
  56. Thread.Sleep(200);
  57. }), "设备状态监听");
  58. }
  59. private void TestData()
  60. {
  61. for (int i = 0; i < 8; i++)
  62. {
  63. DeviceLists.TryAdd($"192.168.1.{i + 1}", new DeviceStatus() { DeviceName = $"测试设备{i + 1}" });
  64. TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
  65. {
  66. DeviceName = $"测试设备{i + 1}",
  67. DeviceNum = i + 1,
  68. Weight = new Random().Next(100, 10000) / 100.0
  69. });
  70. Global.DeviceRawMaterials.Add(new RawMaterialModel()
  71. {
  72. DeviceIp = $"192.168.1.{i + 1}",
  73. RawMaterialName = $"测试设备{i + 1}",
  74. });
  75. devices.Add(new Devices()
  76. {
  77. DeviceName = $"测试设备{i + 1}",
  78. DeviceNum = i + 1,
  79. IpAddress = $"192.168.1.{i + 1}",
  80. });
  81. }
  82. for (int i = 8; i < 16; i++)
  83. {
  84. DeviceLists.TryAdd($"192.168.1.{i + 1}", new DeviceStatus() { DeviceName = $"测试设备{i + 1}" });
  85. BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
  86. {
  87. DeviceName = $"测试设备{i + 1}",
  88. DeviceNum = i + 1,
  89. Weight = new Random().Next(100, 10000) / 100.0
  90. });
  91. Global.DeviceRawMaterials.Add(new RawMaterialModel()
  92. {
  93. DeviceIp = $"192.168.1.{i + 1}",
  94. RawMaterialName = $"测试设备{i + 1}",
  95. });
  96. devices.Add(new Devices()
  97. {
  98. DeviceName = $"测试设备{i + 1}",
  99. DeviceNum = i + 1,
  100. IpAddress = $"192.168.1.{i + 1}",
  101. });
  102. }
  103. }
  104. public void Init()
  105. {
  106. if (!string.IsNullOrEmpty(Json<DevicePar>.Data.BaseParModel.NetworkSegAddress)) IPSegment = Json<DevicePar>.Data.BaseParModel.NetworkSegAddress;
  107. else Json<DevicePar>.Data.BaseParModel.NetworkSegAddress = IPSegment;
  108. //TestData();
  109. IpAddressLines();
  110. //SiemensDevice.GetInstance.Connect(Json<DevicePar>.Data.BaseParModel.DeviceAddress);
  111. DeviceDataInit();
  112. TaskManage.GetInstance.StartLong(new Action(() =>
  113. {
  114. if (IPQueues.Count >= IPLists.Count)
  115. //IpAddressLines();
  116. Thread.Sleep(5000);
  117. }), "配料机设备上线监听", true);
  118. }
  119. public void Rescan()
  120. {
  121. InvalidIP.Clear();
  122. }
  123. public DeviceStatus GetDevice(string ip)
  124. {
  125. if (ip != null)
  126. {
  127. if (DeviceLists.ContainsKey(ip)) return DeviceLists[ip];
  128. else return new DeviceStatus();
  129. //var res = DeviceLists.Values.FirstOrDefault(p => p.IpAddress == ip);
  130. //if (res != null) return res;
  131. }
  132. return new DeviceStatus();
  133. }
  134. public List<DeviceStatus> GetDevice()
  135. {
  136. List<DeviceStatus> deviceStatuses = new List<DeviceStatus>();
  137. foreach (var device in DeviceLists)
  138. {
  139. deviceStatuses.Add(device.Value);
  140. }
  141. return deviceStatuses;
  142. }
  143. private void IpAddressLines()
  144. {
  145. IPLists.Clear();
  146. IPQueues.Clear();
  147. /*for (int i = 1; i <= 255; i++)
  148. {
  149. if (!InvalidIP.Contains($"{IPSegment}{i}") && !DeviceLists.ContainsKey($"{IPSegment}{i}"))
  150. {
  151. string pattern = @"^(([1-9]\d?)|(1\d{2})|(2[01]\d)|(22[0-3]))(\.((1?\d\d?)|(2[04]/d)|(25[0-5]))){3}$";
  152. if (Regex.IsMatch($"{IPSegment}{i}", pattern))
  153. {
  154. IPLists.Add($"{IPSegment}{i}");
  155. }
  156. }
  157. }*/
  158. IPLists.Add($"{IPSegment}10");
  159. IPLists.Add($"{IPSegment}20");
  160. IPLists.Add($"{IPSegment}30");
  161. IPLists.ForEach((item) =>
  162. {
  163. Ping myPing = new Ping();
  164. myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
  165. myPing.SendAsync(item, 1000, null);
  166. });
  167. }
  168. private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
  169. {
  170. if (e.Reply.Status == IPStatus.Success)
  171. {
  172. string ip = e.Reply.Address.ToString();
  173. if (!DeviceLists.ContainsKey(ip))
  174. {
  175. DeviceStatus DS = new DeviceStatus();
  176. DS.modbusTcp.IsReconnect = false;
  177. DS.modbusTcp.ConnectOk = new Action(() =>
  178. {
  179. string DeviceName = DS.modbusTcp.GetString(DeviceAddress.DeviceName, 20).Trim()?.Replace(" ", "");//读取设备名称
  180. if (DeviceName.Length > 0)
  181. {
  182. DeviceLists.TryAdd(ip, DS);
  183. DeviceLists[ip].Init(DeviceName);
  184. DeviceLists[ip].modbusTcp.IsReconnect = false;
  185. App.Current.Dispatcher.Invoke(new Action(() =>
  186. {
  187. devices.Add(new Devices() { DeviceName = DeviceName, IpAddress = ip });
  188. if (TopDeviceCurrentStatuses.Count <= 7)
  189. TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
  190. else
  191. BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
  192. for (int i = 0; i < Json<LocaPar>.Data.Recipes.Count; i++)
  193. {
  194. for (int m = 0; m < Json<LocaPar>.Data.Recipes.ElementAt(i).RawMaterials.Count; m++)
  195. {
  196. if (Json<LocaPar>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp == ip)
  197. {
  198. Json<LocaPar>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).RawMaterialName = DeviceName;
  199. }
  200. }
  201. }
  202. if (Global.DeviceRawMaterials.Count > 0)
  203. {
  204. if (Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == DeviceName) == null)
  205. {
  206. Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
  207. }
  208. }
  209. else
  210. {
  211. Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
  212. }
  213. }));
  214. }
  215. else
  216. {
  217. if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
  218. }
  219. });
  220. DS.modbusTcp.ConnectFail = new Action(() =>
  221. {
  222. if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
  223. });
  224. DS.modbusTcp.Disconnect = new Action(() =>
  225. {
  226. if (InvalidIP.Contains(ip)) InvalidIP.Remove(ip);
  227. var res = devices.FirstOrDefault(P => P.IpAddress == ip);
  228. if (res != null && devices.Contains(res))
  229. {
  230. App.Current.Dispatcher.Invoke(new Action(() =>
  231. {
  232. devices.Remove(res);
  233. var item = Global.DeviceRawMaterials.FirstOrDefault(P => P.RawMaterialName == res.DeviceName);
  234. if (item != null) Global.DeviceRawMaterials.Remove(item);
  235. var topRes = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
  236. var bottomRes = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
  237. if (topRes != null) TopDeviceCurrentStatuses.Remove(topRes);
  238. if (bottomRes != null) BottomDeviceCurrentStatuses.Remove(bottomRes);
  239. }));
  240. }
  241. if (DeviceLists.ContainsKey(ip))
  242. {
  243. DeviceLists[ip].Dispose();
  244. var res11 = DeviceLists[ip];
  245. DeviceLists.TryRemove(ip, out res11);
  246. }
  247. });
  248. Task.Run(new Action(() =>
  249. {
  250. DS.modbusTcp.ModbusTcpConnect(ip, 502);//PLC连接
  251. IPQueues.Enqueue(e.Reply.Address.ToString());
  252. }));
  253. }
  254. else IPQueues.Enqueue(e.Reply.Address.ToString());
  255. }
  256. else IPQueues.Enqueue(e.Reply.Address.ToString());
  257. }
  258. }
  259. public class DeviceStatus
  260. {
  261. #region 对象属性声明
  262. public string DeviceName = String.Empty;
  263. public string IpAddress => modbusTcp.IPAdress;
  264. /// <summary>
  265. /// 设备状态
  266. /// </summary>
  267. public RawMaterialDeviceStatus deviceStatus { get; set; } = new RawMaterialDeviceStatus();
  268. public ModbusTcp modbusTcp = new ModbusTcp();
  269. public bool IsConnected => modbusTcp.Connected;
  270. #endregion
  271. public void Init(string DeviceName)
  272. {
  273. modbusTcp.Show = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
  274. modbusTcp.ShowEx = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
  275. this.DeviceName = DeviceName;
  276. AlarmHelper<AlarmInfo>.Init();
  277. if (modbusTcp.Connected)
  278. {
  279. TaskManage.GetInstance.StartLong(new Action(() =>
  280. {
  281. this.DeviceName = modbusTcp.GetString(DeviceAddress.DeviceName, 20)?.Trim()?.Replace(" ", "");
  282. deviceStatus.RunStatus = (ushort)this.modbusTcp.ReadShort(DeviceAddress.RunStatus); //获取设备运行状态
  283. deviceStatus.WeightFeedback = this.modbusTcp.ReadShort(DeviceAddress.WeightFeedback);//获取设备料仓剩余重量
  284. deviceStatus.NowWeightFeedback = this.modbusTcp.GetReal(DeviceAddress.CutWeightFeedback);//获取下料重量
  285. deviceStatus.DeviceNum = (ushort)this.modbusTcp.ReadShort(DeviceAddress.DeviceNum);//获取设备编号
  286. deviceStatus.DeviceAlarmCode = (ushort)this.modbusTcp.ReadShort(DeviceAddress.DeviceAlarmCode);//获取设备故障编码
  287. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop1 = deviceStatus.DeviceAlarmCode.GetBitValue(1);
  288. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Servo = deviceStatus.DeviceAlarmCode.GetBitValue(2);
  289. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Inverter = deviceStatus.DeviceAlarmCode.GetBitValue(3);
  290. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop2 = deviceStatus.DeviceAlarmCode.GetBitValue(7);
  291. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloUpperLimit = deviceStatus.DeviceAlarmCode.GetBitValue(8);
  292. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloLowerLimit = deviceStatus.DeviceAlarmCode.GetBitValue(9);
  293. if (DeviceName=="A20") {
  294. //MessageNotify.GetInstance.ShowRunLog($"{this.modbusTcp.GetReal(DeviceAddress.WeightSet)}");
  295. }
  296. Thread.Sleep(10);
  297. }), $"{DeviceName} 开始监听", true);
  298. }
  299. }
  300. public void SetDeviceName(string name)
  301. {
  302. this.modbusTcp.Write(DeviceAddress.DeviceName, new ushort[20]);
  303. this.modbusTcp.SetString(DeviceAddress.DeviceName, name);
  304. }
  305. public void StatusReset()
  306. {
  307. this.modbusTcp.Write(DeviceAddress.FinfishStatus, (ushort)1);
  308. //var res = modbusTcp.Read(DeviceAddress.RunStatus);
  309. }
  310. public void Dispose()
  311. {
  312. TaskManage.GetInstance.StopTask($"{DeviceName} 开始监听");
  313. }
  314. public void Start(float Value)
  315. {
  316. if (modbusTcp.Connected)
  317. {
  318. var res = Json<DevicePar>.Data.deviceParModels.FirstOrDefault(p => p.MaterialName == DeviceName);
  319. if (res != null)
  320. {
  321. prop1:
  322. modbusTcp.SetReal(DeviceAddress.SlowlyAddWeight, res.SlowlyAddWeight);
  323. var Value1 = (float)this.modbusTcp.GetReal(DeviceAddress.SlowlyAddWeight);
  324. if (Value1 != null && Value1.ToString() != res.SlowlyAddWeight.ToString()) goto prop1;
  325. prop2:
  326. modbusTcp.SetReal(DeviceAddress.PreCloseValveWeight, res.PreCloseValveWeight);
  327. var Value2 = (float)this.modbusTcp.GetReal(DeviceAddress.PreCloseValveWeight);
  328. if (Value2 != null && Value2.ToString() != res.PreCloseValveWeight.ToString()) goto prop2;
  329. prop3:
  330. modbusTcp.SetUint(DeviceAddress.RapidAcceleration, (uint)res.RapidAcceleration);
  331. var Value3 = this.modbusTcp.GetUint(DeviceAddress.RapidAcceleration);
  332. if (Value3 != null && Value3.ToString() != res.RapidAcceleration.ToString()) goto prop3;
  333. prop4:
  334. modbusTcp.SetUint(DeviceAddress.SlowAcceleration, (uint)res.SlowAcceleration);
  335. var Value4 = this.modbusTcp.GetUint(DeviceAddress.SlowAcceleration);
  336. if (Value4 != null && Value4.ToString() != res.SlowAcceleration.ToString()) goto prop4;
  337. prop5:
  338. modbusTcp.SetUint(DeviceAddress.ServoManualSpeed, (uint)res.ServoManualSpeed);
  339. var Value5 = this.modbusTcp.GetUint(DeviceAddress.ServoManualSpeed);
  340. if (Value5 != null && Value5.ToString() != res.ServoManualSpeed.ToString()) goto prop5;
  341. prop6:
  342. res.SiloUpperLimitWeight = (int)res.SiloUpperLimitWeight + 1;
  343. modbusTcp.SetUint(DeviceAddress.SiloUpperLimitWeight, (uint)res.SiloUpperLimitWeight);
  344. var Value6 = this.modbusTcp.GetUint(DeviceAddress.SiloUpperLimitWeight);
  345. Thread.Sleep(1000);
  346. /*if (Value6 != null && Value6.ToString() != res.SiloUpperLimitWeight.ToString())*/ goto prop6;
  347. prop7:
  348. modbusTcp.SetUint(DeviceAddress.LowerLimitWeightOfSilo, (uint)res.LowerLimitWeightOfSilo);
  349. var Value7 = this.modbusTcp.GetUint(DeviceAddress.LowerLimitWeightOfSilo);
  350. if (Value7 != null && Value7.ToString() != res.LowerLimitWeightOfSilo.ToString()) goto prop7;
  351. prop8:
  352. modbusTcp.SetUint(DeviceAddress.StirringSpeed, (uint)res.StirringSpeed);
  353. var Value8 = this.modbusTcp.GetUint(DeviceAddress.StirringSpeed);
  354. if (Value8 != null && Value8.ToString() != res.StirringSpeed.ToString()) goto prop8;
  355. MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},参数下发完成");
  356. }
  357. prop9:
  358. modbusTcp.SetReal(DeviceAddress.WeightSet, Value);//写入配方量
  359. var Value9 = this.modbusTcp.GetReal(DeviceAddress.WeightSet);
  360. if (Value9 != null && Value9.ToString() != Value.ToString()) goto prop9;
  361. /*
  362. prop10:
  363. modbusTcp.Write(DeviceAddress.Start, (ushort)1);//设备启动写入
  364. var Value10 = (short[])this.modbusTcp.Read(DeviceAddress.Start);
  365. if (Value10 != null && Value10[0] != 1) goto prop10;*/
  366. //modbusTcp.Write(DeviceAddress.Start, (ushort)1);//设备启动写入
  367. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value},味魔方启动");
  368. Thread.Sleep(100);
  369. }
  370. }
  371. }
  372. }