287 lines
10 KiB
C#
287 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Text.RegularExpressions;
|
|
using BlubbFish.IoT.Zway.Events;
|
|
using BlubbFish.IoT.Zway.lib;
|
|
using BlubbFish.Utils.IoT.Interfaces;
|
|
using BlubbFish.Utils.IoT.Interfaces.Language;
|
|
using LitJson;
|
|
|
|
namespace BlubbFish.IoT.Zway.Interfaces {
|
|
public abstract class ACommandClass : IMqtt, ISenml {
|
|
protected HttpConnection http;
|
|
|
|
public delegate void UpdatedValue(Object sender, DeviceUpdateEvent e);
|
|
public abstract event UpdatedValue Update;
|
|
protected enum IgnoredClasses : Int32 {
|
|
Basic = 32,
|
|
ControllerReplication = 33,
|
|
ApplicationStatus = 34,
|
|
SwitchAll = 39,
|
|
SceneActivation = 43,
|
|
SwitchColor = 51,
|
|
MeterPulse = 53,
|
|
MeterTableMonitor = 61,
|
|
ThermostatOperatingState = 66,
|
|
ClimateControlSchedule = 70,
|
|
CRC16 = 86,
|
|
AssociationGroupInformation = 89,
|
|
DeviceResetLocally = 90,
|
|
ZWavePlusInfo = 94,
|
|
MultiChannel = 96,
|
|
DoorLock = 98,
|
|
BarrierOperator = 102,
|
|
ManufacturerSpecific = 114,
|
|
PowerLevel = 115,
|
|
InclusionController = 116,
|
|
Protection = 117,
|
|
NodeNaming = 119,
|
|
FirmwareUpdate = 122,
|
|
Clock = 129,
|
|
Association = 133,
|
|
Version = 134,
|
|
Proprietary = 136,
|
|
TimeParameters = 139,
|
|
MultiChannelAssociation = 142,
|
|
MultiCmd = 143,
|
|
SimpleAVControl = 148,
|
|
Security = 152
|
|
}
|
|
|
|
public enum Classes : Int32 {
|
|
SwitchBinary = 37,
|
|
SwitchMultilevel = 38,
|
|
SceneControllerConf = 45,
|
|
SensorBinary = 48,
|
|
SensorMultilevel = 49,
|
|
Meter = 50,
|
|
ThermostatMode = 64,
|
|
ThermostatSetPoint = 67,
|
|
CentralScene = 91,
|
|
Configuration = 112,
|
|
Alarm = 113,
|
|
Battery = 128,
|
|
Wakeup = 132,
|
|
Indicator = 135,
|
|
AlarmSensor = 156
|
|
}
|
|
|
|
#region Properties
|
|
public Int32 DeviceId { get; }
|
|
public Int32 Instance { get; }
|
|
public Classes Commandclass { get; }
|
|
public String Id { get; }
|
|
public Int32 SensorId { get; }
|
|
public DateTime LastUpdate { get; protected set; }
|
|
public String Name { get; }
|
|
public Boolean Polling { get; set; }
|
|
public Boolean PollOnce { get; set; }
|
|
public ReadOnlyDictionary<Int32, ACommandClass> Sub { get; protected set; }
|
|
public Boolean HasSub { get; protected set; }
|
|
public Boolean IsSub { get; protected set; }
|
|
public Boolean HasReset { get; protected set; }
|
|
#endregion
|
|
|
|
#region Constructor
|
|
protected ACommandClass(JsonData json, Tuple<Int32, Int32, Classes, Int32> id, HttpConnection http, Boolean polling) {
|
|
this.DeviceId = id.Item1;
|
|
this.Instance = id.Item2;
|
|
this.Commandclass = id.Item3;
|
|
this.SensorId = id.Item4;
|
|
this.http = http;
|
|
this.LastUpdate = DateTime.Now;
|
|
this.Polling = polling;
|
|
this.HasSub = false;
|
|
this.HasReset = false;
|
|
this.IsSub = false;
|
|
this.Id = this.DeviceId + "-" + this.Instance + "-" + (Int32)this.Commandclass + "-" + this.SensorId;
|
|
if (ZwayController.namelist.ContainsKey(this.Id)) {
|
|
this.Name = ZwayController.namelist[this.Id];
|
|
}
|
|
}
|
|
|
|
protected ACommandClass(JsonData json, Tuple<Int32, Int32, Classes> id, HttpConnection http, Boolean polling) {
|
|
this.DeviceId = id.Item1;
|
|
this.Instance = id.Item2;
|
|
this.Commandclass = id.Item3;
|
|
this.http = http;
|
|
this.LastUpdate = DateTime.Now;
|
|
this.Polling = polling;
|
|
this.HasSub = false;
|
|
this.HasReset = false;
|
|
this.IsSub = false;
|
|
this.Id = this.DeviceId + "-" + this.Instance + "-" + (Int32)this.Commandclass;
|
|
if (ZwayController.namelist.ContainsKey(this.Id)) {
|
|
this.Name = ZwayController.namelist[this.Id];
|
|
}
|
|
}
|
|
|
|
internal static ACommandClass CreateInstance(JsonData json, Tuple<Int32, Int32, Classes> id, HttpConnection http, Boolean polling) {
|
|
if (json.Keys.Contains("name") &&
|
|
json.Keys.Contains("data") &&
|
|
json["data"].Keys.Contains("supported") &&
|
|
json["data"]["supported"].Keys.Contains("value") &&
|
|
Boolean.Parse(json["data"]["supported"]["value"].ToString()) &&
|
|
!Enum.IsDefined(typeof(IgnoredClasses), (Int32)id.Item3) && Enum.IsDefined(typeof(Classes), id.Item3)) {
|
|
String name = id.Item3.ToString();
|
|
String objectName = "BlubbFish.IoT.Zway.Devices.CommandClasses." + name[0].ToString().ToUpper() + name.Substring(1).ToLower();
|
|
return GetInstanceConcrete(objectName, json, http, id, polling);
|
|
}
|
|
if (!Enum.IsDefined(typeof(IgnoredClasses), (Int32)id.Item3) && !Enum.IsDefined(typeof(Classes), id.Item3)) {
|
|
Helper.WriteError("CommandClass " + id.Item3 + " not exist. (" + id.Item1 + ", " + id.Item2 + ")");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static ACommandClass GetInstanceConcrete(String objectName, JsonData json, HttpConnection http, Tuple<Int32, Int32, Classes> id, Boolean polling) {
|
|
Type t = null;
|
|
try {
|
|
t = Type.GetType(objectName, true);
|
|
} catch (TypeLoadException) {
|
|
Console.Error.WriteLine("Konnte Type " + objectName + " nicht laden!");
|
|
return null;
|
|
}
|
|
return (ACommandClass)t.GetConstructor(new Type[] { typeof(JsonData), typeof(Tuple<Int32, Int32, Classes>), typeof(HttpConnection), typeof(Boolean) }).Invoke(new Object[] { json, id, http, polling });
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Polling
|
|
|
|
internal virtual void Poll() {
|
|
if (this.Polling || this.PollOnce) {
|
|
this.PollOnce = false;
|
|
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + ((Int32)this.Commandclass).ToString() + "].Get()");
|
|
}
|
|
}
|
|
|
|
protected void PollNone() {
|
|
this.PollOnce = false;
|
|
}
|
|
|
|
protected void PollSub() {
|
|
if (this.Polling || this.PollOnce) {
|
|
this.PollOnce = false;
|
|
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + ((Int32)this.Commandclass).ToString() + "].Get(" + this.SensorId + ")");
|
|
}
|
|
}
|
|
|
|
protected void PollPerSub() {
|
|
foreach (KeyValuePair<Int32, ACommandClass> item in this.Sub) {
|
|
item.Value.Poll();
|
|
}
|
|
}
|
|
|
|
protected void PollSubGlobal() {
|
|
Boolean poll = false;
|
|
Boolean pollonce = false;
|
|
foreach (KeyValuePair<Int32, ACommandClass> item in this.Sub) {
|
|
if (item.Value.Polling) {
|
|
poll = true;
|
|
}
|
|
if(item.Value.PollOnce) {
|
|
pollonce = true;
|
|
}
|
|
}
|
|
if (poll || pollonce) {
|
|
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + ((Int32)this.Commandclass).ToString() + "].Get()");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SetValues
|
|
protected void SetInt(Int32 value) {
|
|
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + ((Int32)this.Commandclass).ToString() + "].Set(" + value + ")");
|
|
}
|
|
|
|
protected void SetTuple(Double value1, Double value2) {
|
|
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + ((Int32)this.Commandclass).ToString() + "].Set(" + value1 + "," + value2 + ")");
|
|
}
|
|
|
|
protected void SetTriple(Double v1, Double v2, Double v3) {
|
|
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + ((Int32)this.Commandclass).ToString() + "].Set(" + v1 + "," + v2 + "," + v3 + ")");
|
|
}
|
|
|
|
protected Boolean CheckSetUpdateTime(JsonData json) {
|
|
if (json.Keys.Contains("updateTime") && (json["updateTime"].IsInt || json["updateTime"].IsLong)) {
|
|
DateTime newdate = DateTimeOffset.FromUnixTimeSeconds(Int64.Parse(json["updateTime"].ToString())).ToLocalTime().DateTime;
|
|
if (newdate > this.LastUpdate) {
|
|
this.LastUpdate = newdate;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public virtual void Reset() {
|
|
if (this.HasReset) {
|
|
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + ((Int32)this.Commandclass).ToString() + "].Reset()");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region InternalHelper
|
|
protected Dictionary<String, Object> ToDictionarySub() {
|
|
Dictionary<String, Object> json = new Dictionary<String, Object>();
|
|
foreach (KeyValuePair<Int32, ACommandClass> item in this.Sub) {
|
|
json.Add(item.Key.ToString(), item.Value.ToDictionary());
|
|
}
|
|
return json;
|
|
}
|
|
|
|
protected List<Senml> ToSenmlListSub() {
|
|
List<Senml> list = new List<Senml>();
|
|
foreach (KeyValuePair<Int32, ACommandClass> item in this.Sub) {
|
|
list.AddRange(item.Value.ToSenmlList());
|
|
}
|
|
return list;
|
|
}
|
|
#endregion
|
|
|
|
#region IMqtt
|
|
public String MqttTopic() {
|
|
return this.DeviceId + "/" + this.Instance + "/" + ((Int32)this.Commandclass).ToString() + (this.IsSub ? "/" + this.SensorId : "");
|
|
}
|
|
|
|
public String ToJson() {
|
|
Dictionary<String, Object> json = this.ToDictionary();
|
|
json.Add("LastUpdate", this.LastUpdate.ToString());
|
|
json.Add("Name", this.Name);
|
|
json.Add("Commandclass", this.Commandclass.ToString());
|
|
return JsonMapper.ToJson(json);
|
|
}
|
|
#endregion
|
|
|
|
#region ISenml
|
|
public String ToSenml() {
|
|
List<Senml> l = this.ToSenmlList();
|
|
if(l.Count == 0) {
|
|
return null;
|
|
}
|
|
Dictionary<String, Object> json = new Dictionary<String, Object> {
|
|
{ "e", l },
|
|
{ "bn", this.DeviceId + "/" + this.Instance + "/" + ((Int32)this.Commandclass).ToString() + (this.IsSub ? "/" + this.SensorId : "") + "/" },
|
|
{ "bt", ((DateTimeOffset)this.LastUpdate).ToUnixTimeSeconds() }
|
|
};
|
|
return JsonMapper.ToJson(json);
|
|
}
|
|
|
|
public String SenmlTopic() {
|
|
return "LS/v2/ZGW/";
|
|
}
|
|
#endregion
|
|
|
|
#region Abstract
|
|
protected abstract List<Senml> ToSenmlList();
|
|
public abstract Dictionary<String, Object> ToDictionary();
|
|
internal abstract void SetUpdate(JsonData json, Match match);
|
|
#endregion
|
|
}
|
|
}
|