Skip to content

Commit

Permalink
代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed May 20, 2024
1 parent 72b6197 commit cff1323
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 298 deletions.
31 changes: 11 additions & 20 deletions NewLife.Agent/Command/BaseCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,39 @@

namespace NewLife.Agent.Command;
namespace NewLife.Agent.Command;

/// <summary>
/// 命令处理基类
/// </summary>
public abstract class BaseCommandHandler : ICommandHandler
/// <remarks>
/// 安装并启动服务
/// </remarks>
/// <param name="service"></param>
public abstract class BaseCommandHandler(ServiceBase service) : ICommandHandler
{
/// <summary>
/// 服务
/// </summary>
protected ServiceBase Service { get; }
protected ServiceBase Service { get; } = service;

/// <summary>
/// 命令(命令为唯一标识,子类如果使用相同命令,父类将会被覆盖)
/// </summary>
public abstract String Cmd { get; set; }
public String Cmd { get; set; }

/// <summary>
/// 命令描述
/// </summary>
public abstract String Description { get; set; }
public String Description { get; set; }

/// <summary>
/// 快捷键
/// </summary>
public abstract Char? ShortcutKey { get; set; }

/// <summary>
/// 安装并启动服务
/// </summary>
/// <param name="service"></param>
public BaseCommandHandler(ServiceBase service)
{
Service = service;
}
public Char? ShortcutKey { get; set; }

/// <summary>
/// 是否显示菜单
/// </summary>
/// <returns></returns>
public virtual Boolean IsShowMenu()
{
return ShortcutKey != null;
}
public virtual Boolean IsShowMenu() => ShortcutKey != null;

/// <summary>处理命令</summary>
/// <param name="args"></param>
Expand Down
2 changes: 0 additions & 2 deletions NewLife.Agent/Command/CommandConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ public class CommandConst
/// </summary>
public const String Restart = "-restart";


/// <summary>
/// 看门狗命令
/// </summary>
public const String WatchDog = "-watch";

}
12 changes: 6 additions & 6 deletions NewLife.Agent/Command/CommandHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NewLife.Agent.Command;
public class CommandFactory
{
private readonly List<BaseCommandHandler> _commandHandlerList;
private Dictionary<String, BaseCommandHandler> _commandHandlerDict = new Dictionary<String, BaseCommandHandler>(StringComparer.OrdinalIgnoreCase);
private Dictionary<String, BaseCommandHandler> _commandHandlerDict = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
///
Expand All @@ -34,8 +34,8 @@ public CommandFactory(ServiceBase service, params Assembly[] customCommandHandle
}

// 使用反射获取所有实现了ICommandHandler接口的类型
var commandHandlerTypes = assemblies.Values.SelectMany(n=>n.GetTypes().Where(t => typeof(BaseCommandHandler).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract)).ToList();
var commandHandlers = new List<BaseCommandHandler>();
var commandHandlerTypes = assemblies.Values.SelectMany(n => n.GetTypes().Where(t => typeof(BaseCommandHandler).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract)).ToList();
var commandHandlers = new List<BaseCommandHandler>();
foreach (var type in commandHandlerTypes)
{
var handler = (BaseCommandHandler)Activator.CreateInstance(type, service);
Expand Down Expand Up @@ -103,11 +103,11 @@ public Boolean Handle(Char key, String[] args = null)
public SortedSet<Menu> GetShortcutMenu()
{
var menus = new SortedSet<Menu>();
foreach (var commandHandler in _commandHandlerList)
foreach (var handler in _commandHandlerList)
{
if (commandHandler.ShortcutKey != null && commandHandler.IsShowMenu())
if (handler.ShortcutKey != null && handler.IsShowMenu())
{
menus.Add(new Menu(commandHandler.ShortcutKey.Value, commandHandler.Description, commandHandler.Cmd));
menus.Add(new Menu(handler.ShortcutKey.Value, handler.Description, handler.Cmd, null));
}
}
return menus;
Expand Down
19 changes: 4 additions & 15 deletions NewLife.Agent/CommandHandler/Install.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NewLife.Agent.Command;
using NewLife.Model;

namespace NewLife.Agent.CommandHandler;

Expand All @@ -14,22 +13,13 @@ public class Install : BaseCommandHandler
/// <param name="service"></param>
public Install(ServiceBase service) : base(service)
{
Cmd = CommandConst.Install;
Description = "安装服务";
ShortcutKey = '2';
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.Install;

/// <inheritdoc />
public override String Description { get; set; } = "安装服务";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '2';

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return !Service.Host.IsInstalled(Service.ServiceName);
}
public override Boolean IsShowMenu() => !Service.Host.IsInstalled(Service.ServiceName);

/// <inheritdoc/>
public override void Process(String[] args)
Expand Down Expand Up @@ -85,5 +75,4 @@ public virtual String GetExeName()

return filename;
}

}
12 changes: 3 additions & 9 deletions NewLife.Agent/CommandHandler/InstallAndStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ public class InstallAndStart : BaseCommandHandler
/// <param name="service"></param>
public InstallAndStart(ServiceBase service) : base(service)
{
Cmd = CommandConst.InstallAndStart;
Description = "安装并启动服务";
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.InstallAndStart;

/// <inheritdoc />
public override String Description { get; set; } = "安装并启动服务";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; }

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand All @@ -44,6 +37,7 @@ public override void Process(String[] args)
Thread.Sleep(100);
}
Service.Host.Start(Service.ServiceName);

