CoreThreadTest2.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using parkMonitor.LOG;
  7. using parkMonitor.entity;
  8. using parkMonitor.model;
  9. using System.Configuration;
  10. using System.Threading;
  11. using parkMonitor.server.uiLogServer;
  12. using parkMonitor.tools;
  13. using parkMonitor.DataBase;
  14. namespace parkMonitor.server.CoreThread
  15. {
  16. class CoreThreadTest2 : IEquipments, ICoreThreadDoWorking
  17. {
  18. int equipmentStatus_address;//设备总控状态地址
  19. //获取PLC句柄
  20. IEquipments PLC;
  21. //获取队列句柄
  22. IEquipments queuingThread;
  23. //获取号牌句柄
  24. IEquipments NumMachine;
  25. int localGarageId = 0;
  26. bool isClosing;
  27. string[] equipNames = new string[] { EquipmentName.PLC, EquipmentName.NumMachine, EquipmentName.Web, EquipmentName.Queue };
  28. public CoreThreadTest2()
  29. {
  30. try
  31. {
  32. equipmentStatus_address = Convert.ToInt32(ConfigurationManager.AppSettings["equipmentStatus_address"]);
  33. localGarageId = Convert.ToInt32(ConfigurationManager.AppSettings["localGarageId"]);
  34. DBConnection.Init();
  35. }
  36. catch (Exception) { }
  37. }
  38. public void Start()
  39. {
  40. for (int i = 0; i < equipNames.Length; ++i)
  41. {
  42. IEquipments equip = EquipmentSimpleFactory.ins.CreateEquipment(equipNames[i]);
  43. if (equip != null)
  44. {
  45. equip.Start();
  46. }
  47. }
  48. PLC = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC);
  49. queuingThread = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
  50. NumMachine = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.NumMachine);
  51. //BeginWorking();
  52. //监听总状态改变消息
  53. //由监控线程发出
  54. AsyncCmdServer.ins.listen(AsyncCmdType.TotalStatusChanged, (AlarmStatus type) =>
  55. {
  56. if (type != AlarmStatus.Normal)
  57. {
  58. hangUp();
  59. }
  60. });
  61. }
  62. public AbstractMessage GetMessage()
  63. {
  64. return null;
  65. }
  66. public void SetMessage(AbstractMessage message)
  67. {
  68. }
  69. public void Stop()
  70. {
  71. for (int i = 0; i < equipNames.Length; ++i)
  72. {
  73. IEquipments equip = EquipmentSimpleFactory.ins.FindEquipment(equipNames[i]);
  74. if (equip != null)
  75. {
  76. equip.Stop();
  77. }
  78. }
  79. AbstractCmd.isClosing = true;//停止运行中指令
  80. this.isClosing = true;
  81. }
  82. public void BeginWorking()
  83. {
  84. Task.Factory.StartNew(() =>
  85. {
  86. while (!isClosing)
  87. {
  88. Func<AbstractMessage> func = new Func<AbstractMessage>(GetExecuteNumberPlate);
  89. TaskFactory taskFactory = new TaskFactory();
  90. List<Task> tasklist = new List<Task>();
  91. IAsyncResult result = func.BeginInvoke(null, null);
  92. Action act1 = new Action(() =>
  93. {
  94. GetTotalStatus();
  95. });
  96. tasklist.Add(taskFactory.StartNew(act1));
  97. Task.WaitAll(tasklist.ToArray());
  98. Command queueCmd = (Command)func.EndInvoke(result);
  99. Task.Factory.StartNew(() =>
  100. {
  101. SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
  102. AbstractCmd abstractCmd = simpleCMDFactory.createCmd(queueCmd);
  103. abstractCmd.executeCmd(queueCmd);
  104. });
  105. Thread.Sleep(100);
  106. }
  107. });
  108. //同步数据库
  109. //Task.Factory.StartNew(() =>
  110. //{
  111. // while (true)
  112. // {
  113. // try
  114. // {
  115. // //更新车库表车位数
  116. // int freeSpaceCount = oper.getGarageFreeSpace("SqlConnectionLocation",localGarageId);
  117. // oper.UpdateGarageFreeSpace("SqlConnectionStr",freeSpaceCount, localGarageId);
  118. // //更新车位状态
  119. // //int[] parkingSpace = oper.GetAllParkingSpaceID(localGarageId);
  120. // Dictionary<int, Parking_Space> lps = oper.GetAllParkingSpace("SqlConnectionLocation", localGarageId);
  121. // if (lps != null)
  122. // {
  123. // //Dictionary<>
  124. // for (int i = 1; i <= lps.Count; i++)
  125. // {
  126. // oper.UpdateParkingSpaceState("SqlConnectionStr", lps[i].parkingSpaceID,lps[i].parkingSpaceState);
  127. // }
  128. // }
  129. // }
  130. // catch (Exception)
  131. // {
  132. // Console.WriteLine("无同步数据");
  133. // }
  134. // Thread.Sleep(8000);
  135. // }
  136. //});
  137. }
  138. /// <summary>挂起</summary>
  139. public void hangUp()
  140. {
  141. }
  142. /// <summary>
  143. /// 获取PLC总控状态
  144. /// </summary>
  145. private void GetTotalStatus()
  146. {
  147. PLCMessage PLCMsg = null;
  148. int normalStatus = 0;
  149. while (!isClosing)
  150. {
  151. PLCMsg = (PLCMessage)PLC.GetMessage();
  152. if (PLCMsg != null && PLCMsg.originalPlcList != null && PLCMsg.originalPlcList.Count != 0)
  153. {
  154. normalStatus = Convert.ToInt32(PLCMsg.originalPlcList[equipmentStatus_address].Value);
  155. }
  156. //设备总控状态
  157. if (normalStatus == 1)
  158. {
  159. UILogServer.ins.log("设备总状态正常");
  160. break;
  161. }
  162. Thread.Sleep(100);
  163. }
  164. }
  165. /// <summary>
  166. /// 获取可执行号牌
  167. /// </summary>
  168. private AbstractMessage GetExecuteNumberPlate()
  169. {
  170. AbstractMessage msg = null;
  171. while (!isClosing)
  172. {
  173. msg = queuingThread.GetMessage();
  174. if (msg != null)
  175. {
  176. break;
  177. }
  178. }
  179. return msg;
  180. }
  181. }
  182. }