2024-01-29 15:37:19 +01:00
|
|
|
|
using CShocker.Devices.Abstract;
|
2024-02-01 23:03:28 +01:00
|
|
|
|
using CShocker.Devices.APIs;
|
2024-01-17 22:16:40 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
2024-01-29 15:37:19 +01:00
|
|
|
|
namespace CShocker.Devices.Additional;
|
2024-01-17 22:16:40 +01:00
|
|
|
|
|
2024-02-01 23:03:28 +01:00
|
|
|
|
public class ApiJsonConverter : JsonConverter
|
2024-01-17 22:16:40 +01:00
|
|
|
|
{
|
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
|
|
|
{
|
2024-02-01 23:03:28 +01:00
|
|
|
|
return (objectType == typeof(Api));
|
2024-01-17 22:16:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
JObject jo = JObject.Load(reader);
|
2024-02-04 17:47:36 +01:00
|
|
|
|
DeviceApi apiType = (DeviceApi)jo.SelectToken("ApiType")!.Value<byte>();
|
|
|
|
|
|
2024-01-17 22:16:40 +01:00
|
|
|
|
switch (apiType)
|
|
|
|
|
{
|
2024-01-29 15:37:19 +01:00
|
|
|
|
case DeviceApi.OpenShockHttp:
|
2024-02-04 17:47:36 +01:00
|
|
|
|
return jo.ToObject<OpenShockHttp>()!;
|
2024-01-29 15:37:19 +01:00
|
|
|
|
case DeviceApi.OpenShockSerial:
|
2024-02-04 17:47:36 +01:00
|
|
|
|
return jo.ToObject<OpenShockSerial>()!;
|
2024-01-29 15:37:19 +01:00
|
|
|
|
case DeviceApi.PiShockHttp:
|
2024-02-04 17:47:36 +01:00
|
|
|
|
return jo.ToObject<PiShockHttp>()!;
|
2024-01-29 15:37:19 +01:00
|
|
|
|
case DeviceApi.PiShockSerial:
|
2024-01-17 22:16:40 +01:00
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|