diff --git a/Zway/Devices/CommandClasses/CommandClassSubs/SensorBinarySub.cs b/Zway/Devices/CommandClasses/CommandClassSubs/SensorBinarySub.cs new file mode 100644 index 0000000..99c2f9f --- /dev/null +++ b/Zway/Devices/CommandClasses/CommandClassSubs/SensorBinarySub.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +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.CommandClassSubs { + public class Sensorbinarysub : ACommandClass { + public override event UpdatedValue Update; + + public String Type { get; private set; } + public Boolean Level { get; private set; } + public Sensorbinarysub(JsonData json, Tuple id, HttpConnection http, Boolean polling) : base(json, id, http, polling) { + this.HasReset = true; + this.IsSub = true; + InitComplex(json); + } + + private void InitComplex(JsonData json) { + if (json.Keys.Contains("sensorTypeString") && + json["sensorTypeString"].Keys.Contains("value") && + json.Keys.Contains("level") && + json["level"].Keys.Contains("value")) { + this.Type = json["sensorTypeString"]["value"].ToString(); + this.Level = (Boolean)json["level"]["value"]; + } + } + + internal override void SetUpdate(JsonData json, Match match) { + if (json.Keys.Contains("level") && json["level"].Keys.Contains("value") && + json.Keys.Contains("sensorTypeString") && json["sensorTypeString"].Keys.Contains("value") && + this.CheckSetUpdateTime(json)) { + this.Level = (Boolean)json["level"]["value"]; + this.Type = json["sensorTypeString"]["value"].ToString(); + this.Update?.Invoke(this, new DeviceUpdateEvent(new Tuple(this.Type, this.Level), this.LastUpdate, this)); + } + } + + public override String ToString() { + return "SensorBinary " + this.Name + " [" + this.Id + "]: " + this.Type + " " + this.Level.ToString(); + } + + internal override void Poll() => this.PollNone(); + + public override Dictionary ToDictionary() { + return new Dictionary { + { "Level", this.Level }, + { "Type", this.Type } + }; + } + } +} \ No newline at end of file diff --git a/Zway/Devices/CommandClasses/SensorBinary.cs b/Zway/Devices/CommandClasses/SensorBinary.cs new file mode 100644 index 0000000..9e59265 --- /dev/null +++ b/Zway/Devices/CommandClasses/SensorBinary.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text.RegularExpressions; +using BlubbFish.IoT.Zway.Devices.CommandClasses.CommandClassSubs; +using BlubbFish.IoT.Zway.Events; +using BlubbFish.IoT.Zway.Interfaces; +using BlubbFish.IoT.Zway.lib; +using LitJson; + +namespace BlubbFish.IoT.Zway.Devices.CommandClasses { + /// + /// 48 = SensorBinary + /// + public class Sensorbinary : ACommandClass { + public override event UpdatedValue Update; + + public Sensorbinary(JsonData json, Tuple id, HttpConnection http, Boolean polling) : base(json, id, http, polling) { + this.HasSub = true; + this.InitComplex(json); + foreach (KeyValuePair item in this.Sub) { + item.Value.Update += this.DeviceUpdate; + } + } + + private void DeviceUpdate(Object sender, DeviceUpdateEvent e) { + this.Update?.Invoke(this, e); + } + + internal override void SetUpdate(JsonData json, Match match) { + if (match.Groups[4].Value.StartsWith(".data.")) { + Int32 subid = Int32.Parse(match.Groups[5].Value); + if (this.Sub.ContainsKey(subid)) { + this.Sub[subid].SetUpdate(json, match); + } + } 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"]; + Dictionary subs = new Dictionary(); + foreach (String item in data.Keys) { + if (Int32.TryParse(item, out Int32 subid) && + data[item].Keys.Contains("sensorTypeString") && + data[item].Keys.Contains("level")) { + subs.Add(subid, new Sensorbinarysub(data[item], new Tuple(this.DeviceId, this.Instance, this.Commandclass, subid), this.http, this.Polling)); + } + } + this.Sub = new ReadOnlyDictionary(subs); + } + } + + internal override void Poll() => this.PollSubGlobal(); + + public override Dictionary ToDictionary() => this.ToDictionarySub(); + } +}