ControlHttpServer.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using IvfTl.ControlHost.Debug;
  10. namespace IvfTl.ControlHost
  11. {
  12. /// <summary>
  13. /// control 进程内的本地 HTTP 小服务,只监听 127.0.0.1:port。
  14. /// 阶段1:/ping、/status。阶段2:/status 补全(rich) + /serial/pause|resume(借串口) + /shutdown(受护栏停止)。
  15. /// </summary>
  16. public class ControlHttpServer
  17. {
  18. private readonly int _port;
  19. private readonly Func<StatusDto> _pingProvider; // /ping 轻量存活
  20. private readonly Func<object> _statusProvider; // /status 完整快照(阶段2 §6 三块)
  21. private readonly Func<string, bool> _shutdownHandler; // /shutdown(token 校验后安全停机)
  22. private readonly Func<int, bool> _serialPauseHandler; // /serial/pause(借串口:control 让路该舱)
  23. private readonly Func<int, bool> _serialResumeHandler; // /serial/resume(归还:恢复采集)
  24. private readonly Action<string> _log;
  25. private readonly DebugSessionManager _debug;
  26. private HttpListener _listener;
  27. private CancellationTokenSource _cts;
  28. public ControlHttpServer(
  29. int port,
  30. Func<StatusDto> pingProvider,
  31. Func<object> statusProvider,
  32. Func<string, bool> shutdownHandler,
  33. Func<int, bool> serialPauseHandler,
  34. Func<int, bool> serialResumeHandler,
  35. Action<string> log,
  36. DebugSessionManager debug = null)
  37. {
  38. _port = port;
  39. _pingProvider = pingProvider;
  40. _statusProvider = statusProvider;
  41. _shutdownHandler = shutdownHandler;
  42. _serialPauseHandler = serialPauseHandler;
  43. _serialResumeHandler = serialResumeHandler;
  44. _log = log ?? (_ => { });
  45. _debug = debug;
  46. }
  47. public void Start()
  48. {
  49. _listener = new HttpListener();
  50. // 仅本机回环,拒绝外部访问(防外部调停机/借串口)。
  51. _listener.Prefixes.Add($"http://127.0.0.1:{_port}/");
  52. _listener.Start();
  53. _cts = new CancellationTokenSource();
  54. _log($"ControlHttpServer 监听 http://127.0.0.1:{_port}/");
  55. Task.Run(() => Loop(_cts.Token));
  56. }
  57. private async Task Loop(CancellationToken token)
  58. {
  59. while (!token.IsCancellationRequested)
  60. {
  61. HttpListenerContext ctx;
  62. try { ctx = await _listener.GetContextAsync(); }
  63. catch (Exception ex) { if (!token.IsCancellationRequested) _log("HttpListener 异常:" + ex.Message); break; }
  64. try { Handle(ctx); }
  65. catch (Exception ex) { _log("处理请求异常:" + ex.Message); }
  66. }
  67. }
  68. private void Handle(HttpListenerContext ctx)
  69. {
  70. string path = ctx.Request.Url.AbsolutePath.TrimEnd('/').ToLowerInvariant();
  71. string method = ctx.Request.HttpMethod.ToUpperInvariant();
  72. string body;
  73. int code = 200;
  74. switch (path)
  75. {
  76. case "/ping":
  77. body = JsonConvert.SerializeObject(_pingProvider());
  78. break;
  79. case "/status":
  80. body = JsonConvert.SerializeObject(_statusProvider());
  81. break;
  82. case "/shutdown":
  83. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  84. {
  85. string token = ReadField(ctx, "token");
  86. bool ok = _shutdownHandler != null && _shutdownHandler(token ?? "");
  87. code = ok ? 200 : 403;
  88. body = "{\"ok\":" + (ok ? "true" : "false") + (ok ? "" : ",\"error\":\"token invalid\"") + "}";
  89. }
  90. break;
  91. case "/serial/pause":
  92. case "/serial/resume":
  93. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  94. {
  95. int houseSn = ReadIntField(ctx, "houseSn");
  96. bool isPause = path == "/serial/pause";
  97. var handler = isPause ? _serialPauseHandler : _serialResumeHandler;
  98. bool ok = handler != null && houseSn > 0 && handler(houseSn);
  99. code = ok ? 200 : 400;
  100. body = "{\"ok\":" + (ok ? "true" : "false") + ",\"houseSn\":" + houseSn + (ok ? "" : ",\"error\":\"bad houseSn or handler\"") + "}";
  101. }
  102. break;
  103. case "/debug/acquire":
  104. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  105. {
  106. int houseSn = ReadIntField(ctx, "houseSn");
  107. var r = _debug != null ? _debug.Acquire(houseSn) : DebugCommandResult.Fail("NO_HANDLE", "debug 未装配");
  108. code = r.Ok ? 200 : 409; body = JsonConvert.SerializeObject(r);
  109. }
  110. break;
  111. case "/debug/heartbeat":
  112. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  113. {
  114. var r = _debug != null ? _debug.Heartbeat(ReadField(ctx, "sessionId")) : DebugCommandResult.Fail("SESSION_EXPIRED", "debug 未装配");
  115. code = r.Ok ? 200 : 410; body = JsonConvert.SerializeObject(r);
  116. }
  117. break;
  118. case "/debug/release":
  119. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  120. {
  121. var r = _debug != null ? _debug.Release(ReadField(ctx, "sessionId")) : DebugCommandResult.Okay();
  122. code = 200; body = JsonConvert.SerializeObject(r);
  123. }
  124. break;
  125. case "/debug/command":
  126. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  127. {
  128. var jo = ReadBody(ctx);
  129. string sid = jo?["sessionId"]?.ToString();
  130. string op = jo?["op"]?.ToString();
  131. var argsObj = jo?["args"] as Newtonsoft.Json.Linq.JObject;
  132. var r = _debug != null ? _debug.Execute(sid, op, argsObj) : DebugCommandResult.Fail("SESSION_EXPIRED", "debug 未装配");
  133. code = r.Ok ? 200 : (r.Code == "SESSION_EXPIRED" ? 410 : (r.Code == "OUT_OF_RANGE" ? 400 : 200));
  134. body = JsonConvert.SerializeObject(r);
  135. }
  136. break;
  137. default:
  138. code = 404; body = Err("not found");
  139. break;
  140. }
  141. byte[] buf = Encoding.UTF8.GetBytes(body);
  142. ctx.Response.StatusCode = code;
  143. ctx.Response.ContentType = "application/json";
  144. ctx.Response.ContentLength64 = buf.Length;
  145. ctx.Response.OutputStream.Write(buf, 0, buf.Length);
  146. ctx.Response.OutputStream.Close();
  147. }
  148. private static string Err(string msg) => "{\"ok\":false,\"error\":\"" + msg + "\"}";
  149. /// <summary>读 POST JSON body 的某字符串字段(失败返回 null)。</summary>
  150. private string ReadField(HttpListenerContext ctx, string field)
  151. {
  152. try
  153. {
  154. using (var sr = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding ?? Encoding.UTF8))
  155. {
  156. string raw = sr.ReadToEnd();
  157. if (string.IsNullOrEmpty(raw)) return null;
  158. var jo = JObject.Parse(raw);
  159. return jo[field]?.ToString();
  160. }
  161. }
  162. catch (Exception ex) { _log("解析请求体异常:" + ex.Message); return null; }
  163. }
  164. private int ReadIntField(HttpListenerContext ctx, string field)
  165. {
  166. string s = ReadField(ctx, field);
  167. return int.TryParse(s, out int v) ? v : -1;
  168. }
  169. /// <summary>把 POST body 整体解析为 JObject(失败返回 null)。/debug/command 多字段用。</summary>
  170. private Newtonsoft.Json.Linq.JObject ReadBody(HttpListenerContext ctx)
  171. {
  172. try
  173. {
  174. using (var sr = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding ?? Encoding.UTF8))
  175. {
  176. string raw = sr.ReadToEnd();
  177. return string.IsNullOrEmpty(raw) ? null : Newtonsoft.Json.Linq.JObject.Parse(raw);
  178. }
  179. }
  180. catch (Exception ex) { _log("解析 body 异常:" + ex.Message); return null; }
  181. }
  182. public void Stop()
  183. {
  184. try { _cts?.Cancel(); _listener?.Stop(); _listener?.Close(); }
  185. catch (Exception ex) { _log("ControlHttpServer 停止异常:" + ex.Message); }
  186. }
  187. }
  188. }