Hue/Hue/Devices/Lights/Dimmablelight.cs
BlubbFish da8ffeeffc [NF] Huebot fertig
[NF] Huebot nutzt nun die lokale litjosn und mqtt library
2018-05-02 16:21:34 +00:00

114 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using BlubbFish.IoT.Hue.Events;
using BlubbFish.IoT.Hue.Interfaces;
using BlubbFish.IoT.Hue.lib;
using LitJson;
namespace BlubbFish.IoT.Hue.Devices.Lights {
public class Dimmablelight : ALight {
public enum AlertEffect {
none,
select,
lselect
}
#region Properties
private Boolean _state;
public Boolean State {
get {
return this._state;
}
set {
if(this.SetLightStateAttribute(new Dictionary<String, Object>() { { "on", value } })) {
this._state = value;
this.NotifyClient<LightUpdateEvent>(value);
}
}
}
private Byte _brightness;
public Byte Brightness {
get {
return this._brightness;
}
set {
if(this.SetLightStateAttribute(new Dictionary<String, Object>() { { "bri", value } })) {
this._brightness = value;
this.NotifyClient<LightUpdateEvent>(value);
}
}
}
private AlertEffect _alert;
public AlertEffect Alert {
get {
return this._alert;
}
set {
if(this.SetLightStateAttribute(new Dictionary<String, Object>() { { "alert", value.ToString() } })) {
this._alert = value;
this.NotifyClient<LightUpdateEvent>(value);
}
}
}
public Boolean Reachable { get; private set; }
#endregion
#region Constructor
public Dimmablelight(JsonData json, Tuple<Int32, Types> id, HttpConnection http, Boolean polling) : base(json, id, http, polling) {
this.ComplexInit(json);
}
private void ComplexInit(JsonData json) {
if (json.ContainsKey("state")) {
JsonData state = json["state"];
if (state.ContainsKey("on") && state["on"].IsBoolean) {
this._state = (Boolean)state["on"];
}
if (state.ContainsKey("bri") && state["bri"].IsInt) {
this._brightness = (Byte)state["bri"];
}
if (state.ContainsKey("alert") && state["alert"].IsString) {
this._alert = (AlertEffect)Enum.Parse(typeof(AlertEffect), state["alert"].ToString());
}
if (state.ContainsKey("reachable") && state["reachable"].IsBoolean) {
this.Reachable = (Boolean)state["reachable"];
}
}
}
#endregion
#region AConnector
public override String ToString() {
return "Light " + this.Name + " [" + this.LightId + "]: On-" + this.State.ToString() + " Bri-" + this.Brightness + " Alert-" + this.Alert.ToString() + " Reachable-" + this.Reachable.ToString();
}
public override void SetUpdate(JsonData json) {
base.SetUpdate(json);
if (json.ContainsKey("state") && json["state"].ContainsKey("on") && json["state"]["on"].IsBoolean) {
if (this.State != (Boolean)json["state"]["on"]) {
this._state = (Boolean)json["state"]["on"];
this.NotifyClient<LightUpdateEvent>(this.State);
}
}
if (json.ContainsKey("state") && json["state"].ContainsKey("bri") && json["state"]["bri"].IsInt) {
if (this.Brightness != (Byte)json["state"]["bri"]) {
this._brightness = (Byte)json["state"]["bri"];
this.NotifyClient<LightUpdateEvent>(this.Brightness);
}
}
if (json.ContainsKey("state") && json["state"].ContainsKey("alert") && json["state"]["alert"].IsString) {
if (this.Alert != (AlertEffect)Enum.Parse(typeof(AlertEffect), json["state"]["alert"].ToString())) {
this._alert = (AlertEffect)Enum.Parse(typeof(AlertEffect), json["state"]["alert"].ToString());
this.NotifyClient<LightUpdateEvent>(this.Alert);
}
}
if (json.ContainsKey("state") && json["state"].ContainsKey("reachable") && json["state"]["reachable"].IsBoolean) {
if (this.Reachable != (Boolean)json["state"]["reachable"]) {
this.Reachable = (Boolean)json["state"]["reachable"];
this.NotifyClient<LightUpdateEvent>(this.Reachable);
}
}
}
#endregion
}
}