using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using EntityDataSet; using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd; using IntelligentControlForsx.Service.WindturbineControl.Domain.Task; namespace IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc { public class TaskQueueSvc { /// /// 任务队列 /// public static Dictionary dicTask = new Dictionary(); private static int? controlInterval; /// /// 用户操控时间间隔 /// public static int ControlInterval { get { if (!controlInterval.HasValue) { controlInterval = Convert.ToInt32(ConfigurationSettings.AppSettings["controlInterval"]); } return controlInterval.Value; } set { controlInterval = value; } } #region 单例方法 private static TaskQueueSvc svc = new TaskQueueSvc(); private TaskQueueSvc() { } public static TaskQueueSvc GetTaskQueueSvc() { return svc; } #endregion /// /// 队列添加元素 /// /// 风机编号 /// 风场 /// 类型 /// 用户 /// 遥调值:限功率,限转速使用 public static void QueueAdd(IList windturbineIdList, string stationId, CmdType type, user user, double? adjustmentValue = null) { if (!adjustmentValue.HasValue) { using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { IList list = ctx.windturbine.Where(s => windturbineIdList.Contains(s.ID) && s.WINDPOWERSTATIONID == stationId) .ToList(); IList cmdList = ctx.gycp_cmd_info.Where(s => windturbineIdList.Contains(s.windturbine_id)).ToList(); for (int i = 0; i < list.Count; i++) { ControlTask taskData = new ControlTask(); taskData.WindturbineId = list[i].ID; taskData.StationId = stationId; taskData.ModelId = list[i].MODELID; taskData.ControlType = type; taskData.ControlUserId = user.id; taskData.ControlUserName = user.name; taskData.CreateTime = DateTime.Now; taskData.GycpType = CmdSendService.GYCP_TYPE_YK;//遥控 taskData.CmdIntPtr = ControlIntPtr.dic[list[i].PROJECTID]; if (list[i].MODELID == "UP105-2000-S") { if (type == CmdType.Start) { gycp_cmd_info cmdInfo = cmdList.Where( s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(CmdType.Reset)) .FirstOrDefault(); if (cmdInfo != null) taskData.CmdId = cmdInfo.cmd_id; } else if (type == CmdType.Stop) { gycp_cmd_info cmdInfo = cmdList.Where( s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(CmdType.Start)) .FirstOrDefault(); if (cmdInfo != null) taskData.CmdId = cmdInfo.cmd_id; } } else { gycp_cmd_info cmdInfo = cmdList.Where( s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(type)) .FirstOrDefault(); if (cmdInfo != null) { taskData.CmdId = cmdInfo.cmd_id; taskData.CmdValue = cmdInfo.cmd_value.HasValue ? cmdInfo.cmd_value.Value : 1; } } if (!dicTask.ContainsKey(list[i].ID)) { #region 记录操作日志写入数据库,后续更新 control_log info = new control_log(); info.windturbine_id = taskData.WindturbineId; info.model_id = taskData.ModelId; info.station_id = taskData.StationId; info.control_type = (int)taskData.ControlType; info.cmd_id = taskData.CmdId; info.cmd_value = taskData.CmdValue; info.control_user_id = taskData.ControlUserId; info.send_control_time = DateTime.Now; ctx.Entry(info).State = EntityState.Added; int id = ctx.SaveChanges(); if (id > 0 && info.id != 0) { taskData.LogId = info.id; } #endregion //将任务添加到队列 dicTask.Add(list[i].ID, taskData); } } } } else { using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { IList list = ctx.windturbine.Where(s => windturbineIdList.Contains(s.ID) && s.WINDPOWERSTATIONID == stationId) .ToList(); IList cmdList = ctx.gycp_cmd_info.Where(s => windturbineIdList.Contains(s.windturbine_id)).ToList(); for (int i = 0; i < list.Count; i++) { ControlTask taskData = new ControlTask(); taskData.WindturbineId = list[i].ID; taskData.StationId = stationId; taskData.ModelId = list[i].MODELID; taskData.ControlType = type; taskData.ControlUserId = user.id; taskData.ControlUserName = user.name; taskData.CreateTime = DateTime.Now; taskData.GycpType = CmdSendService.GYCP_TYPE_YT;//遥调 //获取对应的控制命令句柄 taskData.CmdIntPtr = ControlIntPtr.dic[list[i].PROJECTID]; gycp_cmd_info cmdInfo = cmdList.Where( s => s.windturbine_id == list[i].ID && s.type == Convert.ToInt32(type)) .FirstOrDefault(); if (cmdInfo != null) { taskData.CmdId = cmdInfo.cmd_id; taskData.CmdValue = adjustmentValue.Value; } if (!dicTask.ContainsKey(list[i].ID)) { #region 记录操作日志写入数据库,后续更新 control_log info = new control_log(); info.windturbine_id = taskData.WindturbineId; info.model_id = taskData.ModelId; info.station_id = taskData.StationId; info.control_type = (int)taskData.ControlType; info.cmd_id = taskData.CmdId; info.cmd_value = taskData.CmdValue; info.control_user_id = taskData.ControlUserId; info.send_control_time = DateTime.Now; ctx.Entry(info).State = EntityState.Added; int id = ctx.SaveChanges(); if (id > 0 && info.id != 0) { taskData.LogId = info.id; } #endregion //将任务添加到队列 dicTask.Add(list[i].ID, taskData); } } } } } /// /// 队列移除元素(任务已经发送结束,且发送完毕时间超过设置的控制时间间隔) /// public static void QueueRemove() { IList keyList = dicTask.Keys.ToList(); for (int i = 0; i < keyList.Count; i++) { ControlTask task = dicTask[keyList[i]]; #region 将控制任务记录写入数据库 if (task.ModelId == "UP105-2000-S") { //控制命令时间不为空 //重置命令时间不为空 //控制命令错误码不为空 //重置错误码不为空 //以上代表控制命令已经下发结束 if (task.SendControlTime.HasValue && task.SendResetTime.HasValue && task.SendControlErrorInfo.HasValue && task.SendResetlErrorInfo.HasValue) { if (task.IsSave == false) { task.IsSave = true; } } } else { //控制命令时间不为空 //控制命令错误码不为空 //以上代表控制命令已经下发结束 if (task.SendControlTime.HasValue && task.SendControlErrorInfo.HasValue) { if (task.IsSave == false) { task.IsSave = true; } } } #endregion DateTime timeNow = DateTime.Now; TimeSpan span = timeNow - task.CreateTime; if (span.TotalMilliseconds > ControlInterval) { SaveLog(task); dicTask.Remove(keyList[i]); } } } private static void SaveLog(ControlTask task) { using (wisdom_cs_entity ctx = new wisdom_cs_entity()) { control_log info = ctx.control_log.Where(s => s.id == task.LogId).FirstOrDefault(); if (info != null) { info.is_send_over = true; ctx.Entry(info).State = EntityState.Modified; ctx.SaveChanges(); } } } } }