TaskQueueSvc.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using EntityDataSet;
  9. using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd;
  10. using IntelligentControlForsx.Service.WindturbineControl.Domain.Task;
  11. namespace IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc
  12. {
  13. public class TaskQueueSvc
  14. {
  15. /// <summary>
  16. /// 任务队列
  17. /// </summary>
  18. public static Dictionary<string, ControlTask> dicTask = new Dictionary<string, ControlTask>();
  19. private static int? controlInterval;
  20. /// <summary>
  21. /// 用户操控时间间隔
  22. /// </summary>
  23. public static int ControlInterval
  24. {
  25. get
  26. {
  27. if (!controlInterval.HasValue)
  28. {
  29. controlInterval = Convert.ToInt32(ConfigurationSettings.AppSettings["controlInterval"]);
  30. }
  31. return controlInterval.Value;
  32. }
  33. set { controlInterval = value; }
  34. }
  35. #region 单例方法
  36. private static TaskQueueSvc svc = new TaskQueueSvc();
  37. private TaskQueueSvc()
  38. {
  39. }
  40. public static TaskQueueSvc GetTaskQueueSvc()
  41. {
  42. return svc;
  43. }
  44. #endregion
  45. /// <summary>
  46. /// 队列添加元素
  47. /// </summary>
  48. /// <param name="windturbineIdList">风机编号</param>
  49. /// <param name="stationId">风场</param>
  50. /// <param name="type">类型</param>
  51. /// <param name="user">用户</param>
  52. /// <param name="adjustmentValue">遥调值:限功率,限转速使用</param>
  53. public static void QueueAdd(IList<string> windturbineIdList, string stationId, CmdType type, user user, double? adjustmentValue = null)
  54. {
  55. if (!adjustmentValue.HasValue)
  56. {
  57. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  58. {
  59. IList<windturbine> list =
  60. ctx.windturbine.Where(s => windturbineIdList.Contains(s.ID) && s.WINDPOWERSTATIONID == stationId)
  61. .ToList();
  62. IList<gycp_cmd_info> cmdList =
  63. ctx.gycp_cmd_info.Where(s => windturbineIdList.Contains(s.windturbine_id)).ToList();
  64. for (int i = 0; i < list.Count; i++)
  65. {
  66. ControlTask taskData = new ControlTask();
  67. taskData.WindturbineId = list[i].ID;
  68. taskData.StationId = stationId;
  69. taskData.ModelId = list[i].MODELID;
  70. taskData.ControlType = type;
  71. taskData.ControlUserId = user.id;
  72. taskData.ControlUserName = user.name;
  73. taskData.CreateTime = DateTime.Now;
  74. taskData.GycpType = CmdSendService.GYCP_TYPE_YK;//遥控
  75. taskData.CmdIntPtr = ControlIntPtr.dic[list[i].PROJECTID];
  76. if (list[i].MODELID == "UP105-2000-S")
  77. {
  78. if (type == CmdType.Start)
  79. {
  80. gycp_cmd_info cmdInfo =
  81. cmdList.Where(
  82. s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(CmdType.Reset))
  83. .FirstOrDefault();
  84. if (cmdInfo != null)
  85. taskData.CmdId = cmdInfo.cmd_id;
  86. }
  87. else if (type == CmdType.Stop)
  88. {
  89. gycp_cmd_info cmdInfo =
  90. cmdList.Where(
  91. s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(CmdType.Start))
  92. .FirstOrDefault();
  93. if (cmdInfo != null)
  94. taskData.CmdId = cmdInfo.cmd_id;
  95. }
  96. }
  97. else
  98. {
  99. gycp_cmd_info cmdInfo =
  100. cmdList.Where(
  101. s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(type))
  102. .FirstOrDefault();
  103. if (cmdInfo != null)
  104. {
  105. taskData.CmdId = cmdInfo.cmd_id;
  106. taskData.CmdValue = cmdInfo.cmd_value.HasValue ? cmdInfo.cmd_value.Value : 1;
  107. }
  108. }
  109. if (!dicTask.ContainsKey(list[i].ID))
  110. {
  111. #region 记录操作日志写入数据库,后续更新
  112. control_log info = new control_log();
  113. info.windturbine_id = taskData.WindturbineId;
  114. info.model_id = taskData.ModelId;
  115. info.station_id = taskData.StationId;
  116. info.control_type = (int)taskData.ControlType;
  117. info.cmd_id = taskData.CmdId;
  118. info.cmd_value = taskData.CmdValue;
  119. info.control_user_id = taskData.ControlUserId;
  120. info.send_control_time = DateTime.Now;
  121. ctx.Entry(info).State = EntityState.Added;
  122. int id = ctx.SaveChanges();
  123. if (id > 0 && info.id != 0)
  124. {
  125. taskData.LogId = info.id;
  126. }
  127. #endregion
  128. //将任务添加到队列
  129. dicTask.Add(list[i].ID, taskData);
  130. }
  131. }
  132. }
  133. }
  134. else
  135. {
  136. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  137. {
  138. IList<windturbine> list =
  139. ctx.windturbine.Where(s => windturbineIdList.Contains(s.ID) && s.WINDPOWERSTATIONID == stationId)
  140. .ToList();
  141. IList<gycp_cmd_info> cmdList =
  142. ctx.gycp_cmd_info.Where(s => windturbineIdList.Contains(s.windturbine_id)).ToList();
  143. for (int i = 0; i < list.Count; i++)
  144. {
  145. ControlTask taskData = new ControlTask();
  146. taskData.WindturbineId = list[i].ID;
  147. taskData.StationId = stationId;
  148. taskData.ModelId = list[i].MODELID;
  149. taskData.ControlType = type;
  150. taskData.ControlUserId = user.id;
  151. taskData.ControlUserName = user.name;
  152. taskData.CreateTime = DateTime.Now;
  153. taskData.GycpType = CmdSendService.GYCP_TYPE_YT;//遥调
  154. //获取对应的控制命令句柄
  155. taskData.CmdIntPtr = ControlIntPtr.dic[list[i].PROJECTID];
  156. gycp_cmd_info cmdInfo =
  157. cmdList.Where(
  158. s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(type))
  159. .FirstOrDefault();
  160. if (cmdInfo != null)
  161. {
  162. taskData.CmdId = cmdInfo.cmd_id;
  163. taskData.CmdValue = adjustmentValue.Value;
  164. }
  165. if (!dicTask.ContainsKey(list[i].ID))
  166. {
  167. #region 记录操作日志写入数据库,后续更新
  168. control_log info = new control_log();
  169. info.windturbine_id = taskData.WindturbineId;
  170. info.model_id = taskData.ModelId;
  171. info.station_id = taskData.StationId;
  172. info.control_type = (int)taskData.ControlType;
  173. info.cmd_id = taskData.CmdId;
  174. info.cmd_value = taskData.CmdValue;
  175. info.control_user_id = taskData.ControlUserId;
  176. info.send_control_time = DateTime.Now;
  177. ctx.Entry(info).State = EntityState.Added;
  178. int id = ctx.SaveChanges();
  179. if (id > 0 && info.id != 0)
  180. {
  181. taskData.LogId = info.id;
  182. }
  183. #endregion
  184. //将任务添加到队列
  185. dicTask.Add(list[i].ID, taskData);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// 队列移除元素(任务已经发送结束,且发送完毕时间超过设置的控制时间间隔)
  193. /// </summary>
  194. public static void QueueRemove()
  195. {
  196. IList<string> keyList = dicTask.Keys.ToList();
  197. for (int i = 0; i < keyList.Count; i++)
  198. {
  199. ControlTask task = dicTask[keyList[i]];
  200. #region 将控制任务记录写入数据库
  201. if (task.ModelId == "UP105-2000-S")
  202. {
  203. //控制命令时间不为空
  204. //重置命令时间不为空
  205. //控制命令错误码不为空
  206. //重置错误码不为空
  207. //以上代表控制命令已经下发结束
  208. if (task.SendControlTime.HasValue && task.SendResetTime.HasValue && task.SendControlErrorInfo.HasValue && task.SendResetlErrorInfo.HasValue)
  209. {
  210. if (task.IsSave == false)
  211. {
  212. task.IsSave = true;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. //控制命令时间不为空
  219. //控制命令错误码不为空
  220. //以上代表控制命令已经下发结束
  221. if (task.SendControlTime.HasValue && task.SendControlErrorInfo.HasValue)
  222. {
  223. if (task.IsSave == false)
  224. {
  225. task.IsSave = true;
  226. }
  227. }
  228. }
  229. #endregion
  230. DateTime timeNow = DateTime.Now;
  231. TimeSpan span = timeNow - task.CreateTime;
  232. if (span.TotalMilliseconds > ControlInterval)
  233. {
  234. SaveLog(task);
  235. dicTask.Remove(keyList[i]);
  236. }
  237. }
  238. }
  239. private static void SaveLog(ControlTask task)
  240. {
  241. using (wisdom_cs_entity ctx = new wisdom_cs_entity())
  242. {
  243. control_log info = ctx.control_log.Where(s => s.id == task.LogId).FirstOrDefault();
  244. if (info != null)
  245. {
  246. info.is_send_over = true;
  247. ctx.Entry(info).State = EntityState.Modified;
  248. ctx.SaveChanges();
  249. }
  250. }
  251. }
  252. }
  253. }