-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathCommandDispatcher.cs
More file actions
138 lines (127 loc) · 4.54 KB
/
CommandDispatcher.cs
File metadata and controls
138 lines (127 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using autoShell.Handlers;
using autoShell.Handlers.Settings;
using autoShell.Logging;
using autoShell.Services;
using Newtonsoft.Json.Linq;
namespace autoShell;
/// <summary>
/// Routes incoming JSON commands to the appropriate handler via a direct dictionary lookup.
/// </summary>
internal class CommandDispatcher
{
private readonly Dictionary<string, ICommandHandler> _handlers = new(StringComparer.OrdinalIgnoreCase);
private readonly ILogger _logger;
public CommandDispatcher(ILogger logger)
{
_logger = logger;
}
/// <summary>
/// Creates a <see cref="CommandDispatcher"/> with all production services and handlers registered.
/// </summary>
public static CommandDispatcher Create(ILogger logger)
{
return Create(
logger,
new WindowsRegistryService(),
new WindowsSystemParametersService(),
new WindowsProcessService(),
new WindowsAudioService(logger),
new WindowsAppRegistry(logger),
new WindowsDebuggerService(),
new WindowsBrightnessService(logger),
new WindowsDisplayService(logger),
new WindowsWindowService(logger),
new WindowsNetworkService(logger),
new WindowsVirtualDesktopService(logger)
);
}
/// <summary>
/// Creates a <see cref="CommandDispatcher"/> with the specified services, enabling integration testing
/// with mock services while exercising real handler wiring.
/// </summary>
internal static CommandDispatcher Create(
ILogger logger,
IRegistryService registry,
ISystemParametersService systemParams,
IProcessService process,
IAudioService audio,
IAppRegistry appRegistry,
IDebuggerService debugger,
IBrightnessService brightness,
IDisplayService display,
IWindowService window,
INetworkService network,
IVirtualDesktopService virtualDesktop)
{
var dispatcher = new CommandDispatcher(logger);
dispatcher.Register(
new AudioCommandHandler(audio),
new AppCommandHandler(appRegistry, process, window, logger),
new WindowCommandHandler(appRegistry, window),
new ThemeCommandHandler(registry, process, systemParams),
new VirtualDesktopCommandHandler(appRegistry, window, virtualDesktop, logger),
new NetworkCommandHandler(network, process, logger),
new DisplayCommandHandler(display, logger),
new TaskbarSettingsHandler(registry),
new DisplaySettingsHandler(registry, process, brightness, logger),
new PersonalizationSettingsHandler(registry, process),
new MouseSettingsHandler(systemParams, process, logger),
new AccessibilitySettingsHandler(registry, process),
new PrivacySettingsHandler(registry),
new PowerSettingsHandler(registry, process),
new FileExplorerSettingsHandler(registry),
new SystemSettingsHandler(registry, process, logger),
new SystemCommandHandler(process, debugger)
);
return dispatcher;
}
/// <summary>
/// Registers one or more command handlers with the dispatcher.
/// </summary>
public void Register(params ICommandHandler[] handlers)
{
foreach (var handler in handlers)
{
foreach (string command in handler.SupportedCommands)
{
_handlers[command] = handler;
}
}
}
/// <summary>
/// Dispatches all commands in a JSON object to their handlers.
/// </summary>
/// <returns>True if a "quit" command was encountered; otherwise false.</returns>
public bool Dispatch(JObject root)
{
foreach (var kvp in root)
{
string key = kvp.Key;
if (key == "quit")
{
return true;
}
try
{
if (_handlers.TryGetValue(key, out ICommandHandler handler))
{
string value = kvp.Value?.ToString();
handler.Handle(key, value, kvp.Value);
}
else
{
_logger.Debug("Unknown command: " + key);
}
}
catch (Exception ex)
{
_logger.Error(ex);
}
}
return false;
}
}