using DevExpress.Utils.Localization; using Ips.Library.Basic; using Ips.Library.DxpLib; using Ips.Library.Entity; using Ips.Library.WebApi; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Security.Policy; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Ips.Service.CapServer { public partial class DirectXForm1 : DevExpress.XtraEditors.DirectXForm { private bool apiRuning = false; private CancellationTokenSource cts; public DirectXForm1() { InitializeComponent(); this.Size = new Size(1024, 768); this.Text = "采集服务"; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); new Library.DxpLib.LogCtrl() { Parent = groupControl1 }; LoadParams(); } private async void RegistrySvr(CancellationToken token) { while (!this.Disposing && apiRuning) { string getIpUrl = null; try { getIpUrl = AppConst.RegistryUri.AppendUrlSuffix("api/getclientip"); var rsp = await HttpHelper.GetRequestAsync(getIpUrl, 5); if (rsp.code == 0) { IpsLogger.Error(rsp.msg); return; } else { AppConst.LocalIp = rsp.data; } break; } catch (Exception ex) { IpsLogger.Error($"无法注册到{getIpUrl},请检测地址是否正确并确保远程设备设备防火墙入栈规则允许TCP端口{getIpUrl.GetUrlPort()}通过", ex); await Task.Delay(10000, token); } } bool preSucceed = false; while (!this.Disposing && apiRuning) { string url = AppConst.RegistryUri.AppendUrlSuffix("api/SvrStateRpt/Report"); try { var res = await HttpHelper.PostRequestAsync(url, new ModelSvrRpt() { SvrType = EnumSvrType.CapSvr, SvrNo = AppConst.SvrNo, Features = "数据采集 DDC", CapDevType = AppConst.CardType, SvrRptType = EnumSvrRptType.Online, BaseHttpAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}", SwaggerAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/Swagger", }); if (res.code != 200) { IpsLogger.Error($"服务注册异常[url={url}].{res.msg}"); preSucceed = false; } else { if (!preSucceed) IpsLogger.Info($"服务注册成功![url={url}]"); preSucceed = true; } } catch (Exception ex) { IpsLogger.Error($"服务注册异常[url={url}]", ex); preSucceed = false; } await Task.Delay(10000, token); } } private void BtnShowApi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { string addr = $"http://127.0.0.1:{AppConst.LocalPort}/swagger"; try { //System.Diagnostics.Process.Start(addr);//NetFramework可用,NetCore使用CMD启动 ProcessStartInfo info = new ProcessStartInfo(); info.Arguments = $"/C start {addr}"; info.FileName = "cmd"; info.UseShellExecute = false; info.CreateNoWindow = true; Process.Start(info); } catch (Exception ex) { IpsLogger.Error($"无法打开默认浏览器,请手动打开浏览器查看接口文档.地址{addr}", ex); } } private void btnStart_Click(object sender, EventArgs e) { try { if (btnStart.Text == "启动服务") { if (!int.TryParse(txtLocalPort.Text.Trim(), out int localPort)) { throw new Exception("本地端口输入错误"); } if (localPort < 1024 || localPort > 49151) { //1024以下给vip程序预留的,49152-65535用于客户端随机分配的 throw new Exception("本地端口范围[1024,49151]"); } if (txtSvrNo.Text.Trim().Length < 3 || txtSvrNo.Text.Trim().Length > 15) { throw new Exception("服务编号长度范围[3,15]"); } try { var u = new Uri(txtRegistryAddr.Text.Trim()); } catch { throw new Exception("服务注册地址不是有效的URI"); } AppConst.LocalPort = localPort; AppConst.SvrNo = txtSvrNo.Text.Trim(); AppConst.CardType = txtCardType.Text.GetEnumByDisplayName(); AppConst.RegistryUri = txtRegistryAddr.Text.Trim(); WebApiHelper.Start(AppConst.LocalPort, "Ips.Library.Entity.xml", AppConst.DataDir.ToStrArray()); btnStart.Text = "停止服务"; stackPanel1.DisabledChildWithout(btnStart); IpsLogger.Info($"服务启动成功.接口地址http://+:{localPort}/swagger"); IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/{Path.GetFileName(AppConst.DataDir)}"); IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/upload"); IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/download"); IpsLogger.Info($"服务启动成功.静态文件预览地址http://+:{localPort}/logs"); apiRuning = true; cts = new CancellationTokenSource(); RegistrySvr(cts.Token); } else { apiRuning = false; btnStart.Text = "启动服务"; WebApiHelper.Stop(); cts?.Cancel(); stackPanel1.EnableChild(); } SaveParams(); } catch (Exception ex) { IpsLogger.Error(ex.Message, ex); } } void SaveParams() { try { Directory.CreateDirectory("Params"); StringBuilder sb = new StringBuilder(); sb.AppendLine(txtLocalPort.Text); sb.AppendLine(txtCardType.SelectedIndex.ToString()); sb.AppendLine(txtSvrNo.Text); sb.AppendLine(txtRegistryAddr.Text); sb.AppendLine(btnStart.Text); File.WriteAllText("Params\\Params.txt", sb.ToString()); } catch (Exception ex) { IpsLogger.Error("保存Params.txt文件异常", ex); } } void LoadParams() { if (File.Exists("Params\\Params.txt")) { try { var lines = File.ReadAllLines("Params\\Params.txt"); txtLocalPort.Text = lines[0]; txtCardType.SelectedIndex = Convert.ToInt32(lines[1]); txtSvrNo.Text = lines[2]; txtRegistryAddr.Text = lines[3]; if (lines[4] == "停止服务")//上次状态是启动状态,加载后直接启动 { btnStart_Click(null, null); } } catch (Exception ex) { IpsLogger.Error("加载Params.txt文件异常", ex); } } } } }