Skip to content

Commit

Permalink
兼容旧版自定义菜单,并标记为已过时
Browse files Browse the repository at this point in the history
  • Loading branch information
andywu188 committed May 20, 2024
1 parent ab334d4 commit cb4d34f
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 51 deletions.
14 changes: 6 additions & 8 deletions NewLife.Agent/Command/CommandHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,32 @@ public CommandFactory(ServiceBase service, params Assembly[] customCommandHandle
/// </summary>
/// <param name="cmd">命令</param>
/// <param name="args">参数</param>
public void Handle(String cmd, String[] args = null)
public Boolean Handle(String cmd, String[] args = null)
{
if (_commandHandlerDict.TryGetValue(cmd, out var handler))
{
handler.Process(args);
return true;
}
else
{
Console.WriteLine($"您输入的命令参数 [{cmd}] 无效,请重新输入!");
}
return false;
}

/// <summary>
/// 根据快捷键处理命令
/// </summary>
/// <param name="key"></param>
/// <param name="args"></param>
public void Handle(Char key, String[] args = null)
public Boolean Handle(Char key, String[] args = null)
{
foreach (var commandHandler in _commandHandlerList)
{
if (commandHandler.ShortcutKey == key && commandHandler.IsShowMenu())
{
Handle(commandHandler.Cmd, args);
return;
return true;
}
}
Console.WriteLine($"您输入的命令序号 [{key}] 无效,请重新输入!");
return false;
}

/// <summary>
Expand Down
8 changes: 1 addition & 7 deletions NewLife.Agent/Command/InstallAndStartCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ public InstallAndStartCommandHandler(ServiceBase service) : base(service)
/// <inheritdoc />
public override Char? ShortcutKey { get; set; }

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return false;
}

/// <inheritdoc/>
public override void Process(String[] args)
{
// 可能服务已存在,安装时报错,但不要影响服务启动
try
{
new InstallCommandHandler(Service).Process(args);
Service.Command.Handle(CommandConst.Install, args);
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Agent/Command/InstallCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public InstallCommandHandler(ServiceBase service) : base(service)
public override String Cmd { get; set; } = CommandConst.Install;

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

/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '2';
Expand Down
8 changes: 1 addition & 7 deletions NewLife.Agent/Command/ReinstallCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ public ReinstallCommandHandler(ServiceBase service) : base(service)
/// <inheritdoc />
public override Char? ShortcutKey { get; set; }

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return false;
}

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand All @@ -52,7 +46,7 @@ private void Reinstall(String[] args)
XTrace.WriteException(ex);
}

new InstallCommandHandler(Service).Process(args);
Service.Command.Handle(CommandConst.Install, args);

// 稍微等待
for (var i = 0; i < 50; i++)
Expand Down
6 changes: 0 additions & 6 deletions NewLife.Agent/Command/RunServiceCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public RunServiceCommandHandler(ServiceBase service) : base(service)
/// <inheritdoc />
public override Char? ShortcutKey { get; set; }

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return false;
}

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand Down
6 changes: 0 additions & 6 deletions NewLife.Agent/Command/ShowStatusCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ public ShowStatusCommandHandler(ServiceBase service) : base(service)
/// <inheritdoc />
public override Char? ShortcutKey { get; set; } = '1';

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return true;
}

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand Down
6 changes: 0 additions & 6 deletions NewLife.Agent/Command/UninstallCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ public UninstallCommandHandler(ServiceBase service) : base(service)
/// <inheritdoc />
public override Char? ShortcutKey { get; set; }

/// <inheritdoc />
public override Boolean IsShowMenu()
{
return false;
}

/// <inheritdoc/>
public override void Process(String[] args)
{
Expand Down
18 changes: 18 additions & 0 deletions NewLife.Agent/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class Menu : IComparable<Menu>
/// <summary>命令</summary>
public String Cmd { get; set; }

/// <summary>回调方法</summary>
[Obsolete("建议定义命令处理类,并继承 BaseCommandHandler")]
public Action Callback { get; set; }

/// <summary>
/// 实例化
/// </summary>
Expand All @@ -27,6 +31,20 @@ public Menu(Char key, String name, String cmd)
Cmd = cmd;
}

/// <summary>
/// 实例化
/// </summary>
/// <param name="key"></param>
/// <param name="name"></param>
/// <param name="callback"></param>
[Obsolete("建议定义命令处理类,并继承 BaseCommandHandler")]
public Menu(Char key, String name, Action callback)
{
Key = key;
Name = name;
Callback = callback;
}

/// <summary>比较</summary>
/// <param name="other"></param>
/// <returns></returns>
Expand Down
46 changes: 44 additions & 2 deletions NewLife.Agent/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class ServiceBase : DisposeBase
/// <summary>
///
/// </summary>
protected readonly CommandFactory Command;
public CommandFactory Command { get; }

