ControlManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using NEIntelligentControl2.Models;
  2. using NEIntelligentControl2.Models.Messages;
  3. using NEIntelligentControl2.Models.Windturbine;
  4. using NEIntelligentControl2.Service.User;
  5. using NEIntelligentControl2.Service.WebSocket;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace NEIntelligentControl2.Service.Windturbine
  13. {
  14. /// <summary>
  15. /// 风机控制服务
  16. /// </summary>
  17. public class ControlManager
  18. {
  19. private WEBHelper _WEBHelper;
  20. private UrlManager _UrlManager;
  21. private string _Url;
  22. private string[] _ErrorInfo;
  23. public ControlManager(WEBHelper web, UrlManager urlm)
  24. {
  25. _WEBHelper = web;
  26. _UrlManager = urlm;
  27. _Url = _UrlManager.ServicePath;
  28. InitErrorCode();
  29. }
  30. private void InitErrorCode()
  31. {
  32. _ErrorInfo = new string[]
  33. {
  34. "控制成功",
  35. "控制命令发送失败",
  36. "无效的控制地址",
  37. "被控设备异常",
  38. "无效的控制功能",
  39. "网络未连接",
  40. "命令已发送,但是读取结果超时",
  41. "发送指令失败",
  42. "控制的命令出错",
  43. "未知错误",
  44. "未知错误",
  45. "未知错误",
  46. "风机操作过频繁",
  47. "风机被挂牌",
  48. "风机操作与风机状态不符"
  49. };
  50. }
  51. internal string SendLock(Dictionary<string, ControlInstruction> cis)
  52. {
  53. try
  54. {
  55. var cc = _WEBHelper.HttpPostBody<Dictionary<string, ControlInstruction>>($"{_Url}/api/control/lock", cis);
  56. StringBuilder sb = new StringBuilder();
  57. foreach (var v in cc)
  58. {
  59. if (v.Value.ErrorCode != "0")
  60. {
  61. int.TryParse(v.Value.ErrorCode, out int i);
  62. if (i < _ErrorInfo.Length)
  63. {
  64. sb.Append(v.Value.WindturbineId).Append("挂牌失败:").Append(_ErrorInfo[i]).Append("\n");
  65. }
  66. }
  67. }
  68. return sb.ToString();
  69. }
  70. catch { }
  71. return null;
  72. }
  73. /// <summary>
  74. /// 发送指令,不检查用户
  75. /// </summary>
  76. /// <returns></returns>
  77. public string SendCmdNotCheck(Dictionary<string, ControlInstruction> cis)
  78. {
  79. try
  80. {
  81. var cc = _WEBHelper.HttpPostBody<Dictionary<string, ControlInstruction>>($"{_Url}/api/control/", cis);
  82. StringBuilder sb = new StringBuilder();
  83. foreach (var v in cc)
  84. {
  85. if (v.Value.ErrorCode != "0")
  86. {
  87. bool b = int.TryParse(v.Value.ErrorCode, out int i);
  88. if (!b) i = 9;
  89. if (i < _ErrorInfo.Length)
  90. {
  91. sb.Append(v.Value.WindturbineId).Append("控制失败:").Append(_ErrorInfo[i]).Append("\n");
  92. }
  93. }
  94. }
  95. return sb.ToString();
  96. }
  97. catch { }
  98. return null;
  99. }
  100. public string SendCmd(Dictionary<string, ControlInstruction> cis)
  101. {
  102. try
  103. {
  104. var cc = _WEBHelper.HttpPostBody<Dictionary<string, ControlInstruction>>($"{_Url}/api/control/", cis);
  105. StringBuilder sb = new StringBuilder();
  106. foreach (var v in cc)
  107. {
  108. if (v.Value.ErrorCode != "0")
  109. {
  110. bool b = int.TryParse(v.Value.ErrorCode, out int i);
  111. i = !b ? 9 : i;
  112. if (i < _ErrorInfo.Length)
  113. {
  114. sb.Append(v.Value.WindturbineId).Append("控制失败:").Append(_ErrorInfo[i]).Append("\n");
  115. }
  116. }
  117. }
  118. return sb.ToString();
  119. }
  120. catch { }
  121. return null;
  122. }
  123. public ControlRecordPage GetControlRecord(string stationId, string startTime, string endTime)
  124. {
  125. try
  126. {
  127. var v = _WEBHelper.HttpGetJSON<ControlRecordPage>($"{_Url}/control-record/search?stationId={stationId}&startTime={startTime}&endTime={endTime}&pageSize=5000&pageIndex=1");
  128. return v;
  129. }
  130. catch { }
  131. return null;
  132. }
  133. /// <summary>
  134. /// 获取控制日志
  135. /// </summary>
  136. /// <returns></returns>
  137. public string GetControlLog()
  138. {
  139. try
  140. {
  141. var vs = _WEBHelper.HttpGetJSON<List<ControlLog>>($"{_Url}/control-record/log");
  142. if (vs == null || vs.Count <= 0) return "";
  143. var sb = new StringBuilder();
  144. foreach (var v in vs)
  145. {
  146. sb.Append(v.ToString()).Append("\n");
  147. }
  148. return sb.ToString();
  149. }
  150. catch { }
  151. return "";
  152. }
  153. }
  154. class ControlLog
  155. {
  156. private static string[] errorCodes;
  157. /// <summary>
  158. /// 时间戳
  159. /// </summary>
  160. public long Timestamp { get; set; }
  161. /// <summary>
  162. /// 风机ID
  163. /// </summary>
  164. public string WindturbineId { get; set; }
  165. /// <summary>
  166. /// 错误码
  167. /// </summary>
  168. public int ErrorCode { get; set; }
  169. /// <summary>
  170. /// 控制类型
  171. /// </summary>
  172. public OperateStyle Type { get; set; }
  173. public override string ToString()
  174. {
  175. StringBuilder sb = new StringBuilder();
  176. var vs = WindturbineId.Split('_');
  177. var name = $"{vs[0].Substring(0, vs[0].Length - 2)}{vs[1]}";
  178. sb.Append(Timestamp.GetTimeString()).Append("\t").Append(name).Append("\t").Append(Type.GetStringValue());
  179. if (ErrorCode == 0)
  180. {
  181. sb.Append("\t控制成功");
  182. }
  183. else
  184. {
  185. sb.Append("[失败]\t[原因]").Append(GetErrorInfo(ErrorCode));
  186. }
  187. return sb.ToString();
  188. }
  189. private string GetErrorInfo(int errorCode)
  190. {
  191. if (errorCodes == null)
  192. {
  193. errorCodes = new string[]
  194. {
  195. "未知错误",
  196. "控制命令发送失败",
  197. "无效的控制地址",
  198. "被控设备异常",
  199. "无效的控制功能",
  200. "网络未连接",
  201. "命令已发送,但是读取结果超时",
  202. "发送指令失败",
  203. "控制的命令出错",
  204. "未知错误",
  205. "未知错误",
  206. "未知错误",
  207. "风机操作过频繁",
  208. "风机被挂牌",
  209. "风机操作与风机状态不符"
  210. };
  211. }
  212. if (errorCode < 0 || errorCode >= errorCodes.Length)
  213. {
  214. return errorCodes[0];
  215. }
  216. return errorCodes[errorCode];
  217. }
  218. }
  219. }