DirectXForm1.cs 8.6 KB

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