// 稍微等一下,以便后续状态刷新
Thread.Sleep(500);
}
Expand Down
15 changes: 3 additions & 12 deletions NewLife.Agent/CommandHandler/Reinstall.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Xml.Linq;
using NewLife.Agent.Command;
using NewLife.Agent.Command;
using NewLife.Log;
using NewLife.Model;

namespace NewLife.Agent.CommandHandler;

Expand All @@ -16,17 +14,10 @@ public class Reinstall : BaseCommandHandler
/// <param name="service"></param>
public Reinstall(ServiceBase service) : base(service)
{
Cmd = CommandConst.Reinstall;
Description = "重新安装服务";
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.Reinstall;

/// <inheritdoc />
public override String Description { get; set; } = "重新安装服务";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; }

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand Down
18 changes: 5 additions & 13 deletions NewLife.Agent/CommandHandler/Remove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@ public class Remove : BaseCommandHandler
/// <param name="service"></param>
public Remove(ServiceBase service) : base(service)
{
Cmd = CommandConst.Remove;
Description = "卸载服务";
ShortcutKey = '2';
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.Remove;

/// <inheritdoc />
public override String Description { get; set; } = "卸载服务";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '2';

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return Service.Host.IsInstalled(Service.ServiceName);
}
public override Boolean IsShowMenu() => Service.Host.IsInstalled(Service.ServiceName);

/// <inheritdoc/>
public override void Process(String[] args)
{
Service.Host.Remove(Service.ServiceName);

// 稍微等一下,以便后续状态刷新
Thread.Sleep(500);
}
Expand Down
18 changes: 5 additions & 13 deletions NewLife.Agent/CommandHandler/Restart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@ public class Restart : BaseCommandHandler
/// <param name="service"></param>
public Restart(ServiceBase service) : base(service)
{
Cmd = CommandConst.Restart;
Description = "重启服务";
ShortcutKey = '4';
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.Restart;

/// <inheritdoc />
public override String Description { get; set; } = "重启服务";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '4';

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return Service.Host.IsRunning(Service.ServiceName);
}
public override Boolean IsShowMenu() => Service.Host.IsRunning(Service.ServiceName);

/// <inheritdoc/>
public override void Process(String[] args)
{
Service.Host.Restart(Service.ServiceName);

// 稍微等一下,以便后续状态刷新
Thread.Sleep(500);
}
Expand Down
11 changes: 2 additions & 9 deletions NewLife.Agent/CommandHandler/RunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ public class RunService : BaseCommandHandler
/// <param name="service"></param>
public RunService(ServiceBase service) : base(service)
{
Cmd = CommandConst.RunService;
Description = "执行服务";
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.RunService;

/// <inheritdoc />
public override String Description { get; set; } = "执行服务";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; }

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand Down
20 changes: 5 additions & 15 deletions NewLife.Agent/CommandHandler/RunSimulation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Xml.Linq;
using NewLife.Agent.Command;
using NewLife.Agent.Command;

namespace NewLife.Agent.CommandHandler;

Expand All @@ -14,22 +13,13 @@ public class RunSimulation : BaseCommandHandler
/// <param name="service"></param>
public RunSimulation(ServiceBase service) : base(service)
{
Cmd = CommandConst.RunSimulation;
Description = "模拟运行";
ShortcutKey = '5';
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.RunSimulation;

/// <inheritdoc />
public override String Description { get; set; } = "模拟运行";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '5';

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return !Service.Host.IsRunning(Service.ServiceName);
}
public override Boolean IsShowMenu() => !Service.Host.IsRunning(Service.ServiceName);

/// <inheritdoc/>
public override void Process(String[] args)
Expand Down
14 changes: 5 additions & 9 deletions NewLife.Agent/CommandHandler/ShowStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@ public class ShowStatus : BaseCommandHandler
/// <param name="service"></param>
public ShowStatus(ServiceBase service) : base(service)
{
Cmd = CommandConst.ShowStatus;
Description = "显示状态";
ShortcutKey = '1';
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.ShowStatus;

/// <inheritdoc />
public override String Description { get; set; } = "显示状态";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '1';

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand Down Expand Up @@ -57,6 +51,7 @@ public override void Process(String[] args)

// 执行文件路径
if (installed)
{
try
{
var cfg = Service.Host.QueryConfig(name);
Expand All @@ -66,6 +61,7 @@ public override void Process(String[] args)
{
if (XTrace.Log.Level <= LogLevel.Debug) XTrace.Log.Debug("", ex);
}
}

var asm = AssemblyX.Create(Assembly.GetExecutingAssembly());
Console.WriteLine();
Expand Down
18 changes: 5 additions & 13 deletions NewLife.Agent/CommandHandler/Start.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@ public class Start : BaseCommandHandler
/// <param name="service"></param>
public Start(ServiceBase service) : base(service)
{
Cmd = CommandConst.Start;
Description = "启动服务";
ShortcutKey = '3';
}

/// <inheritdoc/>
public override String Cmd { get; set; } = CommandConst.Start;

/// <inheritdoc />
public override String Description { get; set; } = "启动服务";

/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '3';

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return Service.Host.IsInstalled(Service.ServiceName) && !Service.Host.IsRunning(Service.ServiceName);
}
public override Boolean IsShowMenu() => Service.Host.IsInstalled(Service.ServiceName) && !Service.Host.IsRunning(Service.ServiceName);

/// <inheritdoc/>
public override void Process(String[] args)
{
Service.Host.Start(Service.ServiceName);

// 稍微等一下,以便后续状态刷新
Thread.Sleep(500);
}
Expand Down
Loading

0 comments on commit cff1323

Please sign in to comment.