Zway/Zway/Devices/CommandClasses/SensorMultilevel.cs

72 lines
2.6 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>
/// 49 = SensorMultilevel
/// </summary>
public class Sensormultilevel : CommandClass {
public override event UpdatedValue Update;
public ReadOnlyDictionary<Int32, CommandClassSub> Sub { get; private set; }
public Sensormultilevel(JsonData json, HttpConnection http, Tuple<Int32, Int32, Int32> id, Boolean polling) : base(json, http, id, polling) {
this.InitComplex(json);
foreach (KeyValuePair<Int32, CommandClassSub> 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, CommandClassSub> subs = new Dictionary<Int32, CommandClassSub>();
foreach (String item in data.Keys) {
if (Int32.TryParse(item, out Int32 subid) &&
data[item].Keys.Contains("sensorTypeString") &&
data[item].Keys.Contains("val") &&
data[item].Keys.Contains("scaleString")) {
subs.Add(subid, new Sensormultilevelsub(data[item], new Tuple<Int32, Int32, Int32, Int32>(this.DeviceId, this.Instance, this.Commandclass, subid), this.http, this.Polling));
}
}
this.Sub = new ReadOnlyDictionary<Int32, CommandClassSub>(subs);
}
}
internal override void Poll() {
Boolean poll = false;
foreach (KeyValuePair<Int32, CommandClassSub> item in this.Sub) {
if (item.Value.Polling) {
poll = true;
}
}
if (poll) {
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + this.Commandclass + "].Get()");
}
}
}
}