89 lines
3.3 KiB
C#
89 lines
3.3 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 {
|
|
public class Metersub : ACommandClass {
|
|
public override event UpdatedValue Update;
|
|
|
|
#region Properties
|
|
/// <summary>
|
|
/// Type of the Sensor
|
|
/// </summary>
|
|
public String Type { get; private set; }
|
|
/// <summary>
|
|
/// Actual value of the Sensor
|
|
/// </summary>
|
|
public Double Level { get; private set; }
|
|
/// <summary>
|
|
/// Scale of the Sensor
|
|
/// </summary>
|
|
public String Scale { get; private set; }
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public Metersub(JsonData json, Tuple<Int32, Int32, Classes, Int32> id, HttpConnection http, Boolean polling) : base(json, id, http, polling) {
|
|
this.HasReset = true;
|
|
this.IsSub = true;
|
|
InitComplex(json);
|
|
}
|
|
|
|
private void InitComplex(JsonData json) {
|
|
if (json.Keys.Contains("sensorTypeString") &&
|
|
json["sensorTypeString"].Keys.Contains("value") &&
|
|
json.Keys.Contains("val") &&
|
|
json["val"].Keys.Contains("value") &&
|
|
json.Keys.Contains("scaleString") &&
|
|
json["scaleString"].Keys.Contains("value")) {
|
|
this.Type = json["sensorTypeString"]["value"].ToString();
|
|
this.Level = Double.Parse(json["val"]["value"].ToString());
|
|
this.Scale = json["scaleString"]["value"].ToString();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region ACommandClass
|
|
public override String ToString() {
|
|
return "Meter " + this.Name + " [" + this.Id + "]: " + this.Type + " " + this.Level + "" + this.Scale;
|
|
}
|
|
|
|
public override Dictionary<String, Object> ToDictionary() {
|
|
return new Dictionary<String, Object> {
|
|
{ "Level", this.Level },
|
|
{ "Type", this.Type },
|
|
{ "Scale", this.Scale },
|
|
};
|
|
}
|
|
|
|
internal override void SetUpdate(JsonData json, Match match) {
|
|
if (json.Keys.Contains("val") && json["val"].Keys.Contains("value") &&
|
|
json.Keys.Contains("sensorTypeString") && json["sensorTypeString"].Keys.Contains("value") &&
|
|
json.Keys.Contains("scaleString") && json["scaleString"].Keys.Contains("value") &&
|
|
this.CheckSetUpdateTime(json)) {
|
|
this.Level = Double.Parse(json["val"]["value"].ToString());
|
|
this.Type = json["sensorTypeString"]["value"].ToString();
|
|
this.Scale = json["scaleString"]["value"].ToString();
|
|
this.Update?.Invoke(this, new DeviceUpdateEvent(new Tuple<String, String, Double>(this.Type, this.Scale, this.Level), this.LastUpdate, this));
|
|
}
|
|
}
|
|
|
|
internal override void Poll() => this.PollNone();
|
|
#endregion
|
|
|
|
#region ISenml
|
|
protected override List<Senml> ToSenmlList() {
|
|
if (this.Scale == "W") {
|
|
return new List<Senml>() { new SenmlDouble("power", Senml.Units.Watt, this.Level) };
|
|
} else if(this.Scale == "kWh") {
|
|
return new List<Senml>() { new SenmlDouble("consumption", Senml.Units.Joule, this.Level * 1000 * 3.6 * 1000) };
|
|
}
|
|
return new List<Senml>() {};
|
|
}
|
|
#endregion
|
|
}
|
|
} |