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.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Ips.Service.GpuServer { public partial class DirectXForm1 : DevExpress.XtraEditors.DirectXForm { private bool _inited = false; private string _localPort;//本地端口 private string _svrNo;//服务编号 private string _rptAddr;//服务注册上报http地址 public DirectXForm1() { InitializeComponent(); this.Size = new Size(1024, 768); this.Text = "GPU服务"; } protected async override void OnLoad(EventArgs e) { base.OnLoad(e); new Library.DxpLib.LogCtrl() { Parent = groupControl1 }; LoadParams(); if (int.TryParse(_localPort, out int intPort)) { WebApiHelper.Start(intPort, "Ips.Library.Entity.xml"); this.FormClosing += (_, _) => WebApiHelper.Stop(); _inited = true; await StateRpt(); } _inited = true; } private async Task StateRpt() { string localIp; while (true) { string getIpUrl = null; try { if (_rptAddr == null) { await Task.Delay(5000); continue; } if (!_rptAddr.ToLower().StartsWith("http://")) { IpsLogger.Error($"注册地址必须以http://开头"); await Task.Delay(5000); continue; } getIpUrl = _rptAddr.AppendUrlSuffix("api/getclientip"); var rsp = await HttpHelper.GetRequestAsync(getIpUrl, 5); if (rsp.code == 0) { IpsLogger.Error(rsp.msg); return; } else { localIp = rsp.data; } break; } catch (Exception ex) { IpsLogger.Error($"无法连接到{getIpUrl},请检测地址是否正确并确保远程设备设备防火墙入栈规则允许TCP端口{getIpUrl.GetUrlPort()}通过", ex); await Task.Delay(5000); } } AppConst.LocalIp = localIp; IpsLogger.Info($"服务启动成功.接口地址http://{localIp}:{_localPort}/swagger"); bool preSucceed = false; while (!this.Disposing) { string url = _rptAddr.AppendUrlSuffix("api/SvrStateRpt/Report"); try { var dto = new ModelSvrRpt() { SvrType = EnumSvrType.GpuSvr, SvrNo = _svrNo, SvrRptType = EnumSvrRptType.Online, BaseHttpAddr = $"http://{localIp}:{_localPort}", SwaggerAddr = $"http://{localIp}:{_localPort}/Swagger", }; List list = new List(); if (AppConst.UseCg) { list.Add("参数估计"); } if (AppConst.UseTpdx) { list.Add("同频对消"); } dto.Features = string.Join(" ", list); var res = await HttpHelper.PostRequestAsync(url, dto); 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); } } private void Parms_TextChanged(object sender, EventArgs e) { if (!_inited) return; SaveParams(); } void SaveParams() { try { Directory.CreateDirectory("Params"); StringBuilder sb = new StringBuilder(); sb.AppendLine(txtLocalPort.Text); sb.AppendLine(txtSvrNo.Text); sb.AppendLine(txtRegistryAddr.Text); sb.AppendLine(txtUseTpdx.Checked.ToString()); sb.AppendLine(txtUseCg.Checked.ToString()); File.WriteAllText("Params\\Params.txt", sb.ToString()); IpsLogger.Info("用户修改了UI参数!"); } catch (Exception ex) { IpsLogger.Error("保存Params.txt文件异常", ex); } _localPort = txtLocalPort.Text.Trim(); _svrNo = txtSvrNo.Text.Trim(); _rptAddr = txtRegistryAddr.Text.Trim(); AppConst.UseTpdx = txtUseTpdx.Checked; AppConst.UseCg = txtUseCg.Checked; AppConst.SvrNo = _svrNo; } void LoadParams() { if (File.Exists("Params\\Params.txt")) { try { var lines = File.ReadAllLines("Params\\Params.txt"); txtLocalPort.Text = lines[0]; txtSvrNo.Text = lines[1]; txtRegistryAddr.Text = lines[2]; txtUseTpdx.Checked = lines[3].ToLower() == "true"; txtUseCg.Checked = lines[4].ToLower() == "true"; } catch (Exception ex) { IpsLogger.Error("加载Params.txt文件异常", ex); } } _localPort = txtLocalPort.Text.Trim(); _svrNo = txtSvrNo.Text.Trim(); _rptAddr = txtRegistryAddr.Text.Trim(); AppConst.UseTpdx = txtUseCg.Checked; AppConst.UseCg = txtUseCg.Checked; AppConst.SvrNo = _svrNo; } private void BtnShowApi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { string addr = $"http://127.0.0.1:{_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); } } } }