Fix Converters

This commit is contained in:
glax 2024-02-04 17:47:36 +01:00
parent f593efda62
commit 8a640491cb
5 changed files with 26 additions and 44 deletions

View File

@ -7,7 +7,7 @@
<Authors>Glax</Authors>
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>2.1.1</Version>
<Version>2.2.0</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -1,6 +1,5 @@
using CShocker.Devices.Abstract;
using CShocker.Devices.APIs;
using CShocker.Ranges;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -16,33 +15,16 @@ public class ApiJsonConverter : JsonConverter
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>();
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>()!
);
return jo.ToObject<OpenShockHttp>()!;
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>()!
);
return jo.ToObject<OpenShockSerial>()!;
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>()!
);
return jo.ToObject<PiShockHttp>()!;
case DeviceApi.PiShockSerial:
throw new NotImplementedException();
default:

View File

@ -0,0 +1,12 @@
namespace CShocker.Ranges;
public readonly struct IntegerRange
{
public readonly int Min, Max;
public IntegerRange(int min, int max)
{
this.Min = min;
this.Max = max;
}
}

View File

@ -1,5 +1,4 @@
using CShocker.Devices.Abstract;
using CShocker.Shockers.Abstract;
using CShocker.Shockers.Abstract;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -17,22 +16,11 @@ public class ShockerJsonConverter : JsonConverter
JObject jo = JObject.Load(reader);
if (jo.ContainsKey("model")) //OpenShockShocker
{
return new OpenShockShocker(
jo.SelectToken("api")!.ToObject<Api>()!,
jo.SelectToken("name")!.Value<string>()!,
jo.SelectToken("id")!.Value<string>()!,
jo.SelectToken("rfId")!.Value<short>(),
(OpenShockShocker.OpenShockModel)jo.SelectToken("model")!.Value<byte>(),
jo.SelectToken("createdOn")!.Value<DateTime>(),
jo.SelectToken("isPaused")!.Value<bool>()
);
return jo.ToObject<OpenShockShocker>(serializer)!;
}
else //PiShockShocker
{
return new PiShockShocker(
jo.SelectToken("api")!.ToObject<Api>()!,
jo.SelectToken("Code")!.Value<string>()!
);
return jo.ToObject<PiShockShocker>(serializer)!;
}
throw new Exception();
}

View File

@ -1,5 +1,4 @@
using CShocker.Devices.Abstract;
using CShocker.Devices.Additional;
using CShocker.Devices.Additional;
using CShocker.Devices.APIs;
using CShocker.Ranges;
using CShocker.Shockers;
@ -14,9 +13,9 @@ string? apiKey = Console.ReadLine();
while(apiKey is null || apiKey.Length < 1)
apiKey = Console.ReadLine();
/*
OpenShockHttp openShockHttp = new (new IntensityRange(30, 50), new DurationRange(1000, 1000), apiKey, logger: logger);
OpenShockShocker shocker = openShockHttp.GetShockers(apiKey).First();
OpenShockShocker shocker = openShockHttp.GetShockers().First();
shocker.Control(ControlAction.Vibrate, 20, 1000);
File.WriteAllText("shockers.json", JsonConvert.SerializeObject(shocker));
@ -25,9 +24,9 @@ Thread.Sleep(1100); //Wait for previous to end
deserialized.Control(ControlAction.Vibrate, 20, 1000);
shocker.Dispose();
deserialized.Dispose();
*/
/*
#pragma warning disable CA1416
List<SerialPortInfo> serialPorts = SerialHelper.GetSerialPorts();
@ -53,5 +52,6 @@ File.WriteAllText("shockers.json", JsonConvert.SerializeObject(shocker));
OpenShockShocker deserialized = JsonConvert.DeserializeObject<OpenShockShocker>(File.ReadAllText("shockers.json"), new ApiJsonConverter())!;
shocker.Dispose();
deserialized.Dispose();
*/
logger.Dispose();