Compare commits
2 Commits
09ce5cbedc
...
076b6fc271
Author | SHA1 | Date | |
---|---|---|---|
076b6fc271 | |||
610518e5b5 |
@ -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
|
||||
|
@ -7,12 +7,14 @@
|
||||
<Authors>Glax</Authors>
|
||||
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<Version>1.2.5</Version>
|
||||
<Version>1.3.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
|
||||
<PackageReference Include="System.Management" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
@ -1,4 +1,5 @@
|
||||
using CShocker.Ranges;
|
||||
using System.Text.RegularExpressions;
|
||||
using CShocker.Ranges;
|
||||
using CShocker.Shockers.Abstract;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@ -6,13 +7,57 @@ namespace CShocker.Shockers.APIS;
|
||||
|
||||
public class OpenShockSerial : SerialShocker
|
||||
{
|
||||
public OpenShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, ShockerApi.OpenShockSerial, logger)
|
||||
public readonly Dictionary<string, ShockerModel> Model;
|
||||
private const int BaudRate = 115200;
|
||||
public OpenShockSerial(Dictionary<string, ShockerModel> shockerIds, IntensityRange intensityRange, DurationRange durationRange, SerialPortInfo serialPortI, ILogger? logger = null) : base(shockerIds.Keys.ToList(), intensityRange, durationRange, serialPortI, BaudRate, ShockerApi.OpenShockSerial, logger)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
this.Model = shockerIds;
|
||||
}
|
||||
|
||||
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
string json = "{" +
|
||||
$"\"model\":\"{Enum.GetName(Model[shockerId])!.ToLower()}\"," +
|
||||
$"\"id\":{shockerId}," +
|
||||
$"\"type\":\"{ControlActionToString(action)}\"," +
|
||||
$"\"intensity\":{intensity}," +
|
||||
$"\"durationMs\":{duration}" +
|
||||
"}";
|
||||
serialPort.WriteLine(json);
|
||||
}
|
||||
|
||||
public Dictionary<string, ShockerModel> GetShockers()
|
||||
{
|
||||
Dictionary<string, ShockerModel> ret = new();
|
||||
Regex shockerRex = new (@".*FetchDeviceInfo\(\): \[GatewayConnectionManager\] \[[a-z0-9\-]+\] rf=([0-9]{1,5}) model=([0,1])");
|
||||
this.Logger?.Log(LogLevel.Debug, "Restart");
|
||||
serialPort.WriteLine("restart");
|
||||
while (serialPort.ReadLine() is { } line && !line.Contains("Successfully verified auth token"))
|
||||
{
|
||||
this.Logger?.Log(LogLevel.Trace, line);
|
||||
Match match = shockerRex.Match(line);
|
||||
if (match.Success)
|
||||
ret.Add(match.Groups[1].Value, Enum.Parse<ShockerModel>(match.Groups[2].Value));
|
||||
}
|
||||
this.Logger?.Log(LogLevel.Debug, $"Shockers found: \n\t{string.Join("\n\t", ret)}");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public enum ShockerModel : byte
|
||||
{
|
||||
Caixianlin = 0,
|
||||
Petrainer = 1
|
||||
}
|
||||
|
||||
private string ControlActionToString(ControlAction action)
|
||||
{
|
||||
return action switch
|
||||
{
|
||||
ControlAction.Beep => "sound",
|
||||
ControlAction.Vibrate => "vibrate",
|
||||
ControlAction.Shock => "shock",
|
||||
_ => "stop"
|
||||
};
|
||||
}
|
||||
}
|
@ -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 => "",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
}
|
@ -1,11 +1,77 @@
|
||||
using CShocker.Ranges;
|
||||
using System.IO.Ports;
|
||||
using CShocker.Ranges;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Management;
|
||||
using System.Runtime.Versioning;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
|
||||
public abstract class SerialShocker : Shocker
|
||||
{
|
||||
protected SerialShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger)
|
||||
public SerialPortInfo SerialPortI;
|
||||
protected SerialPort serialPort;
|
||||
|
||||
protected SerialShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, SerialPortInfo serialPortI, int baudRate, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger)
|
||||
{
|
||||
this.SerialPortI = serialPortI;
|
||||
this.serialPort = new SerialPort(serialPortI.PortName, baudRate);
|
||||
this.serialPort.Open();
|
||||
}
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static List<SerialPortInfo> GetSerialPorts()
|
||||
{
|
||||
List<SerialPortInfo> ret = new();
|
||||
using (ManagementClass entity = new("Win32_PnPEntity"))
|
||||
{
|
||||
// ReSharper disable once InconsistentNaming
|
||||
const string CUR_CTRL = "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\";
|
||||
|
||||
foreach (ManagementObject instance in entity.GetInstances())
|
||||
{
|
||||
object oGuid;
|
||||
oGuid = instance.GetPropertyValue("ClassGuid");
|
||||
if (oGuid == null || oGuid.ToString()?.ToUpper().Equals("{4D36E978-E325-11CE-BFC1-08002BE10318}") is false)
|
||||
continue; // Skip all devices except device class "PORTS"
|
||||
|
||||
string? caption = instance.GetPropertyValue("Caption")?.ToString();
|
||||
string? manufacturer = instance.GetPropertyValue("Manufacturer")?.ToString();
|
||||
string? deviceID = instance.GetPropertyValue("PnpDeviceID")?.ToString();
|
||||
string regEnum = CUR_CTRL + "Enum\\" + deviceID + "\\Device Parameters";
|
||||
string? portName = Registry.GetValue(regEnum, "PortName", "")?.ToString();
|
||||
|
||||
int s32_Pos = caption.IndexOf(" (COM");
|
||||
if (s32_Pos > 0) // remove COM port from description
|
||||
caption = caption.Substring(0, s32_Pos);
|
||||
|
||||
ret.Add(new SerialPortInfo(
|
||||
portName,
|
||||
caption,
|
||||
manufacturer,
|
||||
deviceID));
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public class SerialPortInfo
|
||||
{
|
||||
public readonly string? PortName, Description, Manufacturer, DeviceID;
|
||||
|
||||
public SerialPortInfo(string? portName, string? description, string? manufacturer, string? deviceID)
|
||||
{
|
||||
this.PortName = portName;
|
||||
this.Description = description;
|
||||
this.Manufacturer = manufacturer;
|
||||
this.DeviceID = deviceID;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return
|
||||
$"PortName: {PortName}\nDescription: {Description}\nManufacturer: {Manufacturer}\nDeviceID: {DeviceID}";
|
||||
}
|
||||
}
|
||||
}
|
63
TestApp/Logger.cs
Normal file
63
TestApp/Logger.cs
Normal 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
28
TestApp/Program.cs
Normal 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
14
TestApp/TestApp.csproj
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user