169 lines
7.5 KiB
C#
169 lines
7.5 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>
|
|
/// 132 = Wakeup
|
|
/// </summary>
|
|
class Wakeup : ACommandClass {
|
|
public override event UpdatedValue Update;
|
|
|
|
#region Properties
|
|
private Int32 _interval;
|
|
public Int32 Interval {
|
|
get {
|
|
return this._interval;
|
|
}
|
|
set {
|
|
if(value >= this.WakeupMin && value <= this.WakeupMax) {
|
|
this.SetTuple(value, this.AgainstNode);
|
|
}
|
|
}
|
|
}
|
|
private Int32 _againstNode;
|
|
public Int32 AgainstNode { get {
|
|
return this._againstNode;
|
|
}
|
|
set {
|
|
this.SetTuple(this.Interval, value);
|
|
}
|
|
}
|
|
public Int32 WakeupMin { get; private set; }
|
|
public Int32 WakeupMax { get; private set; }
|
|
public Boolean HasMinMaxState { get; private set; }
|
|
public Int32 HasMinMaxLevel { get { return (this.HasMinMaxState) ? 1 : 0; } }
|
|
public Int32 WakeupDefault { get; private set; }
|
|
public Boolean HasDefaultState { get; private set; }
|
|
public Int32 HasDefaultLevel { get { return (this.HasDefaultState) ? 1 : 0; } }
|
|
public DateTime LastWakeup { get; private set; }
|
|
public DateTime LastSleep { get; private set; }
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public Wakeup(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")) {
|
|
JsonData data = json["data"];
|
|
if(data.Keys.Contains("interval") && data["interval"].Keys.Contains("value")) {
|
|
this._interval = Int32.Parse(data["interval"]["value"].ToString());
|
|
}
|
|
if (data.Keys.Contains("nodeId") && data["nodeId"].Keys.Contains("value")) {
|
|
this._againstNode = Int32.Parse(data["nodeId"]["value"].ToString());
|
|
}
|
|
this.HasMinMaxState = false;
|
|
if (data.Keys.Contains("min") && data["min"].Keys.Contains("value") && data["min"]["value"] != null) {
|
|
this.WakeupMin = Int32.Parse(data["min"]["value"].ToString());
|
|
this.HasMinMaxState = true;
|
|
}
|
|
if (data.Keys.Contains("max") && data["max"].Keys.Contains("value") && data["max"]["value"] != null) {
|
|
this.WakeupMax = Int32.Parse(data["max"]["value"].ToString());
|
|
this.HasMinMaxState = true;
|
|
}
|
|
this.HasDefaultState = false;
|
|
if (data.Keys.Contains("default") && data["default"].Keys.Contains("value") && data["default"]["value"] != null) {
|
|
this.WakeupDefault = Int32.Parse(data["default"]["value"].ToString());
|
|
this.HasDefaultState = true;
|
|
}
|
|
if (data.Keys.Contains("lastWakeup") && data["lastWakeup"].Keys.Contains("value") && data["lastWakeup"]["value"] != null) {
|
|
this.LastWakeup = DateTimeOffset.FromUnixTimeSeconds(Int64.Parse(data["lastWakeup"]["value"].ToString())).DateTime.ToLocalTime();
|
|
}
|
|
if (data.Keys.Contains("lastSleep") && data["lastSleep"].Keys.Contains("value") && data["lastSleep"]["value"] != null) {
|
|
this.LastSleep = DateTimeOffset.FromUnixTimeSeconds(Int64.Parse(data["lastSleep"]["value"].ToString())).DateTime.ToLocalTime();
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region ACommandClass
|
|
public override String ToString() {
|
|
return "Wakeup " + this.Name + " [" + this.Id + "]: " + this.LastWakeup + "-" + this.LastSleep + " " + this.Interval + " [" + this.WakeupMin + "," + this.WakeupMax + "," + this.WakeupDefault + "," + this.AgainstNode + "]";
|
|
}
|
|
|
|
public override Dictionary<String, Object> ToDictionary() {
|
|
return new Dictionary<String, Object> {
|
|
{ "Interval", this.Interval },
|
|
{ "AgainstNode", this.AgainstNode },
|
|
{ "WakeupMin", this.WakeupMin },
|
|
{ "WakeupMax", this.WakeupMax },
|
|
{ "HasMinMaxState", this.HasMinMaxState },
|
|
{ "HasMinMaxLevel", this.HasMinMaxLevel },
|
|
{ "WakeupDefault", this.WakeupDefault },
|
|
{ "HasDefaultState", this.HasDefaultState },
|
|
{ "HasDefaultLevel", this.HasDefaultLevel },
|
|
{ "LastWakeup", this.LastWakeup.ToString() },
|
|
{ "LastSleep", this.LastSleep.ToString() }
|
|
};
|
|
}
|
|
|
|
internal override void SetUpdate(JsonData json, Match match) {
|
|
Boolean success = false;
|
|
if (match.Groups[4].Value == ".data.lastWakeup") {
|
|
if (json.Keys.Contains("value") && (json["value"].IsInt || json["value"].IsLong)) {
|
|
this.LastWakeup = DateTimeOffset.FromUnixTimeSeconds(Int64.Parse(json["value"].ToString())).DateTime.ToLocalTime();
|
|
success = true;
|
|
}
|
|
} else if (match.Groups[4].Value == ".data.lastSleep") {
|
|
if (json.Keys.Contains("value") && (json["value"].IsInt || json["value"].IsLong)) {
|
|
this.LastSleep = DateTimeOffset.FromUnixTimeSeconds(Int64.Parse(json["value"].ToString())).DateTime.ToLocalTime();
|
|
success = true;
|
|
}
|
|
} else if (match.Groups[4].Value == ".data") {
|
|
if (json.Keys.Contains("interval") && json["interval"].Keys.Contains("value")) {
|
|
this._interval = Int32.Parse(json["interval"]["value"].ToString());
|
|
success = true;
|
|
}
|
|
if (json.Keys.Contains("nodeId") && json["nodeId"].Keys.Contains("value")) {
|
|
this._againstNode = Int32.Parse(json["nodeId"]["value"].ToString());
|
|
success = true;
|
|
}
|
|
this.HasMinMaxState = false;
|
|
if (json.Keys.Contains("min") && json["min"].Keys.Contains("value") && json["min"]["value"] != null) {
|
|
this.WakeupMin = Int32.Parse(json["min"]["value"].ToString());
|
|
success = true;
|
|
this.HasMinMaxState = true;
|
|
}
|
|
if (json.Keys.Contains("max") && json["max"].Keys.Contains("value") && json["max"]["value"] != null) {
|
|
this.WakeupMax = Int32.Parse(json["max"]["value"].ToString());
|
|
success = true;
|
|
this.HasMinMaxState = true;
|
|
}
|
|
this.HasDefaultState = false;
|
|
if (json.Keys.Contains("default") && json["default"].Keys.Contains("value") && json["default"]["value"] != null) {
|
|
this.WakeupDefault = Int32.Parse(json["default"]["value"].ToString());
|
|
success = true;
|
|
this.HasDefaultState = true;
|
|
}
|
|
if (json.Keys.Contains("lastWakeup") && json["lastWakeup"].Keys.Contains("value") && json["lastWakeup"]["value"] != null) {
|
|
this.LastWakeup = DateTimeOffset.FromUnixTimeSeconds(Int64.Parse(json["lastWakeup"]["value"].ToString())).DateTime.ToLocalTime();
|
|
success = true;
|
|
}
|
|
if (json.Keys.Contains("lastSleep") && json["lastSleep"].Keys.Contains("value") && json["lastSleep"]["value"] != null) {
|
|
this.LastSleep = DateTimeOffset.FromUnixTimeSeconds(Int64.Parse(json["lastSleep"]["value"].ToString())).DateTime.ToLocalTime();
|
|
success = true;
|
|
}
|
|
} else {
|
|
Helper.WriteError("Kenne in " + this.Name + " [" + this.Id + "] " + match.Groups[4].Value + " nicht!");
|
|
}
|
|
if (success && this.CheckSetUpdateTime(json)) {
|
|
this.Update?.Invoke(this, new DeviceUpdateEvent(0, this.LastUpdate, this));
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region ISenml
|
|
protected override List<Senml> ToSenmlList() {
|
|
return new List<Senml>() { };
|
|
}
|
|
#endregion
|
|
}
|
|
}
|