Zway/Zway/Devices/CommandClasses/CentralScene.cs
BlubbFish 0834e09178 [BF] v1.3.4.0 Fixing Json Names
[NF] v1.2.2.0 Mqtt can now get and set values
2017-12-19 20:18:30 +00:00

87 lines
3.7 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.Interfaces;
using BlubbFish.IoT.Zway.lib;
using LitJson;
namespace BlubbFish.IoT.Zway.Devices.CommandClasses {
/// <summary>
/// 91 = CentralScene
/// </summary>
public class Centralscene : ACommandClass {
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, Tuple<Int32, Int32, Classes> id, HttpConnection http, Boolean polling) : base(json, id, http, polling) {
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, this));
}
} else {
Helper.WriteError("Kenne in " + this.Name + " [" + this.Id + "] " + match.Groups[4].Value + " nicht!");
}
}
private 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.Id + "]: " + this.Scene+"-"+this.Key;
}
internal override void Poll() => this.PollNone();
public override Dictionary<String, Object> ToDictionary() {
return new Dictionary<String, Object> {
{ "Scene", this.Scene },
{ "Key", this.Key },
};
}
}
}