JsonSerializer
This commit is contained in:
@ -4,7 +4,7 @@ using CShocker.Ranges;
|
||||
using CShocker.Shockers.Abstract;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CShocker.Shockers;
|
||||
namespace CShocker.Shockers.APIS;
|
||||
|
||||
public class OpenShockHttp : HttpShocker
|
||||
{
|
||||
@ -41,7 +41,7 @@ public class OpenShockHttp : HttpShocker
|
||||
};
|
||||
}
|
||||
|
||||
public OpenShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, logger)
|
||||
public OpenShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.OpenShockHttp, logger)
|
||||
{
|
||||
}
|
||||
}
|
@ -2,11 +2,11 @@
|
||||
using CShocker.Shockers.Abstract;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CShocker.Shockers;
|
||||
namespace CShocker.Shockers.APIS;
|
||||
|
||||
public class OpenShockSerial : SerialShocker
|
||||
{
|
||||
public OpenShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
|
||||
public OpenShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, ShockerApi.OpenShockSerial, logger)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
@ -2,11 +2,11 @@
|
||||
using CShocker.Shockers.Abstract;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CShocker.Shockers;
|
||||
namespace CShocker.Shockers.APIS;
|
||||
|
||||
public class PiShockHttp : HttpShocker
|
||||
{
|
||||
public PiShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, logger)
|
||||
public PiShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.PiShockHttp, logger)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
@ -2,11 +2,11 @@
|
||||
using CShocker.Shockers.Abstract;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CShocker.Shockers;
|
||||
namespace CShocker.Shockers.APIS;
|
||||
|
||||
public class PiShockSerial : SerialShocker
|
||||
{
|
||||
public PiShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
|
||||
public PiShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, ShockerApi.PiShockSerial, logger)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
@ -9,7 +9,7 @@ public abstract class HttpShocker : Shocker
|
||||
public string Endpoint { get; init; }
|
||||
public string ApiKey { get; init; }
|
||||
|
||||
protected HttpShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
|
||||
protected HttpShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger)
|
||||
{
|
||||
Endpoint = endpoint;
|
||||
ApiKey = apiKey;
|
||||
|
@ -5,7 +5,7 @@ namespace CShocker.Shockers.Abstract;
|
||||
|
||||
public abstract class SerialShocker : Shocker
|
||||
{
|
||||
protected SerialShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
|
||||
protected SerialShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger)
|
||||
{
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using CShocker.Ranges;
|
||||
using System.Reflection.Metadata;
|
||||
using CShocker.Ranges;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
@ -9,6 +10,7 @@ public abstract class Shocker
|
||||
public readonly IntensityRange IntensityRange;
|
||||
public readonly DurationRange DurationRange;
|
||||
protected ILogger? Logger;
|
||||
public readonly ShockerApi ApiType;
|
||||
|
||||
public void Control(ControlAction action, string? shockerId = null, int? intensity = null, int? duration = null)
|
||||
{
|
||||
@ -26,11 +28,12 @@ public abstract class Shocker
|
||||
|
||||
protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration);
|
||||
|
||||
protected Shocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null)
|
||||
protected Shocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ShockerApi apiType, ILogger? logger = null)
|
||||
{
|
||||
this.ShockerIds = shockerIds;
|
||||
this.IntensityRange = intensityRange;
|
||||
this.DurationRange = durationRange;
|
||||
this.ApiType = apiType;
|
||||
this.Logger = logger;
|
||||
}
|
||||
|
||||
|
9
CShocker/Shockers/Abstract/ShockerApi.cs
Normal file
9
CShocker/Shockers/Abstract/ShockerApi.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
|
||||
public enum ShockerApi
|
||||
{
|
||||
OpenShockHttp = 0,
|
||||
OpenShockSerial = 1,
|
||||
PiShockHttp = 2,
|
||||
PiShockSerial = 3
|
||||
}
|
49
CShocker/Shockers/ShockerJsonConverter.cs
Normal file
49
CShocker/Shockers/ShockerJsonConverter.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using CShocker.Ranges;
|
||||
using CShocker.Shockers.Abstract;
|
||||
using CShocker.Shockers.APIS;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace CShocker.Shockers;
|
||||
|
||||
public class ShockerJsonConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof(Shocker));
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
{
|
||||
JObject jo = JObject.Load(reader);
|
||||
ShockerApi? apiType = jo.SelectToken("ApiType")?.Value<ShockerApi>();
|
||||
|
||||
switch (apiType)
|
||||
{
|
||||
case ShockerApi.OpenShockHttp:
|
||||
return new OpenShockHttp(
|
||||
jo.SelectToken("ShockerIds")!.Value<List<string>>()!,
|
||||
jo.SelectToken("IntensityRange")!.Value<IntensityRange>()!,
|
||||
jo.SelectToken("DurationRange")!.Value<DurationRange>()!,
|
||||
jo.SelectToken("Endpoint")!.Value<string>()!,
|
||||
jo.SelectToken("ApiKey")!.Value<string>()!
|
||||
);
|
||||
case ShockerApi.OpenShockSerial:
|
||||
case ShockerApi.PiShockHttp:
|
||||
case ShockerApi.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");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user