Zway/Zway/Devices/CommandClasses/CommandClassSubs/ThermostatSetPointSub.cs

128 lines
4.8 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.CommandClassSubs {
class Thermostatsetpointsub : ACommandClass {
public override event UpdatedValue Update;
#region Properties
private Double _level;
/// <summary>
/// Target-Temperatur
/// </summary>
public Double Level {
get {
return this._level;
}
set {
if (!this.HasMinMax || (this.HasMinMax && value >= this.TempMin && value <= this.TempMax)) {
this.SetTuple(this.SensorId, (Double)Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2);
}
}
}
/// <summary>
/// Scale of the actor
/// </summary>
public String Scale { get; private set; }
/// <summary>
/// Maximum Temperatur of the actor
/// </summary>
public Double TempMax { get; private set; }
/// <summary>
/// Minimum Temperatur of the actor
/// </summary>
public Double TempMin { get; private set; }
/// <summary>
/// Has Maximum and Minimum
/// </summary>
public Boolean HasMinMax { get; private set; }
/// <summary>
/// Type of the actor
/// </summary>
public String Type { get; private set; }
#endregion
#region Constructor
public Thermostatsetpointsub(JsonData json, Tuple<Int32, Int32, Classes, Int32> id, HttpConnection http, Boolean polling) : base(json, id, http, polling) {
this.IsSub = true;
InitComplex(json);
}
private void InitComplex(JsonData json) {
if (json.Keys.Contains("modeName") && json["modeName"].Keys.Contains("value") &&
json.Keys.Contains("val") && json["val"].Keys.Contains("value") &&
json.Keys.Contains("deviceScaleString") && json["deviceScaleString"].Keys.Contains("value")) {
this.Type = json["modeName"]["value"].ToString();
this._level = Double.Parse(json["val"]["value"].ToString());
this.Scale = json["deviceScaleString"]["value"].ToString();
}
if (json.Keys.Contains("min") && json["min"].Keys.Contains("value") &&
json.Keys.Contains("max") && json["max"].Keys.Contains("value")) {
this.TempMin = Double.Parse(json["min"]["value"].ToString());
this.TempMax = Double.Parse(json["max"]["value"].ToString());
this.HasMinMax = true;
} else {
this.HasMinMax = false;
}
}
#endregion
#region ACommandClass
public override String ToString() {
return "ThermostatSetPoint " + this.Name + " [" + this.Id + "]: " + this.Type + " " + this.Level + "" + this.Scale + " [" + this.TempMin + "," + this.TempMax + "," + this.HasMinMax + "]";
}
public override Dictionary<String, Object> ToDictionary() {
return new Dictionary<String, Object> {
{ "Level", this.Level },
{ "Type", this.Type },
{ "Scale", this.Scale },
{ "TempMax", this.TempMax },
{ "TempMin", this.TempMin },
{ "HasMinMax", this.HasMinMax },
};
}
internal override void SetUpdate(JsonData json, Match match) {
Boolean ret = false;
if (json.Keys.Contains("val") && json["val"].Keys.Contains("value") &&
json.Keys.Contains("deviceScaleString") && json["deviceScaleString"].Keys.Contains("value") &&
json.Keys.Contains("modeName") && json["modeName"].Keys.Contains("value")) {
this._level = Double.Parse(json["val"]["value"].ToString());
this.Scale = json["deviceScaleString"]["value"].ToString();
this.Type = json["modeName"]["value"].ToString();
ret = true;
}
if (json.Keys.Contains("min") && json["val"].Keys.Contains("value") &&
json.Keys.Contains("max") && json["max"].Keys.Contains("value")) {
this.TempMin = Double.Parse(json["min"]["value"].ToString());
this.TempMax = Double.Parse(json["max"]["value"].ToString());
this.HasMinMax = true;
ret = true;
} else {
this.HasMinMax = false;
}
if (ret && this.CheckSetUpdateTime(json)) {
this.Update?.Invoke(this, new DeviceUpdateEvent(new Tuple<String, String, Double, Double, Double, Boolean>(this.Type, this.Scale, this.Level, this.TempMin, this.TempMax, this.HasMinMax), this.LastUpdate, this));
}
}
internal override void Poll() => this.PollSub();
#endregion
#region ISenml
protected override List<Senml> ToSenmlList() {
if(this.Scale == "°C") {
return new List<Senml>() { new SenmlDouble("temperatur", Senml.Units.Celsius, this.Level) };
}
return new List<Senml>() { };
}
#endregion
}
}