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; /// /// 发送命令间隔,主要针对105机型 /// 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 dicTask = TaskQueueSvc.dicTask; IList keyList = dicTask.Keys.ToList(); IList sendTaskListUp82 = new List(); IList sendControlTaskListUp105 = new List(); IList sendResetTaskListUp105 = new List(); 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); } } /// /// Up82机型命令发送 /// /// 发送任务集合 public static void SendCmd_Up82(IList sendTaskList) { Dictionary> dic = GroupByIntPtr(sendTaskList); IList keyList = dic.Keys.ToList(); for (int i = 0; i < keyList.Count; i++) { IList 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"); } } /// /// Up105机型命令发送 /// /// 发送任务集合 /// 是否为发送重置命令操作,是:对应地址发送0,否:对应地址发送1 public static void SendCmd_Up105(IList sendTaskList, bool isRest) { Dictionary> dic = GroupByIntPtr(sendTaskList); IList keyList = dic.Keys.ToList(); for (int i = 0; i < keyList.Count; i++) { IList 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"); } } /// /// 根据句柄将任务集合分组 /// /// /// public static Dictionary> GroupByIntPtr(IList taskList) { Dictionary> dic = new Dictionary>(); IList ptrList = taskList.Select(s => s.CmdIntPtr).ToList().Distinct().ToList(); for (int i = 0; i < ptrList.Count; i++) { IList listData = taskList.Where(s => s.CmdIntPtr == ptrList[i]).ToList(); dic.Add(ptrList[i], listData); } return dic; } } }