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() { { "name", value } })) { this._name = value; this.NotifyClient(value); } } } public String Owner { get; private set; } public Boolean Recycle { get; private set; } public Boolean Locked { get; private set; } public String Picture { get; private set; } public DateTime Lastupdated { get; private set; } public Int32 Version { get; private set; } public ReadOnlyDictionary Lights { get; private set; } public ReadOnlyDictionary LightStates { get; private set; } #endregion #region Constructor public Scene(JsonData json, Tuple id, HttpConnection http, Boolean polling, ReadOnlyDictionary lights) : base(json, new Tuple(id.Item1, 0), http, polling, lights) { this.ComplexInit(json, lights); } private void ComplexInit(JsonData json, ReadOnlyDictionary lights) { if (json.ContainsKey("name") && json["name"].IsString) { this._name = json["name"].ToString(); } if (json.ContainsKey("owner") && json["owner"].IsString) { this.Owner = json["owner"].ToString(); } if (json.ContainsKey("recycle") && json["recycle"].IsBoolean) { 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 lightdict = new Dictionary(); foreach (JsonData item in json["lights"]) { if (Int32.TryParse(item.ToString(), out Int32 lampid)) { if (lights.ContainsKey(lampid)) { lightdict.Add(lampid, lights[lampid]); } } } this.Lights = new ReadOnlyDictionary(lightdict); } if (json.ContainsKey("lightstates") && json["lightstates"].IsObject) { Dictionary lightstates = new Dictionary(); foreach (String item in json["lightstates"].Keys) { LightState l = new LightState(json["lightstates"][item], new Tuple(this.SceneId, Int32.Parse(item)), this.http, this.Polling, lights); l.Update += this.NotifyClientChildren; lightstates.Add(Int32.Parse(item), l); } this.LightStates = new ReadOnlyDictionary(lightstates); } } #endregion #region AConnector public override String ToString() { List lid = new List(); foreach (KeyValuePair item in this.Lights) { lid.Add(item.Key.ToString()); } List lst = new List(); foreach (KeyValuePair 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(this.Name); } } if (json.ContainsKey("owner") && json["owner"].IsString) { if (this.Owner != json["owner"].ToString()) { this.Owner = json["owner"].ToString(); this.NotifyClient(this.Owner); } } if (json.ContainsKey("recycle") && json["recycle"].IsBoolean) { if (this.Recycle != (Boolean)json["recycle"]) { this.Recycle = (Boolean)json["recycle"]; this.NotifyClient(this.Recycle); } } if (json.ContainsKey("locked") && json["locked"].IsBoolean) { if (this.Locked != (Boolean)json["locked"]) { this.Locked = (Boolean)json["locked"]; this.NotifyClient(this.Locked); } } if (json.ContainsKey("picture") && json["picture"].IsString) { if (this.Picture != json["picture"].ToString()) { this.Picture = json["picture"].ToString(); this.NotifyClient(this.Picture); } } if (json.ContainsKey("lastupdated") && json["lastupdated"].IsString) { if (this.Lastupdated != DateTime.Parse(json["lastupdated"].ToString())) { this.Lastupdated = DateTime.Parse(json["lastupdated"].ToString()); this.NotifyClient(this.Lastupdated); } } if (json.ContainsKey("version") && json["version"].IsInt) { if (this.Version != (Int32)json["version"]) { this.Version = (Int32)json["version"]; this.NotifyClient(this.Version); } } if (json.ContainsKey("lightstates") && json["lightstates"].IsObject) { foreach (String item in json["lightstates"].Keys) { if(this.LightStates.ContainsKey(Int32.Parse(item))) { this.LightStates[Int32.Parse(item)].SetUpdate(json["lightstates"][Int32.Parse(item)]); } } } } #endregion } }