diff --git a/Hue/Devices/Configs/Config.cs b/Hue/Devices/Configs/Config.cs new file mode 100644 index 0000000..5674980 --- /dev/null +++ b/Hue/Devices/Configs/Config.cs @@ -0,0 +1,368 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using BlubbFish.IoT.Hue.Events; +using BlubbFish.IoT.Hue.Interfaces; +using BlubbFish.IoT.Hue.lib; +using LitJson; + +namespace BlubbFish.IoT.Hue.Devices.Configs { + public class Config : AConfig { + #region Properties + private String _name; + public String Name { + get { + return this._name; + } + set { + if(this.SetConfig(new Dictionary() { { "name", value } })) { + this._name = value; + this.NotifyClient(value); + } + } + } + private Boolean _linkbutton; + public Boolean Linkbutton { + get { + return this._linkbutton; + } + set { + if (this.SetConfig(new Dictionary() { { "linkbutton", value } })) { + this._linkbutton = value; + this.NotifyClient(value); + } + } + } + private String _ipaddress; + public String Ipaddress { + get { + return this._ipaddress; + } + set { + if (this.SetConfig(new Dictionary() { { "ipaddress", value } })) { + this._ipaddress = value; + this.NotifyClient(value); + } + } + } + private String _netmask; + public String Netmask { + get { + return this._netmask; + } + set { + if (this.SetConfig(new Dictionary() { { "netmask", value } })) { + this._netmask = value; + this.NotifyClient(value); + } + } + } + private String _gateway; + public String Gateway { + get { + return this._gateway; + } + set { + if (this.SetConfig(new Dictionary() { { "gateway", value } })) { + this._gateway = value; + this.NotifyClient(value); + } + } + } + private Boolean _dhcp; + public Boolean Dhcp { + get { + return this._dhcp; + } + set { + if (this.SetConfig(new Dictionary() { { "dhcp", value } })) { + this._dhcp = value; + this.NotifyClient(value); + } + } + } + private DateTime _utc; + public DateTime Utc { + get { + return this._utc; + } + set { + if (this.SetConfig(new Dictionary() { { "UTC", value } })) { + this._utc = value; + this.NotifyClient(value); + } + } + } + private String _timezone; + public String Timezone { + get { + return this._timezone; + } + set { + if (this.SetConfig(new Dictionary() { { "timezone", value } })) { + this._timezone = value; + this.NotifyClient(value); + } + } + } + private Byte _zigbeechannel; + public Byte Zigbeechannel { + get { + return this._zigbeechannel; + } + set { + if (this.SetConfig(new Dictionary() { { "zigbeechannel", value } })) { + this._zigbeechannel = value; + this.NotifyClient(value); + } + } + } + public String Apiversion { get; private set; } + public String Swversion { get; private set; } + public String Mac { get; private set; } + public Boolean Portalservices { get; private set; } + public DateTime Localtime { get; private set; } + public String Modelid { get; private set; } + public String Bridgeid { get; private set; } + public String Replacesbridgeid { get; private set; } + public Boolean Factorynew { get; private set; } + public String Datastoreversion { get; private set; } + public String Starterkitid { get; private set; } + public SwUpdate SwUpdate2 { get; private set; } + public Portalstate Portalstate { get; private set; } + public ReadOnlyDictionary Whitelist { get; private set; } + #endregion + + #region Constructor + public Config(JsonData json, HttpConnection http, Boolean polling) : base(json, http, polling) { + this.ComplexInit(json); + } + + private void ComplexInit(JsonData json) { + if (json.ContainsKey("name") && json["name"].IsString) { + this._name = json["name"].ToString(); + } + if (json.ContainsKey("apiversion") && json["apiversion"].IsString) { + this.Apiversion = json["apiversion"].ToString(); + } + if (json.ContainsKey("swversion") && json["swversion"].IsString) { + this.Swversion = json["swversion"].ToString(); + } + if (json.ContainsKey("linkbutton") && json["linkbutton"].IsBoolean) { + this._linkbutton = (Boolean)json["linkbutton"]; + } + if (json.ContainsKey("ipaddress") && json["ipaddress"].IsString) { + this._ipaddress = json["ipaddress"].ToString(); + } + if (json.ContainsKey("mac") && json["mac"].IsString) { + this.Mac = json["mac"].ToString(); + } + if (json.ContainsKey("netmask") && json["netmask"].IsString) { + this._netmask = json["netmask"].ToString(); + } + if (json.ContainsKey("gateway") && json["gateway"].IsString) { + this._gateway = json["gateway"].ToString(); + } + if (json.ContainsKey("dhcp") && json["dhcp"].IsBoolean) { + this._dhcp = (Boolean)json["dhcp"]; + } + if (json.ContainsKey("portalservices") && json["portalservices"].IsBoolean) { + this.Portalservices = (Boolean)json["portalservices"]; + } + if (json.ContainsKey("UTC") && json["UTC"].IsString) { + this._utc = DateTime.Parse(json["UTC"].ToString()); + } + if (json.ContainsKey("localtime") && json["localtime"].IsString) { + this.Localtime = DateTime.Parse(json["localtime"].ToString()); + } + if (json.ContainsKey("timezone") && json["timezone"].IsString) { + this._timezone = json["timezone"].ToString(); + } + if (json.ContainsKey("zigbeechannel") && json["zigbeechannel"].IsInt) { + this._zigbeechannel = (Byte)json["zigbeechannel"]; + } + if (json.ContainsKey("modelid") && json["modelid"].IsString) { + this.Modelid = json["modelid"].ToString(); + } + if (json.ContainsKey("bridgeid") && json["bridgeid"].IsString) { + this.Bridgeid = json["bridgeid"].ToString(); + } + if (json.ContainsKey("replacesbridgeid") && json["replacesbridgeid"] != null && json["replacesbridgeid"].IsString) { + this.Replacesbridgeid = json["replacesbridgeid"].ToString(); + } + if (json.ContainsKey("factorynew") && json["factorynew"].IsBoolean) { + this.Factorynew = (Boolean)json["factorynew"]; + } + if (json.ContainsKey("datastoreversion") && json["datastoreversion"].IsString) { + this.Datastoreversion = json["datastoreversion"].ToString(); + } + if (json.ContainsKey("starterkitid") && json["starterkitid"].IsString) { + this.Starterkitid = json["starterkitid"].ToString(); + } + if (json.ContainsKey("swupdate2") && json["swupdate2"].IsObject) { + this.SwUpdate2 = new SwUpdate(json["swupdate2"], this.http, this.Polling); + this.SwUpdate2.Update += this.NotifyClientChildren; + } + if (json.ContainsKey("whitelist") && json["whitelist"].IsObject) { + this.CreateWhitelist(json["whitelist"]); + } + if (json.ContainsKey("portalstate") && json["portalstate"].IsObject) { + this.Portalstate = new Portalstate(json["portalstate"], this.http, this.Polling); + this.Portalstate.Update += this.NotifyClientChildren; + } + } + + private void CreateWhitelist(JsonData json) { + Dictionary whitelists = new Dictionary(); + foreach (String item in json.Keys) { + Whitelist w = new Whitelist(json[item], new Tuple(item), this.http, this.Polling); + w.Update += this.NotifyClientChildren; + whitelists.Add(item, w); + } + this.Whitelist = new ReadOnlyDictionary(whitelists); + } + #endregion + + #region AConnector + public override String ToString() { + return "Config: " + this.Name + " Ip-" + this.Ipaddress + " DHCP-" + this.Dhcp + " Time-" + this.Localtime + " User-" + this.Whitelist.Count; + } + + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("name") && json["name"].IsString) { + if (this.Name != json["name"].ToString()) { + this._name = json["name"].ToString(); + this.NotifyClient(this.Name); + } + } + if (json.ContainsKey("apiversion") && json["apiversion"].IsString) { + if (this.Apiversion != json["apiversion"].ToString()) { + this.Apiversion = json["apiversion"].ToString(); + this.NotifyClient(this.Apiversion); + } + } + if (json.ContainsKey("swversion") && json["swversion"].IsString) { + if (this.Swversion != json["swversion"].ToString()) { + this.Swversion = json["swversion"].ToString(); + this.NotifyClient(this.Swversion); + } + } + if (json.ContainsKey("linkbutton") && json["linkbutton"].IsBoolean) { + if (this.Linkbutton != (Boolean)json["linkbutton"]) { + this._linkbutton = (Boolean)json["linkbutton"]; + this.NotifyClient(this.Linkbutton); + } + } + if (json.ContainsKey("ipaddress") && json["ipaddress"].IsString) { + if (this.Ipaddress != json["ipaddress"].ToString()) { + this._ipaddress = json["ipaddress"].ToString(); + this.NotifyClient(this.Ipaddress); + } + } + if (json.ContainsKey("mac") && json["mac"].IsString) { + if (this.Mac != json["mac"].ToString()) { + this.Mac = json["mac"].ToString(); + this.NotifyClient(this.Mac); + } + } + if (json.ContainsKey("netmask") && json["netmask"].IsString) { + if (this.Netmask != json["netmask"].ToString()) { + this._netmask = json["netmask"].ToString(); + this.NotifyClient(this.Netmask); + } + } + if (json.ContainsKey("gateway") && json["gateway"].IsString) { + if (this.Gateway != json["gateway"].ToString()) { + this._gateway = json["gateway"].ToString(); + this.NotifyClient(this.Gateway); + } + } + if (json.ContainsKey("dhcp") && json["dhcp"].IsBoolean) { + if (this.Dhcp != (Boolean)json["dhcp"]) { + this._dhcp = (Boolean)json["dhcp"]; + this.NotifyClient(this.Dhcp); + } + } + if (json.ContainsKey("portalservices") && json["portalservices"].IsBoolean) { + if (this.Portalservices != (Boolean)json["portalservices"]) { + this.Portalservices = (Boolean)json["portalservices"]; + this.NotifyClient(this.Portalservices); + } + } + if (json.ContainsKey("UTC") && json["UTC"].IsString) { + if (this.Utc != DateTime.Parse(json["UTC"].ToString())) { + this._utc = DateTime.Parse(json["UTC"].ToString()); + this.NotifyClient(this.Utc); + } + } + if (json.ContainsKey("localtime") && json["localtime"].IsString) { + if (this.Localtime != DateTime.Parse(json["localtime"].ToString())) { + this.Localtime = DateTime.Parse(json["localtime"].ToString()); + this.NotifyClient(this.Localtime); + } + } + if (json.ContainsKey("timezone") && json["timezone"].IsString) { + if (this.Timezone != json["timezone"].ToString()) { + this._timezone = json["timezone"].ToString(); + this.NotifyClient(this.Timezone); + } + } + if (json.ContainsKey("zigbeechannel") && json["zigbeechannel"].IsInt) { + if (this.Zigbeechannel != (Byte)json["zigbeechannel"]) { + this._zigbeechannel = (Byte)json["zigbeechannel"]; + this.NotifyClient(this.Zigbeechannel); + } + } + if (json.ContainsKey("modelid") && json["modelid"].IsString) { + if (this.Modelid != json["modelid"].ToString()) { + this.Modelid = json["modelid"].ToString(); + this.NotifyClient(this.Modelid); + } + } + if (json.ContainsKey("bridgeid") && json["bridgeid"].IsString) { + if (this.Bridgeid != json["bridgeid"].ToString()) { + this.Bridgeid = json["bridgeid"].ToString(); + this.NotifyClient(this.Bridgeid); + } + } + if (json.ContainsKey("replacesbridgeid") && json["replacesbridgeid"] != null && json["replacesbridgeid"].IsString) { + if (this.Replacesbridgeid != json["replacesbridgeid"].ToString()) { + this.Replacesbridgeid = json["replacesbridgeid"].ToString(); + this.NotifyClient(this.Replacesbridgeid); + } + } + if (json.ContainsKey("factorynew") && json["factorynew"].IsBoolean) { + if (this.Factorynew != (Boolean)json["factorynew"]) { + this.Factorynew = (Boolean)json["factorynew"]; + this.NotifyClient(this.Factorynew); + } + } + if (json.ContainsKey("datastoreversion") && json["datastoreversion"].IsString) { + if (this.Datastoreversion != json["datastoreversion"].ToString()) { + this.Datastoreversion = json["datastoreversion"].ToString(); + this.NotifyClient(this.Datastoreversion); + } + } + if (json.ContainsKey("starterkitid") && json["starterkitid"].IsString) { + if (this.Starterkitid != json["starterkitid"].ToString()) { + this.Starterkitid = json["starterkitid"].ToString(); + this.NotifyClient(this.Starterkitid); + } + } + if (json.ContainsKey("swupdate2") && json["swupdate2"].IsObject) { + this.SwUpdate2?.SetUpdate(json["swupdate2"]); + } + if (json.ContainsKey("whitelist") && json["whitelist"].IsObject) { + foreach (String item in json["whitelist"].Keys) { + if(this.Whitelist.ContainsKey(item)) { + this.Whitelist[item].SetUpdate(json["whitelist"][item]); + } + } + } + if (json.ContainsKey("portalstate") && json["portalstate"].IsObject) { + this.Portalstate?.SetUpdate(json["portalstate"]); + } + } + #endregion + } +} diff --git a/Hue/Devices/Configs/Portalstate.cs b/Hue/Devices/Configs/Portalstate.cs new file mode 100644 index 0000000..9351161 --- /dev/null +++ b/Hue/Devices/Configs/Portalstate.cs @@ -0,0 +1,71 @@ +using System; +using BlubbFish.IoT.Hue.Events; +using BlubbFish.IoT.Hue.Interfaces; +using BlubbFish.IoT.Hue.lib; +using LitJson; + +namespace BlubbFish.IoT.Hue.Devices.Configs { + public class Portalstate : AConfig { + + #region Properties + public Boolean Signedon { get; private set; } + public Boolean Incoming { get; private set; } + public Boolean Outgoing { get; private set; } + public String Communication { get; private set; } + #endregion + + #region Constrctor + public Portalstate(JsonData json, HttpConnection http, Boolean polling) : base(json, http, polling) { + this.ComplexInit(json); + } + + private void ComplexInit(JsonData json) { + if (json.ContainsKey("signedon") && json["signedon"].IsBoolean) { + this.Signedon = (Boolean)json["signedon"]; + } + if (json.ContainsKey("incoming") && json["incoming"].IsBoolean) { + this.Incoming = (Boolean)json["incoming"]; + } + if (json.ContainsKey("outgoing") && json["outgoing"].IsBoolean) { + this.Outgoing = (Boolean)json["outgoing"]; + } + if (json.ContainsKey("communication") && json["communication"].IsString) { + this.Communication = json["communication"].ToString(); + } + } + #endregion + + #region AConnector + public override String ToString() { + return "Portalstate: On-" + this.Signedon + " In-" + this.Incoming + " Out-" + this.Outgoing + " Communication-" + this.Communication; + } + + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("signedon") && json["signedon"].IsBoolean) { + if (this.Signedon != (Boolean)json["signedon"]) { + this.Signedon = (Boolean)json["signedon"]; + this.NotifyClient(this.Signedon); + } + } + if (json.ContainsKey("incoming") && json["incoming"].IsBoolean) { + if (this.Incoming != (Boolean)json["incoming"]) { + this.Incoming = (Boolean)json["incoming"]; + this.NotifyClient(this.Incoming); + } + } + if (json.ContainsKey("outgoing") && json["outgoing"].IsBoolean) { + if (this.Outgoing != (Boolean)json["outgoing"]) { + this.Outgoing = (Boolean)json["outgoing"]; + this.NotifyClient(this.Outgoing); + } + } + if (json.ContainsKey("communication") && json["communication"].IsString) { + if (this.Communication != json["communication"].ToString()) { + this.Communication = json["communication"].ToString(); + this.NotifyClient(this.Communication); + } + } + } + #endregion + } +} diff --git a/Hue/Devices/Configs/SwUpdate.cs b/Hue/Devices/Configs/SwUpdate.cs new file mode 100644 index 0000000..fa40245 --- /dev/null +++ b/Hue/Devices/Configs/SwUpdate.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlubbFish.IoT.Hue.Events; +using BlubbFish.IoT.Hue.Interfaces; +using BlubbFish.IoT.Hue.lib; +using LitJson; + +namespace BlubbFish.IoT.Hue.Devices.Configs { + public class SwUpdate : AConfig { + #region Properties + public Boolean Checkforupdate { get; private set; } + public DateTime Lastchange { get; private set; } + public String Bridgestate { get; private set; } + public DateTime Bridgelastinstall { get; private set; } + public String State { get; private set; } + public String Autoinstallupdatetime { get; private set; } + public Boolean Autoinstallon { get; private set; } + #endregion + + #region Constructor + public SwUpdate(JsonData json, HttpConnection http, Boolean polling) : base(json, http, polling) { + this.ComplexInit(json); + } + + private void ComplexInit(JsonData json) { + if(json.ContainsKey("checkforupdate") && json["checkforupdate"].IsBoolean) { + this.Checkforupdate = (Boolean)json["checkforupdate"]; + } + if (json.ContainsKey("lastchange") && json["lastchange"].IsString) { + this.Lastchange = DateTime.Parse(json["lastchange"].ToString()).ToLocalTime(); + } + if (json.ContainsKey("bridge") && json["bridge"].IsObject) { + if (json["bridge"].ContainsKey("state") && json["bridge"]["state"].IsString) { + this.Bridgestate = json["bridge"]["state"].ToString(); + } + if (json["bridge"].ContainsKey("lastinstall") && json["bridge"]["lastinstall"].IsString) { + this.Bridgelastinstall = DateTime.Parse(json["bridge"]["lastinstall"].ToString()).ToLocalTime(); + } + } + if (json.ContainsKey("state") && json["state"].IsString) { + this.State = json["state"].ToString(); + } + if (json.ContainsKey("autoinstall") && json["autoinstall"].IsObject) { + if (json["autoinstall"].ContainsKey("updatetime") && json["autoinstall"]["updatetime"].IsString) { + this.Autoinstallupdatetime = json["autoinstall"]["updatetime"].ToString(); + } + if (json["autoinstall"].ContainsKey("on") && json["autoinstall"]["on"].IsBoolean) { + this.Autoinstallon = (Boolean)json["autoinstall"]["on"]; + } + } + } + #endregion + + #region AConnector + public override String ToString() { + return "Updates: Check-" + this.Checkforupdate + " LatestCheck-" + this.Lastchange + " Updates-" + this.Bridgestate + " Installed-" + this.Bridgelastinstall + " State-" + this.State + " AutoTime-" + this.Autoinstallupdatetime + " AutoOn-" + this.Autoinstallon; + } + + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("checkforupdate") && json["checkforupdate"].IsBoolean) { + if (this.Checkforupdate != (Boolean)json["checkforupdate"]) { + this.Checkforupdate = (Boolean)json["checkforupdate"]; + this.NotifyClient(this.Checkforupdate); + } + } + if (json.ContainsKey("lastchange") && json["lastchange"].IsString) { + if (this.Lastchange != DateTime.Parse(json["lastchange"].ToString()).ToLocalTime()) { + this.Lastchange = DateTime.Parse(json["lastchange"].ToString()).ToLocalTime(); + this.NotifyClient(this.Lastchange); + } + } + if (json.ContainsKey("bridge") && json["bridge"].ContainsKey("state") && json["bridge"]["state"].IsString) { + if (this.Bridgestate != json["bridge"]["state"].ToString()) { + this.Bridgestate = json["bridge"]["state"].ToString(); + this.NotifyClient(this.Bridgestate); + } + } + if (json.ContainsKey("bridge") && json["bridge"].ContainsKey("lastinstall") && json["bridge"]["lastinstall"].IsString) { + if (this.Bridgelastinstall != DateTime.Parse(json["bridge"]["lastinstall"].ToString()).ToLocalTime()) { + this.Bridgelastinstall = DateTime.Parse(json["bridge"]["lastinstall"].ToString()).ToLocalTime(); + this.NotifyClient(this.Bridgelastinstall); + } + } + if (json.ContainsKey("state") && json["state"].IsString) { + if (this.State != json["state"].ToString()) { + this.State = json["state"].ToString(); + this.NotifyClient(this.State); + } + } + if (json.ContainsKey("autoinstall") && json["autoinstall"].ContainsKey("updatetime") && json["autoinstall"]["updatetime"].IsString) { + if (this.Autoinstallupdatetime != json["autoinstall"]["updatetime"].ToString()) { + this.Autoinstallupdatetime = json["autoinstall"]["updatetime"].ToString(); + this.NotifyClient(this.Autoinstallupdatetime); + } + } + if (json.ContainsKey("autoinstall") && json["autoinstall"].ContainsKey("on") && json["autoinstall"]["on"].IsBoolean) { + if (this.Autoinstallon != (Boolean)json["autoinstall"]["on"]) { + this.Autoinstallon = (Boolean)json["autoinstall"]["on"]; + this.NotifyClient(this.Autoinstallon); + } + } + } + #endregion + } +} diff --git a/Hue/Devices/Configs/Whitelist.cs b/Hue/Devices/Configs/Whitelist.cs new file mode 100644 index 0000000..5fdb47b --- /dev/null +++ b/Hue/Devices/Configs/Whitelist.cs @@ -0,0 +1,62 @@ +using System; +using BlubbFish.IoT.Hue.Events; +using BlubbFish.IoT.Hue.Interfaces; +using BlubbFish.IoT.Hue.lib; +using LitJson; + +namespace BlubbFish.IoT.Hue.Devices.Configs { + public class Whitelist : AConfig { + #region Properties + public DateTime Lastusedate { get; private set; } + public DateTime Createdate { get; private set; } + public String Name { get; private set; } + public String UserId { get; } + #endregion + + #region Constructor + public Whitelist(JsonData json, Tuple id, HttpConnection http, Boolean polling) : base(json, http, polling) { + this.UserId = id.Item1; + this.ComplexInit(json); + } + + private void ComplexInit(JsonData json) { + if (json.ContainsKey("last use date") && json["last use date"].IsString) { + this.Lastusedate = DateTime.Parse(json["last use date"].ToString()).ToLocalTime(); + } + if (json.ContainsKey("create date") && json["create date"].IsString) { + this.Createdate = DateTime.Parse(json["create date"].ToString()).ToLocalTime(); + } + if (json.ContainsKey("name") && json["name"].IsString) { + this.Name = json["name"].ToString(); + } + } + #endregion + + #region AConnector + public override String ToString() { + return "User [" + this.UserId + "]: " + this.Name + " (Create-" + this.Createdate + " LastOn-" + this.Lastusedate + ")"; + } + + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("last use date") && json["last use date"].IsString) { + if (this.Lastusedate != DateTime.Parse(json["last use date"].ToString()).ToLocalTime()) { + this.Lastusedate = DateTime.Parse(json["last use date"].ToString()).ToLocalTime(); + this.NotifyClient(this.Lastusedate); + } + } + if (json.ContainsKey("create date") && json["create date"].IsString) { + if (this.Createdate != DateTime.Parse(json["create date"].ToString()).ToLocalTime()) { + this.Createdate = DateTime.Parse(json["create date"].ToString()).ToLocalTime(); + this.NotifyClient(this.Createdate); + } + } + if (json.ContainsKey("name") && json["name"].IsString) { + if (this.Name != json["name"].ToString()) { + this.Name = json["name"].ToString(); + this.NotifyClient(this.Name); + } + } + } + #endregion + } +} diff --git a/Hue/Devices/Groups/Room.cs b/Hue/Devices/Groups/Room.cs index 40249e6..5bb8fb0 100644 --- a/Hue/Devices/Groups/Room.cs +++ b/Hue/Devices/Groups/Room.cs @@ -1,28 +1,32 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using BlubbFish.IoT.Hue.Events; using BlubbFish.IoT.Hue.Interfaces; using BlubbFish.IoT.Hue.lib; using LitJson; namespace BlubbFish.IoT.Hue.Devices.Groups { public class Room : AGroup { - public override event UpdatedValue Update; - - #region Properties - private AlertEffect _alert; public enum AlertEffect { none, select, lselect } + + #region Properties + private AlertEffect _alert; + private Boolean _state; public Boolean State { get { return this._state; } set { - this.SetGroupAction(new Dictionary() { { "on", value } }); + if(this.SetGroupAction(new Dictionary() { { "on", value } })) { + this._state = value; + this.NotifyClient(value); + } } } private Byte _brightness; @@ -31,7 +35,10 @@ namespace BlubbFish.IoT.Hue.Devices.Groups { return this._brightness; } set { - this.SetGroupAction(new Dictionary() { { "bri", value } }); + if(this.SetGroupAction(new Dictionary() { { "bri", value } })) { + this._brightness = value; + this.NotifyClient(value); + } } } public AlertEffect Alert { @@ -39,7 +46,10 @@ namespace BlubbFish.IoT.Hue.Devices.Groups { return this._alert; } set { - this.SetGroupAction(new Dictionary() { { "alert", value } }); + if(this.SetGroupAction(new Dictionary() { { "alert", value } })) { + this._alert = value; + this.NotifyClient(value); + } } } #endregion @@ -66,14 +76,6 @@ namespace BlubbFish.IoT.Hue.Devices.Groups { #endregion #region AConnector - public override Dictionary ToDictionary() { - return new Dictionary { - { "State", this.State }, - { "Brightness", this.Brightness }, - { "Alert", this.Alert.ToString() } - }; - } - public override String ToString() { List lid = new List(); foreach (KeyValuePair item in this.Lights) { @@ -81,6 +83,28 @@ namespace BlubbFish.IoT.Hue.Devices.Groups { } return "Group " + this.Name + " [" + this.GroupId + "]: (Class-"+ Helper.GetEnumDescription(this.Class) + ", ["+String.Join(",",lid.ToArray())+"]) On-" + this.State.ToString() + " Bri-" + this.Brightness + " Alert-" + this.Alert.ToString() + " AllOn-" + this.StateAllOn.ToString() + " AnyOn-" + this.StateAnyOn.ToString(); } + + public override void SetUpdate(JsonData json) { + base.SetUpdate(json); + if (json.ContainsKey("on") && json["on"].IsBoolean) { + if (this.State != (Boolean)json["on"]) { + this._state = (Boolean)json["on"]; + this.NotifyClient(this.State); + } + } + if (json.ContainsKey("bri") && json["bri"].IsInt) { + if (this.Brightness != (Byte)json["bri"]) { + this._brightness = (Byte)json["bri"]; + this.NotifyClient(this.Brightness); + } + } + if (json.ContainsKey("alert") && json["alert"].IsString) { + if (this.Alert != (AlertEffect)Enum.Parse(typeof(AlertEffect), json["alert"].ToString())) { + this._alert = (AlertEffect)Enum.Parse(typeof(AlertEffect), json["alert"].ToString()); + this.NotifyClient(this.Alert); + } + } + } #endregion } } diff --git a/Hue/Devices/Lights/Dimmablelight.cs b/Hue/Devices/Lights/Dimmablelight.cs index 5d823a0..688c409 100644 --- a/Hue/Devices/Lights/Dimmablelight.cs +++ b/Hue/Devices/Lights/Dimmablelight.cs @@ -1,12 +1,12 @@ 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 override event UpdatedValue Update; public enum AlertEffect { none, select, @@ -20,7 +20,10 @@ namespace BlubbFish.IoT.Hue.Devices.Lights { return this._state; } set { - this.SetLightState(new Dictionary() { { "on", value } }); + if(this.SetLightStateAttribute(new Dictionary() { { "on", value } })) { + this._state = value; + this.NotifyClient(value); + } } } private Byte _brightness; @@ -29,7 +32,10 @@ namespace BlubbFish.IoT.Hue.Devices.Lights { return this._brightness; } set { - this.SetLightState(new Dictionary() { { "bri", value } }); + if(this.SetLightStateAttribute(new Dictionary() { { "bri", value } })) { + this._brightness = value; + this.NotifyClient(value); + } } } private AlertEffect _alert; @@ -38,7 +44,10 @@ namespace BlubbFish.IoT.Hue.Devices.Lights { return this._alert; } set { - this.SetLightState(new Dictionary() { { "alert", value } }); + if(this.SetLightStateAttribute(new Dictionary() { { "alert", value } })) { + this._alert = value; + this.NotifyClient(value); + } } } public Boolean Reachable { get; private set; } @@ -72,14 +81,32 @@ namespace BlubbFish.IoT.Hue.Devices.Lights { 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 Dictionary ToDictionary() { - return new Dictionary { - { "State", this.State }, - { "Brightness", this.Brightness }, - { "Alert", this.Alert.ToString() }, - { "Reachable", this.Reachable } - }; + 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(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(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(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(this.Reachable); + } + } } #endregion } diff --git a/Hue/Devices/Scenes/LightState.cs b/Hue/Devices/Scenes/LightState.cs index c168d74..18b350e 100644 --- a/Hue/Devices/Scenes/LightState.cs +++ b/Hue/Devices/Scenes/LightState.cs @@ -1,13 +1,13 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using BlubbFish.IoT.Hue.Events; using BlubbFish.IoT.Hue.Interfaces; using BlubbFish.IoT.Hue.lib; using LitJson; namespace BlubbFish.IoT.Hue.Devices.Scenes { - public class LightState : AConnector { - public override event UpdatedValue Update; - + public class LightState : AScene { #region Properties private Boolean _state; public Boolean State { @@ -15,7 +15,10 @@ namespace BlubbFish.IoT.Hue.Devices.Scenes { return this._state; } set { - this.SetLightScene(new Dictionary() { { "on", value } }); + if (this.SetLightSceneAttribute(new Dictionary() { { "on", value } })) { + this._state = value; + this.NotifyClient(value); + } } } private Byte _brightness; @@ -24,18 +27,16 @@ namespace BlubbFish.IoT.Hue.Devices.Scenes { return this._brightness; } set { - this.SetLightScene(new Dictionary() { { "bri", value } }); + if(this.SetLightSceneAttribute(new Dictionary() { { "bri", value } })) { + this._brightness = value; + this.NotifyClient(value); + } } } - public String SceneId { get; } - public Int32 LightStateId { get; } #endregion #region Constructor - public LightState(JsonData json, Tuple id, HttpConnection http) { - this.http = http; - this.SceneId = id.Item1; - this.LightStateId = id.Item2; + public LightState(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) : base(json, id, http, polling, lights){ this.ComplexInit(json); } @@ -49,21 +50,25 @@ namespace BlubbFish.IoT.Hue.Devices.Scenes { } #endregion - private void SetLightScene(Dictionary value) { - this.PutDictionary("scenes/" + this.SceneId + "/lights/" + this.LightStateId + "/state", value); - } - #region AConnector - public override Dictionary ToDictionary() { - return new Dictionary { - { "State", this.State.ToString() }, - { "Brightness", this.Brightness } - }; - } - public override String ToString() { return "Scene LightState " + this.SceneId + " [" + this.LightStateId + "]: On-" + this.State.ToString() + " Bri-" + this.Brightness; } + + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("on") && json["on"].IsBoolean) { + if (this.State != (Boolean)json["on"]) { + this._state = (Boolean)json["on"]; + this.NotifyClient(this.State); + } + } + if (json.ContainsKey("bri") && json["bri"].IsInt) { + if (this.Brightness != (Byte)json["bri"]) { + this._brightness = (Byte)json["bri"]; + this.NotifyClient(this.Brightness); + } + } + } #endregion } } diff --git a/Hue/Devices/Scenes/Scene.cs b/Hue/Devices/Scenes/Scene.cs index 8006242..a9dcd39 100644 --- a/Hue/Devices/Scenes/Scene.cs +++ b/Hue/Devices/Scenes/Scene.cs @@ -1,30 +1,46 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using BlubbFish.IoT.Hue.Events; using BlubbFish.IoT.Hue.Interfaces; using BlubbFish.IoT.Hue.lib; using LitJson; namespace BlubbFish.IoT.Hue.Devices.Scenes { class Scene : AScene { - public override event UpdatedValue Update; - #region Properties + private String _name; + public String Name { + get { + return this._name; + } + set { + if (this.SetSceneAttribute(new Dictionary() { { "name", value } })) { + this._name = value; + this.NotifyClient(value); + } + } + } public String Owner { get; private set; } public Boolean Recycle { get; private set; } public Boolean Locked { get; private set; } public String Picture { get; private set; } public DateTime Lastupdated { get; private set; } public Int32 Version { get; private set; } + public ReadOnlyDictionary Lights { get; private set; } + public ReadOnlyDictionary LightStates { get; private set; } #endregion #region Constructor - public Scene(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) : base(json, id, http, polling, lights) { - this.ComplexInit(json); + public Scene(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) : base(json, new Tuple(id.Item1, 0), http, polling, lights) { + this.ComplexInit(json, lights); } - private void ComplexInit(JsonData json) { - if(json.ContainsKey("owner") && json["owner"].IsString) { + private void ComplexInit(JsonData json, ReadOnlyDictionary lights) { + if (json.ContainsKey("name") && json["name"].IsString) { + this._name = json["name"].ToString(); + } + if (json.ContainsKey("owner") && json["owner"].IsString) { this.Owner = json["owner"].ToString(); } if (json.ContainsKey("recycle") && json["recycle"].IsBoolean) { @@ -42,21 +58,30 @@ namespace BlubbFish.IoT.Hue.Devices.Scenes { if (json.ContainsKey("version") && json["version"].IsInt) { this.Version = (Int32)json["version"]; } + if (json.ContainsKey("lights") && json["lights"].IsArray) { + Dictionary lightdict = new Dictionary(); + foreach (JsonData item in json["lights"]) { + if (Int32.TryParse(item.ToString(), out Int32 lampid)) { + if (lights.ContainsKey(lampid)) { + lightdict.Add(lampid, lights[lampid]); + } + } + } + this.Lights = new ReadOnlyDictionary(lightdict); + } + if (json.ContainsKey("lightstates") && json["lightstates"].IsObject) { + Dictionary lightstates = new Dictionary(); + foreach (String item in json["lightstates"].Keys) { + LightState l = new LightState(json["lightstates"][item], new Tuple(this.SceneId, Int32.Parse(item)), this.http, this.Polling, lights); + l.Update += this.NotifyClientChildren; + lightstates.Add(Int32.Parse(item), l); + } + this.LightStates = new ReadOnlyDictionary(lightstates); + } } #endregion #region AConnector - public override Dictionary ToDictionary() { - return new Dictionary { - { "owner", this.Owner }, - { "recycle", this.Recycle }, - { "locked", this.Locked }, - { "picture", this.Picture }, - { "lastupdated", this.Lastupdated }, - { "version", this.Version } - }; - } - public override String ToString() { List lid = new List(); foreach (KeyValuePair item in this.Lights) { @@ -68,6 +93,58 @@ namespace BlubbFish.IoT.Hue.Devices.Scenes { } return "Scene " + this.Name + " [" + this.SceneId + "]: (["+ String.Join(",", lid.ToArray()) +"]) Light-States ["+ String.Join(",", lst.ToArray())+"]"; } + + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("name") && json["name"].IsString) { + if (this.Name != json["name"].ToString()) { + this._name = json["name"].ToString(); + this.NotifyClient(this.Name); + } + } + if (json.ContainsKey("owner") && json["owner"].IsString) { + if (this.Owner != json["owner"].ToString()) { + this.Owner = json["owner"].ToString(); + this.NotifyClient(this.Owner); + } + } + if (json.ContainsKey("recycle") && json["recycle"].IsBoolean) { + if (this.Recycle != (Boolean)json["recycle"]) { + this.Recycle = (Boolean)json["recycle"]; + this.NotifyClient(this.Recycle); + } + } + if (json.ContainsKey("locked") && json["locked"].IsBoolean) { + if (this.Locked != (Boolean)json["locked"]) { + this.Locked = (Boolean)json["locked"]; + this.NotifyClient(this.Locked); + } + } + if (json.ContainsKey("picture") && json["picture"].IsString) { + if (this.Picture != json["picture"].ToString()) { + this.Picture = json["picture"].ToString(); + this.NotifyClient(this.Picture); + } + } + if (json.ContainsKey("lastupdated") && json["lastupdated"].IsString) { + if (this.Lastupdated != DateTime.Parse(json["lastupdated"].ToString())) { + this.Lastupdated = DateTime.Parse(json["lastupdated"].ToString()); + this.NotifyClient(this.Lastupdated); + } + } + if (json.ContainsKey("version") && json["version"].IsInt) { + if (this.Version != (Int32)json["version"]) { + this.Version = (Int32)json["version"]; + this.NotifyClient(this.Version); + } + } + if (json.ContainsKey("lightstates") && json["lightstates"].IsObject) { + foreach (String item in json["lightstates"].Keys) { + if(this.LightStates.ContainsKey(Int32.Parse(item))) { + this.LightStates[Int32.Parse(item)].SetUpdate(json["lightstates"][Int32.Parse(item)]); + } + } + } + } #endregion } } diff --git a/Hue/Devices/Sensors/Daylight.cs b/Hue/Devices/Sensors/Daylight.cs new file mode 100644 index 0000000..3788885 --- /dev/null +++ b/Hue/Devices/Sensors/Daylight.cs @@ -0,0 +1,121 @@ +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() { { "on", value } })) { + this._on = value; + this.NotifyClient(value); + } + } + } + public Boolean Configured { get; private set; } + private Int32 _sunriseoffset; + public Int32 Sunriseoffset { + get { + return this._sunriseoffset; + } + set { + if(this.SetSensorConfig(new Dictionary() { { "sunriseoffset", value } })) { + this._sunriseoffset = value; + this.NotifyClient(value); + } + } + } + private Int32 _sunsetoffset; + public Int32 Sunsetoffset { + get { + return this._sunsetoffset; + } + set { + if(this.SetSensorConfig(new Dictionary() { { "sunsetoffset", value } })) { + this._sunsetoffset = value; + this.NotifyClient(value); + } + } + } + public Boolean IsDaylight { get; private set; } + #endregion + + #region Constructor + public Daylight(JsonData json, Tuple 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(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(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(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(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(this.IsDaylight); + } + } + } + #endregion + } +} diff --git a/Hue/Events/UpdateEvent.cs b/Hue/Events/UpdateEvent.cs index 00432d4..117e8e6 100644 --- a/Hue/Events/UpdateEvent.cs +++ b/Hue/Events/UpdateEvent.cs @@ -42,4 +42,18 @@ namespace BlubbFish.IoT.Hue.Events { public SceneUpdateEvent(Object value, DateTime time, Object parent) : base(value, time, parent) { } } + public class SensorUpdateEvent : AllUpdateEvent { + public SensorUpdateEvent() { + } + + public SensorUpdateEvent(Object value, DateTime time, Object parent) : base(value, time, parent) { + } + } + public class ConfigUpdateEvent : AllUpdateEvent { + public ConfigUpdateEvent() { + } + + public ConfigUpdateEvent(Object value, DateTime time, Object parent) : base(value, time, parent) { + } + } } diff --git a/Hue/Hue.csproj b/Hue/Hue.csproj index 4fecf08..40eaaf9 100644 --- a/Hue/Hue.csproj +++ b/Hue/Hue.csproj @@ -41,8 +41,14 @@ + + + + + + @@ -51,6 +57,7 @@ + diff --git a/Hue/HueController.cs b/Hue/HueController.cs index 995d448..cc68f8a 100644 --- a/Hue/HueController.cs +++ b/Hue/HueController.cs @@ -21,6 +21,8 @@ namespace BlubbFish.IoT.Hue { public ReadOnlyDictionary Lights { get; private set; } public ReadOnlyDictionary Groups { get; private set; } public ReadOnlyDictionary Scenes { get; private set; } + public ReadOnlyDictionary Sensors { get; private set; } + public AConfig Config { get; private set; } public HueController(Dictionary settings, Boolean enablePoll = true) { this.polling = enablePoll; @@ -50,10 +52,10 @@ namespace BlubbFish.IoT.Hue { foreach (KeyValuePair item in this.Scenes) { item.Value.Update += this.AllUpdate; } - /*foreach (KeyValuePair item in this.Sensors) { + foreach (KeyValuePair item in this.Sensors) { item.Value.Update += this.AllUpdate; } - this.Config.Update += this.AllUpdate;*/ + this.Config.Update += this.AllUpdate; } private void Poll() { @@ -65,25 +67,59 @@ namespace BlubbFish.IoT.Hue { } private void Updater() { - //throw new NotImplementedException(); + while (true) { + Thread.Sleep(1000 * 10); //Every Minute + JsonData update = this.http.GetJson(""); + Helper.SetUpdate(this.Lights, update, "lights"); + Helper.SetUpdate(this.Groups, update, "groups"); + Helper.SetUpdate(this.Sensors, update, "sensors"); + Helper.SetUpdate(this.Scenes, update, "scenes"); + if (update.ContainsKey("config")) { + this.Config.SetUpdate(update); + } + } } + + private void CreateAll(JsonData json) { if(json.ContainsKey("lights")) { - this.CreateLights(json["lights"]); + this.Lights = new ReadOnlyDictionary(this.CreateItemsInt32(json["lights"], false)); } if(json.ContainsKey("groups")) { - this.CreateGroups(json["groups"]); + this.Groups = new ReadOnlyDictionary(this.CreateItemsInt32(json["groups"])); } - if(json.ContainsKey("scenes")) { + if (json.ContainsKey("sensors")) { + this.Sensors = new ReadOnlyDictionary(this.CreateItemsInt32(json["sensors"], false)); + } + if (json.ContainsKey("scenes")) { this.CreateScenes(json["scenes"]); } + if(json.ContainsKey("config")) { + this.Config = AConfig.CreateInstanceConcrete(json["config"], this.http, this.polling); + } + } + + private Dictionary CreateItemsInt32(JsonData json, Boolean withLights = true) { + Dictionary items = new Dictionary(); + foreach (String itemid in json.Keys) { + T item = default(T); + if (withLights) { + item = (T)AConnector.CreateInstance(typeof(T), json[itemid], new Tuple(Int32.Parse(itemid)), this.http, this.polling, this.Lights); + } else { + item = (T)AConnector.CreateInstance(typeof(T), json[itemid], new Tuple(Int32.Parse(itemid)), this.http, this.polling); + } + if(item != null) { + items.Add(Int32.Parse(itemid), item); + } + } + return items; } private void CreateScenes(JsonData json) { Dictionary scenes = new Dictionary(); foreach (String sceneid in json.Keys) { - AScene s = AScene.CreateScene(json[sceneid], new Tuple(sceneid), this.http, this.polling, this.Lights); + AScene s = AScene.CreateInstanceConcrete(json[sceneid], new Tuple(sceneid), this.http, this.polling, this.Lights); if (s != null) { scenes.Add(sceneid, s); } @@ -91,28 +127,6 @@ namespace BlubbFish.IoT.Hue { this.Scenes = new ReadOnlyDictionary(scenes); } - private void CreateGroups(JsonData json) { - Dictionary groups = new Dictionary(); - foreach (String groupid in json.Keys) { - AGroup g = AGroup.CreateGroup(json[groupid], new Tuple(Int32.Parse(groupid)), this.http, this.polling, this.Lights); - if (g != null) { - groups.Add(Int32.Parse(groupid), g); - } - } - this.Groups = new ReadOnlyDictionary(groups); - } - - private void CreateLights(JsonData json) { - Dictionary lights = new Dictionary(); - foreach (String lightid in json.Keys) { - ALight l = ALight.CreateLight(json[lightid], new Tuple(Int32.Parse(lightid)), this.http, this.polling); - if(l != null) { - lights.Add(Int32.Parse(lightid), l); - } - } - this.Lights = new ReadOnlyDictionary(lights); - } - private void CreateKey(String server) { this.http = new HttpConnection(server); JsonData json = this.http.PostJson("", "{\"devicetype\": \"Huelib#BlubbFish\"}"); @@ -148,8 +162,8 @@ namespace BlubbFish.IoT.Hue { this.Lights = null; this.Groups = null; this.Scenes = null; - /*this.Sensors = null; - this.Config = null;*/ + this.Sensors = null; + this.Config = null; this.disposedValue = true; } } diff --git a/Hue/Interfaces/AConfig.cs b/Hue/Interfaces/AConfig.cs new file mode 100644 index 0000000..3fc018e --- /dev/null +++ b/Hue/Interfaces/AConfig.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BlubbFish.IoT.Hue.Devices.Configs; +using BlubbFish.IoT.Hue.lib; +using LitJson; + +namespace BlubbFish.IoT.Hue.Interfaces { + public abstract class AConfig : AConnector, IMqtt { + #region Constructor + public AConfig(JsonData json, HttpConnection http, Boolean polling) : base(http, polling){ + } + + internal static AConfig CreateInstanceConcrete(JsonData json, HttpConnection http, Boolean polling) { + return new Config(json, http, polling); + } + #endregion + + protected Boolean SetConfig(Dictionary dictionary) { + return this.PutDictionary("config", dictionary); + } + + #region IMqtt + public String MqttTopic() { + return "config"; + } + public String ToJson() { + return JsonMapper.ToJson(this.ToDictionary()); + } + #endregion + } +} diff --git a/Hue/Interfaces/AConnector.cs b/Hue/Interfaces/AConnector.cs index 28bcef5..b4872fc 100644 --- a/Hue/Interfaces/AConnector.cs +++ b/Hue/Interfaces/AConnector.cs @@ -1,5 +1,8 @@ using System; +using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Reflection; using BlubbFish.IoT.Hue.Events; using BlubbFish.IoT.Hue.lib; using LitJson; @@ -7,14 +10,75 @@ using LitJson; namespace BlubbFish.IoT.Hue.Interfaces { public abstract class AConnector { protected HttpConnection http; + public delegate void UpdatedValue(Object sender, AllUpdateEvent e); + public event UpdatedValue Update; - protected void PutDictionary(String address, Dictionary value) { - this.http.PutJson(address, JsonMapper.ToJson(value)); + #region Properties + public DateTime LastUpdate { get; private set; } + public Boolean Polling { get; set; } + public Boolean PollOnce { get; set; } + #endregion + + #region Constructor + public AConnector(HttpConnection http, Boolean polling) { + this.http = http; + this.Polling = polling; + this.LastUpdate = DateTime.Now; } - public delegate void UpdatedValue(Object sender, AllUpdateEvent e); + internal static Object CreateInstance(Type type, JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { + return type.GetMethod("CreateInstanceConcrete", new Type[] { typeof(JsonData), typeof(Tuple), typeof(HttpConnection), typeof(Boolean), typeof(ReadOnlyDictionary) }).Invoke(null, new Object[] { json, id, http, polling, lights }); + } + + internal static Object CreateInstance(Type type, JsonData json, Tuple id, HttpConnection http, Boolean polling) { + return type.GetMethod("CreateInstanceConcrete", new Type[] { typeof(JsonData), typeof(Tuple), typeof(HttpConnection), typeof(Boolean) }).Invoke(null, new Object[] { json, id, http, polling }); + } + #endregion + + protected Boolean PutDictionary(String address, Dictionary value) { + JsonData json = this.http.PutJson(address, JsonMapper.ToJson(value)); + if (json.IsArray) { + return json[0].ContainsKey("success"); + } + return false; + } + + public virtual Dictionary ToDictionary() { + Dictionary dictionary = new Dictionary(); + foreach (PropertyInfo item in this.GetType().GetProperties()) { + if (item.CanRead && item.GetValue(this) != null) { + if (item.GetValue(this).GetType().GetMethod("ToDictionary") != null) { + dictionary.Add(item.Name, item.GetValue(this).GetType().GetMethod("ToDictionary").Invoke(item.GetValue(this), null)); + } else if (Helper.HasInterface(item.GetValue(this).GetType(), "IDictionary")) { + Dictionary subdict = new Dictionary(); + foreach (DictionaryEntry subitem in (IDictionary)item.GetValue(this)) { + if (subitem.Value.GetType().GetMethod("ToDictionary") != null) { + subdict.Add(subitem.Key.ToString(), subitem.Value.GetType().GetMethod("ToDictionary").Invoke(subitem.Value, null)); + } + } + dictionary.Add(item.Name, subdict); + } else if (item.GetValue(this).GetType().BaseType == typeof(Enum)) { + dictionary.Add(item.Name, Helper.GetEnumDescription((Enum)item.GetValue(this))); + } else { + dictionary.Add(item.Name, item.GetValue(this)); + } + } + } + return dictionary; + } + + protected void NotifyClient(Object value) { + this.LastUpdate = DateTime.Now; + this.Update?.Invoke(this, (AllUpdateEvent)Activator.CreateInstance(typeof(T), new Object[] { value, this.LastUpdate, this })); + } + + protected void NotifyClientChildren(Object sender, AllUpdateEvent e) { + this.Update?.Invoke(sender, e); + } + + #region Abstracts public abstract override String ToString(); - public abstract Dictionary ToDictionary(); - public abstract event UpdatedValue Update; + public abstract void SetUpdate(JsonData json); + #endregion } } diff --git a/Hue/Interfaces/AGroup.cs b/Hue/Interfaces/AGroup.cs index f3731e2..ad78e53 100644 --- a/Hue/Interfaces/AGroup.cs +++ b/Hue/Interfaces/AGroup.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; +using BlubbFish.IoT.Hue.Events; using BlubbFish.IoT.Hue.lib; using LitJson; @@ -45,42 +46,67 @@ namespace BlubbFish.IoT.Hue.Interfaces { } #region Properties - public DateTime LastUpdate { get; protected set; } - public Boolean Polling { get; set; } public String Name { get { return this._name; } set { - this._name = value; - this.SetGroupAttribute(new Dictionary() { { "name", value } }); + if(this.SetGroupAttribute(new Dictionary() { { "name", value } })) { + this._name = value; + this.NotifyClient(value); + } } } public Int32 GroupId { get; } public Types Groupclass { get; } public Boolean StateAnyOn { get; protected set; } public Boolean StateAllOn { get; protected set; } - public Dictionary Lights { get; protected set; } + public ReadOnlyDictionary Lights { get; protected set; } public GroupClass Class { get { return this._class; } set { - this._class = value; - this.SetGroupAttribute(new Dictionary() { { "class", Helper.GetEnumDescription(value) } }); + if(this.SetGroupAttribute(new Dictionary() { { "class", Helper.GetEnumDescription(value) } })) { + this._class = value; + this.NotifyClient(value); + } } } #endregion #region Constructor - protected AGroup(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { + protected AGroup(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) : base(http, polling) { this.GroupId = id.Item1; this.Groupclass = id.Item2; - this.http = http; - this.LastUpdate = DateTime.Now; - this.Polling = polling; this.ComplexInit(json, lights); } - internal static AGroup CreateGroup(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { + private void ComplexInit(JsonData json, ReadOnlyDictionary lights) { + if (json.ContainsKey("lights") && json["lights"].IsArray) { + Dictionary lightdict = new Dictionary(); + foreach (JsonData item in json["lights"]) { + if (Int32.TryParse(item.ToString(), out Int32 lampid)) { + if (lights.ContainsKey(lampid)) { + lightdict.Add(lampid, lights[lampid]); + } + } + } + this.Lights = new ReadOnlyDictionary(lightdict); + } + if (json.ContainsKey("name") && json["name"].IsString) { + this._name = json["name"].ToString(); + } + if (json.ContainsKey("state") && json["state"].ContainsKey("any_on") && json["state"]["any_on"].IsBoolean) { + this.StateAnyOn = (Boolean)json["state"]["any_on"]; + } + if (json.ContainsKey("state") && json["state"].ContainsKey("all_on") && json["state"]["all_on"].IsBoolean) { + this.StateAllOn = (Boolean)json["state"]["all_on"]; + } + if (json.ContainsKey("class") && json["class"].IsString) { + this._class = (GroupClass)Enum.Parse(typeof(GroupClass), json["class"].ToString().ToEnumString()); + } + } + + public static AGroup CreateInstanceConcrete(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { String type = ""; if (json.ContainsKey("type")) { type = json["type"].ToString().ToEnumString(); @@ -107,54 +133,46 @@ namespace BlubbFish.IoT.Hue.Interfaces { } #endregion - private void ComplexInit(JsonData json, ReadOnlyDictionary lights) { - if (json.ContainsKey("lights") && json["lights"].IsArray) { - this.Lights = new Dictionary(); - foreach (JsonData item in json["lights"]) { - if (Int32.TryParse(item.ToString(), out Int32 lampid)) { - if (lights.ContainsKey(lampid)) { - this.Lights.Add(lampid, lights[lampid]); - } - } + protected Boolean SetGroupAttribute(Dictionary value) { + return this.PutDictionary("groups/" + this.GroupId, value); + } + + protected Boolean SetGroupAction(Dictionary value) { + return this.PutDictionary("groups/" + this.GroupId + "/action", value); + } + + #region AConnector + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("name") && json["name"].IsString) { + if (this.Name != json["name"].ToString()) { + this._name = json["name"].ToString(); + this.NotifyClient(this.Name); } } - if (json.ContainsKey("name")) { - this._name = json["name"].ToString(); - } if (json.ContainsKey("state") && json["state"].ContainsKey("any_on") && json["state"]["any_on"].IsBoolean) { - this.StateAnyOn = (Boolean)json["state"]["any_on"]; + if (this.StateAnyOn != (Boolean)json["state"]["any_on"]) { + this.StateAnyOn = (Boolean)json["state"]["any_on"]; + this.NotifyClient(this.StateAnyOn); + } } if (json.ContainsKey("state") && json["state"].ContainsKey("all_on") && json["state"]["all_on"].IsBoolean) { - this.StateAllOn = (Boolean)json["state"]["all_on"]; + if (this.StateAllOn != (Boolean)json["state"]["all_on"]) { + this.StateAllOn = (Boolean)json["state"]["all_on"]; + this.NotifyClient(this.StateAllOn); + } } - if (json.ContainsKey("class")) { - this._class = (GroupClass)Enum.Parse(typeof(GroupClass), json["class"].ToString().ToEnumString()); + if (json.ContainsKey("class") && json["class"].IsString) { + if (this.Class != (GroupClass)Enum.Parse(typeof(GroupClass), json["class"].ToString().ToEnumString())) { + this._class = (GroupClass)Enum.Parse(typeof(GroupClass), json["class"].ToString().ToEnumString()); + this.NotifyClient(this.Class); + } } } - - protected void SetGroupAttribute(Dictionary value) { - this.PutDictionary("groups/" + this.GroupId, value); - } - - protected void SetGroupAction(Dictionary value) { - this.PutDictionary("groups/" + this.GroupId + "/action", value); - } + #endregion #region IMqtt public String ToJson() { - Dictionary json = this.ToDictionary(); - json.Add("StateAnyOn", this.StateAnyOn.ToString()); - json.Add("StateAllOn", this.StateAllOn.ToString()); - json.Add("Class", Helper.GetEnumDescription(this.Class)); - json.Add("LastUpdate", this.LastUpdate.ToString()); - json.Add("Name", this.Name); - json.Add("Lightclass", this.Groupclass.ToString()); - Dictionary lights = new Dictionary(); - foreach (KeyValuePair item in this.Lights) { - lights.Add(item.Key.ToString(), item.Value.ToDictionary()); - } - json.Add("lights", lights); - return JsonMapper.ToJson(json); + return JsonMapper.ToJson(this.ToDictionary()); } public String MqttTopic() { diff --git a/Hue/Interfaces/ALight.cs b/Hue/Interfaces/ALight.cs index 6c042bf..53d6e31 100644 --- a/Hue/Interfaces/ALight.cs +++ b/Hue/Interfaces/ALight.cs @@ -17,23 +17,33 @@ namespace BlubbFish.IoT.Hue.Interfaces { #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; } + private String _name; + public String Name { + get { + return this._name; + } + set { + if (this.SetLightAttribute(new Dictionary() { { "name", value } })) { + this._name = value; + this.NotifyClient(value); + } + } + } #endregion #region Constructor - protected ALight(JsonData json, Tuple id, HttpConnection http, Boolean polling) { + protected ALight(JsonData json, Tuple id, HttpConnection http, Boolean polling): base(http, polling) { this.LightId = id.Item1; this.Lightclass = id.Item2; - this.http = http; - this.LastUpdate = DateTime.Now; - this.Polling = polling; this.ComplexInit(json); } + private void ComplexInit(JsonData json) { + if (json.ContainsKey("name")) { + this._name = json["name"].ToString(); + } + } - internal static ALight CreateLight(JsonData json, Tuple id, HttpConnection http, Boolean polling) { + public static ALight CreateInstanceConcrete(JsonData json, Tuple id, HttpConnection http, Boolean polling) { String type = ""; if (json.ContainsKey("type")) { type = json["type"].ToString().ToEnumString(); @@ -60,14 +70,11 @@ namespace BlubbFish.IoT.Hue.Interfaces { } #endregion - private void ComplexInit(JsonData json) { - if (json.ContainsKey("name")) { - this.Name = json["name"].ToString(); - } + protected Boolean SetLightStateAttribute(Dictionary value) { + return this.PutDictionary("lights/" + this.LightId + "/state", value); } - - protected void SetLightState(Dictionary value) { - this.PutDictionary("lights/" + this.LightId + "/state", value); + protected Boolean SetLightAttribute(Dictionary value) { + return this.PutDictionary("lights/" + this.LightId, value); } #region IMqtt @@ -76,11 +83,18 @@ namespace BlubbFish.IoT.Hue.Interfaces { } public String ToJson() { - Dictionary json = this.ToDictionary(); - json.Add("LastUpdate", this.LastUpdate.ToString()); - json.Add("Name", this.Name); - json.Add("Lightclass", this.Lightclass.ToString()); - return JsonMapper.ToJson(json); + return JsonMapper.ToJson(this.ToDictionary()); + } + #endregion + + #region AConnector + public override void SetUpdate(JsonData json) { + if(json.ContainsKey("name") && json["name"].IsString) { + if(this.Name != json["name"].ToString()) { + this._name = json["name"].ToString(); + this.NotifyClient(this.Name); + } + } } #endregion } diff --git a/Hue/Interfaces/AScene.cs b/Hue/Interfaces/AScene.cs index 3fbe906..35fc321 100644 --- a/Hue/Interfaces/AScene.cs +++ b/Hue/Interfaces/AScene.cs @@ -7,94 +7,32 @@ using LitJson; namespace BlubbFish.IoT.Hue.Interfaces { public abstract class AScene : AConnector, IMqtt { - private Boolean Polling; - private String _name; - #region Properties public String SceneId { get; } - public DateTime LastUpdate { get; } - public Dictionary Lights { get; private set; } - public Dictionary LightStates { get; private set; } - public String Name { - get { - return this._name; - } - set { - this._name = value; - this.SetSceneAttribute(new Dictionary() { { "name", value } }); - } - } + public Int32 LightStateId { get; } #endregion #region Constructor - protected AScene(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { + protected AScene(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) : base(http, polling) { this.SceneId = id.Item1; - this.http = http; - this.LastUpdate = DateTime.Now; - this.Polling = polling; - this.ComplexInit(json, lights); + this.LightStateId = id.Item2; } - internal static AScene CreateScene(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { - String name = "BlubbFish.IoT.Hue.Devices.Scenes.Scene"; - JsonData scenedata = http.GetJson("scenes/" + id.Item1); - return GetInstanceConcrete(name, scenedata, id, http, polling, lights); - } - - private static AScene GetInstanceConcrete(String name, JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { - Type t = null; - try { - t = Type.GetType(name, true); - } catch (TypeLoadException) { - Helper.WriteError("Konnte Type " + name + " nicht laden!"); - return null; - } - return (AScene)t.GetConstructor(new Type[] { typeof(JsonData), typeof(Tuple), typeof(HttpConnection), typeof(Boolean), typeof(ReadOnlyDictionary) }).Invoke(new Object[] { json, id, http, polling, lights }); + public static AScene CreateInstanceConcrete(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) { + return new Scene(http.GetJson("scenes/" + id.Item1), id, http, polling, lights); } #endregion - private void ComplexInit(JsonData json, ReadOnlyDictionary lights) { - if (json.ContainsKey("lights") && json["lights"].IsArray) { - this.Lights = new Dictionary(); - foreach (JsonData item in json["lights"]) { - if (Int32.TryParse(item.ToString(), out Int32 lampid)) { - if (lights.ContainsKey(lampid)) { - this.Lights.Add(lampid, lights[lampid]); - } - } - } - } - if (json.ContainsKey("name")) { - this._name = json["name"].ToString(); - } - if (json.ContainsKey("lightstates")) { - this.LightStates = new Dictionary(); - foreach (String item in json["lightstates"].Keys) { - this.LightStates.Add(Int32.Parse(item), new LightState(json["lightstates"][item], new Tuple(this.SceneId, Int32.Parse(item)), this.http)); - } - } + protected Boolean SetSceneAttribute(Dictionary dictionary) { + return this.PutDictionary("scenes/" + this.SceneId, dictionary); } - - protected void SetSceneAttribute(Dictionary dictionary) { - this.PutDictionary("scenes/" + this.SceneId, dictionary); + protected Boolean SetLightSceneAttribute(Dictionary value) { + return this.PutDictionary("scenes/" + this.SceneId + "/lights/" + this.LightStateId + "/state", value); } #region IMqtt public String ToJson() { - Dictionary json = this.ToDictionary(); - json.Add("LastUpdate", this.LastUpdate.ToString()); - json.Add("Name", this.Name); - Dictionary lights = new Dictionary(); - foreach (KeyValuePair item in this.Lights) { - lights.Add(item.Key.ToString(), item.Value.ToDictionary()); - } - json.Add("lights", lights); - Dictionary states = new Dictionary(); - foreach (KeyValuePair item in this.LightStates) { - states.Add(item.Key.ToString(), item.Value.ToDictionary()); - } - json.Add("lightstates", states); - return JsonMapper.ToJson(json); + return JsonMapper.ToJson(this.ToDictionary()); } public String MqttTopic() { diff --git a/Hue/Interfaces/ASensor.cs b/Hue/Interfaces/ASensor.cs new file mode 100644 index 0000000..664fd10 --- /dev/null +++ b/Hue/Interfaces/ASensor.cs @@ -0,0 +1,127 @@ +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 ASensor : AConnector, IMqtt { + + + public enum Types { + Daylight + } + public enum TypesIgnore { + Zgpswitch, + Zllswitch, + Zllpresence, + Zlltemperature, + Clipswitch, + Clipopenclose, + Clippresence, + Cliptemperature, + Cliphumidity, + Cliplightlevel, + Zlllightlevel, + Clipgenericflag, + Clipgenericstatus + } + + #region Properties + public Int32 SensorId { get; } + public Types Sensorclass { get; } + public DateTime Lastupdated { get; protected set; } + private String _name; + public String Name { + get { + return this._name; + } + set { + this._name = value; + this.SetSensorAttribute(new Dictionary() { { "name", value } }); + } + } + #endregion + + #region Constructor + protected ASensor(JsonData json, Tuple id, HttpConnection http, Boolean polling) : base(http, polling) { + this.SensorId = id.Item1; + this.Sensorclass = id.Item2; + this.ComplexInit(json); + } + + private void ComplexInit(JsonData json) { + if (json.ContainsKey("name")) { + this._name = json["name"].ToString(); + } + if (json.ContainsKey("state")) { + JsonData state = json["state"]; + if (state.ContainsKey("lastupdated") && state["lastupdated"].IsString) { + this.Lastupdated = DateTime.Parse(state["lastupdated"].ToString()); + } + } + } + + public static ASensor CreateInstanceConcrete(JsonData json, Tuple 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.Sensors." + type; + return GetInstanceConcrete(name, json, new Tuple(id.Item1, (Types)Enum.Parse(typeof(Types), type)), http, polling); + } + if (!Enum.IsDefined(typeof(TypesIgnore), type) && !Enum.IsDefined(typeof(Types), type)) { + Helper.WriteError("Sensorclass " + type + " not exist."); + } + return null; + } + + private static ASensor GetInstanceConcrete(String name, JsonData json, Tuple 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 (ASensor)t.GetConstructor(new Type[] { typeof(JsonData), typeof(Tuple), typeof(HttpConnection), typeof(Boolean) }).Invoke(new Object[] { json, id, http, polling }); + } + #endregion + + protected void SetSensorAttribute(Dictionary dictionary) { + this.PutDictionary("sensors/" + this.SensorId, dictionary); + } + + protected Boolean SetSensorConfig(Dictionary dictionary) { + return this.PutDictionary("sensors/" + this.SensorId + "/config", dictionary); + } + + #region AConnector + public override void SetUpdate(JsonData json) { + if (json.ContainsKey("name") && json["name"].IsString) { + if (this.Name != json["name"].ToString()) { + this._name = json["name"].ToString(); + this.NotifyClient(this.Name); + } + } + if (json.ContainsKey("state") && json["state"].ContainsKey("lastupdated") && json["state"]["lastupdated"].IsString) { + if (this.Lastupdated != DateTime.Parse(json["state"]["lastupdated"].ToString())) { + this.Lastupdated = DateTime.Parse(json["state"]["lastupdated"].ToString()); + this.NotifyClient(this.Lastupdated); + } + } + } + #endregion + + #region IMqtt + public String MqttTopic() { + return "groups/" + this.SensorId + "/" + this.Sensorclass.ToString(); + } + + public String ToJson() { + return JsonMapper.ToJson(this.ToDictionary()); + } + #endregion + } +} diff --git a/Hue/Interfaces/IMqtt.cs b/Hue/Interfaces/IMqtt.cs index fc96a62..36001bc 100644 --- a/Hue/Interfaces/IMqtt.cs +++ b/Hue/Interfaces/IMqtt.cs @@ -5,8 +5,16 @@ using System.Text; using System.Threading.Tasks; namespace BlubbFish.IoT.Hue.Interfaces { - interface IMqtt { + public interface IMqtt { + /// + /// Gibt einen JSON-String der Eigenschaften eines Objekts zurück + /// + /// JSON-String String ToJson(); + /// + /// Gibt das MQTT-Topic als String zurück unter dem die Daten des Objekts Publiziert werden sollen + /// + /// MQTT-Topic String MqttTopic(); } } diff --git a/Hue/bin/Release/Hue.dll b/Hue/bin/Release/Hue.dll new file mode 100644 index 0000000..f73a4a4 Binary files /dev/null and b/Hue/bin/Release/Hue.dll differ diff --git a/Hue/bin/Release/litjson.dll b/Hue/bin/Release/litjson.dll new file mode 100644 index 0000000..892f38a Binary files /dev/null and b/Hue/bin/Release/litjson.dll differ diff --git a/Hue/lib/Helper.cs b/Hue/lib/Helper.cs index e0724f9..ab9ba57 100644 --- a/Hue/lib/Helper.cs +++ b/Hue/lib/Helper.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.ObjectModel; using System.ComponentModel; using System.Reflection; +using LitJson; namespace BlubbFish.IoT.Hue.lib { class Helper { @@ -20,6 +22,34 @@ namespace BlubbFish.IoT.Hue.lib { return value.ToString(); } } + internal static Boolean HasInterface(Type o, String type) { + foreach (Type item in o.GetInterfaces()) { + if (item.Name == type) { + return true; + } + } + return false; + } + + public static void SetUpdate(ReadOnlyDictionary list, JsonData json, String key) { + if (json.ContainsKey(key)) { + foreach (String item in json[key].Keys) { + if (list.ContainsKey(Int32.Parse(item))) { + list[Int32.Parse(item)].GetType().GetMethod("SetUpdate").Invoke(list[Int32.Parse(item)], new Object[] { json[key][item] }); + } + } + } + } + + public static void SetUpdate(ReadOnlyDictionary list, JsonData json, String key) { + if (json.ContainsKey(key)) { + foreach (String item in json[key].Keys) { + if (list.ContainsKey(item)) { + list[item].GetType().GetMethod("SetUpdate").Invoke(list[item], new Object[] { json[key][item] }); + } + } + } + } } public static class StringHelper { public static String ToEnumString(this String text) {