DirectXForm1.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using DevExpress.CodeParser;
  2. using Ips.Library.Basic;
  3. using Ips.Library.DxpLib;
  4. using Ips.Library.Entity;
  5. using Ips.Library.WebApi;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Reflection;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. namespace Ips.Service.CpuServer
  19. {
  20. public partial class DirectXForm1 : DevExpress.XtraEditors.DirectXForm
  21. {
  22. private bool apiRuning = false;
  23. private CancellationTokenSource cts;
  24. public DirectXForm1()
  25. {
  26. InitializeComponent();
  27. this.Size = new Size(1024, 768);
  28. this.Text = "CPU服务";
  29. }
  30. protected override void OnLoad(EventArgs e)
  31. {
  32. base.OnLoad(e);
  33. new Library.DxpLib.LogCtrl() { Parent = groupControl1 };
  34. LoadParams();
  35. }
  36. private async void RegistrySvr(CancellationToken token)
  37. {
  38. while (!this.Disposing && apiRuning)
  39. {
  40. string getIpUrl = null;
  41. try
  42. {
  43. getIpUrl = AppConst.RegistryUri.AppendUrlSuffix("api/getclientip");
  44. var rsp = await HttpHelper.GetRequestAsync<string>(getIpUrl, 5);
  45. if (rsp.code == 0)
  46. {
  47. IpsLogger.Error(rsp.msg);
  48. return;
  49. }
  50. else
  51. {
  52. AppConst.LocalIp = rsp.data;
  53. }
  54. break;
  55. }
  56. catch (Exception ex)
  57. {
  58. IpsLogger.Error($"无法注册到{getIpUrl},请检测地址是否正确并确保远程设备设备防火墙入栈规则允许TCP端口{getIpUrl.GetUrlPort()}通过", ex);
  59. await Task.Delay(10000, token);
  60. }
  61. }
  62. bool preSucceed = false;
  63. while (!this.Disposing && apiRuning)
  64. {
  65. string url = AppConst.RegistryUri.AppendUrlSuffix("api/SvrStateRpt/Report");
  66. try
  67. {
  68. var dto = new ModelSvrRpt()
  69. {
  70. SvrType = EnumSvrType.CapSvr,
  71. SvrNo = AppConst.SvrNo,
  72. SvrRptType = EnumSvrRptType.Online,
  73. BaseHttpAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}",
  74. SwaggerAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/Swagger",
  75. };
  76. List<string> list = new List<string>();
  77. if (AppConst.UseCg)
  78. {
  79. list.Add("参数估计");
  80. }
  81. if (AppConst.UseTpdx)
  82. {
  83. list.Add("同频对消");
  84. }
  85. if (AppConst.UseSigCheck)
  86. {
  87. list.Add("信号检测");
  88. }
  89. dto.Features = string.Join(" ", list);
  90. var res = await HttpHelper.PostRequestAsync<object>(url, dto);
  91. if (res.code != 200)
  92. {
  93. IpsLogger.Error($"服务注册异常[url={url}].{res.msg}");
  94. preSucceed = false;
  95. }
  96. else
  97. {
  98. if (!preSucceed)
  99. IpsLogger.Info($"服务注册成功![url={url}]");
  100. preSucceed = true;
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. IpsLogger.Error($"服务注册异常[url={url}]", ex);
  106. preSucceed = false;
  107. }
  108. await Task.Delay(10000, token);
  109. }
  110. }
  111. private void BtnShowApi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  112. {
  113. string addr = $"http://127.0.0.1:{AppConst.LocalPort}/swagger";
  114. try
  115. {
  116. //System.Diagnostics.Process.Start(addr);//NetFramework可用,NetCore使用CMD启动
  117. ProcessStartInfo info = new ProcessStartInfo();
  118. info.Arguments = $"/C start {addr}";
  119. info.FileName = "cmd";
  120. info.UseShellExecute = false;
  121. info.CreateNoWindow = true;
  122. Process.Start(info);
  123. }
  124. catch (Exception ex)
  125. {
  126. IpsLogger.Error($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}", ex);
  127. }
  128. }
  129. private void btnStart_Click(object sender, EventArgs e)
  130. {
  131. try
  132. {
  133. if (btnStart.Text == "启动服务")
  134. {
  135. if (!int.TryParse(txtLocalPort.Text.Trim(), out int localPort))
  136. {
  137. throw new Exception("本地端口输入错误");
  138. }
  139. if (localPort < 1024 || localPort > 49151)
  140. {
  141. //1024以下给vip程序预留的,49152-65535用于客户端随机分配的
  142. throw new Exception("本地端口范围[1024,49151]");
  143. }
  144. if (txtSvrNo.Text.Trim().Length < 3 || txtSvrNo.Text.Trim().Length > 15)
  145. {
  146. throw new Exception("服务编号长度范围[3,15]");
  147. }
  148. try
  149. {
  150. var u = new Uri(txtRegistryAddr.Text.Trim());
  151. }
  152. catch
  153. {
  154. throw new Exception("服务注册地址不是有效的URI");
  155. }
  156. AppConst.LocalPort = localPort;
  157. AppConst.SvrNo = txtSvrNo.Text.Trim();
  158. AppConst.RegistryUri = txtRegistryAddr.Text.Trim();
  159. AppConst.UseTpdx = txtUseTpdx.Checked;
  160. AppConst.UseCg = txtUseCg.Checked;
  161. AppConst.UseSigCheck = txtUseCheck.Checked;
  162. WebApiHelper.Start(AppConst.LocalPort, "Ips.Library.Entity.xml");
  163. btnStart.Text = "停止服务";
  164. stackPanel1.DisabledChildWithout(btnStart);
  165. IpsLogger.Info($"服务启动成功.接口地址http://+:{localPort}/swagger");
  166. IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/upload");
  167. IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/download");
  168. IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/logs");
  169. apiRuning = true;
  170. cts = new CancellationTokenSource();
  171. RegistrySvr(cts.Token);
  172. }
  173. else
  174. {
  175. apiRuning = false;
  176. btnStart.Text = "启动服务";
  177. WebApiHelper.Stop();
  178. cts?.Cancel();
  179. stackPanel1.EnableChild();
  180. }
  181. SaveParams();
  182. }
  183. catch (Exception ex)
  184. {
  185. IpsLogger.Error(ex.Message, ex);
  186. }
  187. }
  188. void SaveParams()
  189. {
  190. try
  191. {
  192. Directory.CreateDirectory("Params");
  193. StringBuilder sb = new StringBuilder();
  194. sb.AppendLine(txtLocalPort.Text);
  195. sb.AppendLine(txtSvrNo.Text);
  196. sb.AppendLine(txtRegistryAddr.Text);
  197. sb.AppendLine(txtUseTpdx.Checked.ToString());
  198. sb.AppendLine(txtUseCg.Checked.ToString());
  199. sb.AppendLine(txtUseCheck.Checked.ToString());
  200. sb.AppendLine(btnStart.Text);
  201. File.WriteAllText("Params\\Params.txt", sb.ToString());
  202. }
  203. catch (Exception ex)
  204. {
  205. IpsLogger.Error("保存Params.txt文件异常", ex);
  206. }
  207. }
  208. void LoadParams()
  209. {
  210. if (File.Exists("Params\\Params.txt"))
  211. {
  212. try
  213. {
  214. var lines = File.ReadAllLines("Params\\Params.txt");
  215. txtLocalPort.Text = lines[0];
  216. txtSvrNo.Text = lines[1];
  217. txtRegistryAddr.Text = lines[2];
  218. txtUseTpdx.Checked = lines[3].ToLower() == "true";
  219. txtUseCg.Checked = lines[4].ToLower() == "true";
  220. txtUseCheck.Checked= lines[5].ToLower() == "true";
  221. if (lines[6] == "停止服务")//上次状态是启动状态,加载后直接启动
  222. {
  223. btnStart_Click(null, null);
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. IpsLogger.Error("加载Params.txt文件异常", ex);
  229. }
  230. }
  231. }
  232. }
  233. }