Change a lot of stuff
This commit is contained in:
+25
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27004.2010
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hue", "Hue\Hue.csproj", "{738A6137-8FCA-4CE1-BD1C-042B21B6B5F2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{738A6137-8FCA-4CE1-BD1C-042B21B6B5F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{738A6137-8FCA-4CE1-BD1C-042B21B6B5F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{738A6137-8FCA-4CE1-BD1C-042B21B6B5F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{738A6137-8FCA-4CE1-BD1C-042B21B6B5F2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {8F059288-842C-4EA3-AFD4-9AE2E17BD1B3}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -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<String, Object>() { { "name", value } })) {
|
||||
this._name = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private Boolean _linkbutton;
|
||||
public Boolean Linkbutton {
|
||||
get {
|
||||
return this._linkbutton;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "linkbutton", value } })) {
|
||||
this._linkbutton = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private String _ipaddress;
|
||||
public String Ipaddress {
|
||||
get {
|
||||
return this._ipaddress;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "ipaddress", value } })) {
|
||||
this._ipaddress = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private String _netmask;
|
||||
public String Netmask {
|
||||
get {
|
||||
return this._netmask;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "netmask", value } })) {
|
||||
this._netmask = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private String _gateway;
|
||||
public String Gateway {
|
||||
get {
|
||||
return this._gateway;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "gateway", value } })) {
|
||||
this._gateway = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private Boolean _dhcp;
|
||||
public Boolean Dhcp {
|
||||
get {
|
||||
return this._dhcp;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "dhcp", value } })) {
|
||||
this._dhcp = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private DateTime _utc;
|
||||
public DateTime Utc {
|
||||
get {
|
||||
return this._utc;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "UTC", value } })) {
|
||||
this._utc = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private String _timezone;
|
||||
public String Timezone {
|
||||
get {
|
||||
return this._timezone;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "timezone", value } })) {
|
||||
this._timezone = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private Byte _zigbeechannel;
|
||||
public Byte Zigbeechannel {
|
||||
get {
|
||||
return this._zigbeechannel;
|
||||
}
|
||||
set {
|
||||
if (this.SetConfig(new Dictionary<String, Object>() { { "zigbeechannel", value } })) {
|
||||
this._zigbeechannel = value;
|
||||
this.NotifyClient<ConfigUpdateEvent>(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<String, Whitelist> 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<String, Whitelist> whitelists = new Dictionary<String, Whitelist>();
|
||||
foreach (String item in json.Keys) {
|
||||
Whitelist w = new Whitelist(json[item], new Tuple<String>(item), this.http, this.Polling);
|
||||
w.Update += this.NotifyClientChildren;
|
||||
whitelists.Add(item, w);
|
||||
}
|
||||
this.Whitelist = new ReadOnlyDictionary<String, Whitelist>(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<ConfigUpdateEvent>(this.Name);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("apiversion") && json["apiversion"].IsString) {
|
||||
if (this.Apiversion != json["apiversion"].ToString()) {
|
||||
this.Apiversion = json["apiversion"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Apiversion);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("swversion") && json["swversion"].IsString) {
|
||||
if (this.Swversion != json["swversion"].ToString()) {
|
||||
this.Swversion = json["swversion"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Swversion);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("linkbutton") && json["linkbutton"].IsBoolean) {
|
||||
if (this.Linkbutton != (Boolean)json["linkbutton"]) {
|
||||
this._linkbutton = (Boolean)json["linkbutton"];
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Linkbutton);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("ipaddress") && json["ipaddress"].IsString) {
|
||||
if (this.Ipaddress != json["ipaddress"].ToString()) {
|
||||
this._ipaddress = json["ipaddress"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Ipaddress);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("mac") && json["mac"].IsString) {
|
||||
if (this.Mac != json["mac"].ToString()) {
|
||||
this.Mac = json["mac"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Mac);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("netmask") && json["netmask"].IsString) {
|
||||
if (this.Netmask != json["netmask"].ToString()) {
|
||||
this._netmask = json["netmask"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Netmask);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("gateway") && json["gateway"].IsString) {
|
||||
if (this.Gateway != json["gateway"].ToString()) {
|
||||
this._gateway = json["gateway"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Gateway);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("dhcp") && json["dhcp"].IsBoolean) {
|
||||
if (this.Dhcp != (Boolean)json["dhcp"]) {
|
||||
this._dhcp = (Boolean)json["dhcp"];
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Dhcp);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("portalservices") && json["portalservices"].IsBoolean) {
|
||||
if (this.Portalservices != (Boolean)json["portalservices"]) {
|
||||
this.Portalservices = (Boolean)json["portalservices"];
|
||||
this.NotifyClient<ConfigUpdateEvent>(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<ConfigUpdateEvent>(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<ConfigUpdateEvent>(this.Localtime);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("timezone") && json["timezone"].IsString) {
|
||||
if (this.Timezone != json["timezone"].ToString()) {
|
||||
this._timezone = json["timezone"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Timezone);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("zigbeechannel") && json["zigbeechannel"].IsInt) {
|
||||
if (this.Zigbeechannel != (Byte)json["zigbeechannel"]) {
|
||||
this._zigbeechannel = (Byte)json["zigbeechannel"];
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Zigbeechannel);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("modelid") && json["modelid"].IsString) {
|
||||
if (this.Modelid != json["modelid"].ToString()) {
|
||||
this.Modelid = json["modelid"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Modelid);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("bridgeid") && json["bridgeid"].IsString) {
|
||||
if (this.Bridgeid != json["bridgeid"].ToString()) {
|
||||
this.Bridgeid = json["bridgeid"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(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<ConfigUpdateEvent>(this.Replacesbridgeid);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("factorynew") && json["factorynew"].IsBoolean) {
|
||||
if (this.Factorynew != (Boolean)json["factorynew"]) {
|
||||
this.Factorynew = (Boolean)json["factorynew"];
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Factorynew);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("datastoreversion") && json["datastoreversion"].IsString) {
|
||||
if (this.Datastoreversion != json["datastoreversion"].ToString()) {
|
||||
this.Datastoreversion = json["datastoreversion"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Datastoreversion);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("starterkitid") && json["starterkitid"].IsString) {
|
||||
if (this.Starterkitid != json["starterkitid"].ToString()) {
|
||||
this.Starterkitid = json["starterkitid"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(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
|
||||
}
|
||||
}
|
||||
@@ -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<ConfigUpdateEvent>(this.Signedon);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("incoming") && json["incoming"].IsBoolean) {
|
||||
if (this.Incoming != (Boolean)json["incoming"]) {
|
||||
this.Incoming = (Boolean)json["incoming"];
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Incoming);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("outgoing") && json["outgoing"].IsBoolean) {
|
||||
if (this.Outgoing != (Boolean)json["outgoing"]) {
|
||||
this.Outgoing = (Boolean)json["outgoing"];
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Outgoing);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("communication") && json["communication"].IsString) {
|
||||
if (this.Communication != json["communication"].ToString()) {
|
||||
this.Communication = json["communication"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Communication);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -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<ConfigUpdateEvent>(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<ConfigUpdateEvent>(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<ConfigUpdateEvent>(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<ConfigUpdateEvent>(this.Bridgelastinstall);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("state") && json["state"].IsString) {
|
||||
if (this.State != json["state"].ToString()) {
|
||||
this.State = json["state"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(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<ConfigUpdateEvent>(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<ConfigUpdateEvent>(this.Autoinstallon);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<ConfigUpdateEvent>(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<ConfigUpdateEvent>(this.Createdate);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("name") && json["name"].IsString) {
|
||||
if (this.Name != json["name"].ToString()) {
|
||||
this.Name = json["name"].ToString();
|
||||
this.NotifyClient<ConfigUpdateEvent>(this.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
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 enum AlertEffect {
|
||||
none,
|
||||
select,
|
||||
lselect
|
||||
}
|
||||
|
||||
#region Properties
|
||||
private AlertEffect _alert;
|
||||
|
||||
private Boolean _state;
|
||||
public Boolean State {
|
||||
get {
|
||||
return this._state;
|
||||
}
|
||||
set {
|
||||
if(this.SetGroupAction(new Dictionary<String, Object>() { { "on", value } })) {
|
||||
this._state = value;
|
||||
this.NotifyClient<GroupUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private Byte _brightness;
|
||||
public Byte Brightness {
|
||||
get {
|
||||
return this._brightness;
|
||||
}
|
||||
set {
|
||||
if(this.SetGroupAction(new Dictionary<String, Object>() { { "bri", value } })) {
|
||||
this._brightness = value;
|
||||
this.NotifyClient<GroupUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
public AlertEffect Alert {
|
||||
get {
|
||||
return this._alert;
|
||||
}
|
||||
set {
|
||||
if(this.SetGroupAction(new Dictionary<String, Object>() { { "alert", value } })) {
|
||||
this._alert = value;
|
||||
this.NotifyClient<GroupUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public Room(JsonData json, Tuple<Int32, Types> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) : base(json, id, http, polling, lights) {
|
||||
this.ComplexInit(json);
|
||||
}
|
||||
|
||||
private void ComplexInit(JsonData json) {
|
||||
if (json.ContainsKey("action")) {
|
||||
JsonData state = json["action"];
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region AConnector
|
||||
public override String ToString() {
|
||||
List<String> lid = new List<String>();
|
||||
foreach (KeyValuePair<Int32, ALight> item in this.Lights) {
|
||||
lid.Add(item.Key.ToString());
|
||||
}
|
||||
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<GroupUpdateEvent>(this.State);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("bri") && json["bri"].IsInt) {
|
||||
if (this.Brightness != (Byte)json["bri"]) {
|
||||
this._brightness = (Byte)json["bri"];
|
||||
this.NotifyClient<GroupUpdateEvent>(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<GroupUpdateEvent>(this.Alert);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
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 : AScene {
|
||||
#region Properties
|
||||
private Boolean _state;
|
||||
public Boolean State {
|
||||
get {
|
||||
return this._state;
|
||||
}
|
||||
set {
|
||||
if (this.SetLightSceneAttribute(new Dictionary<String, Object>() { { "on", value } })) {
|
||||
this._state = value;
|
||||
this.NotifyClient<SceneUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
private Byte _brightness;
|
||||
public Byte Brightness {
|
||||
get {
|
||||
return this._brightness;
|
||||
}
|
||||
set {
|
||||
if(this.SetLightSceneAttribute(new Dictionary<String, Object>() { { "bri", value } })) {
|
||||
this._brightness = value;
|
||||
this.NotifyClient<SceneUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public LightState(JsonData json, Tuple<String, Int32> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) : base(json, id, http, polling, lights){
|
||||
this.ComplexInit(json);
|
||||
}
|
||||
|
||||
private void ComplexInit(JsonData json) {
|
||||
if (json.ContainsKey("on") && json["on"].IsBoolean) {
|
||||
this._state = (Boolean)json["on"];
|
||||
}
|
||||
if (json.ContainsKey("bri") && json["bri"].IsInt) {
|
||||
this._brightness = (Byte)json["bri"];
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region AConnector
|
||||
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<SceneUpdateEvent>(this.State);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("bri") && json["bri"].IsInt) {
|
||||
if (this.Brightness != (Byte)json["bri"]) {
|
||||
this._brightness = (Byte)json["bri"];
|
||||
this.NotifyClient<SceneUpdateEvent>(this.Brightness);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
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 {
|
||||
#region Properties
|
||||
private String _name;
|
||||
public String Name {
|
||||
get {
|
||||
return this._name;
|
||||
}
|
||||
set {
|
||||
if (this.SetSceneAttribute(new Dictionary<String, Object>() { { "name", value } })) {
|
||||
this._name = value;
|
||||
this.NotifyClient<SceneUpdateEvent>(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<Int32, ALight> Lights { get; private set; }
|
||||
public ReadOnlyDictionary<Int32, LightState> LightStates { get; private set; }
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public Scene(JsonData json, Tuple<String> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) : base(json, new Tuple<String, Int32>(id.Item1, 0), http, polling, lights) {
|
||||
this.ComplexInit(json, lights);
|
||||
}
|
||||
|
||||
private void ComplexInit(JsonData json, ReadOnlyDictionary<Int32, ALight> 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) {
|
||||
this.Recycle = (Boolean)json["recycle"];
|
||||
}
|
||||
if (json.ContainsKey("locked") && json["locked"].IsBoolean) {
|
||||
this.Locked = (Boolean)json["locked"];
|
||||
}
|
||||
if (json.ContainsKey("picture") && json["picture"].IsString) {
|
||||
this.Picture = json["picture"].ToString();
|
||||
}
|
||||
if (json.ContainsKey("lastupdated") && json["lastupdated"].IsString) {
|
||||
this.Lastupdated = DateTime.Parse(json["lastupdated"].ToString());
|
||||
}
|
||||
if (json.ContainsKey("version") && json["version"].IsInt) {
|
||||
this.Version = (Int32)json["version"];
|
||||
}
|
||||
if (json.ContainsKey("lights") && json["lights"].IsArray) {
|
||||
Dictionary<Int32, ALight> lightdict = new Dictionary<Int32, ALight>();
|
||||
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<Int32, ALight>(lightdict);
|
||||
}
|
||||
if (json.ContainsKey("lightstates") && json["lightstates"].IsObject) {
|
||||
Dictionary<Int32, LightState> lightstates = new Dictionary<Int32, LightState>();
|
||||
foreach (String item in json["lightstates"].Keys) {
|
||||
LightState l = new LightState(json["lightstates"][item], new Tuple<String, Int32>(this.SceneId, Int32.Parse(item)), this.http, this.Polling, lights);
|
||||
l.Update += this.NotifyClientChildren;
|
||||
lightstates.Add(Int32.Parse(item), l);
|
||||
}
|
||||
this.LightStates = new ReadOnlyDictionary<Int32, LightState>(lightstates);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region AConnector
|
||||
public override String ToString() {
|
||||
List<String> lid = new List<String>();
|
||||
foreach (KeyValuePair<Int32, ALight> item in this.Lights) {
|
||||
lid.Add(item.Key.ToString());
|
||||
}
|
||||
List<String> lst = new List<String>();
|
||||
foreach (KeyValuePair<Int32, LightState> item in this.LightStates) {
|
||||
lst.Add(item.Value.ToString());
|
||||
}
|
||||
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<SceneUpdateEvent>(this.Name);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("owner") && json["owner"].IsString) {
|
||||
if (this.Owner != json["owner"].ToString()) {
|
||||
this.Owner = json["owner"].ToString();
|
||||
this.NotifyClient<SceneUpdateEvent>(this.Owner);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("recycle") && json["recycle"].IsBoolean) {
|
||||
if (this.Recycle != (Boolean)json["recycle"]) {
|
||||
this.Recycle = (Boolean)json["recycle"];
|
||||
this.NotifyClient<SceneUpdateEvent>(this.Recycle);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("locked") && json["locked"].IsBoolean) {
|
||||
if (this.Locked != (Boolean)json["locked"]) {
|
||||
this.Locked = (Boolean)json["locked"];
|
||||
this.NotifyClient<SceneUpdateEvent>(this.Locked);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("picture") && json["picture"].IsString) {
|
||||
if (this.Picture != json["picture"].ToString()) {
|
||||
this.Picture = json["picture"].ToString();
|
||||
this.NotifyClient<SceneUpdateEvent>(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<SceneUpdateEvent>(this.Lastupdated);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("version") && json["version"].IsInt) {
|
||||
if (this.Version != (Int32)json["version"]) {
|
||||
this.Version = (Int32)json["version"];
|
||||
this.NotifyClient<SceneUpdateEvent>(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
|
||||
}
|
||||
}
|
||||
@@ -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<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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.Events {
|
||||
public class AllUpdateEvent : EventArgs {
|
||||
|
||||
public AllUpdateEvent() {
|
||||
}
|
||||
|
||||
public AllUpdateEvent(Object value, DateTime time, Object parent) {
|
||||
this.GetValue = value;
|
||||
this.UpdateTime = time;
|
||||
this.Parent = parent;
|
||||
}
|
||||
|
||||
public Object GetValue { get; }
|
||||
public DateTime UpdateTime { get; }
|
||||
public Object Parent { get; private set; }
|
||||
}
|
||||
public class LightUpdateEvent : AllUpdateEvent {
|
||||
public LightUpdateEvent() {
|
||||
}
|
||||
|
||||
public LightUpdateEvent(Object value, DateTime time, Object parent) : base(value, time, parent) {
|
||||
}
|
||||
}
|
||||
public class GroupUpdateEvent : AllUpdateEvent {
|
||||
public GroupUpdateEvent() {
|
||||
}
|
||||
|
||||
public GroupUpdateEvent(Object value, DateTime time, Object parent) : base(value, time, parent) {
|
||||
}
|
||||
}
|
||||
|
||||
public class SceneUpdateEvent : AllUpdateEvent {
|
||||
public SceneUpdateEvent() {
|
||||
}
|
||||
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.Exceptions {
|
||||
[Serializable]
|
||||
public class ConnectionException : Exception {
|
||||
public ConnectionException() { }
|
||||
public ConnectionException(String message) : base(message) { }
|
||||
public ConnectionException(String message, Exception innerException) : base(message, innerException) { }
|
||||
protected ConnectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{738A6137-8FCA-4CE1-BD1C-042B21B6B5F2}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>BlubbFish.IoT.Hue</RootNamespace>
|
||||
<AssemblyName>Hue</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Devices\Configs\Config.cs" />
|
||||
<Compile Include="Devices\Configs\Portalstate.cs" />
|
||||
<Compile Include="Devices\Configs\SwUpdate.cs" />
|
||||
<Compile Include="Devices\Configs\Whitelist.cs" />
|
||||
<Compile Include="Devices\Scenes\LightState.cs" />
|
||||
<Compile Include="Devices\Scenes\Scene.cs" />
|
||||
<Compile Include="Devices\Sensors\Daylight.cs" />
|
||||
<Compile Include="Interfaces\AConfig.cs" />
|
||||
<Compile Include="Interfaces\AGroup.cs" />
|
||||
<Compile Include="Devices\Groups\Room.cs" />
|
||||
<Compile Include="Devices\Lights\Dimmablelight.cs" />
|
||||
<Compile Include="Interfaces\ALight.cs" />
|
||||
<Compile Include="Events\UpdateEvent.cs" />
|
||||
<Compile Include="Exceptions\HueExceptions.cs" />
|
||||
<Compile Include="Interfaces\AScene.cs" />
|
||||
<Compile Include="Interfaces\AConnector.cs" />
|
||||
<Compile Include="Interfaces\ASensor.cs" />
|
||||
<Compile Include="Interfaces\IMqtt.cs" />
|
||||
<Compile Include="HueController.cs" />
|
||||
<Compile Include="lib\Helper.cs" />
|
||||
<Compile Include="lib\HttpClient.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj">
|
||||
<Project>{91a14cd2-2940-4500-8193-56d37edddbaa}</Project>
|
||||
<Name>litjson_4.7.1</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,192 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading;
|
||||
using BlubbFish.IoT.Hue.Events;
|
||||
using BlubbFish.IoT.Hue.Exceptions;
|
||||
using BlubbFish.IoT.Hue.Interfaces;
|
||||
using BlubbFish.IoT.Hue.lib;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.IoT.Hue {
|
||||
public class HueController : IDisposable {
|
||||
private Boolean polling;
|
||||
private HttpConnection http;
|
||||
private Thread updatethread;
|
||||
private Thread pollthread;
|
||||
|
||||
public delegate void DataUpdate(Object sender, AllUpdateEvent e);
|
||||
public event DataUpdate Update;
|
||||
|
||||
public ReadOnlyDictionary<Int32, ALight> Lights { get; private set; }
|
||||
public ReadOnlyDictionary<Int32, AGroup> Groups { get; private set; }
|
||||
public ReadOnlyDictionary<String, AScene> Scenes { get; private set; }
|
||||
public ReadOnlyDictionary<Int32, ASensor> Sensors { get; private set; }
|
||||
public AConfig Config { get; private set; }
|
||||
|
||||
public HueController(Dictionary<String,String> settings, Boolean enablePoll = true) {
|
||||
this.polling = enablePoll;
|
||||
if(!settings.ContainsKey("server")) {
|
||||
throw new ArgumentException("Missing \"server\" in Configurationfile");
|
||||
}
|
||||
if(!settings.ContainsKey("key")) {
|
||||
this.CreateKey(settings["server"]);
|
||||
return;
|
||||
}
|
||||
this.Connect(settings["server"], settings["key"]);
|
||||
}
|
||||
|
||||
private void Connect(String server, String key) {
|
||||
this.http = new HttpConnection(server, key);
|
||||
this.CreateAll(this.http.GetJson(""));
|
||||
this.updatethread = new Thread(this.Updater);
|
||||
this.updatethread.Start();
|
||||
this.pollthread = new Thread(this.Poll);
|
||||
this.pollthread.Start();
|
||||
foreach (KeyValuePair<Int32, ALight> item in this.Lights) {
|
||||
item.Value.Update += this.AllUpdate;
|
||||
}
|
||||
foreach (KeyValuePair<Int32, AGroup> item in this.Groups) {
|
||||
item.Value.Update += this.AllUpdate;
|
||||
}
|
||||
foreach (KeyValuePair<String, AScene> item in this.Scenes) {
|
||||
item.Value.Update += this.AllUpdate;
|
||||
}
|
||||
foreach (KeyValuePair<Int32, ASensor> item in this.Sensors) {
|
||||
item.Value.Update += this.AllUpdate;
|
||||
}
|
||||
this.Config.Update += this.AllUpdate;
|
||||
}
|
||||
|
||||
private void Poll() {
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public AConnector GetConnector(String type, String id) {
|
||||
switch (type) {
|
||||
case "sensor":
|
||||
return this.Sensors.ContainsKey(Int32.Parse(id)) ? this.Sensors[Int32.Parse(id)] : null;
|
||||
case "scene":
|
||||
return this.Scenes.ContainsKey(id) ? this.Scenes[id] : null;
|
||||
case "light":
|
||||
return this.Lights.ContainsKey(Int32.Parse(id)) ? this.Lights[Int32.Parse(id)] : null;
|
||||
case "group":
|
||||
return this.Groups.ContainsKey(Int32.Parse(id)) ? this.Groups[Int32.Parse(id)] : null;
|
||||
case "config":
|
||||
return this.Config;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void AllUpdate(Object sender, AllUpdateEvent e) {
|
||||
this.Update?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
private void Updater() {
|
||||
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.Lights = new ReadOnlyDictionary<Int32, ALight>(this.CreateItemsInt32<ALight>(json["lights"], false));
|
||||
}
|
||||
if(json.ContainsKey("groups")) {
|
||||
this.Groups = new ReadOnlyDictionary<Int32, AGroup>(this.CreateItemsInt32<AGroup>(json["groups"]));
|
||||
}
|
||||
if (json.ContainsKey("sensors")) {
|
||||
this.Sensors = new ReadOnlyDictionary<Int32, ASensor>(this.CreateItemsInt32<ASensor>(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<Int32, T> CreateItemsInt32<T>(JsonData json, Boolean withLights = true) {
|
||||
Dictionary<Int32, T> items = new Dictionary<Int32, T>();
|
||||
foreach (String itemid in json.Keys) {
|
||||
T item = default(T);
|
||||
if (withLights) {
|
||||
item = (T)AConnector.CreateInstance(typeof(T), json[itemid], new Tuple<Int32>(Int32.Parse(itemid)), this.http, this.polling, this.Lights);
|
||||
} else {
|
||||
item = (T)AConnector.CreateInstance(typeof(T), json[itemid], new Tuple<Int32>(Int32.Parse(itemid)), this.http, this.polling);
|
||||
}
|
||||
if(item != null) {
|
||||
items.Add(Int32.Parse(itemid), item);
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private void CreateScenes(JsonData json) {
|
||||
Dictionary<String, AScene> scenes = new Dictionary<String, AScene>();
|
||||
foreach (String sceneid in json.Keys) {
|
||||
AScene s = AScene.CreateInstanceConcrete(json[sceneid], new Tuple<String>(sceneid), this.http, this.polling, this.Lights);
|
||||
if (s != null) {
|
||||
scenes.Add(sceneid, s);
|
||||
}
|
||||
}
|
||||
this.Scenes = new ReadOnlyDictionary<String, AScene>(scenes);
|
||||
}
|
||||
|
||||
private void CreateKey(String server) {
|
||||
this.http = new HttpConnection(server);
|
||||
JsonData json = this.http.PostJson("", "{\"devicetype\": \"Huelib#BlubbFish\"}");
|
||||
if(json.IsArray && json[0].ContainsKey("error") && json[0]["error"].ContainsKey("type") && json[0]["error"]["type"].IsInt) {
|
||||
if((Int32)json[0]["error"]["type"] == 101) {
|
||||
Helper.WriteError("Please press the linkbutton on the Hue-Bridge! Then press return!");
|
||||
}
|
||||
}
|
||||
Console.ReadLine();
|
||||
json = this.http.PostJson("", "{\"devicetype\": \"Huelib#BlubbFish\"}");
|
||||
String key = "";
|
||||
if(json.IsArray && json[0].ContainsKey("success") && json[0]["success"].ContainsKey("username") && json[0]["success"]["username"].IsString) {
|
||||
key = json[0]["success"]["username"].ToString();
|
||||
}
|
||||
Helper.WriteError("Create \"key\" in config file! key=" + key);
|
||||
throw new ConnectionException("Create \"key\" in config file! key=" + key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#region IDisposable Support
|
||||
private Boolean disposedValue = false;
|
||||
|
||||
protected virtual void Dispose(Boolean disposing) {
|
||||
if (!this.disposedValue) {
|
||||
if (disposing) {
|
||||
this.updatethread.Abort();
|
||||
this.pollthread.Abort();
|
||||
}
|
||||
this.updatethread = null;
|
||||
this.pollthread = null;
|
||||
this.Lights = null;
|
||||
this.Groups = null;
|
||||
this.Scenes = null;
|
||||
this.Sensors = null;
|
||||
this.Config = null;
|
||||
this.disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Dispose(true);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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<String, Object> dictionary) {
|
||||
return this.PutDictionary("config", dictionary);
|
||||
}
|
||||
|
||||
#region IMqtt
|
||||
public String MqttTopic() {
|
||||
return "config";
|
||||
}
|
||||
public String ToJson() {
|
||||
return JsonMapper.ToJson(this.ToDictionary());
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
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;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.Interfaces {
|
||||
public abstract class AConnector {
|
||||
protected HttpConnection http;
|
||||
public delegate void UpdatedValue(Object sender, AllUpdateEvent e);
|
||||
public event UpdatedValue Update;
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
internal static Object CreateInstance(Type type, JsonData json, Tuple<Int32> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) {
|
||||
return type.GetMethod("CreateInstanceConcrete", new Type[] { typeof(JsonData), typeof(Tuple<Int32>), typeof(HttpConnection), typeof(Boolean), typeof(ReadOnlyDictionary<Int32, ALight>) }).Invoke(null, new Object[] { json, id, http, polling, lights });
|
||||
}
|
||||
|
||||
internal static Object CreateInstance(Type type, JsonData json, Tuple<Int32> id, HttpConnection http, Boolean polling) {
|
||||
return type.GetMethod("CreateInstanceConcrete", new Type[] { typeof(JsonData), typeof(Tuple<Int32>), typeof(HttpConnection), typeof(Boolean) }).Invoke(null, new Object[] { json, id, http, polling });
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected Boolean PutDictionary(String address, Dictionary<String, Object> value) {
|
||||
JsonData json = this.http.PutJson(address, JsonMapper.ToJson(value));
|
||||
if (json.IsArray) {
|
||||
return json[0].ContainsKey("success");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual Dictionary<String, Object> ToDictionary() {
|
||||
Dictionary<String, Object> dictionary = new Dictionary<String, Object>();
|
||||
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<String, Object> subdict = new Dictionary<String, Object>();
|
||||
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<T>(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 void SetUpdate(JsonData json);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using BlubbFish.IoT.Hue.Events;
|
||||
using BlubbFish.IoT.Hue.lib;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.Interfaces {
|
||||
public abstract class AGroup : AConnector, IMqtt {
|
||||
protected String _name;
|
||||
protected GroupClass _class;
|
||||
|
||||
public enum GroupClass {
|
||||
[Description("Living room")]
|
||||
Livingroom,
|
||||
Kitchen,
|
||||
Dining,
|
||||
Bedroom,
|
||||
[Description("Kids bedroom")]
|
||||
Kidsbedroom,
|
||||
Bathroom,
|
||||
Nursery,
|
||||
Recreation,
|
||||
Office,
|
||||
Gym,
|
||||
Hallway,
|
||||
Toilet,
|
||||
[Description("Front door")]
|
||||
Frontdoor,
|
||||
Garage,
|
||||
Terrace,
|
||||
Garden,
|
||||
Driveway,
|
||||
Carport,
|
||||
Other
|
||||
}
|
||||
public enum Types {
|
||||
Room
|
||||
}
|
||||
public enum TypesIgnore {
|
||||
Luminaire,
|
||||
Lightsource,
|
||||
LightGroup,
|
||||
Entertainment
|
||||
}
|
||||
|
||||
#region Properties
|
||||
public String Name { get {
|
||||
return this._name;
|
||||
} set {
|
||||
if(this.SetGroupAttribute(new Dictionary<String, Object>() { { "name", value } })) {
|
||||
this._name = value;
|
||||
this.NotifyClient<GroupUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
public Int32 GroupId { get; }
|
||||
public Types Groupclass { get; }
|
||||
public Boolean StateAnyOn { get; protected set; }
|
||||
public Boolean StateAllOn { get; protected set; }
|
||||
public ReadOnlyDictionary<Int32, ALight> Lights { get; protected set; }
|
||||
public GroupClass Class {
|
||||
get {
|
||||
return this._class;
|
||||
}
|
||||
set {
|
||||
if(this.SetGroupAttribute(new Dictionary<String, Object>() { { "class", Helper.GetEnumDescription(value) } })) {
|
||||
this._class = value;
|
||||
this.NotifyClient<GroupUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
protected AGroup(JsonData json, Tuple<Int32, Types> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) : base(http, polling) {
|
||||
this.GroupId = id.Item1;
|
||||
this.Groupclass = id.Item2;
|
||||
this.ComplexInit(json, lights);
|
||||
}
|
||||
|
||||
private void ComplexInit(JsonData json, ReadOnlyDictionary<Int32, ALight> lights) {
|
||||
if (json.ContainsKey("lights") && json["lights"].IsArray) {
|
||||
Dictionary<Int32, ALight> lightdict = new Dictionary<Int32, ALight>();
|
||||
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<Int32, ALight>(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<Int32> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) {
|
||||
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.Groups." + type;
|
||||
return GetInstanceConcrete(name, json, new Tuple<Int32, Types>(id.Item1, (Types)Enum.Parse(typeof(Types), type)), http, polling, lights);
|
||||
}
|
||||
if (!Enum.IsDefined(typeof(TypesIgnore), type) && !Enum.IsDefined(typeof(Types), type)) {
|
||||
Helper.WriteError("Lightclass " + type + " not exist.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static AGroup GetInstanceConcrete(String name, JsonData json, Tuple<Int32, Types> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) {
|
||||
Type t = null;
|
||||
try {
|
||||
t = Type.GetType(name, true);
|
||||
} catch (TypeLoadException) {
|
||||
Helper.WriteError("Konnte Type " + name + " nicht laden!");
|
||||
return null;
|
||||
}
|
||||
return (AGroup)t.GetConstructor(new Type[] { typeof(JsonData), typeof(Tuple<Int32, Types>), typeof(HttpConnection), typeof(Boolean), typeof(ReadOnlyDictionary<Int32, ALight>) }).Invoke(new Object[] { json, id, http, polling, lights });
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected Boolean SetGroupAttribute(Dictionary<String, Object> value) {
|
||||
return this.PutDictionary("groups/" + this.GroupId, value);
|
||||
}
|
||||
|
||||
protected Boolean SetGroupAction(Dictionary<String, Object> 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<GroupUpdateEvent>(this.Name);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("state") && json["state"].ContainsKey("any_on") && json["state"]["any_on"].IsBoolean) {
|
||||
if (this.StateAnyOn != (Boolean)json["state"]["any_on"]) {
|
||||
this.StateAnyOn = (Boolean)json["state"]["any_on"];
|
||||
this.NotifyClient<GroupUpdateEvent>(this.StateAnyOn);
|
||||
}
|
||||
}
|
||||
if (json.ContainsKey("state") && json["state"].ContainsKey("all_on") && json["state"]["all_on"].IsBoolean) {
|
||||
if (this.StateAllOn != (Boolean)json["state"]["all_on"]) {
|
||||
this.StateAllOn = (Boolean)json["state"]["all_on"];
|
||||
this.NotifyClient<GroupUpdateEvent>(this.StateAllOn);
|
||||
}
|
||||
}
|
||||
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<GroupUpdateEvent>(this.Class);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IMqtt
|
||||
public String ToJson() {
|
||||
return JsonMapper.ToJson(this.ToDictionary());
|
||||
}
|
||||
|
||||
public String MqttTopic() {
|
||||
return "group/" + this.GroupId;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
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; }
|
||||
private String _name;
|
||||
public String Name {
|
||||
get {
|
||||
return this._name;
|
||||
}
|
||||
set {
|
||||
if (this.SetLightAttribute(new Dictionary<String, Object>() { { "name", value } })) {
|
||||
this._name = value;
|
||||
this.NotifyClient<LightUpdateEvent>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
protected ALight(JsonData json, Tuple<Int32, Types> id, HttpConnection http, Boolean polling): base(http, polling) {
|
||||
this.LightId = id.Item1;
|
||||
this.Lightclass = id.Item2;
|
||||
this.ComplexInit(json);
|
||||
}
|
||||
private void ComplexInit(JsonData json) {
|
||||
if (json.ContainsKey("name")) {
|
||||
this._name = json["name"].ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public static ALight CreateInstanceConcrete(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
|
||||
|
||||
protected Boolean SetLightStateAttribute(Dictionary<String, Object> value) {
|
||||
return this.PutDictionary("lights/" + this.LightId + "/state", value);
|
||||
}
|
||||
protected Boolean SetLightAttribute(Dictionary<String, Object> value) {
|
||||
return this.PutDictionary("lights/" + this.LightId, value);
|
||||
}
|
||||
|
||||
#region IMqtt
|
||||
public String MqttTopic() {
|
||||
return "light/" + this.LightId;
|
||||
}
|
||||
|
||||
public String ToJson() {
|
||||
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<LightUpdateEvent>(this.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using BlubbFish.IoT.Hue.Devices.Scenes;
|
||||
using BlubbFish.IoT.Hue.lib;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.Interfaces {
|
||||
public abstract class AScene : AConnector, IMqtt {
|
||||
#region Properties
|
||||
public String SceneId { get; }
|
||||
public Int32 LightStateId { get; }
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
protected AScene(JsonData json, Tuple<String, Int32> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) : base(http, polling) {
|
||||
this.SceneId = id.Item1;
|
||||
this.LightStateId = id.Item2;
|
||||
}
|
||||
|
||||
public static AScene CreateInstanceConcrete(JsonData json, Tuple<String> id, HttpConnection http, Boolean polling, ReadOnlyDictionary<Int32, ALight> lights) {
|
||||
return new Scene(http.GetJson("scenes/" + id.Item1), id, http, polling, lights);
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected Boolean SetSceneAttribute(Dictionary<String, Object> dictionary) {
|
||||
return this.PutDictionary("scenes/" + this.SceneId, dictionary);
|
||||
}
|
||||
protected Boolean SetLightSceneAttribute(Dictionary<String, Object> value) {
|
||||
return this.PutDictionary("scenes/" + this.SceneId + "/lights/" + this.LightStateId + "/state", value);
|
||||
}
|
||||
|
||||
#region IMqtt
|
||||
public String ToJson() {
|
||||
return JsonMapper.ToJson(this.ToDictionary());
|
||||
}
|
||||
|
||||
public String MqttTopic() {
|
||||
return "scene/" + this.SceneId;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
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<String, Object>() { { "name", value } });
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
protected ASensor(JsonData json, Tuple<Int32, Types> 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<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.Sensors." + 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("Sensorclass " + type + " not exist.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static ASensor 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 (ASensor)t.GetConstructor(new Type[] { typeof(JsonData), typeof(Tuple<Int32, Types>), typeof(HttpConnection), typeof(Boolean) }).Invoke(new Object[] { json, id, http, polling });
|
||||
}
|
||||
#endregion
|
||||
|
||||
protected void SetSensorAttribute(Dictionary<String, Object> dictionary) {
|
||||
this.PutDictionary("sensors/" + this.SensorId, dictionary);
|
||||
}
|
||||
|
||||
protected Boolean SetSensorConfig(Dictionary<String, Object> 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<SensorUpdateEvent>(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<SensorUpdateEvent>(this.Lastupdated);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IMqtt
|
||||
public String MqttTopic() {
|
||||
return "sensor/" + this.SensorId;
|
||||
}
|
||||
|
||||
public String ToJson() {
|
||||
return JsonMapper.ToJson(this.ToDictionary());
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.Interfaces {
|
||||
public interface IMqtt {
|
||||
/// <summary>
|
||||
/// Gibt einen JSON-String der Eigenschaften eines Objekts zurück
|
||||
/// </summary>
|
||||
/// <returns>JSON-String</returns>
|
||||
String ToJson();
|
||||
/// <summary>
|
||||
/// Gibt das MQTT-Topic als String zurück unter dem die Daten des Objekts Publiziert werden sollen
|
||||
/// </summary>
|
||||
/// <returns>MQTT-Topic</returns>
|
||||
String MqttTopic();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||
// die einer Assembly zugeordnet sind.
|
||||
[assembly: AssemblyTitle("Hue")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Hue")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
||||
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||
[assembly: Guid("738a6137-8fca-4ce1-bd1c-042b21b6b5f2")]
|
||||
|
||||
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||
//
|
||||
// Hauptversion
|
||||
// Nebenversion
|
||||
// Buildnummer
|
||||
// Revision
|
||||
//
|
||||
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
||||
// indem Sie "*" wie unten gezeigt eingeben:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.lib {
|
||||
class Helper {
|
||||
internal static void WriteError(String text) {
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.Error.WriteLine("ERROR: " + text);
|
||||
Console.ResetColor();
|
||||
}
|
||||
public static String GetEnumDescription(Enum value) {
|
||||
FieldInfo fi = value.GetType().GetField(value.ToString());
|
||||
|
||||
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
|
||||
if (attributes != null && attributes.Length > 0) {
|
||||
return attributes[0].Description;
|
||||
} else {
|
||||
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<T>(ReadOnlyDictionary<Int32, T> 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<T>(ReadOnlyDictionary<String, T> 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) {
|
||||
if(text.Length == 0) {
|
||||
return text;
|
||||
}
|
||||
if(text.Length == 1) {
|
||||
return text.ToUpper();
|
||||
}
|
||||
text = text.Replace(" ", "");
|
||||
text = text.ToLower();
|
||||
return text[0].ToString().ToUpper() + text.Substring(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.IoT.Hue.lib {
|
||||
public class HttpConnection {
|
||||
private String server;
|
||||
private Object getLock = new Object();
|
||||
private enum RequestMethod {
|
||||
GET,
|
||||
POST,
|
||||
PUT
|
||||
}
|
||||
|
||||
public HttpConnection(String server) {
|
||||
this.server = "http://" + server + "/api/";
|
||||
this.Init(false);
|
||||
}
|
||||
|
||||
public HttpConnection(String server, String key) {
|
||||
this.server = "http://" + server + "/api/" + key + "/";
|
||||
this.Init(true);
|
||||
}
|
||||
|
||||
private void Init(Boolean withuser = true) {
|
||||
this.RequestString("config", "", false);
|
||||
if(withuser) {
|
||||
String i = this.RequestString("", "", false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal JsonData PostJson(String address, String json) {
|
||||
return this.ToJson(this.RequestString(address, json, true, RequestMethod.POST));
|
||||
}
|
||||
|
||||
internal JsonData PutJson(String address, String json) {
|
||||
return this.ToJson(this.RequestString(address, json, true, RequestMethod.PUT));
|
||||
}
|
||||
|
||||
internal JsonData GetJson(String address) {
|
||||
return this.ToJson(this.RequestString(address));
|
||||
}
|
||||
|
||||
private JsonData ToJson(String text) {
|
||||
if (text == null) {
|
||||
return new JsonData();
|
||||
}
|
||||
try {
|
||||
return JsonMapper.ToObject(text);
|
||||
} catch (Exception) {
|
||||
return new JsonData();
|
||||
}
|
||||
}
|
||||
|
||||
private String RequestString(String address, String json = "", Boolean withoutput = true, RequestMethod method = RequestMethod.GET) {
|
||||
String ret = null;
|
||||
lock (this.getLock) {
|
||||
HttpWebRequest request = WebRequest.CreateHttp(this.server + address);
|
||||
request.Timeout = 5000;
|
||||
if(method == RequestMethod.POST || method == RequestMethod.PUT) {
|
||||
Byte[] requestdata = Encoding.ASCII.GetBytes(json);
|
||||
request.ContentLength = requestdata.Length;
|
||||
request.Method = method.ToString();
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
using (Stream stream = request.GetRequestStream()) {
|
||||
stream.Write(requestdata, 0, requestdata.Length);
|
||||
}
|
||||
}
|
||||
try {
|
||||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized) {
|
||||
Console.Error.WriteLine("Benutzer oder Passwort falsch!");
|
||||
throw new Exceptions.ConnectionException("Benutzer oder Passwort falsch!");
|
||||
}
|
||||
if (withoutput) {
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||
ret = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Helper.WriteError("Konnte keine Verbindung zum Razzbery Server herstellen. Resource: \"" + this.server + address + "\" Fehler: " + e.Message);
|
||||
return null;
|
||||
//throw new Exceptions.ConnectionException("Konnte keine Verbindung zum Razzbery Server herstellen: " + e.Message);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user