88 lines
3.0 KiB
C#
88 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BlubbFish.IoT.Hue.Events;
|
|
using BlubbFish.IoT.Hue.lib;
|
|
using LitJson;
|
|
|
|
namespace BlubbFish.IoT.Hue.Interfaces {
|
|
public abstract class ALight : AConnector, IMqtt {
|
|
public enum Types {
|
|
Dimmablelight
|
|
}
|
|
public enum TypesIgnore {
|
|
Colorlight,
|
|
Extendedcolorlight
|
|
}
|
|
|
|
#region Properties
|
|
public Int32 LightId { get; }
|
|
public Types Lightclass { get; }
|
|
public DateTime LastUpdate { get; protected set; }
|
|
public String Name { get; protected set; }
|
|
public Boolean Polling { get; set; }
|
|
public Boolean PollOnce { get; set; }
|
|
#endregion
|
|
|
|
#region Constructor
|
|
protected ALight(JsonData json, Tuple<Int32, Types> id, HttpConnection http, Boolean polling) {
|
|
this.LightId = id.Item1;
|
|
this.Lightclass = id.Item2;
|
|
this.http = http;
|
|
this.LastUpdate = DateTime.Now;
|
|
this.Polling = polling;
|
|
this.ComplexInit(json);
|
|
}
|
|
|
|
internal static ALight CreateLight(JsonData json, Tuple<Int32> id, HttpConnection http, Boolean polling) {
|
|
String type = "";
|
|
if (json.ContainsKey("type")) {
|
|
type = json["type"].ToString().ToEnumString();
|
|
}
|
|
if (type != "" && !Enum.IsDefined(typeof(TypesIgnore), type) && Enum.IsDefined(typeof(Types), type)) {
|
|
String name = "BlubbFish.IoT.Hue.Devices.Lights." + type;
|
|
return GetInstanceConcrete(name, json, new Tuple<Int32, Types>(id.Item1, (Types)Enum.Parse(typeof(Types), type)), http, polling);
|
|
}
|
|
if(!Enum.IsDefined(typeof(TypesIgnore), type) && !Enum.IsDefined(typeof(Types), type)) {
|
|
Helper.WriteError("Lightclass " + type + " not exist.");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static ALight GetInstanceConcrete(String name, JsonData json, Tuple<Int32, Types> id, HttpConnection http, Boolean polling) {
|
|
Type t = null;
|
|
try {
|
|
t = Type.GetType(name, true);
|
|
} catch (TypeLoadException) {
|
|
Helper.WriteError("Konnte Type " + name + " nicht laden!");
|
|
return null;
|
|
}
|
|
return (ALight)t.GetConstructor(new Type[] { typeof(JsonData), typeof(Tuple<Int32, Types>), typeof(HttpConnection), typeof(Boolean) }).Invoke(new Object[] { json, id, http, polling });
|
|
}
|
|
#endregion
|
|
|
|
private void ComplexInit(JsonData json) {
|
|
if (json.ContainsKey("name")) {
|
|
this.Name = json["name"].ToString();
|
|
}
|
|
}
|
|
|
|
protected void SetLightState(Dictionary<String, Object> value) {
|
|
this.PutDictionary("lights/" + this.LightId + "/state", value);
|
|
}
|
|
|
|
#region IMqtt
|
|
public String MqttTopic() {
|
|
return "lights/" + this.LightId + "/" + this.Lightclass.ToString();
|
|
}
|
|
|
|
public String ToJson() {
|
|
Dictionary<String, Object> json = this.ToDictionary();
|
|
json.Add("LastUpdate", this.LastUpdate.ToString());
|
|
json.Add("Name", this.Name);
|
|
json.Add("Lightclass", this.Lightclass.ToString());
|
|
return JsonMapper.ToJson(json);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|