163 lines
5.6 KiB
C#
163 lines
5.6 KiB
C#
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 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, Sensor> item in this.Sensors) {
|
|
item.Value.Update += this.AllUpdate;
|
|
}
|
|
this.Config.Update += this.AllUpdate;*/
|
|
}
|
|
|
|
private void Poll() {
|
|
//throw new NotImplementedException();
|
|
}
|
|
|
|
private void AllUpdate(Object sender, AllUpdateEvent e) {
|
|
this.Update?.Invoke(sender, e);
|
|
}
|
|
|
|
private void Updater() {
|
|
//throw new NotImplementedException();
|
|
}
|
|
|
|
private void CreateAll(JsonData json) {
|
|
if(json.ContainsKey("lights")) {
|
|
this.CreateLights(json["lights"]);
|
|
}
|
|
if(json.ContainsKey("groups")) {
|
|
this.CreateGroups(json["groups"]);
|
|
}
|
|
if(json.ContainsKey("scenes")) {
|
|
this.CreateScenes(json["scenes"]);
|
|
}
|
|
}
|
|
|
|
private void CreateScenes(JsonData json) {
|
|
Dictionary<String, AScene> scenes = new Dictionary<String, AScene>();
|
|
foreach (String sceneid in json.Keys) {
|
|
AScene s = AScene.CreateScene(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 CreateGroups(JsonData json) {
|
|
Dictionary<Int32, AGroup> groups = new Dictionary<Int32, AGroup>();
|
|
foreach (String groupid in json.Keys) {
|
|
AGroup g = AGroup.CreateGroup(json[groupid], new Tuple<Int32>(Int32.Parse(groupid)), this.http, this.polling, this.Lights);
|
|
if (g != null) {
|
|
groups.Add(Int32.Parse(groupid), g);
|
|
}
|
|
}
|
|
this.Groups = new ReadOnlyDictionary<Int32, AGroup>(groups);
|
|
}
|
|
|
|
private void CreateLights(JsonData json) {
|
|
Dictionary<Int32, ALight> lights = new Dictionary<Int32, ALight>();
|
|
foreach (String lightid in json.Keys) {
|
|
ALight l = ALight.CreateLight(json[lightid], new Tuple<Int32>(Int32.Parse(lightid)), this.http, this.polling);
|
|
if(l != null) {
|
|
lights.Add(Int32.Parse(lightid), l);
|
|
}
|
|
}
|
|
this.Lights = new ReadOnlyDictionary<Int32, ALight>(lights);
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|