2024-02-01 23:03:28 +01:00
|
|
|
|
using CShocker.Devices.Abstract;
|
|
|
|
|
using CShocker.Shockers.Abstract;
|
2024-01-29 15:37:19 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
|
|
namespace CShocker.Shockers.Additional;
|
|
|
|
|
|
|
|
|
|
public class ShockerJsonConverter : JsonConverter
|
|
|
|
|
{
|
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
|
|
|
{
|
2024-02-01 23:03:28 +01:00
|
|
|
|
return (objectType == typeof(Shocker));
|
2024-01-29 15:37:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
JObject jo = JObject.Load(reader);
|
|
|
|
|
if (jo.ContainsKey("model")) //OpenShockShocker
|
|
|
|
|
{
|
2024-02-01 23:03:28 +01:00
|
|
|
|
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>()
|
|
|
|
|
);
|
2024-01-29 15:37:19 +01:00
|
|
|
|
}
|
|
|
|
|
else //PiShockShocker
|
|
|
|
|
{
|
2024-02-01 23:03:28 +01:00
|
|
|
|
return new PiShockShocker(
|
|
|
|
|
jo.SelectToken("api")!.ToObject<Api>()!,
|
|
|
|
|
jo.SelectToken("Code")!.Value<string>()!
|
|
|
|
|
);
|
2024-01-29 15:37:19 +01:00
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|