Fix OpenShockHttp: Wrong json caused Bad Request
Get OpenShock Shockers from API. Save Shockers for PiShock and OpenShock in different structs Implement Action Queue, to avoid synchronous actions getting lost. Moved SerialPortInfo to own file Created ShockerJsonConverter Better separation of Devices/APIs and Shockers
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
using CShocker.Ranges;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
|
||||
public abstract class HttpShocker : Shocker
|
||||
{
|
||||
protected readonly HttpClient HttpClient = new();
|
||||
// ReSharper disable twice MemberCanBeProtected.Global external usage
|
||||
public string Endpoint { get; init; }
|
||||
public string ApiKey { get; init; }
|
||||
|
||||
protected HttpShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string apiKey, string endpoint, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger)
|
||||
{
|
||||
Endpoint = endpoint;
|
||||
ApiKey = apiKey;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{base.ToString()}\n" +
|
||||
$"Endpoint: {Endpoint}\n" +
|
||||
$"ApiKey: {ApiKey}";
|
||||
}
|
||||
}
|
@@ -1,77 +0,0 @@
|
||||
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
|
||||
{
|
||||
public SerialPortInfo SerialPortI;
|
||||
protected readonly 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? s32Pos = caption?.IndexOf(" (COM");
|
||||
if (s32Pos > 0) // remove COM port from description
|
||||
caption = caption?.Substring(0, (int)s32Pos);
|
||||
|
||||
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}";
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,53 +1,5 @@
|
||||
using System.Reflection.Metadata;
|
||||
using CShocker.Ranges;
|
||||
using Microsoft.Extensions.Logging;
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
|
||||
public abstract class Shocker
|
||||
public interface IShocker
|
||||
{
|
||||
// ReSharper disable 4 times MemberCanBePrivate.Global external use
|
||||
public readonly List<string> ShockerIds;
|
||||
public readonly IntensityRange IntensityRange;
|
||||
public readonly DurationRange DurationRange;
|
||||
protected ILogger? Logger;
|
||||
public readonly ShockerApi ApiType;
|
||||
|
||||
public void Control(ControlAction action, string? shockerId = null, int? intensity = null, int? duration = null)
|
||||
{
|
||||
int i = intensity ?? IntensityRange.GetRandomRangeValue();
|
||||
int d = duration ?? DurationRange.GetRandomRangeValue();
|
||||
this.Logger?.Log(LogLevel.Information, $"{action} {(intensity is not null ? $"Overwrite {i}" : $"{i}")} {(duration is not null ? $"Overwrite {d}" : $"{d}")}");
|
||||
if (action is ControlAction.Nothing)
|
||||
return;
|
||||
if(shockerId is null)
|
||||
foreach (string shocker in ShockerIds)
|
||||
ControlInternal(action, shocker, i, d);
|
||||
else
|
||||
ControlInternal(action, shockerId, i, d);
|
||||
}
|
||||
|
||||
protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration);
|
||||
|
||||
protected Shocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ShockerApi apiType, ILogger? logger = null)
|
||||
{
|
||||
this.ShockerIds = shockerIds;
|
||||
this.IntensityRange = intensityRange;
|
||||
this.DurationRange = durationRange;
|
||||
this.ApiType = apiType;
|
||||
this.Logger = logger;
|
||||
}
|
||||
|
||||
public void SetLogger(ILogger? logger)
|
||||
{
|
||||
this.Logger = logger;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"ShockerType: {Enum.GetName(typeof(ShockerApi), this.ApiType)}\n" +
|
||||
$"Shocker-IDs: {string.Join(", ", this.ShockerIds)}\n" +
|
||||
$"IntensityRange: {IntensityRange}\n" +
|
||||
$"DurationRange: {DurationRange}";
|
||||
}
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
|
||||
public enum ShockerApi : byte
|
||||
{
|
||||
OpenShockHttp = 0,
|
||||
OpenShockSerial = 1,
|
||||
PiShockHttp = 2,
|
||||
PiShockSerial = 3
|
||||
}
|
Reference in New Issue
Block a user