123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using DevExpress.CodeParser;
- 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 apiRuning = false;
- private CancellationTokenSource cts;
- public DirectXForm1()
- {
- InitializeComponent();
- this.Size = new Size(1024, 768);
- this.Text = "GPU服务";
- }
- 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<string>(getIpUrl);
- 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 dto = new ModelSvrRpt()
- {
- SvrType = EnumSvrType.GpuSvr,
- SvrNo = AppConst.SvrNo,
- SvrRptType = EnumSvrRptType.Online,
- BaseHttpAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}",
- SwaggerAddr = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/Swagger",
- };
- List<string> list = new List<string>();
- if (AppConst.UseCg)
- {
- list.Add("参数估计");
- }
- if (AppConst.UseTpdx)
- {
- list.Add("同频对消");
- }
- dto.Features = string.Join(" ", list);
- var res = await HttpHelper.PostRequestAsync<object>(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, 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.RegistryUri = txtRegistryAddr.Text.Trim();
- AppConst.UseTpdx = txtUseTpdx.Checked;
- AppConst.UseCg = txtUseCg.Checked;
- WebApiHelper.Start(AppConst.LocalPort, "Ips.Library.Entity.xml");
- btnStart.Text = "停止服务";
- stackPanel1.DisabledChildWithout(btnStart);
- IpsLogger.Info($"服务启动成功.接口地址http://+:{localPort}/swagger");
- 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(txtSvrNo.Text);
- sb.AppendLine(txtRegistryAddr.Text);
- sb.AppendLine(txtUseTpdx.Checked.ToString());
- sb.AppendLine(txtUseCg.Checked.ToString());
- 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];
- txtSvrNo.Text = lines[1];
- txtRegistryAddr.Text = lines[2];
- txtUseTpdx.Checked = lines[3].ToLower() == "true";
- txtUseCg.Checked = lines[4].ToLower() == "true";
- if (lines[5] == "停止服务")//上次状态是启动状态,加载后直接启动
- {
- btnStart_Click(null, null);
- }
- }
- catch (Exception ex)
- {
- IpsLogger.Error("加载Params.txt文件异常", ex);
- }
- }
- }
- }
- }
|