123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using NEIntelligentControl2.Models;
- using NEIntelligentControl2.Models.Messages;
- using NEIntelligentControl2.Models.Windturbine;
- using NEIntelligentControl2.Service.User;
- using NEIntelligentControl2.Service.WebSocket;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace NEIntelligentControl2.Service.Windturbine
- {
- /// <summary>
- /// 风机控制服务
- /// </summary>
- public class ControlManager
- {
- private WEBHelper _WEBHelper;
- private UrlManager _UrlManager;
- private string _Url;
- private string[] _ErrorInfo;
- public ControlManager(WEBHelper web, UrlManager urlm)
- {
- _WEBHelper = web;
- _UrlManager = urlm;
- _Url = _UrlManager.ServicePath;
- InitErrorCode();
- }
- private void InitErrorCode()
- {
- _ErrorInfo = new string[]
- {
- "控制成功",
- "控制命令发送失败",
- "无效的控制地址",
- "被控设备异常",
- "无效的控制功能",
- "网络未连接",
- "命令已发送,但是读取结果超时",
- "发送指令失败",
- "控制的命令出错",
- "未知错误",
- "未知错误",
- "未知错误",
- "风机操作过频繁",
- "风机被挂牌",
- "风机操作与风机状态不符"
- };
- }
- internal string SendLock(Dictionary<string, ControlInstruction> cis)
- {
- try
- {
- var cc = _WEBHelper.HttpPostBody<Dictionary<string, ControlInstruction>>($"{_Url}/api/control/lock", cis);
- StringBuilder sb = new StringBuilder();
- foreach (var v in cc)
- {
- if (v.Value.ErrorCode != "0")
- {
- int.TryParse(v.Value.ErrorCode, out int i);
- if (i < _ErrorInfo.Length)
- {
- sb.Append(v.Value.WindturbineId).Append("挂牌失败:").Append(_ErrorInfo[i]).Append("\n");
- }
- }
- }
- return sb.ToString();
- }
- catch { }
- return null;
- }
- /// <summary>
- /// 发送指令,不检查用户
- /// </summary>
- /// <returns></returns>
- public string SendCmdNotCheck(Dictionary<string, ControlInstruction> cis)
- {
- try
- {
- var cc = _WEBHelper.HttpPostBody<Dictionary<string, ControlInstruction>>($"{_Url}/api/control/", cis);
- StringBuilder sb = new StringBuilder();
- foreach (var v in cc)
- {
- if (v.Value.ErrorCode != "0")
- {
- bool b = int.TryParse(v.Value.ErrorCode, out int i);
- if (!b) i = 9;
- if (i < _ErrorInfo.Length)
- {
- sb.Append(v.Value.WindturbineId).Append("控制失败:").Append(_ErrorInfo[i]).Append("\n");
- }
- }
- }
- return sb.ToString();
- }
- catch { }
- return null;
- }
- public string SendCmd(Dictionary<string, ControlInstruction> cis)
- {
- try
- {
- var cc = _WEBHelper.HttpPostBody<Dictionary<string, ControlInstruction>>($"{_Url}/api/control/", cis);
- StringBuilder sb = new StringBuilder();
- foreach (var v in cc)
- {
- if (v.Value.ErrorCode != "0")
- {
- bool b = int.TryParse(v.Value.ErrorCode, out int i);
- i = !b ? 9 : i;
- if (i < _ErrorInfo.Length)
- {
- sb.Append(v.Value.WindturbineId).Append("控制失败:").Append(_ErrorInfo[i]).Append("\n");
- }
- }
- }
- return sb.ToString();
- }
- catch { }
- return null;
- }
- public ControlRecordPage GetControlRecord(string stationId, string startTime, string endTime)
- {
- try
- {
- var v = _WEBHelper.HttpGetJSON<ControlRecordPage>($"{_Url}/control-record/search?stationId={stationId}&startTime={startTime}&endTime={endTime}&pageSize=5000&pageIndex=1");
- return v;
- }
- catch { }
- return null;
- }
- /// <summary>
- /// 获取控制日志
- /// </summary>
- /// <returns></returns>
- public string GetControlLog()
- {
- try
- {
- var vs = _WEBHelper.HttpGetJSON<List<ControlLog>>($"{_Url}/control-record/log");
- if (vs == null || vs.Count <= 0) return "";
- var sb = new StringBuilder();
- foreach (var v in vs)
- {
- sb.Append(v.ToString()).Append("\n");
- }
- return sb.ToString();
- }
- catch { }
- return "";
- }
- }
- class ControlLog
- {
- private static string[] errorCodes;
- /// <summary>
- /// 时间戳
- /// </summary>
- public long Timestamp { get; set; }
- /// <summary>
- /// 风机ID
- /// </summary>
- public string WindturbineId { get; set; }
- /// <summary>
- /// 错误码
- /// </summary>
- public int ErrorCode { get; set; }
- /// <summary>
- /// 控制类型
- /// </summary>
- public OperateStyle Type { get; set; }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- var vs = WindturbineId.Split('_');
- var name = $"{vs[0].Substring(0, vs[0].Length - 2)}{vs[1]}";
- sb.Append(Timestamp.GetTimeString()).Append("\t").Append(name).Append("\t").Append(Type.GetStringValue());
- if (ErrorCode == 0)
- {
- sb.Append("\t控制成功");
- }
- else
- {
- sb.Append("[失败]\t[原因]").Append(GetErrorInfo(ErrorCode));
- }
- return sb.ToString();
- }
- private string GetErrorInfo(int errorCode)
- {
- if (errorCodes == null)
- {
- errorCodes = new string[]
- {
- "未知错误",
- "控制命令发送失败",
- "无效的控制地址",
- "被控设备异常",
- "无效的控制功能",
- "网络未连接",
- "命令已发送,但是读取结果超时",
- "发送指令失败",
- "控制的命令出错",
- "未知错误",
- "未知错误",
- "未知错误",
- "风机操作过频繁",
- "风机被挂牌",
- "风机操作与风机状态不符"
- };
- }
- if (errorCode < 0 || errorCode >= errorCodes.Length)
- {
- return errorCodes[0];
- }
- return errorCodes[errorCode];
- }
- }
- }
|