Zway/Zway/Devices/CommandClasses/SensorBinary.cs

61 lines
2.3 KiB
C#

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 {
/// <summary>
/// 48 = SensorBinary
/// </summary>
public class Sensorbinary : ACommandClass {
public override event UpdatedValue Update;
public Sensorbinary(JsonData json, Tuple<Int32, Int32, Classes> id, HttpConnection http, Boolean polling) : base(json, id, http, polling) {
this.HasSub = true;
this.InitComplex(json);
foreach (KeyValuePair<Int32, ACommandClass> 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<Int32, ACommandClass> subs = new Dictionary<Int32, ACommandClass>();
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<Int32, Int32, Classes, Int32>(this.DeviceId, this.Instance, this.Commandclass, subid), this.http, this.Polling));
}
}
this.Sub = new ReadOnlyDictionary<Int32, ACommandClass>(subs);
}
}
internal override void Poll() => this.PollSubGlobal();
public override Dictionary<String, Object> ToDictionary() => this.ToDictionarySub();
}
}