Files
Zway/Zway/Devices/CommandClass.cs
T
BlubbFish 99dc37d751 [NF] Zway-Bot hinzugefügt
[NF] Viele kleine Änderungen an Zwaylib zur Verbesserung der Kompatibilität
2017-11-25 11:00:48 +00:00

103 lines
4.0 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 LitJson;
namespace BlubbFish.IoT.Zway.Devices {
public abstract class CommandClass : ICommandClass {
protected HttpConnection http;
public abstract event UpdatedValue Update;
private enum Ignored {
Basic = 32,
ManufacturerSpecific = 114,
PowerLevel = 115,
Protection = 117,
NodeNaming = 119,
FirmwareUpdate = 122,
Association = 133,
Version = 134,
MultiChannelAssociation = 142,
MultiCmd = 143
}
public Int32 DeviceId { get; }
public Int32 Instance { get; }
public Int32 Commandclass { get; }
public String Id { get; }
public DateTime LastUpdate { get; private set; }
public String Name { get; }
public Boolean Polling { get; set; }
protected CommandClass(JsonData json, HttpConnection http, Tuple<Int32, Int32, Int32> id, 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.Id = this.DeviceId + "-" + this.Instance + "-" + this.Commandclass;
if (ZwayController.namelist.ContainsKey(this.Id)) {
this.Name = ZwayController.namelist[this.Id];
}
}
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;
}
}
internal static CommandClass CreateInstance(JsonData json, Tuple<Int32, Int32, Int32> 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(Ignored), id.Item3)) {
String name = json["name"].ToString();
String objectName = "BlubbFish.IoT.Zway.Devices.CommandClasses." + name[0].ToString().ToUpper() + name.Substring(1).ToLower();
return GetInstanceConcrete(objectName, json, http, id, polling);
}
return null;
}
private static CommandClass GetInstanceConcrete(String objectName, JsonData json, HttpConnection http, Tuple<Int32, Int32, Int32> 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 (CommandClass)t.GetConstructor(new Type[] { typeof(JsonData), typeof(HttpConnection), typeof(Tuple<Int32, Int32, Int32>), typeof(Boolean) }).Invoke(new Object[] { json, http, id, polling });
}
internal abstract void SetUpdate(JsonData json, Match match);
internal virtual void Poll() {
if(this.Polling) {
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + this.Commandclass + "].Get()");
}
}
protected void SetInt(Int32 value) {
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + this.Commandclass + "].Set("+value+")");
}
protected void SetIntTuple(Int32 value1, Int32 value2) {
this.http.GetVoid("ZWave.zway/Run/devices[" + this.DeviceId + "].instances[" + this.Instance + "].commandClasses[" + this.Commandclass + "].Set(" + value1 + "," + value2 + ")");
}
}
}