123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using EntityDataSet;
- using IntelligentControlForsx.Service.WindturbineControl.Domain.Task;
- using log4net;
- namespace IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc
- {
- public class TaskSendSvc
- {
- private static ILog logger = LogManager.GetLogger("AppInfoLog");
- private static int? sendInterval;
- /// <summary>
- /// 发送命令间隔,主要针对105机型
- /// </summary>
- public static int SendInterval
- {
- get
- {
- if (!sendInterval.HasValue)
- {
- sendInterval = Convert.ToInt32(ConfigurationSettings.AppSettings["sendInterval"]);
- }
- return sendInterval.Value;
- }
- set { sendInterval = value; }
- }
- #region 单例构造方法
- private static TaskSendSvc svc = new TaskSendSvc();
- private TaskSendSvc()
- {
- }
- public static TaskSendSvc GetTaskSendSvc()
- {
- return svc;
- }
- #endregion
- public static void SendCmd()
- {
- while (true)
- {
- Thread.Sleep(500);
- TaskQueueSvc.QueueRemove();
- Dictionary<string, ControlTask> dicTask = TaskQueueSvc.dicTask;
- IList<string> keyList = dicTask.Keys.ToList();
- IList<ControlTask> sendTaskListUp82 = new List<ControlTask>();
- IList<ControlTask> sendControlTaskListUp105 = new List<ControlTask>();
- IList<ControlTask> sendResetTaskListUp105 = new List<ControlTask>();
- foreach (string key in keyList)
- {
- var task = dicTask[key];
- if (task.ModelId == "UP105-2000-S")
- {
- if (task.SendControlTime.HasValue && !task.SendResetTime.HasValue)//已发送控制命令,但未发送重置命令
- {
- DateTime timeNow = DateTime.Now;
- TimeSpan span = timeNow - task.SendControlTime.Value;
- if (span.TotalMilliseconds > SendInterval)
- sendResetTaskListUp105.Add(task);
- }
- else if (!task.SendControlTime.HasValue && !task.SendResetTime.HasValue) //控制命令和重置命令都未发送
- sendControlTaskListUp105.Add(task);
- }
- else
- {
- if (!task.SendControlTime.HasValue)
- sendTaskListUp82.Add(task);
- }
- }
- SendCmd_Up82(sendTaskListUp82);
- SendCmd_Up105(sendControlTaskListUp105, false);
- SendCmd_Up105(sendResetTaskListUp105, true);
- }
- }
- /// <summary>
- /// Up82机型命令发送
- /// </summary>
- /// <param name="sendTaskList">发送任务集合</param>
- public static void SendCmd_Up82(IList<ControlTask> sendTaskList)
- {
- Dictionary<IntPtr, IList<ControlTask>> dic = GroupByIntPtr(sendTaskList);
- IList<IntPtr> keyList = dic.Keys.ToList();
- for (int i = 0; i < keyList.Count; i++)
- {
- IList<ControlTask> list = dic[keyList[i]];
- byte[] typeArr = list.Select(s => s.GycpType).ToArray();
- uint[] addressArr = list.Select(s => (uint)s.CmdId).ToArray();
- float[] valueArr = list.Select(s => (float)s.CmdValue).ToArray();
- DateTime nowTime = DateTime.Now;
- for (int j = 0; j < list.Count; j++)
- {
- TaskQueueSvc.dicTask[list[j].WindturbineId].SendControlTime = nowTime;
- }
- logger.Info("发送指令UP82");
- CmdSendService.gycp_control(keyList[i], typeArr, addressArr, valueArr, (uint)addressArr.Length);
- for (int j = 0; j < addressArr.Length; j++)
- {
- Console.WriteLine("====="+addressArr[j]);
- }
- logger.Info("发送结束UP82");
-
- }
- }
- /// <summary>
- /// Up105机型命令发送
- /// </summary>
- /// <param name="sendTaskList">发送任务集合</param>
- /// <param name="isRest">是否为发送重置命令操作,是:对应地址发送0,否:对应地址发送1</param>
- public static void SendCmd_Up105(IList<ControlTask> sendTaskList, bool isRest)
- {
- Dictionary<IntPtr, IList<ControlTask>> dic = GroupByIntPtr(sendTaskList);
- IList<IntPtr> keyList = dic.Keys.ToList();
- for (int i = 0; i < keyList.Count; i++)
- {
- IList<ControlTask> list = dic[keyList[i]];
- byte[] typeArr = list.Select(s => s.GycpType).ToArray();
- uint[] addressArr = list.Select(s => (uint)s.CmdId).ToArray();
- float[] valueArr = null;
- if (isRest)
- valueArr = list.Select(s => (float)0.0).ToArray();//如果是重置操作则命令值为0
- else
- valueArr = list.Select(s => (float)1.0).ToArray();//如果是命令操作则命令值为1
- DateTime nowTime = DateTime.Now;
- for (int j = 0; j < list.Count; j++)
- {
- if (isRest)
- TaskQueueSvc.dicTask[list[j].WindturbineId].SendResetTime = nowTime;
- else
- TaskQueueSvc.dicTask[list[j].WindturbineId].SendControlTime = nowTime;
- }
- logger.Info("发送指令UP105");
- CmdSendService.gycp_control(keyList[i], typeArr, addressArr, valueArr, (uint)addressArr.Length); ;
- logger.Info("发送指令UP105");
- }
- }
- /// <summary>
- /// 根据句柄将任务集合分组
- /// </summary>
- /// <param name="taskList"></param>
- /// <returns></returns>
- public static Dictionary<IntPtr, IList<ControlTask>> GroupByIntPtr(IList<ControlTask> taskList)
- {
- Dictionary<IntPtr, IList<ControlTask>> dic = new Dictionary<IntPtr, IList<ControlTask>>();
- IList<IntPtr> ptrList = taskList.Select(s => s.CmdIntPtr).ToList().Distinct().ToList();
- for (int i = 0; i < ptrList.Count; i++)
- {
- IList<ControlTask> listData = taskList.Where(s => s.CmdIntPtr == ptrList[i]).ToList();
- dic.Add(ptrList[i], listData);
- }
- return dic;
- }
- }
- }
|