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

86 lines
2.6 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>
/// 37 = SwitchBinary
/// </summary>
public class Switchbinary : ACommandClass {
public override event UpdatedValue Update;
#region Properties
/// <summary>
/// Int representation of Boolean
/// </summary>
public Int32 Level {
get {
return (this.State) ? 1 : 0;
}
set {
this.State = (value == 1);
}
}
private Boolean _state;
/// <summary>
/// State if Switch is activated
/// </summary>
public Boolean State {
get {
return this._state;
}
set {
this.SetInt(value ? 255 : 0);
}
}
#endregion
#region Constructor
public Switchbinary(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") && json["data"]["level"]["value"].IsBoolean) {
this._state = (Boolean)json["data"]["level"]["value"];
}
}
#endregion
#region ACommandClass
public override String ToString() {
return "SwitchBinary " + this.Name + " [" + this.Id + "]: " + this.State;
}
public override Dictionary<String, Object> ToDictionary() {
return new Dictionary<String, Object> {
{ "State", this.State },
{ "Level", this.Level }
};
}
internal override void SetUpdate(JsonData json, Match match) {
if(match.Groups[4].Value == ".data.level") {
if (json.Keys.Contains("value") && json["value"].IsBoolean && this.CheckSetUpdateTime(json)) {
this._state = (Boolean)json["value"];
this.Update?.Invoke(this, new DeviceUpdateEvent(this.State, 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 SenmlBool("state", Senml.Units.CounterValue, this.State) };
}
#endregion
}
}