Hue/Hue/Devices/Sensors/Daylight.cs
BlubbFish 4178079f4d [NF] Hue-Lib fertig
[DW] Hue-Bot beginn Arbeit
2018-05-01 21:46:56 +00:00

122 lines
4.7 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.Sensors {
class Daylight : ASensor {
#region Properties
private Boolean _on;
public Boolean On {
get {
return this._on;
}
set {
if(this.SetSensorConfig(new Dictionary<String, Object>() { { "on", value } })) {
this._on = value;
this.NotifyClient<SensorUpdateEvent>(value);
}
}
}
public Boolean Configured { get; private set; }
private Int32 _sunriseoffset;
public Int32 Sunriseoffset {
get {
return this._sunriseoffset;
}
set {
if(this.SetSensorConfig(new Dictionary<String, Object>() { { "sunriseoffset", value } })) {
this._sunriseoffset = value;
this.NotifyClient<SensorUpdateEvent>(value);
}
}
}
private Int32 _sunsetoffset;
public Int32 Sunsetoffset {
get {
return this._sunsetoffset;
}
set {
if(this.SetSensorConfig(new Dictionary<String, Object>() { { "sunsetoffset", value } })) {
this._sunsetoffset = value;
this.NotifyClient<SensorUpdateEvent>(value);
}
}
}
public Boolean IsDaylight { get; private set; }
#endregion
#region Constructor
public Daylight(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("config")) {
JsonData config = json["config"];
if(config.ContainsKey("on") && config["on"].IsBoolean) {
this._on = (Boolean)config["on"];
}
if(config.ContainsKey("configured") && config["configured"].IsBoolean) {
this.Configured = (Boolean)config["configured"];
}
if(config.ContainsKey("sunriseoffset") && config["sunriseoffset"].IsInt) {
this._sunriseoffset = (Int32)config["sunriseoffset"];
}
if(config.ContainsKey("sunsetoffset") && config["sunsetoffset"].IsInt) {
this._sunsetoffset = (Int32)config["sunsetoffset"];
}
}
if(json.ContainsKey("state")) {
JsonData state = json["state"];
if(state.ContainsKey("daylight") && state["daylight"].IsBoolean) {
this.IsDaylight = (Boolean)state["daylight"];
}
}
}
#endregion
#region AConnector
public override String ToString() {
return "Daylight " + this.Name + " [" + this.SensorId + "]: IsDaylight-" + this.IsDaylight.ToString() + " On-" + this.On.ToString() + " Configured-" + this.Configured.ToString() + " Sunriseoffset-" + this.Sunriseoffset.ToString() + " Sunsetoffset-" + this.Sunsetoffset.ToString();
}
public override void SetUpdate(JsonData json) {
base.SetUpdate(json);
if (json.ContainsKey("config") && json["config"].ContainsKey("on") && json["config"]["on"].IsBoolean) {
if (this.On != (Boolean)json["config"]["on"]) {
this._on = (Boolean)json["config"]["on"];
this.NotifyClient<SensorUpdateEvent>(this.On);
}
}
if (json.ContainsKey("config") && json["config"].ContainsKey("configured") && json["config"]["configured"].IsBoolean) {
if (this.Configured != (Boolean)json["config"]["configured"]) {
this.Configured = (Boolean)json["config"]["configured"];
this.NotifyClient<SensorUpdateEvent>(this.Configured);
}
}
if (json.ContainsKey("config") && json["config"].ContainsKey("sunriseoffset") && json["config"]["sunriseoffset"].IsInt) {
if (this.Sunriseoffset != (Int32)json["config"]["sunriseoffset"]) {
this._sunriseoffset = (Int32)json["config"]["sunriseoffset"];
this.NotifyClient<SensorUpdateEvent>(this.Sunriseoffset);
}
}
if (json.ContainsKey("config") && json["config"].ContainsKey("sunsetoffset") && json["config"]["sunsetoffset"].IsInt) {
if (this.Sunsetoffset != (Int32)json["config"]["sunsetoffset"]) {
this._sunsetoffset = (Int32)json["config"]["sunsetoffset"];
this.NotifyClient<SensorUpdateEvent>(this.Sunsetoffset);
}
}
if (json.ContainsKey("state") && json["state"].ContainsKey("daylight") && json["state"]["daylight"].IsBoolean) {
if (this.IsDaylight != (Boolean)json["state"]["daylight"]) {
this.IsDaylight = (Boolean)json["state"]["daylight"];
this.NotifyClient<SensorUpdateEvent>(this.IsDaylight);
}
}
}
#endregion
}
}