终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
2 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using BPA.Helper;
  2. using BPA.Message.Enum;
  3. using BPA.Models;
  4. using BPASmartClient.Device;
  5. using BPASmartClient.EventBus;
  6. using BPASmartClient.Modbus;
  7. using BPASmartClient.Model;
  8. using BPASmartClient.Model.PLC;
  9. using BPASmartClient.Model.;
  10. using BPASmartClient.Model.ƻ;
  11. using BPASmartClient.MorkMW.Model;
  12. using Newtonsoft.Json;
  13. using Org.BouncyCastle.Bcpg.OpenPgp;
  14. using System;
  15. using System.Collections.Concurrent;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Net.Http.Json;
  19. using System.Threading;
  20. using System.Windows.Documents;
  21. using static BPASmartClient.EventBus.EventBus;
  22. namespace BPASmartClient.MorkMW
  23. {
  24. public class Control_MorkMW : BaseDevice
  25. {
  26. ModbusTcp modbus;
  27. public override DeviceClientType DeviceType => DeviceClientType.MORKMW;
  28. GVL_MorkMW morkMW = new GVL_MorkMW();
  29. public override void DoMain()
  30. {
  31. ConnectKlpRobot("192.168.0.100", 8001);
  32. ServerInit();
  33. DataParse();
  34. }
  35. private void ServerInit()
  36. {
  37. //䷽Ϣ
  38. EventBus.EventBus.GetInstance().Subscribe<MorkMWGoodsEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  39. {
  40. if (@event == null) return;
  41. if (@event is MorkMWGoodsEvent recipe)
  42. {
  43. listMorkMWGoods= recipe.morkMWPushMessage.MorkMWGoods;
  44. DeviceProcessLogShow($"յƻ{listMorkMWGoods.Count}Ʒ䷽");
  45. }
  46. });
  47. }
  48. public override void MainTask()
  49. {
  50. ToGetWink();
  51. ToMixWink();
  52. ToPourWink();
  53. SignalDetect();
  54. }
  55. /// <summary>
  56. /// Ӿƹ
  57. /// </summary>
  58. public void ToGetWink()
  59. {
  60. if (!GVL_MorkMW.AllowLocalSimOrder)
  61. {
  62. if (morkMW.RobotIdle && orderLocInfos.Count > 0 && !morkMW.TaskLock && !morkMW.PourWinkComplete && morkMW.CupSignal)
  63. {
  64. DeviceProcessLogShow("ʼ");
  65. morkMW.TaskLock = true;
  66. if (orderLocInfos.TryDequeue(out OrderLocInfo res))
  67. {
  68. foreach (var item in res.mixWink)
  69. {
  70. switch (item.Loc)
  71. {
  72. // case 7: ToMixWink(); break;
  73. case 1: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  74. case 2: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  75. case 3: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  76. case 4: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  77. case 5: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  78. case 6: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
  79. }
  80. switch (item.Loc)
  81. {
  82. //case 7: while (!morkMW.MixWinkComplte) { Thread.Sleep(5); } break;
  83. case 1: while (!morkMW.TakeWinkOneComplete) { Thread.Sleep(5); } break;
  84. case 2: while (!morkMW.TakeWinkTwoComplete) { Thread.Sleep(5); } break;
  85. case 3: while (!morkMW.TakeWinkThreeComplete) { Thread.Sleep(5); } break;
  86. case 4: while (!morkMW.TakeWinkFourComplete) { Thread.Sleep(5); } break;
  87. case 5: while (!morkMW.TakeWinkFiveComplete) { Thread.Sleep(5); } break;
  88. case 6: while (!morkMW.TakeWinkSixComplete) { Thread.Sleep(5); } break;
  89. }
  90. }
  91. morkMW.AllowMixWink = true;//Ӿɣ
  92. // morkMW.AllowPourWink = true;//ɣ
  93. }
  94. }
  95. }
  96. else
  97. {
  98. if (morkMW.RobotIdle && orderLocInfos.Count > 0 && !morkMW.TaskLock && !morkMW.PourWinkComplete && morkMW.CupSignal)
  99. {
  100. DeviceProcessLogShow("ʼ");
  101. morkMW.TaskLock = true;
  102. if (simOrderLocInfos.TryDequeue(out SimOrderLocInfo res))
  103. {
  104. foreach (var item in res.mixWink)
  105. {
  106. switch (item.Loc)
  107. {
  108. // case 7: ToMixWink(); break;
  109. case 1: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  110. case 2: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  111. case 3: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  112. case 4: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  113. case 5: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  114. case 6: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
  115. }
  116. switch (item.Loc)
  117. {
  118. //case 7: while (!morkMW.MixWinkComplte) { Thread.Sleep(5); } break;
  119. case 1: while (!morkMW.TakeWinkOneComplete) { Thread.Sleep(5); } break;
  120. case 2: while (!morkMW.TakeWinkTwoComplete) { Thread.Sleep(5); } break;
  121. case 3: while (!morkMW.TakeWinkThreeComplete) { Thread.Sleep(5); } break;
  122. case 4: while (!morkMW.TakeWinkFourComplete) { Thread.Sleep(5); } break;
  123. case 5: while (!morkMW.TakeWinkFiveComplete) { Thread.Sleep(5); } break;
  124. case 6: while (!morkMW.TakeWinkSixComplete) { Thread.Sleep(5); } break;
  125. }
  126. }
  127. DeviceProcessLogShow($"λýӾ");
  128. morkMW.AllowMixWink = true;//Ӿɣ
  129. // morkMW.AllowPourWink = true;//ɣ
  130. }
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// ƹ
  136. /// </summary>
  137. public void ToMixWink()
  138. {
  139. // modbus.WriteSingleRegister(0000, 7);//˵
  140. if (morkMW.AllowMixWink)
  141. {
  142. DeviceProcessLogShow("ʼ");
  143. modbus.WriteSingleRegister(0000, 7);//˵
  144. morkMW.AllowMixWink = false;
  145. }
  146. }
  147. /// <summary>
  148. /// ȥӦλýӾ
  149. /// </summary>
  150. /// <param name="loc"></param>
  151. /// <param name="time"></param>
  152. public void ToSpecifiedLocTakeWink(int loc, int time)
  153. {
  154. //ͽӾź
  155. switch (loc)
  156. {
  157. case 1: modbus.WriteSingleRegister(0000, 1);DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
  158. case 2: modbus.WriteSingleRegister(0000, 2);DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
  159. case 3: modbus.WriteSingleRegister(0000, 3);DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
  160. case 4: modbus.WriteSingleRegister(0000, 4);DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
  161. case 5: modbus.WriteSingleRegister(0000, 5);DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
  162. case 6: modbus.WriteSingleRegister(0000, 6); DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
  163. }
  164. //ȴ˵Ӿλ
  165. switch (loc)
  166. {
  167. case 1: while (!morkMW.ArriveWinkOneLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
  168. case 2: while (!morkMW.ArriveWinkTwoLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
  169. case 3: while (!morkMW.ArriveWinkThreeLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
  170. case 4: while (!morkMW.ArriveWinkFourLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
  171. case 5: while (!morkMW.ArriveWinkFiveLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
  172. case 6: while (!morkMW.ArriveWinkSixLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
  173. }
  174. Thread.Sleep(time * 1000);//ӾƵȴʱ
  175. //ͽӾź
  176. switch (loc)
  177. {
  178. case 1: modbus.WriteSingleCoil(4596, true);DeviceProcessLogShow($"{loc}λýӾ"); break;
  179. case 2: modbus.WriteSingleCoil(4597, true);DeviceProcessLogShow($"{loc}λýӾ"); break;
  180. case 3: modbus.WriteSingleCoil(4598, true);DeviceProcessLogShow($"{loc}λýӾ"); break;
  181. case 4: modbus.WriteSingleCoil(4599, true);DeviceProcessLogShow($"{loc}λýӾ"); break;
  182. case 5: modbus.WriteSingleCoil(4600, true);DeviceProcessLogShow($"{loc}λýӾ"); break;
  183. case 6: modbus.WriteSingleCoil(4601, true); DeviceProcessLogShow($"{loc}λýӾ"); break;
  184. }
  185. }
  186. /// <summary>
  187. /// ƹ
  188. /// </summary>
  189. public void ToPourWink()
  190. {
  191. //if (morkMW.AllowPourWink && morkMW.CupSignal)
  192. //{
  193. // morkMW.AllowPourWink = false;
  194. // modbus.WriteSingleRegister(0000, 8);//
  195. //}
  196. if (morkMW.CupSignal && RTrig.GetInstance("AllowOut").Start(morkMW.MixWinkComplte))
  197. {
  198. DeviceProcessLogShow("ʼ");
  199. modbus.WriteSingleRegister(0000, 8);//
  200. }
  201. }
  202. /// <summary>
  203. /// źż
  204. /// </summary>
  205. public void SignalDetect()
  206. {
  207. if (RTrig.GetInstance("AllComplete").Start(morkMW.ProcessComplete))
  208. {
  209. morkMW.PourWinkComplete = true;
  210. DeviceProcessLogShow("ɣȡߵþƱ");
  211. }
  212. if (morkMW.PourWinkComplete)
  213. {
  214. if (TTrig.GetInstance("PoutWinkCom").Start(morkMW.CupSignal))
  215. {
  216. DeviceProcessLogShow("ͻȡߵþƱ,ÿվƱԴ´ζ");
  217. }
  218. if (RTrig.GetInstance("PoutWinkCom").Start(morkMW.CupSignal))
  219. {
  220. DeviceProcessLogShow("վƱλִһ");
  221. morkMW.PourWinkComplete = false;
  222. morkMW.TaskLock = false;
  223. Thread.Sleep(2000);
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// ջ״̬
  229. /// </summary>
  230. public override void ReadData()
  231. {
  232. //GetStatus("M230.0", new Action<object>((obj) =>
  233. //{
  234. //}));
  235. if (modbus.Connected)
  236. {
  237. morkMW.ArriveWinkOneLoc = modbus.ReadCoils(4616);
  238. morkMW.ArriveWinkTwoLoc = modbus.ReadCoils(4617);
  239. morkMW.ArriveWinkThreeLoc = modbus.ReadCoils(4618);
  240. morkMW.ArriveWinkFourLoc = modbus.ReadCoils(4619);
  241. morkMW.ArriveWinkFiveLoc = modbus.ReadCoils(4620);
  242. morkMW.ArriveWinkSixLoc = modbus.ReadCoils(4621);
  243. morkMW.RobotIdle = modbus.ReadCoils(4201);
  244. morkMW.CupSignal = modbus.ReadInputs(0);
  245. morkMW.TakeWinkOneComplete = modbus.ReadCoils(4606);
  246. morkMW.TakeWinkTwoComplete = modbus.ReadCoils(4607);
  247. morkMW.TakeWinkThreeComplete = modbus.ReadCoils(4608);
  248. morkMW.TakeWinkFourComplete = modbus.ReadCoils(4609);
  249. morkMW.TakeWinkFiveComplete = modbus.ReadCoils(4610);
  250. morkMW.TakeWinkSixComplete = modbus.ReadCoils(4611);
  251. morkMW.MixWinkComplte = modbus.ReadCoils(4612);
  252. morkMW.ProcessComplete = modbus.ReadCoils(4613);
  253. }
  254. else
  255. {
  256. modbus.ModbusTcpConnect("192.168.0.100", 8001);
  257. }
  258. }
  259. public override void ResetProgram()
  260. {
  261. morkMW = null;
  262. morkMW = new GVL_MorkMW();
  263. }
  264. public override void SimOrder()
  265. {
  266. EventBus.EventBus.GetInstance().Subscribe<MorkMWSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  267. {
  268. if (@event != null && @event is MorkMWSimOrder order)
  269. {
  270. if (order.mixWink.Count > 0)
  271. {
  272. string guid = Guid.NewGuid().ToString();
  273. simOrderLocInfos.Enqueue(new SimOrderLocInfo { mixWink = order.mixWink, SuborderId = guid });
  274. DeviceProcessLogShow("յģⶩ");
  275. }
  276. }
  277. });
  278. }
  279. public override void Stop()
  280. {
  281. }
  282. private void GetStatus(string key, Action<object> action)
  283. {
  284. if (peripheralStatus.ContainsKey(key))
  285. {
  286. if (peripheralStatus[key] != null)
  287. {
  288. action?.Invoke(peripheralStatus[key]);
  289. }
  290. }
  291. }
  292. /// <summary>
  293. /// д
  294. /// </summary>
  295. /// <param name="address"></param>
  296. /// <param name="value"></param>
  297. private void WriteData(string address, object value)
  298. {
  299. EventBus.EventBus.GetInstance().Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value });
  300. }
  301. /// <summary>
  302. /// ݽ
  303. /// </summary>
  304. public void DataParse()
  305. {
  306. EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
  307. {
  308. if (@event != null && @event is DoOrderEvent order)
  309. {
  310. var res= listMorkMWGoods.FirstOrDefault(p => p.GoodsKey == order.MorkOrder.GoodsKey);
  311. if (res != null)
  312. {
  313. string guid = Guid.NewGuid().ToString();
  314. orderLocInfos.Enqueue(new OrderLocInfo { mixWink = res.MorkMWBoms, SuborderId = order.MorkOrder.SuborderId });
  315. DeviceProcessLogShow($"յС򶩵š{order.MorkOrder.SuborderId}");
  316. }
  317. }
  318. });
  319. }
  320. /// <summary>
  321. /// ӿջ
  322. /// </summary>
  323. /// <param name="ip"></param>
  324. /// <param name="port"></param>
  325. public void ConnectKlpRobot(string ip, int port)
  326. {
  327. modbus = new ModbusTcp();
  328. modbus.Show += new Action<string>((s) =>
  329. {
  330. if (s != null) DeviceProcessLogShow(s);
  331. });
  332. modbus.ShowEx += new Action<string>((s) =>
  333. {
  334. if ((s != null)) DeviceProcessLogShow(s);
  335. });
  336. modbus.ModbusTcpConnect(ip, port);
  337. }
  338. /// <summary>
  339. /// С򶩵
  340. /// </summary>
  341. public ConcurrentQueue<OrderLocInfo> orderLocInfos = new ConcurrentQueue<OrderLocInfo>();
  342. /// <summary>
  343. /// ģⶩ
  344. /// </summary>
  345. public ConcurrentQueue<SimOrderLocInfo> simOrderLocInfos = new ConcurrentQueue<SimOrderLocInfo>();
  346. /// <summary>
  347. /// ƻ䷽
  348. /// </summary>
  349. public List<MORKMWGoods> listMorkMWGoods = new List<MORKMWGoods>();
  350. }
  351. }