Skip to content

Commit 59a1969

Browse files
committed
Added logging for KeyboardKeys class, clean up
1 parent e3f2c06 commit 59a1969

File tree

3 files changed

+72
-67
lines changed

3 files changed

+72
-67
lines changed

Windows/ConsoleApp/Logging/Logging.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ZwiftPlayConsoleApp.BLE;
22
using ZwiftPlayConsoleApp.Zap.Proto;
3+
using ZwiftPlayConsoleApp.Utils;
34

45
namespace ZwiftPlayConsoleApp.Logging
56

@@ -18,6 +19,7 @@ public class LoggingConfig
1819
public bool EnableBleManagerLogging { get; set; }
1920
public bool EnableZwiftDeviceLogging { get; set; }
2021
public bool EnableControllerNotificationLogging { get; set; }
22+
public bool EnableKeyboardKeysLogging { get; set; }
2123
public bool EnableAppLogging { get; set; }
2224
}
2325

@@ -69,6 +71,7 @@ private bool ShouldLog()
6971
nameof(ZwiftPlayBleManager) => _config.EnableBleManagerLogging,
7072
nameof(ZwiftPlayDevice) => _config.EnableZwiftDeviceLogging,
7173
nameof(ControllerNotification) => _config.EnableControllerNotificationLogging,
74+
nameof(KeyboardKeys) => _config.EnableKeyboardKeysLogging,
7275
nameof(App) => _config.EnableAppLogging,
7376
_ => false
7477
};

