CShock/CShocker/Shockers/Additional/ShockerJsonConverter.cs
glax ac19e20fb7 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
2024-01-29 15:37:19 +01:00

48 lines
1.5 KiB
C#

using CShocker.Shockers.Abstract;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CShocker.Shockers.Additional;
public class ShockerJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IShocker));
}
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
if (jo.ContainsKey("model")) //OpenShockShocker
{
return new OpenShockShocker()
{
name = jo.SelectToken("name")!.Value<string>()!,
id = jo.SelectToken("id")!.Value<string>()!,
rfId = jo.SelectToken("rfId")!.Value<short>(),
model = (OpenShockShocker.OpenShockModel)jo.SelectToken("model")!.Value<byte>(),
createdOn = jo.SelectToken("createdOn")!.Value<DateTime>(),
isPaused = jo.SelectToken("isPaused")!.Value<bool>()
};
}
else //PiShockShocker
{
return new PiShockShocker()
{
Code = jo.SelectToken("Code")!.Value<string>()!
};
}
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");
}
}