Zway/Zway/Devices/CommandClasses/CentralScene.cs

77 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using BlubbFish.IoT.Zway.Events;
using BlubbFish.IoT.Zway.lib;
using LitJson;
namespace BlubbFish.IoT.Zway.Devices.CommandClasses {
public class Centralscene : CommandClass {
public override event UpdatedValue Update;
public ReadOnlyDictionary<Int32, ReadOnlyCollection<Int32>> ValidScenesModes { get; private set; }
public Int32 Scene { get; private set; }
public Int32 Key { get; private set; }
public Centralscene(JsonData json, HttpConnection http, Tuple<Int32, Int32, Int32> id) : base(json, http, id) {
this.ValidScenesModes = new ReadOnlyDictionary<Int32, ReadOnlyCollection<Int32>>(new Dictionary<Int32, ReadOnlyCollection<Int32>>());
this.InitComplex(json);
}
internal override void SetUpdate(JsonData json, Match match) {
if (match.Groups[4].Value == ".data.currentScene") {
if (json.Keys.Contains("value")) {
this.Scene = Int32.Parse(json["value"].ToString());
}
} else if (match.Groups[4].Value == ".data.keyAttribute") {
if (json.Keys.Contains("value") && this.CheckSetUpdateTime(json)) {
this.Key = Int32.Parse(json["value"].ToString());
this.Update?.Invoke(this, new DeviceUpdateEvent(new Tuple<Int32, Int32>(this.Scene, this.Key), this.LastUpdate));
}
} else {
throw new NotImplementedException();
}
}
protected override void InitComplex(JsonData json) {
if (json.Keys.Contains("data")) {
JsonData data = json["data"];
if(data.Keys.Contains("sceneSupportedKeyAttributesMask")) {
Dictionary<Int32, ReadOnlyCollection<Int32>> scenes = new Dictionary<Int32, ReadOnlyCollection<Int32>>();
foreach (String item in data["sceneSupportedKeyAttributesMask"].Keys) {
if (Int32.TryParse(item, out Int32 mode) &&
data["sceneSupportedKeyAttributesMask"][item].Keys.Contains("value") &&
data["sceneSupportedKeyAttributesMask"][item]["value"].IsArray) {
JsonData values = data["sceneSupportedKeyAttributesMask"][item]["value"];
List<Int32> modes = new List<Int32>();
foreach (JsonData value in values) {
modes.Add(Int32.Parse(value.ToString()));
}
scenes.Add(mode, new ReadOnlyCollection<Int32>(modes));
}
}
this.ValidScenesModes = new ReadOnlyDictionary<Int32, ReadOnlyCollection<Int32>>(scenes);
if (data.Keys.Contains("currentScene") &&
data["currentScene"].Keys.Contains("value") &&
data["currentScene"]["value"] != null) {
this.Scene = Int32.Parse(data["currentScene"]["value"].ToString());
}
if (data.Keys.Contains("keyAttribute") &&
data["keyAttribute"].Keys.Contains("value") &&
data["keyAttribute"]["value"] != null) {
this.Key = Int32.Parse(data["keyAttribute"]["value"].ToString());
}
}
}
}
public override String ToString() {
return "CentralScene " + this.Name + ": "+this.Scene+"-"+this.Key;
}
internal override void Poll() {
}
}
}