123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading;
- using EntityDataSet;
- using IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd;
- namespace IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc
- {
- public class ControlSvc
- {
- private static ControlSvc svc = new ControlSvc();
- private ControlSvc()
- {
- }
- public static ControlSvc GetControlSvc()
- {
- return svc;
- }
- /// <summary>
- /// 发送控制指令--1.5MW机型
- /// </summary>
- /// <param name="intPtr">控制句柄</param>
- /// <param name="cmdList">控制信息集合</param>
- /// <param name="faultSendList">发送失败的集合</param>
- /// <returns>发送成功的风机Id集合</returns>
- public static IList<string> SendCmd_Up82(IntPtr intPtr, IList<WindturbineCmdInfo> cmdList, out IList<string> faultSendList)
- {
- byte[] typeArr = cmdList.Select(s => (byte)s.ControlType).ToArray();
- uint[] addressArr = cmdList.Select(s => (uint)s.CmdId).ToArray();
- float[] valueArr = cmdList.Select(s => s.CmdValue).ToArray();
- int count = CmdSendService.gycp_control(intPtr, typeArr, addressArr, valueArr, (uint)addressArr.Length);
- //获取发送失败的风机Id集合
- faultSendList = GetSendFaultWindtrubineIds();
- //收集并返回发送成功的风机Id集合
- IList<string> fault = faultSendList;
- return cmdList.Where(s => !fault.Contains(s.WindturbineId)).Select(k => k.WindturbineId).ToList();
- }
- /// <summary>
- /// 发送控制指令2MW机型
- /// </summary>
- /// <param name="intPtr">控制句柄</param>
- /// <param name="cmdList">控制信息集合</param>
- /// <param name="faultSendList">发送失败的集合</param>
- /// <returns>送成功的风机Id集合</returns>
- public static IList<string> SendCmd_Up105(IntPtr intPtr, IList<WindturbineCmdInfo> cmdList, out IList<string> faultSendList)
- {
- /*
- * 停机操作:停机地址发送1,停机地址发送0
- * 启动操作:复位地址发送1,复位地址发送0
- */
- byte[] typeArr = cmdList.Select(s => (byte)s.ControlType).ToArray();
- uint[] addressArr = cmdList.Select(s => (uint)s.CmdId).ToArray();
- float[] valueArr1 = cmdList.Select(s => s.CmdValue).ToArray();
- float[] valueArr0 = cmdList.Select(s => s.CmdValue).ToArray();
- int count1 = CmdSendService.gycp_control(intPtr, typeArr, addressArr, valueArr1, (uint)addressArr.Length);
- IList<string> faultSend1 = GetSendFaultWindtrubineIds();
- Thread.Sleep(800);
- int count0 = CmdSendService.gycp_control(intPtr, typeArr, addressArr, valueArr0, (uint)addressArr.Length);
- IList<string> faultSend0 = GetSendFaultWindtrubineIds();
- //将两次发送的命令失败集合合并(并集)并去重
- faultSendList = faultSend1.Union(faultSend0).ToList().Distinct().ToList();
- //收集并返回发送成功的风机Id集合
- IList<string> fault = faultSendList;
- return cmdList.Where(s => !fault.Contains(s.WindturbineId)).Select(k => k.WindturbineId).ToList();
- }
- /// <summary>
- /// 根据风机编号及其操作类型发送操作指令
- /// </summary>
- /// <param name="windturbinList">风机编号集合</param>
- /// <param name="stationId">风场编号</param>
- /// <param name="cmdType">操作类型</param>
- /// <param name="faultSendList">指令发送失败的风机集合</param>
- /// <param name="description">控制描述(手动,执行推荐)</param>
- /// <param name="user">操作人</param>
- /// <returns>控制指令是否全部发送成功</returns>
- public bool SendCmdByWindturbineIdList(IList<string> windturbinList, string stationId, CmdType cmdType, out IList<string> faultSendList, string description, user user)
- {
- faultSendList = new List<string>();
- using (wisdom_cs_entity ctx = new wisdom_cs_entity())
- {
- IList<windturbine> list =
- ctx.windturbine.Where(s => windturbinList.Contains(s.ID) && s.WINDPOWERSTATIONID == stationId)
- .ToList();
- IList<control_info> controlInfoListUp82 = new List<control_info>();
- IList<WindturbineCmdInfo> cmdInfoListUp82 = new List<WindturbineCmdInfo>();
- IList<control_info> controlInfoListUp105 = new List<control_info>();
- IList<WindturbineCmdInfo> cmdInfoListUp105 = new List<WindturbineCmdInfo>();
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i].MODELID != "UP105-2000-S")
- {
- #region 操控记录实体
- control_info info = new control_info();
- info.control_description = description;
- info.user_id = user.id;
- info.time = DateTime.Now;
- info.success_time = null;
- info.windturbine_id = list[i].ID;
- info.type = (int)cmdType;
- controlInfoListUp82.Add(info);
- #endregion
- #region 风机命令实体
- WindturbineCmdInfo cmdInfo = new WindturbineCmdInfo();
- if (list[i].GYCP_ID.HasValue)
- cmdInfo.CmdIntPtr = ControlIntPtr.dic[list[i].PROJECTID];
- cmdInfo.WindturbineId = list[i].ID;
- cmdInfo.CmdType = cmdType;
- cmdInfo.ControlType = CmdSendService.GYCP_TYPE_YK;
- int controlType = 0;
- if (cmdInfo.CmdType == CmdType.Start)
- controlType = 1; //启动
- else if (cmdInfo.CmdType == CmdType.Stop)
- controlType = 2; //停机
- else if (cmdInfo.CmdType == CmdType.Maintain)
- controlType = 3; //维护
- else if (cmdInfo.CmdType == CmdType.UnMaintain)
- controlType = 4; //取消维护
- else if (cmdInfo.CmdType == CmdType.Reset)
- controlType = 5; //复位
- //获取控制点
- string windturbineId = list[i].ID;
- gycp_cmd_info gycpCmdInfo =
- ctx.gycp_cmd_info.Where(
- s => s.type == controlType && s.windturbine_id == windturbineId)
- .FirstOrDefault();
- //获取控制地址
- gycp_address address = null;
- if (list[i].GYCP_ID.HasValue)
- {
- int gycpId = list[i].GYCP_ID.Value;
- address = ctx.gycp_address.Where(s => s.id == gycpId).FirstOrDefault();
- }
- if (address != null && gycpCmdInfo != null)
- {
- cmdInfo.CmdId = gycpCmdInfo.cmd_id;
- if (gycpCmdInfo.cmd_value.HasValue)
- cmdInfo.CmdValue = gycpCmdInfo.cmd_value.Value;
- }
- cmdInfo.UserId = user.id;
- cmdInfo.Time = DateTime.Now;
- cmdInfo.IsSuccessSend = false;
- cmdInfoListUp82.Add(cmdInfo);
- #endregion
- }
- else
- {
- #region 操控记录实体
- control_info info = new control_info();
- info.control_description = description;
- info.user_id = user.id;
- info.time = DateTime.Now;
- info.success_time = null;
- info.windturbine_id = list[i].ID;
- info.type = (int)cmdType;
- controlInfoListUp105.Add(info);
- #endregion
- #region 风机命令实体
- WindturbineCmdInfo cmdInfo = new WindturbineCmdInfo();
- if (list[i].GYCP_ID.HasValue)
- cmdInfo.CmdIntPtr = ControlIntPtr.dic[list[i].PROJECTID];
- cmdInfo.WindturbineId = list[i].ID;
- cmdInfo.CmdType = cmdType;
- cmdInfo.ControlType = CmdSendService.GYCP_TYPE_YK;
- int controlType = 0;
- if (cmdInfo.CmdType == CmdType.Start)
- controlType = 5; //启动
- else if (cmdInfo.CmdType == CmdType.Stop)
- controlType = 1; //停机
- //获取控制点
- string windturbineId = list[i].ID;
- gycp_cmd_info gycpCmdInfo =
- ctx.gycp_cmd_info.Where(
- s => s.type == controlType && s.windturbine_id == windturbineId)
- .FirstOrDefault();
- //获取控制地址
- gycp_address address = null;
- if (list[i].GYCP_ID.HasValue)
- {
- int gycpId = list[i].GYCP_ID.Value;
- address = ctx.gycp_address.Where(s => s.id == gycpId).FirstOrDefault();
- }
- if (address != null && gycpCmdInfo != null)
- {
- cmdInfo.CmdId = gycpCmdInfo.cmd_id;
- if (gycpCmdInfo.cmd_value.HasValue)
- cmdInfo.CmdValue = gycpCmdInfo.cmd_value.Value;
- }
- cmdInfo.UserId = user.id;
- cmdInfo.Time = DateTime.Now;
- cmdInfo.IsSuccessSend = false;
- cmdInfoListUp105.Add(cmdInfo);
- #endregion
- }
- }
- var groupResultUp82 = cmdInfoListUp82.GroupBy(x => x.CmdIntPtr);
- foreach (var info in groupResultUp82)
- {
- IList<string> falutList = new List<string>();
- List<WindturbineCmdInfo> itemList = info.ToList();
- IList<string> successWindturbineIdList = SendCmd_Up82(itemList[0].CmdIntPtr, itemList, out falutList);
- #region 保存命令执行成功的记录
- IList<control_info> successList =
- controlInfoListUp82.Where(s => successWindturbineIdList.Contains(s.windturbine_id)).ToList();
- for (int i = 0; i < successList.Count; i++)
- {
- ctx.Entry(successList[i]).State = EntityState.Added;
- ctx.SaveChanges();
- }
- #endregion
- faultSendList = faultSendList.Union(falutList).ToList();//收集命令发送失败的风机集合
- }
- var groupResultUp105 = cmdInfoListUp105.GroupBy(x => x.CmdIntPtr);
- foreach (var info in groupResultUp105)
- {
- IList<string> falutList = new List<string>();
- List<WindturbineCmdInfo> itemList = info.ToList();
- IList<string> successWindturbineIdList = SendCmd_Up105(itemList[0].CmdIntPtr, itemList, out falutList);
- #region 保存命令执行成功的记录
- IList<control_info> successList =
- controlInfoListUp105.Where(s => successWindturbineIdList.Contains(s.windturbine_id)).ToList();
- for (int i = 0; i < successList.Count; i++)
- {
- ctx.Entry(successList[i]).State = EntityState.Added;
- ctx.SaveChanges();
- }
- #endregion
- faultSendList = faultSendList.Union(falutList).ToList();//收集命令发送失败的风机集合
- }
- if (faultSendList.Count <= 0)
- return true;
- else
- return false;
- }
- }
- /// <summary>
- /// 根据回调函数返回的数据得到命令发送失败的风机Id集合
- /// </summary>
- /// <returns></returns>
- public static IList<String> GetSendFaultWindtrubineIds()
- {
- IList<String> list = new List<string>();
- ControlResult result = ControlIntPtr.result;
- if (result != null)
- {
- for (int i = 0; i < result.Errors.Length; i++)
- {
- //错误值为1时代表发送失败
- if (result.Errors[i] == 1)
- {
- using (wisdom_cs_entity ctx = new wisdom_cs_entity())
- {
- gycp_cmd_info info =
- ctx.gycp_cmd_info.Where(s => s.cmd_id == result.Addresses[i]).FirstOrDefault();
- if (info != null)
- list.Add(info.windturbine_id);
- }
- }
- }
- ControlIntPtr.result = new ControlResult();
- }
- return list;
- }
- }
- }
|