You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.3 KiB
C#

9 months ago
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NLog;
using POSV.Service;
using POSV.Service.Entity;
using POSV.Service.Utils;
namespace POSV.WindowsServiceManager
{
public partial class MainForm : Form
{
private static Logger logger = NLog.LogManager.GetCurrentClassLogger();
private int _port = 2018;
public MainForm(int port = 2018)
{
InitializeComponent();
this._port = port;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//系统端口信息
this._port = Global.Instance.GlobalConfigIntValue(ConfigConstant.SYSTEM_DEFAULT_PORT , 2018);
this.edPort.Text = Convert.ToString(this._port);
this.edPort.Tag = this._port;
Task.Factory.StartNew(() =>
{
this.Invoke(new Action(() =>
{
DisableAll();
CheckServices();
UpdateUiTimer.Start();
}));
}).ContinueWith(task =>
{
this.Invoke(new Action(() =>
{
}));
});
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (WindowState == FormWindowState.Minimized)
{
Hide();
}
else
{
Show();
}
}
private void DisableAll()
{
InstallService.Enabled = false;
UninstallService.Enabled = false;
StopService.Enabled = false;
StartService.Enabled = false;
updatePortBt.Enabled = false;
}
private void CheckServices()
{
ServiceControllerStatus? status = ServiceHelper.CheckServiceStatus();
if (status == null)
{
InstallService.Enabled = true;
UninstallService.Enabled = false;
StopService.Enabled = false;
StartService.Enabled = false;
updatePortBt.Enabled = false;
}
else
{
InstallService.Enabled = false;
updatePortBt.Enabled = true;
UninstallService.Enabled = true;
switch (status)
{
case ServiceControllerStatus.ContinuePending:
StopService.Enabled = false;
StartService.Enabled = false;
break;
case ServiceControllerStatus.PausePending:
StopService.Enabled = false;
StartService.Enabled = false;
break;
case ServiceControllerStatus.Paused:
StopService.Enabled = false;
StartService.Enabled = true;
break;
case ServiceControllerStatus.Running:
StopService.Enabled = true;
StartService.Enabled = false;
break;
case ServiceControllerStatus.StartPending:
StopService.Enabled = false;
StartService.Enabled = false;
break;
case ServiceControllerStatus.StopPending:
StopService.Enabled = false;
StartService.Enabled = false;
break;
case ServiceControllerStatus.Stopped:
StopService.Enabled = false;
StartService.Enabled = true;
break;
default:
break;
}
}
}
private void UpdateUiTimerTick(object sender, EventArgs e)
{
CheckServices();
}
private void NotifyIconClick(object sender, EventArgs e)
{
if (WindowState != FormWindowState.Minimized)
{
WindowState = FormWindowState.Minimized;
}
Show();
WindowState = FormWindowState.Normal;
}
private void NotifyIconDoubleClick(object sender, EventArgs e)
{
if (WindowState != FormWindowState.Minimized)
{
WindowState = FormWindowState.Minimized;
}
Show();
WindowState = FormWindowState.Normal;
}
private void InstallServiceClick(object sender, EventArgs e)
{
DisableAll();
ServiceHelper.InstallWindowsService();
}
private void StartServiceClick(object sender, EventArgs e)
{
DisableAll();
ServiceHelper.StartService();
}
private void StopServiceClick(object sender, EventArgs e)
{
DisableAll();
ServiceHelper.StopService();
}
private void UninstallServiceClick(object sender, EventArgs e)
{
DisableAll();
ServiceHelper.UninstallWindowsService();
}
private void UpdatePortBtClick(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(this.edPort.Text.Trim()))
{
this.lblInfo.Text = "端口不能为空,本次操作无效";
return;
}
if (!IsInt(this.edPort.Text.Trim()))
{
this.lblInfo.Text = "端口必须为数字,本次操作无效";
return;
}
int port = Convert.ToInt32(this.edPort.Text.Trim());
if (PortInUse(port) || PortInUse(port + 1) || PortInUse(port + 2))
{
this.lblInfo.Text = "端口被占用,本次操作无效";
return;
}
string tenantId = Global.Instance.GlobalConfigStringValue(ConfigConstant.SYSTEM_TENANT , "373001");
//系统默认端口
var defaultPort = new Config();
defaultPort.Id = IdWorkerUtils.Instance.NextId();
defaultPort.Group = ConfigConstant.SYSTEM_GROUP;
defaultPort.TenantId = tenantId;
defaultPort.Keys = ConfigConstant.SYSTEM_DEFAULT_PORT;
defaultPort.Values = port.ToString();
defaultPort.Name = "消息中心默认端口";
var webPort = new Config();
webPort.Id = IdWorkerUtils.Instance.NextId();
webPort.Group = ConfigConstant.SYSTEM_GROUP;
webPort.TenantId = tenantId;
webPort.Keys = ConfigConstant.SYSTEM_WEB_PORT;
webPort.Values = (port + 1).ToString();
webPort.Name = "消息中心Web端口";
var mqttPort = new Config();
mqttPort.Id = IdWorkerUtils.Instance.NextId();
mqttPort.Group = ConfigConstant.SYSTEM_GROUP;
mqttPort.TenantId = tenantId;
mqttPort.Keys = ConfigConstant.SYSTEM_MQTT_PORT;
mqttPort.Values = (port + 2).ToString();
mqttPort.Name = "消息中心MQTT端口";
lock (Global.Instance.SyncLock)
{
using (var db = Global.Instance.OpenDataBase)
{
using (var trans = db.GetTransaction())
{
db.Save<Config>(defaultPort);
db.Save<Config>(webPort);
db.Save<Config>(mqttPort);
trans.Complete();
}
}
}
DisableAll();
this.lblInfo.Text = "服务停止中.....";
//停止服务
ServiceHelper.StopService();
while (ServiceHelper.CheckServiceStatus() == ServiceControllerStatus.StopPending)
{
Thread.Sleep(100);
}
this.lblInfo.Text = "服务重启中.....";
ServiceHelper.StartService();
this.lblInfo.Text = "服务运行中....";
}
catch(Exception ex)
{
logger.Error(ex , "更新端口异常");
}
}
private bool IsInt(string value)
{
return Regex.IsMatch(value , @"^[+-]?\d*$");
}
private bool PortInUse(int port)
{
bool inUse = false;
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
foreach (IPEndPoint endPoint in ipEndPoints)
{
if (endPoint.Port == port)
{
inUse = true;
break;
}
}
return inUse;
}
}
}