CoreThreadTest2.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.Database2;
  14. using MySql.Data.MySqlClient;
  15. using parkMonitor.server.CoreThread;
  16. namespace parkMonitor.server.CoreThread
  17. {
  18. class CoreThreadTest2 : IEquipments, ICoreThreadDoWorking
  19. {
  20. int equipmentStatus_address;//设备总控状态地址
  21. bool initialized = false;
  22. /// <summary>
  23. /// 获取PLC句柄
  24. /// </summary>
  25. IEquipments PLC;
  26. /// <summary>
  27. /// 获取队列句柄
  28. /// </summary>
  29. IEquipments queuingThread;
  30. /// <summary>
  31. /// 获取号牌句柄
  32. /// </summary>
  33. IEquipments NumMachine;
  34. bool isClosing;
  35. string[] equipNames = new string[] { EquipmentName.PLC, EquipmentName.NumMachine, EquipmentName.Web, EquipmentName.Queue };
  36. public CoreThreadTest2()
  37. {
  38. try
  39. {
  40. EntityForCore.ins.globalStatus = false;
  41. equipmentStatus_address = Convert.ToInt32(ConfigurationManager.AppSettings["equipmentStatus_address"]);
  42. }
  43. catch (Exception) { }
  44. }
  45. public void Start()
  46. {
  47. //创建所有实例
  48. for (int i = 0; i < equipNames.Length; ++i)
  49. {
  50. IEquipments equip = EquipmentSimpleFactory.ins.CreateEquipment(equipNames[i]);
  51. }
  52. //启动所有实例
  53. for (int i = 0; i < equipNames.Length; ++i)
  54. {
  55. IEquipments equip = EquipmentSimpleFactory.ins.FindEquipment(equipNames[i]);
  56. equip.Start();
  57. }
  58. PLC = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.PLC);
  59. queuingThread = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.Queue);
  60. NumMachine = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.NumMachine);
  61. if (!(EntityForCore.ins.globalStatus = EntityForCore.ins.Init()))
  62. {
  63. UILogServer.ins.error("系统初始化异常,请检查配置");
  64. }
  65. initialized = true;
  66. //BeginWorking();
  67. //监听总状态改变消息
  68. //由监控线程发出
  69. AsyncCmdServer.ins.listen(AsyncCmdType.TotalStatusChanged, (AlarmStatus type) =>
  70. {
  71. if (type != AlarmStatus.Normal)
  72. {
  73. hangUp();
  74. }
  75. });
  76. }
  77. public AbstractMessage GetMessage()
  78. {
  79. return null;
  80. }
  81. public void SetMessage(AbstractMessage message)
  82. {
  83. }
  84. public void Stop()
  85. {
  86. for (int i = 0; i < equipNames.Length; ++i)
  87. {
  88. IEquipments equip = EquipmentSimpleFactory.ins.FindEquipment(equipNames[i]);
  89. if (equip != null)
  90. {
  91. equip.Stop();
  92. }
  93. }
  94. AbstractCmd.isClosing = true;//停止运行中指令
  95. this.isClosing = true;
  96. }
  97. /// <summary>
  98. /// 核心线程工作
  99. /// </summary>
  100. public void BeginWorking()
  101. {
  102. Timer checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 3600000, 3600000);
  103. Timer killTimeoutConnection = new System.Threading.Timer(KillTimeoutConnection, null, 3600000, 3600000);
  104. //Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 10000, 300000);
  105. //Timer logTimer = new System.Threading.Timer(displayLog, null, 1000, 30000);
  106. while (!isClosing)
  107. {
  108. if (initialized && EntityForCore.ins.globalStatus)
  109. {
  110. Command queueCmd = null;
  111. //等待号牌资源
  112. Task getCmd = Task.Factory.StartNew(() =>
  113. {
  114. while (true)
  115. {
  116. if (EntityForCore.ins.globalStatus)
  117. {
  118. queueCmd = (Command)queuingThread.GetMessage();
  119. if (queueCmd != null)
  120. {
  121. //远端或本地异常,遇到相应指令则退回
  122. if ((!queueCmd.manual && ConnectionPoolManager.malfunctionRemote) || (queueCmd.manual && ConnectionPoolManager.malfunctionLocal))
  123. {
  124. queueCmd.returnedCount = 1;
  125. queuingThread.SetMessage((Command)queueCmd.Clone());
  126. Thread.Sleep(5000);
  127. }
  128. //判断当前计数是否允许线程创建,机械手资源争抢严重则将指令丢回队列
  129. else if (queueCmd.id / 6 + 1 == Robot.robot1.id)
  130. {
  131. if ((queueCmd.commandType == 's' && Robot.robot1.parkWaitCount >= 1) || (queueCmd.commandType == 'f' && Robot.robot1.fetchWaitCount >= 1))
  132. {
  133. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "等待机械手1资源线程过多:" + Robot.robot1.parkWaitCount + "," + Robot.robot1.fetchWaitCount);
  134. queueCmd.returnedCount = 10000;
  135. queuingThread.SetMessage((Command)queueCmd.Clone());
  136. Thread.Sleep(15000);
  137. continue;
  138. }
  139. }
  140. else if (queueCmd.id / 6 + 1 == Robot.robot2.id)
  141. {
  142. if ((queueCmd.commandType == 's' && Robot.robot2.parkWaitCount >= 1) || (queueCmd.commandType == 'f' && Robot.robot2.fetchWaitCount >= 1))
  143. {
  144. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "等待机械手2资源线程过多:" + Robot.robot2.parkWaitCount + "," + Robot.robot2.fetchWaitCount);
  145. queueCmd.returnedCount = 10000;
  146. queuingThread.SetMessage((Command)queueCmd.Clone());
  147. Thread.Sleep(15000);
  148. continue;
  149. }
  150. }
  151. break;
  152. }
  153. }Thread.Sleep(2000);
  154. }
  155. });
  156. Task.WaitAll(getCmd);
  157. //等待系统总状态
  158. Task getSysStatus = Task.Factory.StartNew(() =>
  159. {
  160. GetTotalStatus();
  161. });
  162. Task.WaitAll(getSysStatus);
  163. //对依赖同一机械手的线程计数
  164. if (queueCmd.id / 6 + 1 == Robot.robot1.id)
  165. {
  166. if (queueCmd.commandType == 's')
  167. {
  168. Robot.robot1.parkWaitCount += 1;
  169. }
  170. else if (queueCmd.commandType == 'f')
  171. {
  172. Robot.robot1.fetchWaitCount += 1;
  173. }
  174. }
  175. else if (queueCmd.id / 6 + 1 == Robot.robot2.id)
  176. {
  177. if (queueCmd.commandType == 's')
  178. {
  179. Robot.robot2.parkWaitCount += 1;
  180. }
  181. else if (queueCmd.commandType == 'f')
  182. {
  183. Robot.robot2.fetchWaitCount += 1;
  184. }
  185. }
  186. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "准备,等待机械手1资源停取线程计数:" + Robot.robot1.parkWaitCount + "," + Robot.robot1.fetchWaitCount);
  187. Task t = Task.Factory.StartNew(() =>
  188. {
  189. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "开始");
  190. SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
  191. AbstractCmd abstractCmd = simpleCMDFactory.createCmd(queueCmd);
  192. abstractCmd.executeCmd(queueCmd);
  193. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "完成");
  194. });
  195. if (t != null)
  196. {
  197. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "状态:" + t.Status);
  198. }
  199. else
  200. {
  201. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "---线程创建:" + queueCmd.LicenseNum + "为空");
  202. }
  203. }
  204. Thread.Sleep(300);
  205. }
  206. }
  207. /// <summary>挂起</summary>
  208. public void hangUp()
  209. {
  210. }
  211. /// <summary>
  212. /// 获取PLC总控状态
  213. /// </summary>
  214. private void GetTotalStatus()
  215. {
  216. PLCMessage PLCMsg = null;
  217. int normalStatus = 0;
  218. while (!isClosing)
  219. {
  220. PLCMsg = (PLCMessage)PLC.GetMessage();
  221. if (PLCMsg != null && PLCMsg.originalPlcList != null && PLCMsg.originalPlcList.Count != 0)
  222. {
  223. normalStatus = Convert.ToInt32(PLCMsg.originalPlcList[equipmentStatus_address].Value);
  224. }
  225. //设备总控状态
  226. if (normalStatus == 1 && EntityForCore.ins.globalStatus)
  227. {
  228. UILogServer.ins.log("设备总状态正常");
  229. Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "设备总状态正常");
  230. break;
  231. }
  232. Thread.Sleep(200);
  233. }
  234. }
  235. /// <summary>
  236. /// 执行日志文件数据库操作
  237. /// </summary>
  238. /// <param name="o"></param>
  239. private void displayLog(object o)
  240. {
  241. while (!EntityForCore.ins.globalStatus)
  242. {
  243. Thread.Sleep(1000);
  244. }
  245. lock (Parking_Space.spaceLock)
  246. {
  247. string sqlStatus;
  248. string sqlMsg;
  249. int count;
  250. Log.ReadLog(out sqlStatus, out sqlMsg, out count);
  251. List<string> strs = new List<string>();
  252. strs.Add(sqlMsg);
  253. if (count > 0)
  254. {
  255. if (sqlStatus == "1")
  256. {
  257. int parkingRecordsID = 0;
  258. try
  259. {
  260. //Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out parkingRecordsID);
  261. parkingRecordsID = DBOperation.InsertParkingRecords(EntityForCore.remoteBQ, strs);
  262. if (parkingRecordsID == 0)
  263. {
  264. Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
  265. }
  266. }
  267. catch
  268. {
  269. Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
  270. //throw;
  271. }
  272. }
  273. if (sqlStatus == "0")
  274. {
  275. bool result;
  276. try
  277. {
  278. //Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out temp);
  279. result = DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
  280. if (!result)
  281. {
  282. Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
  283. }
  284. }
  285. catch
  286. {
  287. Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
  288. //throw;
  289. }
  290. }
  291. Log.ReadLog(out sqlStatus, out sqlMsg, out count);
  292. }
  293. }
  294. }
  295. private void CheckRemotePool(object o)
  296. {
  297. ConnectionPoolManager.CheckConnPooling(3, 10, ConnectionPoolManager.remoteConf, EntityForCore.remoteBQ);
  298. }
  299. private void CheckLocalPool(object o)
  300. {
  301. ConnectionPoolManager.CheckConnPooling(3, 10, ConnectionPoolManager.localConf, EntityForCore.localBQ);
  302. }
  303. private void KillTimeoutConnection(object o)
  304. {
  305. DBOperation.KillTimeOutConnection(null, 3600, EntityForCore.remoteBQ);
  306. }
  307. }
  308. }