终端一体化运控平台
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.
 
 
 

363 rader
17 KiB

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