Add TestApp

This commit is contained in:
glax 2024-01-20 20:02:12 +01:00
parent 610518e5b5
commit 076b6fc271
5 changed files with 134 additions and 2 deletions

View File

@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CShocker", "CShocker\CShocker.csproj", "{244585F2-3AD8-4B3A-B6DA-3B0D3EFB745F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.csproj", "{3C3879F1-AB10-41C7-BF1A-3C1DA850970B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -12,5 +14,9 @@ Global
{244585F2-3AD8-4B3A-B6DA-3B0D3EFB745F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{244585F2-3AD8-4B3A-B6DA-3B0D3EFB745F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{244585F2-3AD8-4B3A-B6DA-3B0D3EFB745F}.Release|Any CPU.Build.0 = Release|Any CPU
{3C3879F1-AB10-41C7-BF1A-3C1DA850970B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C3879F1-AB10-41C7-BF1A-3C1DA850970B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C3879F1-AB10-41C7-BF1A-3C1DA850970B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C3879F1-AB10-41C7-BF1A-3C1DA850970B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -6,13 +6,34 @@ namespace CShocker.Shockers.APIS;
public class PiShockSerial : SerialShocker
{
public PiShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, ShockerApi.PiShockSerial, logger)
private const int BaudRate = 115200;
public PiShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, SerialPortInfo serialPortI, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, serialPortI, BaudRate, ShockerApi.PiShockSerial, logger)
{
throw new NotImplementedException();
}
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
throw new NotImplementedException();
string json = "{" +
"\"cmd\": \"operate\"," +
"\"value\":{" +
$"\"op\": \"{ControlActionToOp(action)}\"," +
$"\"duration\": {duration}," +
$"\"intensity\": {intensity}," +
$"\"id\": " +
"}" +
"}";
serialPort.WriteLine(json);
}
private static string ControlActionToOp(ControlAction action)
{
return action switch
{
ControlAction.Beep => "",
ControlAction.Vibrate => "vibrate",
ControlAction.Shock => "",
_ => ""
};
}
}

63
TestApp/Logger.cs Normal file
View File

@ -0,0 +1,63 @@
using Microsoft.Extensions.Logging;
public class Logger : ILogger
{
private LogLevel _enabledLoglevel;
private readonly ConsoleColor _defaultForegroundColor = Console.ForegroundColor;
private readonly ConsoleColor _defaultBackgroundColor = Console.BackgroundColor;
public Logger(LogLevel logLevel = LogLevel.Trace)
{
_enabledLoglevel = logLevel;
}
public void UpdateLogLevel(LogLevel logLevel)
{
this._enabledLoglevel = logLevel;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
return;
Console.ForegroundColor = ForegroundColorForLogLevel(logLevel);
Console.BackgroundColor = BackgroundColorForLogLevel(logLevel);
Console.Write(logLevel.ToString()[..3].ToUpper());
Console.ResetColor();
// ReSharper disable once LocalizableElement
Console.Write($" [{DateTime.UtcNow:HH:mm:ss.fff}] ");
Console.WriteLine(formatter.Invoke(state, exception));
}
public bool IsEnabled(LogLevel logLevel)
{
return logLevel >= _enabledLoglevel;
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
private ConsoleColor ForegroundColorForLogLevel(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Error or LogLevel.Critical => ConsoleColor.Black,
LogLevel.Debug => ConsoleColor.Black,
LogLevel.Information => ConsoleColor.White,
_ => _defaultForegroundColor
};
}
private ConsoleColor BackgroundColorForLogLevel(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Error or LogLevel.Critical => ConsoleColor.Red,
LogLevel.Debug => ConsoleColor.Yellow,
LogLevel.Information => ConsoleColor.Black,
_ => _defaultBackgroundColor
};
}
}

28
TestApp/Program.cs Normal file
View File

@ -0,0 +1,28 @@
using CShocker.Ranges;
using CShocker.Shockers.Abstract;
using CShocker.Shockers.APIS;
using Microsoft.Extensions.Logging;
Logger logger = new (LogLevel.Trace);
#pragma warning disable CA1416
List<SerialShocker.SerialPortInfo> serialPorts = SerialShocker.GetSerialPorts();
if (serialPorts.Count < 1)
return;
for(int i = 0; i < serialPorts.Count; i++)
Console.WriteLine($"{i}) {serialPorts[i]}");
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:");
string? selectedPortStr = Console.ReadLine();
int selectedPort = -1;
while (!int.TryParse(selectedPortStr, out selectedPort) || selectedPort < 0 || selectedPort > serialPorts.Count - 1)
{
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:");
selectedPortStr = Console.ReadLine();
}
OpenShockSerial shockSerial = new (new Dictionary<string, OpenShockSerial.ShockerModel>(), new IntensityRange(30,50), new DurationRange(1000,1000), serialPorts[selectedPort], logger);
Dictionary<string, OpenShockSerial.ShockerModel> shockers = shockSerial.GetShockers();
Console.ReadKey();

14
TestApp/TestApp.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CShocker\CShocker.csproj" />
</ItemGroup>
</Project>