#region 构造
/// <summary>初始化</summary>
Expand Down Expand Up @@ -196,13 +196,27 @@ protected virtual void ProcessMenu()

try
{
Command.Handle(key.KeyChar, args);
var result = Command.Handle(key.KeyChar, args);
if (!result)
{
// 兼容旧版本自定义菜单,相关代码已过时
var menu = _Menus.FirstOrDefault(e => e.Key == key.KeyChar);
if (menu != null)
{
menu.Callback();

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / test

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'

Check warning on line 206 in NewLife.Agent/ServiceBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

'Menu.Callback' is obsolete: '建议定义命令处理类,并继承 BaseCommandHandler'
}
else
{
Console.WriteLine($"您输入的命令序号 [{key.KeyChar}] 无效,请重新输入!");
}
}
}
catch (Exception ex)
{
XTrace.WriteException(ex);
}
Console.WriteLine();
Thread.Sleep(1000);
}
}

Expand All @@ -222,10 +236,38 @@ protected virtual void ShowMenu()
Console.WriteLine($" {menu.Key}{menu.Name}\t{menu.Cmd}");
}

//兼容旧版本菜单,相关代码已过时
if (_Menus.Count > 0)
{
//foreach (var item in _Menus)
//{
// Console.WriteLine("{0} {1}", item.Key, item.Value.Name);
//}
foreach (var menu in _Menus)
{
Console.WriteLine($" {menu.Key}{menu.Name}\t");
}
}

Console.WriteLine($" 0、 退出\t");
Console.WriteLine();
Console.ForegroundColor = color;
}
private readonly List<Menu> _Menus = [];
/// <summary>添加菜单</summary>
/// <param name="key"></param>
/// <param name="name"></param>
/// <param name="callbak"></param>
[Obsolete("建议定义命令处理类,并继承 BaseCommandHandler")]
public void AddMenu(Char key, String name, Action callbak)
{
//if (!_Menus.ContainsKey(key))
//{
_Menus.RemoveAll(e => e.Key == key);
_Menus.Add(new Menu(key, name, callbak));
//}
}

#endregion

#region 服务控制
Expand Down
7 changes: 7 additions & 0 deletions Zero.Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ public MyServices()

DisplayName = "服务代理例程";
Description = "用于承载各种服务的服务代理!";

AddMenu('v', "测试回调菜单【已过时】", TestMenuCallback);
}
#endregion

private void TestMenuCallback()
{
Console.WriteLine("这是[测试回调菜单]输出");
}

#region 核心
private TimerX _timer;
private TimerX _timer2;
Expand Down
18 changes: 17 additions & 1 deletion Zero.Agent/TestCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Threading;
using NewLife.Agent;
using NewLife.Agent.Command;

Expand All @@ -12,23 +14,37 @@ public class TestCommandHandler : BaseCommandHandler

public TestCommandHandler(ServiceBase service) : base(service)
{
//本功能主要是用于演示如何定义自己的命令处理器,所有命令处理器必需指定Cmd和Description,
//ShortcutKey用于定义快捷键,如果不需要快捷键,可以不指定,将不会显示在菜单中
}

public override Boolean IsShowMenu()
{
return true;
//是否显示在菜单中,默认情况下,不需要重写,基类中自动判断是否有快捷键来决定是否显示
//另外,还可以根据服务的运行状态来决定是否显示,比如:
//只有服务已安装时才显示: return Service.Host.IsInstalled(Service.ServiceName);
//只有服务已在运行中时才显示: return Service.Host.IsRunning(Service.ServiceName);

return base.IsShowMenu();
}

public override void Process(String[] args)
{
Console.WriteLine("这是[测试自定义菜单]处理程序,开始输出 九九乘法表");
Thread.Sleep(1000);
for (var i = 1; i <= 9; i++)
{
for (var j = 1; j <= i; j++)
{
Console.Write($"{j}x{i}={i * j}\t");
}
Console.WriteLine();
Thread.Sleep(200);
}

if (args.Contains("-showme"))
{
Console.WriteLine("这是[测试自定义菜单]处理程序,显示了自定义参数 -showme");
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions Zero.Agent/WebWatchDogCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ public class WebWatchDogCommandHandler : WatchDogCommandHandler

public WebWatchDogCommandHandler(ServiceBase service) : base(service)
{
//本功能主要是用于演示如何覆盖默认的看门狗保护服务,实现自己的看门狗逻辑,其他需要覆盖基础命令处理器也可以参照这个类实现
}

public override Boolean IsShowMenu()
{
return true;
}


public override void Process(String[] args)
{
Console.WriteLine("这是[WEB看门狗保护服务]处理程序,开始检查WEB服务是否可以正常访问");
Expand Down
1 change: 0 additions & 1 deletion Zero.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public MyServices()

protected override void Init()
{
var set = Setting.Current;
base.Init();

// 依赖网络
Expand Down

0 comments on commit cb4d34f

Please sign in to comment.