Files
Zway/Zway/Devices/CommandClasses/SwitchMultilevel.cs
T

77 lines
2.4 KiB
C#

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 BlubbFish.Utils.IoT.Interfaces.Language;
using LitJson;
namespace BlubbFish.IoT.Zway.Devices.CommandClasses {
/// <summary>
/// 38 = SwitchMultilevel
/// </summary>
public class Switchmultilevel : ACommandClass {
public override event UpdatedValue Update;
#region Properties
private Int32 _level;
/// <summary>
/// Level of thw Switch
/// </summary>
public Int32 Level {
get {
return this._level;
}
set {
if(value == 0 || (value > 0 && value <= 99) || value == 255) {
this.SetTuple(value, 0);
}
}
}
#endregion
#region Constructor
public Switchmultilevel(JsonData json, Tuple<Int32, Int32, Classes> id, HttpConnection http, Boolean polling) : base(json, id, http, polling) {
this.InitComplex(json);
}
private void InitComplex(JsonData json) {
if (json.Keys.Contains("data") && json["data"].Keys.Contains("level") && json["data"]["level"].Keys.Contains("value")) {
this._level = Int32.Parse(json["data"]["level"]["value"].ToString());
}
}
#endregion
#region ACommandClass
public override String ToString() {
return "SwitchMultilevel " + this.Name + " [" + this.Id + "]: " + this.Level;
}
public override Dictionary<String, Object> ToDictionary() {
return new Dictionary<String, Object> {
{ "Level", this.Level }
};
}
internal override void SetUpdate(JsonData json, Match match) {
if(match.Groups[4].Value == ".data.level") {
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 if (match.Groups[4].Value == ".data.prevLevel") {
} 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("state", Senml.Units.Percent, (Double)this.Level/255) };
}
#endregion
}
}