TaskSendSvc.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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;
  8. using System.Threading.Tasks;
  9. using EntityDataSet;
  10. using IntelligentControlForsx.Service.WindturbineControl.Domain.Task;
  11. using log4net;
  12. namespace IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc
  13. {
  14. public class TaskSendSvc
  15. {
  16. private static ILog logger = LogManager.GetLogger("AppInfoLog");
  17. private static int? sendInterval;
  18. /// <summary>
  19. /// 发送命令间隔,主要针对105机型
  20. /// </summary>
  21. public static int SendInterval
  22. {
  23. get
  24. {
  25. if (!sendInterval.HasValue)
  26. {
  27. sendInterval = Convert.ToInt32(ConfigurationSettings.AppSettings["sendInterval"]);
  28. }
  29. return sendInterval.Value;
  30. }
  31. set { sendInterval = value; }
  32. }
  33. #region 单例构造方法
  34. private static TaskSendSvc svc = new TaskSendSvc();
  35. private TaskSendSvc()
  36. {
  37. }
  38. public static TaskSendSvc GetTaskSendSvc()
  39. {
  40. return svc;
  41. }
  42. #endregion
  43. public static void SendCmd()
  44. {
  45. while (true)
  46. {
  47. Thread.Sleep(500);
  48. TaskQueueSvc.QueueRemove();
  49. Dictionary<string, ControlTask> dicTask = TaskQueueSvc.dicTask;
  50. IList<string> keyList = dicTask.Keys.ToList();
  51. IList<ControlTask> sendTaskListUp82 = new List<ControlTask>();
  52. IList<ControlTask> sendControlTaskListUp105 = new List<ControlTask>();
  53. IList<ControlTask> sendResetTaskListUp105 = new List<ControlTask>();
  54. foreach (string key in keyList)
  55. {
  56. var task = dicTask[key];
  57. if (task.ModelId == "UP105-2000-S")
  58. {
  59. if (task.SendControlTime.HasValue && !task.SendResetTime.HasValue)//已发送控制命令,但未发送重置命令
  60. {
  61. DateTime timeNow = DateTime.Now;
  62. TimeSpan span = timeNow - task.SendControlTime.Value;
  63. if (span.TotalMilliseconds > SendInterval)
  64. sendResetTaskListUp105.Add(task);
  65. }
  66. else if (!task.SendControlTime.HasValue && !task.SendResetTime.HasValue) //控制命令和重置命令都未发送
  67. sendControlTaskListUp105.Add(task);
  68. }
  69. else
  70. {
  71. if (!task.SendControlTime.HasValue)
  72. sendTaskListUp82.Add(task);
  73. }
  74. }
  75. SendCmd_Up82(sendTaskListUp82);
  76. SendCmd_Up105(sendControlTaskListUp105, false);
  77. SendCmd_Up105(sendResetTaskListUp105, true);
  78. }
  79. }
  80. /// <summary>
  81. /// Up82机型命令发送
  82. /// </summary>
  83. /// <param name="sendTaskList">发送任务集合</param>
  84. public static void SendCmd_Up82(IList<ControlTask> sendTaskList)
  85. {
  86. Dictionary<IntPtr, IList<ControlTask>> dic = GroupByIntPtr(sendTaskList);
  87. IList<IntPtr> keyList = dic.Keys.ToList();
  88. for (int i = 0; i < keyList.Count; i++)
  89. {
  90. IList<ControlTask> list = dic[keyList[i]];
  91. byte[] typeArr = list.Select(s => s.GycpType).ToArray();
  92. uint[] addressArr = list.Select(s => (uint)s.CmdId).ToArray();
  93. float[] valueArr = list.Select(s => (float)s.CmdValue).ToArray();
  94. DateTime nowTime = DateTime.Now;
  95. for (int j = 0; j < list.Count; j++)
  96. {
  97. TaskQueueSvc.dicTask[list[j].WindturbineId].SendControlTime = nowTime;
  98. }
  99. logger.Info("发送指令UP82");
  100. CmdSendService.gycp_control(keyList[i], typeArr, addressArr, valueArr, (uint)addressArr.Length);
  101. for (int j = 0; j < addressArr.Length; j++)
  102. {
  103. Console.WriteLine("====="+addressArr[j]);
  104. }
  105. logger.Info("发送结束UP82");
  106. }
  107. }
  108. /// <summary>
  109. /// Up105机型命令发送
  110. /// </summary>
  111. /// <param name="sendTaskList">发送任务集合</param>
  112. /// <param name="isRest">是否为发送重置命令操作,是:对应地址发送0,否:对应地址发送1</param>
  113. public static void SendCmd_Up105(IList<ControlTask> sendTaskList, bool isRest)
  114. {
  115. Dictionary<IntPtr, IList<ControlTask>> dic = GroupByIntPtr(sendTaskList);
  116. IList<IntPtr> keyList = dic.Keys.ToList();
  117. for (int i = 0; i < keyList.Count; i++)
  118. {
  119. IList<ControlTask> list = dic[keyList[i]];
  120. byte[] typeArr = list.Select(s => s.GycpType).ToArray();
  121. uint[] addressArr = list.Select(s => (uint)s.CmdId).ToArray();
  122. float[] valueArr = null;
  123. if (isRest)
  124. valueArr = list.Select(s => (float)0.0).ToArray();//如果是重置操作则命令值为0
  125. else
  126. valueArr = list.Select(s => (float)1.0).ToArray();//如果是命令操作则命令值为1
  127. DateTime nowTime = DateTime.Now;
  128. for (int j = 0; j < list.Count; j++)
  129. {
  130. if (isRest)
  131. TaskQueueSvc.dicTask[list[j].WindturbineId].SendResetTime = nowTime;
  132. else
  133. TaskQueueSvc.dicTask[list[j].WindturbineId].SendControlTime = nowTime;
  134. }
  135. logger.Info("发送指令UP105");
  136. CmdSendService.gycp_control(keyList[i], typeArr, addressArr, valueArr, (uint)addressArr.Length); ;
  137. logger.Info("发送指令UP105");
  138. }
  139. }
  140. /// <summary>
  141. /// 根据句柄将任务集合分组
  142. /// </summary>
  143. /// <param name="taskList"></param>
  144. /// <returns></returns>
  145. public static Dictionary<IntPtr, IList<ControlTask>> GroupByIntPtr(IList<ControlTask> taskList)
  146. {
  147. Dictionary<IntPtr, IList<ControlTask>> dic = new Dictionary<IntPtr, IList<ControlTask>>();
  148. IList<IntPtr> ptrList = taskList.Select(s => s.CmdIntPtr).ToList().Distinct().ToList();
  149. for (int i = 0; i < ptrList.Count; i++)
  150. {
  151. IList<ControlTask> listData = taskList.Where(s => s.CmdIntPtr == ptrList[i]).ToList();
  152. dic.Add(ptrList[i], listData);
  153. }
  154. return dic;
  155. }
  156. }
  157. }