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

714 lines
38 KiB

  1. //#define Debug
  2. using BPA.Communication;
  3. using BPA.Helper;
  4. using BPASmartClient.CustomResource.Pages.Model;
  5. using BPASmartClient.DosingSystem.Model;
  6. using BPASmartClient.DosingSystem.ViewModel;
  7. //using BPASmartClient.Modbus;
  8. using BPASmartClient.Model;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Diagnostics;
  14. using System.Linq;
  15. using System.Net.NetworkInformation;
  16. using System.Reflection;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace BPASmartClient.DosingSystem
  20. {
  21. public class DeviceInquire
  22. {
  23. int stockCount;//料仓数
  24. private volatile static DeviceInquire _Instance;
  25. public static DeviceInquire GetInstance => _Instance ?? (_Instance = new DeviceInquire());
  26. private DeviceInquire() { }
  27. string IPSegment = "192.168.0.";
  28. ConcurrentDictionary<string, DeviceStatus> DeviceLists = new ConcurrentDictionary<string, DeviceStatus>();
  29. List<string> InvalidIP = new List<string>();//无效 IP 集合
  30. List<string> IPLists = new List<string>();//启动 Ping 任务IP集合
  31. ConcurrentQueue<string> IPQueues = new ConcurrentQueue<string>();//pincomplete 完成队列
  32. public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  33. public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  34. public ObservableCollection<Devices> devices { get; set; } = new ObservableCollection<Devices>();
  35. private void DeviceDataInit()
  36. {
  37. TaskManage.GetInstance.StartLong(new Action(() =>
  38. {
  39. for (int i = 0; i < DeviceLists.Count; i++)
  40. {
  41. string deviceName = DeviceLists.ElementAt(i).Value.DeviceName;
  42. int TopIndex = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  43. int BottomIndex = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  44. if (TopIndex >= 0 && TopIndex < TopDeviceCurrentStatuses.Count)
  45. {
  46. TopDeviceCurrentStatuses.ElementAt(TopIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
  47. TopDeviceCurrentStatuses.ElementAt(TopIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  48. }
  49. if (BottomIndex >= 0 && BottomIndex < BottomDeviceCurrentStatuses.Count)
  50. {
  51. BottomDeviceCurrentStatuses.ElementAt(BottomIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
  52. BottomDeviceCurrentStatuses.ElementAt(BottomIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  53. }
  54. 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*/);
  55. if (deviceIndex >= 0 && deviceIndex < devices.Count)
  56. {
  57. devices.ElementAt(deviceIndex).DeviceName = DeviceLists.ElementAt(i).Value.DeviceName;
  58. devices.ElementAt(deviceIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  59. }
  60. }
  61. Thread.Sleep(200);
  62. }), "设备状态监听", true);
  63. }
  64. private void aa(int num, string name)
  65. {
  66. DeviceLists.TryAdd($"192.168.1.{num}", new DeviceStatus() { DeviceName = $"{name}" });
  67. TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
  68. {
  69. DeviceName = $"{name}",
  70. DeviceNum = num,
  71. Weight = new Random().Next(100, 10000) / 100.0
  72. });
  73. Global.DeviceRawMaterials.Add(new RawMaterialModel()
  74. {
  75. DeviceIp = $"192.168.1.{num}",
  76. RawMaterialName = $"{name}",
  77. });
  78. devices.Add(new Devices()
  79. {
  80. DeviceName = $"{name}",
  81. DeviceNum = num,
  82. IpAddress = $"192.168.1.{num}",
  83. });
  84. }
  85. private void TestData()
  86. {
  87. aa(1, "盐");
  88. aa(2, "郫县豆瓣");
  89. aa(3, "芥菜");
  90. aa(4, "调味剂");
  91. aa(5, "香料");
  92. aa(6, "鸡精");
  93. aa(7, "味精");
  94. for (int i = 0; i < 8; i++)
  95. {
  96. DeviceLists.TryAdd($"192.168.1.{i + 1}", new DeviceStatus() { DeviceName = $"测试设备{i + 1}" });
  97. TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
  98. {
  99. DeviceName = $"测试设备{i + 1}",
  100. DeviceNum = i + 1,
  101. Weight = new Random().Next(100, 10000) / 100.0
  102. });
  103. Global.DeviceRawMaterials.Add(new RawMaterialModel()
  104. {
  105. DeviceIp = $"192.168.1.{i + 1}",
  106. RawMaterialName = $"测试设备{i + 1}",
  107. });
  108. devices.Add(new Devices()
  109. {
  110. DeviceName = $"测试设备{i + 1}",
  111. DeviceNum = i + 1,
  112. IpAddress = $"192.168.1.{i + 1}",
  113. });
  114. }
  115. for (int i = 8; i < 16; i++)
  116. {
  117. DeviceLists.TryAdd($"192.168.1.{i + 1}", new DeviceStatus() { DeviceName = $"测试设备{i + 1}" });
  118. BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus()
  119. {
  120. DeviceName = $"测试设备{i + 1}",
  121. DeviceNum = i + 1,
  122. Weight = new Random().Next(100, 10000) / 100.0
  123. });
  124. Global.DeviceRawMaterials.Add(new RawMaterialModel()
  125. {
  126. DeviceIp = $"192.168.1.{i + 1}",
  127. RawMaterialName = $"测试设备{i + 1}",
  128. });
  129. devices.Add(new Devices()
  130. {
  131. DeviceName = $"测试设备{i + 1}",
  132. DeviceNum = i + 1,
  133. IpAddress = $"192.168.1.{i + 1}",
  134. });
  135. }
  136. }
  137. public void Init()
  138. {
  139. if (!string.IsNullOrEmpty(Json<DevicePar>.Data.BaseParModel.NetworkSegAddress)) IPSegment = Json<DevicePar>.Data.BaseParModel.NetworkSegAddress;
  140. else Json<DevicePar>.Data.BaseParModel.NetworkSegAddress = IPSegment;
  141. //TestData();
  142. IpAddressLines();
  143. Task.Factory.StartNew(() =>
  144. {
  145. SiemensDevice.GetInstance.Connect(Json<DevicePar>.Data.BaseParModel.DeviceAddress);
  146. });
  147. DeviceDataInit();
  148. TaskManage.GetInstance.StartLong(new Action(() =>
  149. {
  150. if (IPQueues.Count >= IPLists.Count)
  151. IpAddressLines();
  152. Thread.Sleep(5000);
  153. }), "配料机设备上线监听", true);
  154. }
  155. public void Rescan()
  156. {
  157. InvalidIP.Clear();
  158. }
  159. public DeviceStatus GetDevice(string ip)
  160. {
  161. if (ip != null)
  162. {
  163. if (DeviceLists.ContainsKey(ip)) return DeviceLists[ip];
  164. else return new DeviceStatus();
  165. //var res = DeviceLists.Values.FirstOrDefault(p => p.IpAddress == ip);
  166. //if (res != null) return res;
  167. }
  168. return new DeviceStatus();
  169. }
  170. public List<DeviceStatus> GetDevice()
  171. {
  172. List<DeviceStatus> deviceStatuses = new List<DeviceStatus>();
  173. foreach (var device in DeviceLists)
  174. {
  175. deviceStatuses.Add(device.Value);
  176. }
  177. return deviceStatuses;
  178. }
  179. private void IpAddressLines()
  180. {
  181. IPLists.Clear();
  182. IPQueues.Clear();
  183. /*for (int i = 1; i <= 255; i++)
  184. {
  185. if (!InvalidIP.Contains($"{IPSegment}{i}") && !DeviceLists.ContainsKey($"{IPSegment}{i}"))
  186. {
  187. string pattern = @"^(([1-9]\d?)|(1\d{2})|(2[01]\d)|(22[0-3]))(\.((1?\d\d?)|(2[04]/d)|(25[0-5]))){3}$";
  188. if (Regex.IsMatch($"{IPSegment}{i}", pattern))
  189. {
  190. IPLists.Add($"{IPSegment}{i}");
  191. }
  192. }
  193. }*/
  194. stockCount = Json<DevicePar>.Data.BaseParModel.StockCount;
  195. for (int i = 0; i < stockCount; i++)
  196. {
  197. IPLists.Add($"{IPSegment}{(i + 1) * 10}");
  198. }
  199. IPLists.ForEach((item) =>
  200. {
  201. Ping myPing = new Ping();
  202. myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
  203. myPing.SendAsync(item, 1000, null);
  204. });
  205. }
  206. private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
  207. {
  208. if (e.Reply != null && e.Reply.Status == IPStatus.Success)
  209. {
  210. string ip = e.Reply.Address.ToString();
  211. if (!DeviceLists.ContainsKey(ip))
  212. {
  213. DeviceStatus DS = new DeviceStatus();
  214. DS.modbusTcp.IsReconnect = false;
  215. DS.modbusTcp.Connected = new Action(() =>
  216. {
  217. var tt = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
  218. string DeviceName = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content?.Trim()?.Replace(" ", "");//读取设备名称
  219. if (DeviceName.Length > 0)
  220. {
  221. DeviceLists.TryAdd(ip, DS);
  222. DeviceLists[ip].Init(DeviceName);
  223. DeviceLists[ip].modbusTcp.IsReconnect = false;
  224. App.Current.Dispatcher.Invoke(new Action(() =>
  225. {
  226. devices.Add(new Devices() { DeviceName = DeviceName, IpAddress = ip });
  227. //2023/7/3调整
  228. if (stockCount <= 8 || (stockCount > 8 && TopDeviceCurrentStatuses.Count <= stockCount / 2 + 1 && int.TryParse(ip.Split('.')[3], out int res) && res <= (stockCount / 2 + 1) * 10))
  229. TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
  230. else
  231. BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
  232. for (int i = 0; i < Json<LocalRecipe>.Data.Recipes.Count; i++)
  233. {
  234. for (int m = 0; m < Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.Count; m++)
  235. {
  236. if (Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp == ip)
  237. {
  238. Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).RawMaterialName = DeviceName;
  239. }
  240. }
  241. }
  242. if (Global.DeviceRawMaterials.Count > 0)
  243. {
  244. if (Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == DeviceName) == null)
  245. {
  246. Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
  247. }
  248. }
  249. else
  250. {
  251. Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
  252. }
  253. }));
  254. }
  255. else
  256. {
  257. //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
  258. }
  259. });
  260. DS.modbusTcp.ConnectFail = new Action(() =>
  261. {
  262. //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
  263. });
  264. DS.modbusTcp.Disconnect = new Action(() =>
  265. {
  266. if (InvalidIP.Contains(ip)) InvalidIP.Remove(ip);
  267. var res = devices.FirstOrDefault(P => P.IpAddress == ip);
  268. if (res != null && devices.Contains(res))
  269. {
  270. App.Current.Dispatcher.Invoke(new Action(() =>
  271. {
  272. devices.Remove(res);
  273. var item = Global.DeviceRawMaterials.FirstOrDefault(P => P.RawMaterialName == res.DeviceName);
  274. if (item != null) Global.DeviceRawMaterials.Remove(item);
  275. var topRes = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
  276. var bottomRes = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
  277. if (topRes != null) TopDeviceCurrentStatuses.Remove(topRes);
  278. if (bottomRes != null) BottomDeviceCurrentStatuses.Remove(bottomRes);
  279. }));
  280. }
  281. if (DeviceLists.ContainsKey(ip))
  282. {
  283. DeviceLists[ip].Dispose();
  284. var res11 = DeviceLists[ip];
  285. DeviceLists.TryRemove(ip, out res11);
  286. }
  287. });
  288. Task.Run(new Action(() =>
  289. {
  290. DS.modbusTcp.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = ip, Port = 502 });//PLC连接
  291. IPQueues.Enqueue(e.Reply.Address.ToString());
  292. }));
  293. }
  294. else IPQueues.Enqueue(e.Reply.Address.ToString());
  295. }
  296. else
  297. {
  298. if (e.Reply != null)
  299. IPQueues.Enqueue(e.Reply.Address.ToString());
  300. }
  301. }
  302. }
  303. public class DeviceStatus
  304. {
  305. #region 对象属性声明
  306. public string DeviceName = String.Empty;
  307. public string IpAddress => modbusTcp.GetConfigPar().IpAddress;
  308. /// <summary>
  309. /// 设备状态
  310. /// </summary>
  311. public RawMaterialDeviceStatus deviceStatus { get; set; } = new RawMaterialDeviceStatus();
  312. public ModbusTcp modbusTcp = new ModbusTcp();
  313. public bool IsConnected => modbusTcp.IsConnected();
  314. #endregion
  315. public void Init(string DeviceName)
  316. {
  317. //modbusTcp.Show = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
  318. //modbusTcp.ShowEx = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
  319. this.DeviceName = DeviceName;
  320. AlarmHelper<AlarmInfo>.Init();
  321. if (IsConnected)
  322. {
  323. TaskManage.GetInstance.StartLong(new Action(() =>
  324. {
  325. var dn = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
  326. if (dn.IsSuccess) { this.DeviceName = dn.Content.Trim().Replace(" ", ""); deviceStatus.DeviceName = this.DeviceName; }
  327. //获取设备运行状态
  328. var rs = this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  329. if (rs.IsSuccess) deviceStatus.RunStatus = rs.Content;
  330. ////获取设备料仓剩余重量
  331. var wf = this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  332. if (wf.IsSuccess) deviceStatus.WeightFeedback = (float)(wf.Content);
  333. ////获取下料重量
  334. var nwf = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  335. if (nwf.IsSuccess) deviceStatus.NowWeightFeedback = nwf.Content;
  336. ////获取设备编号
  337. var dnum = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  338. if (dnum.IsSuccess) deviceStatus.DeviceNum = dnum.Content;
  339. ////获取设备故障编码
  340. var dac = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  341. if (dac.IsSuccess) deviceStatus.DeviceAlarmCode = dac.Content;
  342. //获取原料类型
  343. var mt = this.modbusTcp.Read<ushort>(DeviceAddress.MaterialDeviceType.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  344. if (mt.IsSuccess) deviceStatus.RawMaterialType = mt.Content;
  345. //获取工作模式
  346. var wm = this.modbusTcp.Read<ushort>(DeviceAddress.WorkModel.ToAdd(),1, BPA.Helper.DataFormat.CDAB);
  347. if (wm.IsSuccess) deviceStatus.WorkModel = wm.Content.GetBitValue(1);
  348. //慢加重量
  349. var ssaw = this.modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  350. if (ssaw.IsSuccess) deviceStatus.SlowAddWeight = ssaw.Content;
  351. //提前关阀重量
  352. var pcvw = this.modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  353. if (pcvw.IsSuccess) deviceStatus.PreCloseValueWeight = pcvw.Content;
  354. //快加速度
  355. var ra = this.modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  356. if (ra.IsSuccess) deviceStatus.RapidAcceleration = ra.Content;
  357. //慢加速度
  358. var sa = this.modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  359. if (sa.IsSuccess) deviceStatus.SlowAcceleration = sa.Content;
  360. //伺服手动速度
  361. var sms = this.modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  362. if (sms.IsSuccess) deviceStatus.ServoManualSpeed = sms.Content;
  363. //料仓上限重量
  364. var ulw = this.modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  365. if (ulw.IsSuccess) deviceStatus.SiloUpperLimitWeight = ulw.Content;
  366. //料仓下限重量
  367. var llw = this.modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  368. if (llw.IsSuccess) deviceStatus.SiloLowerLimitWeight = llw.Content;
  369. //料仓搅拌速度
  370. var ss = this.modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  371. if (ss.IsSuccess) deviceStatus.StirringSpeed = ss.Content;
  372. //this.DeviceName = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content.Trim().Replace(" ", "");
  373. //deviceStatus.RunStatus = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd()).Content; //获取设备运行状态
  374. //deviceStatus.WeightFeedback = (float)this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd()).Content;//获取设备料仓剩余重量
  375. //deviceStatus.NowWeightFeedback = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd()).Content;//获取下料重量
  376. //deviceStatus.DeviceNum = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd()).Content;//获取设备编号
  377. //deviceStatus.DeviceAlarmCode = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd()).Content;//获取设备故障编码
  378. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop1 = deviceStatus.DeviceAlarmCode.GetBitValue(1);
  379. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Servo = deviceStatus.DeviceAlarmCode.GetBitValue(2);
  380. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Inverter = deviceStatus.DeviceAlarmCode.GetBitValue(3);
  381. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop2 = deviceStatus.DeviceAlarmCode.GetBitValue(7);
  382. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloUpperLimit = deviceStatus.DeviceAlarmCode.GetBitValue(8);
  383. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloLowerLimit = deviceStatus.DeviceAlarmCode.GetBitValue(9);
  384. Thread.Sleep(10);
  385. }), $"{DeviceName} 开始监听", true);
  386. TaskManage.GetInstance.StartLong(new Action(() =>
  387. {
  388. Thread.Sleep(1000);
  389. if (ExcuteControl.GetInstance.mqttCollection.ContainsKey($"Stock{IpAddress.Split(".")[3].Substring(0, 1)}") && ExcuteControl.GetInstance.mqttCollection[$"Stock{IpAddress.Split(".")[3].Substring(0, 1)}"]._mqttClient.IsConnected)
  390. {
  391. StockPropertyReport();
  392. StockEventReport();
  393. }
  394. }),$"{DeviceName}属性实时上报",true);
  395. }
  396. }
  397. /// <summary>
  398. /// 料仓属性上报
  399. /// </summary>
  400. public void StockPropertyReport()
  401. {
  402. #if !Debug
  403. AliyunStockPropertyReportModel model = new AliyunStockPropertyReportModel();
  404. model.StockOnlineStatus = this.modbusTcp.IsConnected()==true?1:0;
  405. model.StockRealWeight = (int)deviceStatus.WeightFeedback;
  406. model.BucketRealWeight = deviceStatus.NowWeightFeedback;
  407. model.StockDeviceNum = (int)deviceStatus.DeviceNum;
  408. model.StockDeviceName = deviceStatus.DeviceName;
  409. model.StockWorkModel = deviceStatus.WorkModel == true ? 1 : 0;
  410. model.StockSlowlyAddWeight = deviceStatus.SlowAddWeight;
  411. model.StockPreCloseValueWeight = deviceStatus.PreCloseValueWeight;
  412. model.StockRapidAcceleration = (int)deviceStatus.RapidAcceleration;
  413. model.StockSlowAcceleration = (int)deviceStatus.SlowAcceleration;
  414. model.StockServoManualSpeed = (int)deviceStatus.ServoManualSpeed;
  415. model.StockUpperLimitWeight = (int)deviceStatus.SiloUpperLimitWeight;
  416. model.StockLowerLimitWeight = (int)deviceStatus.SiloLowerLimitWeight;
  417. model.StockStirringSpeed = (int)deviceStatus.StirringSpeed;
  418. switch (deviceStatus.RawMaterialType)
  419. {
  420. case 1:
  421. model.StockMaterialType = "膏体"; break;
  422. case 2:
  423. model.StockMaterialType = "液体"; break;
  424. case 3:
  425. model.StockMaterialType = "粉体"; break;
  426. }
  427. switch (deviceStatus.RunStatus)
  428. {
  429. case 0:
  430. model.StockBatchStatus = "待机"; break;
  431. case 1:
  432. model.StockBatchStatus = "等待配料"; break;
  433. case 2:
  434. model.StockBatchStatus = "配料中"; break;
  435. case 3:
  436. model.StockBatchStatus = "配料完成"; break;
  437. }
  438. #endif
  439. #if Debug
  440. //本地模拟测试
  441. AliyunStockPropertyReportModel model = new AliyunStockPropertyReportModel();
  442. model.StockRealWeight = 1;
  443. model.BucketRealWeight = 2;
  444. model.StockDeviceNum = 2;
  445. model.StockDeviceName ="测试";
  446. model.StockWorkModel = 0;
  447. model.StockSlowlyAddWeight = 6;
  448. model.StockPreCloseValueWeight = 7;
  449. model.StockRapidAcceleration =46;
  450. model.StockSlowAcceleration = 24;
  451. model.StockServoManualSpeed = 35;
  452. model.StockUpperLimitWeight = 57;
  453. model.StockLowerLimitWeight = 34;
  454. model.StockStirringSpeed = 54;
  455. model.StockMaterialType = "粉体";
  456. model.StockBatchStatus = "待机";
  457. #endif
  458. ExcuteControl.GetInstance.StokcPropertyReport(IpAddress, model);
  459. }
  460. /// <summary>
  461. /// 事件上报
  462. /// </summary>
  463. public void StockEventReport()
  464. {
  465. #if !Debug
  466. try
  467. {
  468. if (RTrig.GetInstance($"stop1:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop1))
  469. {
  470. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk1StatusEvent", AliyunMaterialModelType.StockModel);
  471. }
  472. if (RTrig.GetInstance($"Servo:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Servo))
  473. {
  474. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockServoFalutEvent", AliyunMaterialModelType.StockModel);
  475. }
  476. if (RTrig.GetInstance($"EStop2:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop2))
  477. {
  478. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk2StatusEvent", AliyunMaterialModelType.StockModel);
  479. }
  480. if (RTrig.GetInstance($"SiloUpperLimit:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloUpperLimit))
  481. {
  482. if (DeviceName == "盐")
  483. MessageNotify.GetInstance.ShowRunLog("料仓1 上限报警");
  484. if (DeviceName == "A20")
  485. MessageNotify.GetInstance.ShowRunLog("料仓2 上限报警");
  486. if (DeviceName == "A30")
  487. MessageNotify.GetInstance.ShowRunLog("料仓3 上限报警");
  488. if (DeviceName == "A40")
  489. MessageNotify.GetInstance.ShowRunLog("料仓4 上限报警");
  490. if (DeviceName == "A50")
  491. MessageNotify.GetInstance.ShowRunLog("料仓5 上限报警");
  492. if (DeviceName == "A60")
  493. MessageNotify.GetInstance.ShowRunLog("料仓6 上限报警");
  494. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockUpLimitEvent", AliyunMaterialModelType.StockModel);
  495. }
  496. if (RTrig.GetInstance($"SiloLowerLimit:{IpAddress}").Start(AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloLowerLimit))
  497. {
  498. if (DeviceName == "盐")
  499. MessageNotify.GetInstance.ShowRunLog("料仓1 下限报警");
  500. if (DeviceName == "A20")
  501. MessageNotify.GetInstance.ShowRunLog("料仓2 下限报警");
  502. if (DeviceName == "A30")
  503. MessageNotify.GetInstance.ShowRunLog("料仓3 下限报警");
  504. if (DeviceName == "A40")
  505. MessageNotify.GetInstance.ShowRunLog("料仓4 下限报警");
  506. if (DeviceName == "A50")
  507. MessageNotify.GetInstance.ShowRunLog("料仓5 下限报警");
  508. if (DeviceName == "A60")
  509. MessageNotify.GetInstance.ShowRunLog("料仓6 下限报警");
  510. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockDownLimitEvent", AliyunMaterialModelType.StockModel);
  511. }
  512. }
  513. catch (Exception)
  514. {
  515. //throw;
  516. }
  517. #endif
  518. #if Debug
  519. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk1StatusEvent", AliyunMaterialModelType.StockModel);
  520. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockServoFalutEvent", AliyunMaterialModelType.StockModel);
  521. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockJerk2StatusEvent", AliyunMaterialModelType.StockModel);
  522. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockUpLimitEvent", AliyunMaterialModelType.StockModel);
  523. ExcuteControl.GetInstance.AliyunEventReport(IpAddress, new AliyunStockEventReportModel { Result = "1" }, "StockDownLimitEvent", AliyunMaterialModelType.StockModel);
  524. #endif
  525. }
  526. public void SetDeviceName(string name)
  527. {
  528. this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), new ushort[20]);
  529. this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), name);
  530. }
  531. public void StatusReset()
  532. {
  533. // prop1:
  534. this.modbusTcp.Write(DeviceAddress.FinfishStatus.ToAdd(), (ushort)1);
  535. //MessageNotify.GetInstance.ShowRunLog($"料仓复位,值:{1}");
  536. //var value = this.modbusTcp.Read<ushort>(DeviceAddress.FinfishStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  537. //if (value != null && value.ToString() != 1.ToString()) goto prop1;
  538. }
  539. public void Dispose()
  540. {
  541. TaskManage.GetInstance.StopTask($"{DeviceName} 开始监听");
  542. }
  543. public void Start(float Value)
  544. {
  545. if (modbusTcp.IsConnected())
  546. {
  547. var res = Json<DevicePar>.Data.deviceParModels.FirstOrDefault(p => p.MaterialName == DeviceName);
  548. if (res != null)
  549. {
  550. //modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
  551. //modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
  552. //modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
  553. //modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
  554. //modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
  555. //modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
  556. //modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
  557. //modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
  558. //MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
  559. prop1:
  560. modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
  561. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加重量:{res.SlowlyAddWeight}");
  562. var value1 = modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  563. if (value1 != null && value1.ToString() != res.SlowlyAddWeight.ToString()) goto prop1;
  564. prop2:
  565. modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
  566. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置提前关阀重量:{res.PreCloseValveWeight}");
  567. var value2 = modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  568. if (value2 != null && value2.ToString() != res.PreCloseValveWeight.ToString()) goto prop2;
  569. prop3:
  570. modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
  571. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置快加速度:{res.RapidAcceleration}");
  572. var value3 = modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  573. if (value3 != null && value3.ToString() != res.RapidAcceleration.ToString()) goto prop3;
  574. prop4:
  575. modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
  576. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加速度:{res.SlowAcceleration}");
  577. var value4 = modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  578. if (value4 != null && value4.ToString() != res.SlowAcceleration.ToString()) goto prop4;
  579. prop5:
  580. modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
  581. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置伺服手动速度:{res.ServoManualSpeed}");
  582. var value5 = modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  583. if (value5 != null && value5.ToString() != res.ServoManualSpeed.ToString()) goto prop5;
  584. prop6:
  585. modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
  586. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓上限重量:{res.SiloUpperLimitWeight}");
  587. var value6 = modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  588. if (value6 != null && value6.ToString() != res.SiloUpperLimitWeight.ToString()) goto prop6;
  589. prop7:
  590. modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
  591. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓下限重量:{res.LowerLimitWeightOfSilo}");
  592. var value7 = modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  593. if ((value7 != null && value7.ToString() != res.LowerLimitWeightOfSilo.ToString())) goto prop7;
  594. prop8:
  595. modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
  596. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置搅拌速度:{res.StirringSpeed}");
  597. var value8 = modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  598. if (value8 != null && value8.ToString() != res.StirringSpeed.ToString()) goto prop8;
  599. MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
  600. }
  601. prop9:
  602. modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
  603. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value}");
  604. var value9 = modbusTcp.Read<float>(DeviceAddress.WeightSet.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  605. if ((value9 != null && value9.ToString() != Value.ToString()) || value9 == 0) goto prop9;
  606. //prop10:
  607. //MessageNotify.GetInstance.ShowRunLog($"写入配方重量,值{Value}:{value9}");
  608. //modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
  609. //var value10 = modbusTcp.Read<ushort>(DeviceAddress.Start.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  610. //if ((value10 != null && value10.ToString() != 1.ToString())||value10==0) goto prop10;
  611. //modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
  612. modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
  613. Task.Run(() =>
  614. {
  615. while (true)
  616. {
  617. ushort c = modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  618. MessageNotify.GetInstance.ShowRunLog($"{IpAddress}:LW51,值:{modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content}");
  619. string str = DecToBinary(c);
  620. if (str[8].ToString() == 0.ToString())
  621. {
  622. modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
  623. }
  624. else
  625. return;
  626. }
  627. });
  628. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value},味魔方启动配料");
  629. }
  630. }
  631. static string DecToBinary(int a)
  632. {
  633. int r;
  634. string s = "";
  635. do
  636. {
  637. r = a % 2;
  638. a /= 2;
  639. s = r + s;
  640. }
  641. while (a != 0);
  642. int length = s.Length;
  643. for (int i = 0; i < 16 - length; i++)
  644. {
  645. s = 0 + s;
  646. }
  647. return s;
  648. }
  649. }
  650. }