ProjectsOld/Hue/Hue/Devices/Scenes/Scene.cs
2018-05-02 18:33:27 +02:00

151 lines
6.0 KiB
C#

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
}
}