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:
9
CShocker/Devices/Additional/ControlActionEnum.cs
Normal file
9
CShocker/Devices/Additional/ControlActionEnum.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace CShocker.Devices.Additional;
|
||||
|
||||
public enum ControlAction
|
||||
{
|
||||
Beep,
|
||||
Vibrate,
|
||||
Shock,
|
||||
Nothing
|
||||
}
|
9
CShocker/Devices/Additional/DeviceApiEnum.cs
Normal file
9
CShocker/Devices/Additional/DeviceApiEnum.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace CShocker.Devices.Additional;
|
||||
|
||||
public enum DeviceApi : byte
|
||||
{
|
||||
OpenShockHttp = 0,
|
||||
OpenShockSerial = 1,
|
||||
PiShockHttp = 2,
|
||||
PiShockSerial = 3
|
||||
}
|
61
CShocker/Devices/Additional/DeviceJsonConverter.cs
Normal file
61
CShocker/Devices/Additional/DeviceJsonConverter.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using CShocker.Devices.Abstract;
|
||||
using CShocker.Ranges;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace CShocker.Devices.Additional;
|
||||
|
||||
public class DeviceJsonConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof(Device));
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
{
|
||||
JObject jo = JObject.Load(reader);
|
||||
DeviceApi? apiType = (DeviceApi?)jo.SelectToken("ApiType")?.Value<byte>();
|
||||
|
||||
switch (apiType)
|
||||
{
|
||||
case DeviceApi.OpenShockHttp:
|
||||
return new OpenShockHttp(
|
||||
jo.SelectToken("IntensityRange")!.ToObject<IntensityRange>()!,
|
||||
jo.SelectToken("DurationRange")!.ToObject<DurationRange>()!,
|
||||
jo.SelectToken("ApiKey")!.Value<string>()!,
|
||||
jo.SelectToken("Endpoint")!.Value<string>()!
|
||||
);
|
||||
case DeviceApi.OpenShockSerial:
|
||||
return new OpenShockSerial(
|
||||
jo.SelectToken("IntensityRange")!.ToObject<IntensityRange>()!,
|
||||
jo.SelectToken("DurationRange")!.ToObject<DurationRange>()!,
|
||||
jo.SelectToken("SerialPortI")!.ToObject<SerialPortInfo>()!,
|
||||
jo.SelectToken("ApiKey")!.Value<string>()!,
|
||||
jo.SelectToken("Endpoint")!.Value<string>()!
|
||||
);
|
||||
case DeviceApi.PiShockHttp:
|
||||
return new PiShockHttp(
|
||||
jo.SelectToken("IntensityRange")!.ToObject<IntensityRange>()!,
|
||||
jo.SelectToken("DurationRange")!.ToObject<DurationRange>()!,
|
||||
jo.SelectToken("ApiKey")!.Value<string>()!,
|
||||
jo.SelectToken("Username")!.Value<string>()!,
|
||||
jo.SelectToken("Endpoint")!.Value<string>()!
|
||||
);
|
||||
case DeviceApi.PiShockSerial:
|
||||
throw new NotImplementedException();
|
||||
default:
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanWrite => false;
|
||||
|
||||
/// <summary>
|
||||
/// Don't call this
|
||||
/// </summary>
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
throw new Exception("Dont call this");
|
||||
}
|
||||
}
|
47
CShocker/Devices/Additional/SerialHelper.cs
Normal file
47
CShocker/Devices/Additional/SerialHelper.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System.Management;
|
||||
using System.Runtime.Versioning;
|
||||
using CShocker.Devices.Abstract;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace CShocker.Devices.Additional;
|
||||
|
||||
public static class SerialHelper
|
||||
{
|
||||
[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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user