Rewrite Hierachy that shockers now contain the api they use.
This commit is contained in:
@ -1,5 +1,24 @@
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
using CShocker.Devices.Abstract;
|
||||
using CShocker.Devices.Additional;
|
||||
|
||||
public interface IShocker
|
||||
namespace CShocker.Shockers.Abstract;
|
||||
|
||||
public abstract class Shocker : IDisposable
|
||||
{
|
||||
public Api Api { get; }
|
||||
|
||||
internal Shocker(Api api)
|
||||
{
|
||||
this.Api = api;
|
||||
}
|
||||
|
||||
public void Control(ControlAction action, int? intensity = null, int? duration = null)
|
||||
{
|
||||
this.Api.Control(action, intensity, duration, this);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Api.Dispose();
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using CShocker.Shockers.Abstract;
|
||||
using CShocker.Devices.Abstract;
|
||||
using CShocker.Shockers.Abstract;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
@ -8,7 +9,7 @@ public class ShockerJsonConverter : JsonConverter
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof(IShocker));
|
||||
return (objectType == typeof(Shocker));
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
@ -16,22 +17,22 @@ public class ShockerJsonConverter : JsonConverter
|
||||
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>()
|
||||
};
|
||||
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>()
|
||||
);
|
||||
}
|
||||
else //PiShockShocker
|
||||
{
|
||||
return new PiShockShocker()
|
||||
{
|
||||
Code = jo.SelectToken("Code")!.Value<string>()!
|
||||
};
|
||||
return new PiShockShocker(
|
||||
jo.SelectToken("api")!.ToObject<Api>()!,
|
||||
jo.SelectToken("Code")!.Value<string>()!
|
||||
);
|
||||
}
|
||||
throw new Exception();
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using CShocker.Devices.Abstract;
|
||||
using CShocker.Shockers.Abstract;
|
||||
|
||||
namespace CShocker.Shockers;
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
public struct OpenShockShocker : IShocker
|
||||
public class OpenShockShocker : Shocker
|
||||
{
|
||||
public string name, id;
|
||||
public short rfId;
|
||||
@ -13,6 +14,18 @@ public struct OpenShockShocker : IShocker
|
||||
public DateTime createdOn;
|
||||
public bool isPaused;
|
||||
|
||||
public OpenShockShocker(Api api, string name, string id, short rfId, OpenShockModel model, DateTime createdOn, bool isPaused) : base (api)
|
||||
{
|
||||
if (api is not OpenShockApi)
|
||||
throw new Exception($"API-Type {api.GetType().FullName} is not usable with Shocker {this.GetType().FullName}");
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
this.rfId = rfId;
|
||||
this.model = model;
|
||||
this.createdOn = createdOn;
|
||||
this.isPaused = isPaused;
|
||||
}
|
||||
|
||||
public enum OpenShockModel : byte
|
||||
{
|
||||
CaiXianlin = 0,
|
||||
|
@ -1,8 +1,9 @@
|
||||
using CShocker.Shockers.Abstract;
|
||||
using CShocker.Devices.Abstract;
|
||||
using CShocker.Shockers.Abstract;
|
||||
|
||||
namespace CShocker.Shockers;
|
||||
|
||||
public struct PiShockShocker : IShocker
|
||||
public class PiShockShocker : Shocker
|
||||
{
|
||||
public string Code;
|
||||
|
||||
@ -20,4 +21,11 @@ public struct PiShockShocker : IShocker
|
||||
{
|
||||
return Code.GetHashCode();
|
||||
}
|
||||
|
||||
public PiShockShocker(Api api, string code) : base(api)
|
||||
{
|
||||
if (api is not PiShockApi)
|
||||
throw new Exception($"API-Type {api.GetType().FullName} is not usable with Shocker {this.GetType().FullName}");
|
||||
Code = code;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user