99 lines
3.4 KiB
C#
99 lines
3.4 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 BlubbFish.Utils.IoT.Interfaces.Language;
|
|
using LitJson;
|
|
|
|
namespace BlubbFish.IoT.Zway.Devices.CommandClasses {
|
|
/// <summary>
|
|
/// 64 = ThermostatMode
|
|
/// </summary>
|
|
public class Thermostatmode : ACommandClass {
|
|
public override event UpdatedValue Update;
|
|
|
|
#region Properties
|
|
/// <summary>
|
|
/// Allowd Modes
|
|
/// </summary>
|
|
public ReadOnlyDictionary<Int32, String> ValidModes { get; private set; }
|
|
private Int32 _level;
|
|
/// <summary>
|
|
/// Actual mode
|
|
/// </summary>
|
|
public Int32 Level {
|
|
get { return this._level; }
|
|
set {
|
|
if (this.ValidModes.ContainsKey(value)) {
|
|
this.SetInt(value);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public Thermostatmode(JsonData json, Tuple<Int32, Int32, Classes> id, HttpConnection http, Boolean polling) : base(json, id, http, polling) {
|
|
this.ValidModes = new ReadOnlyDictionary<Int32, String>(new Dictionary<Int32, String>());
|
|
this.InitComplex(json);
|
|
}
|
|
|
|
private void InitComplex(JsonData json) {
|
|
if (json.Keys.Contains("data")) {
|
|
JsonData data = json["data"];
|
|
Dictionary<Int32, String> modes = new Dictionary<Int32, String>();
|
|
foreach (String item in data.Keys) {
|
|
if (Int32.TryParse(item, out Int32 mode) &&
|
|
data[item].Keys.Contains("modeName") &&
|
|
data[item]["modeName"].Keys.Contains("value")) {
|
|
modes.Add(mode, data[item]["modeName"]["value"].ToString());
|
|
}
|
|
}
|
|
this.ValidModes = new ReadOnlyDictionary<Int32, String>(modes);
|
|
if (data.Keys.Contains("mode") &&
|
|
data["mode"].Keys.Contains("value")) {
|
|
this._level = Int32.Parse(data["mode"]["value"].ToString());
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region ACommandClass
|
|
public override String ToString() {
|
|
return "ThermostatMode " + this.Name + " [" + this.Id + "]: " + this.ValidModes[this.Level];
|
|
}
|
|
|
|
public override Dictionary<String, Object> ToDictionary() {
|
|
Dictionary<String, String> modes = new Dictionary<String, String>();
|
|
foreach (KeyValuePair<Int32, String> item in this.ValidModes) {
|
|
modes.Add(item.Key.ToString(), item.Value);
|
|
}
|
|
Dictionary<String, Object> json = new Dictionary<String, Object> {
|
|
{ "Level", this.Level },
|
|
{ "ValidModes", modes }
|
|
};
|
|
return json;
|
|
}
|
|
|
|
internal override void SetUpdate(JsonData json, Match match) {
|
|
if(match.Groups[4].Value == ".data.mode") {
|
|
if(json.Keys.Contains("value") && this.CheckSetUpdateTime(json)) {
|
|
this._level = Int32.Parse(json["value"].ToString());
|
|
this.Update?.Invoke(this, new DeviceUpdateEvent(this.Level, this.LastUpdate, this));
|
|
}
|
|
} else {
|
|
Helper.WriteError("Kenne in " + this.Name + " [" + this.Id + "] " + match.Groups[4].Value + " nicht!");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region ISenml
|
|
protected override List<Senml> ToSenmlList() {
|
|
return new List<Senml>() { new SenmlDouble("mode", Senml.Units.CounterValue, this.Level) };
|
|
}
|
|
#endregion
|
|
}
|
|
}
|