Windows/ConsoleApp/Program.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using ZwiftPlayConsoleApp.Configuration;
44
using ZwiftPlayConsoleApp.Zap;
55
using Microsoft.Extensions.DependencyInjection;
6-
using Microsoft.Extensions.Logging;
76
using ZwiftPlayConsoleApp.Logging;
87
using ZwiftPlayConsoleApp.Utils;
98
using Microsoft.Extensions.Configuration;
@@ -101,38 +100,38 @@ private static void ConfigureServices(IServiceCollection services, Config config
101100
{
102101
ValidateConfiguration(config);
103102

103+
// 1. Configuration setup
104104
IConfiguration configuration = new ConfigurationBuilder()
105105
.SetBasePath(Directory.GetCurrentDirectory())
106106
.AddJsonFile("Configuration/AppSettings.json", optional: false)
107107
.Build();
108108

109-
services.Configure<AppSettings>(configuration.GetSection("AppSettings"));
110-
109+
// 2. Logging setup
111110
var loggingConfig = new LoggingConfig
112111
{
113112
EnableBleManagerLogging = false,
114113
EnableZwiftDeviceLogging = false,
115114
EnableControllerNotificationLogging = false,
115+
EnableKeyboardKeysLogging = false,
116116
EnableAppLogging = false
117117
};
118118

119-
KeyboardKeys.Initialize(config);
119+
// 3. Register configurations
120120
services.AddSingleton(config);
121+
services.AddSingleton(loggingConfig);
122+
services.Configure<AppSettings>(configuration.GetSection("AppSettings"));
121123

122-
services.AddLogging(builder =>
123-
{
124-
builder.SetMinimumLevel(LogLevel.Debug);
125-
builder.AddConsole(options =>
126-
{
127-
options.LogToStandardErrorThreshold = LogLevel.Debug;
128-
});
129-
});
124+
// 4. Register loggers
125+
var logger = new ConfigurableLogger(loggingConfig, nameof(App));
126+
services.AddSingleton<IZwiftLogger>(logger);
130127

131-
services.AddSingleton(loggingConfig);
132-
services.AddSingleton<IZwiftLogger>(provider => new ConfigurableLogger(loggingConfig));
128+
// 5. Initialize components
129+
KeyboardKeys.Initialize(config, logger);
130+
131+
// 6. Register application services
133132
services.AddSingleton<App>();
134133
services.AddSingleton<ZwiftPlayDevice>();
135-
}
134+
}
136135
private static void ValidateConfiguration(Config config)
137136
{
138137
if (config.UseMapping && string.IsNullOrEmpty(config.MappingFilePath))

Windows/ConsoleApp/Utils/KeyboardKeys.cs

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ZwiftPlayConsoleApp.BLE;
33
using ZwiftPlayConsoleApp.Configuration;
44
using System.Text.Json;
5+
using ZwiftPlayConsoleApp.Logging;
56

67
namespace ZwiftPlayConsoleApp.Utils;
78

@@ -53,67 +54,70 @@ public class KeyboardKeys
5354
public const int Z = 0x5A;
5455
public const int SUBTRACT = 0x6D; // Numpad minus
5556
public const int ADD = 0x6B; // Numpad plus
56-
5757
private static Config? _config;
58-
59-
public static void Initialize(Config config)
58+
private static ConfigurableLogger? _logger;
59+
public static void Initialize(Config config, IZwiftLogger logger)
6060
{
6161
_config = config;
62+
_logger = new ConfigurableLogger(
63+
((ConfigurableLogger)logger)._config,
64+
nameof(KeyboardKeys)
65+
);
6266
}
63-
public static void ProcessZwiftPlay(ButtonChange change)
64-
{
65-
if (_config == null || !_config.SendKeys)
66-
{
67-
return;
68-
}
69-
70-
(byte? keyCode, bool withShift) = _config.UseMapping
71-
? GetMappedKey(_config.KeyboardMapping.ButtonToKeyMap, change.Button)
72-
: (GetKeyCode(change.Button), false);
73-
74-
if (keyCode == null)
75-
{
76-
return;
77-
}
78-
79-
if (change.IsPressed)
80-
{
81-
PressKey((byte)keyCode, withShift);
82-
}
83-
else
84-
{
85-
ReleaseKey((byte)keyCode, withShift);
86-
}
87-
}
88-
89-
private static (byte? keyCode, bool withShift) GetMappedKey(Dictionary<ZwiftPlayButton, KeyMapping> mapping, ZwiftPlayButton button)
90-
{
91-
if (mapping.TryGetValue(button, out var keyMapping))
92-
{
93-
var withShift = keyMapping.OriginalMapping.StartsWith("SHIFT+", StringComparison.OrdinalIgnoreCase);
94-
//Console.WriteLine($"Mapped {button} to {keyMapping.OriginalMapping} with shift: {withShift}");
95-
return (keyMapping.KeyCode, withShift);
96-
}
97-
return (null, false);
98-
}
99-
100-
private static byte? GetKeyCode(ZwiftPlayButton button)
101-
{
102-
switch (button)
103-
{
104-
case ZwiftPlayButton.Up:
105-
return UP;
106-
case ZwiftPlayButton.Down:
107-
return DOWN;
108-
case ZwiftPlayButton.Left:
109-
return LEFT;
67+
public static void ProcessZwiftPlay(ButtonChange change)
68+
{
69+
if (_config == null || !_config.SendKeys)
70+
{
71+
return;
72+
}
73+
74+
(byte? keyCode, bool withShift) = _config.UseMapping
75+
? GetMappedKey(_config.KeyboardMapping.ButtonToKeyMap, change.Button)
76+
: (GetKeyCode(change.Button), false);
77+
78+
if (keyCode == null)
79+
{
80+
return;
81+
}
82+
83+
if (change.IsPressed)
84+
{
85+
PressKey((byte)keyCode, withShift);
86+
}
87+
else
88+
{
89+
ReleaseKey((byte)keyCode, withShift);
90+
}
91+
}
92+
93+
private static (byte? keyCode, bool withShift) GetMappedKey(Dictionary<ZwiftPlayButton, KeyMapping> mapping, ZwiftPlayButton button)
94+
{
95+
if (mapping.TryGetValue(button, out var keyMapping))
96+
{
97+
var withShift = keyMapping.OriginalMapping.StartsWith("SHIFT+", StringComparison.OrdinalIgnoreCase);
98+
_logger?.LogInfo($"Mapped {button} to {keyMapping.OriginalMapping} with shift: {withShift}");
99+
//Console.WriteLine($"Mapped {button} to {keyMapping.OriginalMapping} with shift: {withShift}");
100+
return (keyMapping.KeyCode, withShift);
101+
}
102+
return (null, false);
103+
}
104+
105+
private static byte? GetKeyCode(ZwiftPlayButton button)
106+
{
107+
switch (button)
108+
{
109+
case ZwiftPlayButton.Up:
110+
return UP;
111+
case ZwiftPlayButton.Down:
112+
return DOWN;
113+
case ZwiftPlayButton.Left:
114+
return LEFT;
110115
case ZwiftPlayButton.Right:
111116
return RIGHT;
112117
case ZwiftPlayButton.LeftShoulder:
113118
return LCONTROL;
114119
case ZwiftPlayButton.LeftPower:
115120
break;
116-
117121
case ZwiftPlayButton.A:
118122
return A;
119123
case ZwiftPlayButton.B:
@@ -126,7 +130,6 @@ private static (byte? keyCode, bool withShift) GetMappedKey(Dictionary<ZwiftPlay
126130
return RCONTROL;
127131
case ZwiftPlayButton.RightPower:
128132
break;
129-
130133
default:
131134
throw new ArgumentOutOfRangeException(nameof(button), button, null);
132135
}

0 commit comments

Comments
 (0)