123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- 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
- {
- /// <summary>
- /// 任务队列
- /// </summary>
- public static Dictionary<string, ControlTask> dicTask = new Dictionary<string, ControlTask>();
- private static int? controlInterval;
- /// <summary>
- /// 用户操控时间间隔
- /// </summary>
- 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
- /// <summary>
- /// 队列添加元素
- /// </summary>
- /// <param name="windturbineIdList">风机编号</param>
- /// <param name="stationId">风场</param>
- /// <param name="type">类型</param>
- /// <param name="user">用户</param>
- /// <param name="adjustmentValue">遥调值:限功率,限转速使用</param>
- public static void QueueAdd(IList<string> windturbineIdList, string stationId, CmdType type, user user, double? adjustmentValue = null)
- {
- if (!adjustmentValue.HasValue)
- {
- using (wisdom_cs_entity ctx = new wisdom_cs_entity())
- {
- IList<windturbine> list =
- ctx.windturbine.Where(s => windturbineIdList.Contains(s.ID) && s.WINDPOWERSTATIONID == stationId)
- .ToList();
- IList<gycp_cmd_info> 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<windturbine> list =
- ctx.windturbine.Where(s => windturbineIdList.Contains(s.ID) && s.WINDPOWERSTATIONID == stationId)
- .ToList();
- IList<gycp_cmd_info> 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);
- }
- }
- }
- }
- }
- /// <summary>
- /// 队列移除元素(任务已经发送结束,且发送完毕时间超过设置的控制时间间隔)
- /// </summary>
- public static void QueueRemove()
- {
- IList<string> 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();
- }
- }
- }
- }
- }
|