[NF] Zway-Bot hinzugefügt

[NF] Viele kleine Änderungen an Zwaylib zur Verbesserung der Kompatibilität
This commit is contained in:
2017-11-25 11:00:48 +00:00
parent 88fa19a604
commit 99dc37d751
26 changed files with 437 additions and 158 deletions
+54 -22
View File
@@ -1,28 +1,38 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using BlubbFish.IoT.Zway.Devices;
using BlubbFish.IoT.Zway.Events;
using BlubbFish.IoT.Zway.Interfaces;
using BlubbFish.IoT.Zway.lib;
using LitJson;
namespace BlubbFish.IoT.Zway {
public class ZwayController : IDisposable {
private HttpConnection http;
private Dictionary<Int32, Device> devices = new Dictionary<Int32, Device>();
private Thread updatethread;
private Thread pollthread;
public static ReadOnlyDictionary<String, String> namelist;
private Boolean polling;
public delegate void DataUpdate(Object sender, DeviceUpdateEvent e);
public event DataUpdate Update;
public ZwayController(String server, String user, String pass) {
public ReadOnlyDictionary<Int32, Device> Devices { get; private set; }
public ZwayController(String server, String user, String pass, Dictionary<String, String> names, Boolean enablePoll = true) {
namelist = new ReadOnlyDictionary<String, String>(names);
this.polling = enablePoll;
this.Connect(server, user, pass);
}
public ZwayController(Dictionary<String, String> settings) {
public ZwayController(Dictionary<String, String> settings, Dictionary<String, String> names, Boolean enablePoll = true) {
namelist = new ReadOnlyDictionary<String, String>(names);
this.polling = enablePoll;
this.Connect(settings["server"], settings["user"], settings["pass"]);
}
@@ -34,12 +44,12 @@ namespace BlubbFish.IoT.Zway {
CultureInfo.DefaultThreadCurrentUICulture = info;
Thread.CurrentThread.CurrentCulture = info;
Thread.CurrentThread.CurrentUICulture = info;
this.ParseNamespace(this.http.GetJson("ZWaveAPI/Run/devices"));
this.CreateDevices(this.http.GetJson("ZWaveAPI/Run/devices"));
this.updatethread = new Thread(this.Updater);
this.updatethread.Start();
this.pollthread = new Thread(this.Poll);
this.pollthread.Start();
foreach (KeyValuePair<Int32, Device> item in this.devices) {
foreach (KeyValuePair<Int32, Device> item in this.Devices) {
item.Value.Update += this.DeviceUpdate;
}
}
@@ -56,13 +66,37 @@ namespace BlubbFish.IoT.Zway {
Thread.CurrentThread.CurrentCulture = info;
Thread.CurrentThread.CurrentUICulture = info;
while(true) {
foreach (KeyValuePair<Int32, Device> item in this.devices) {
foreach (KeyValuePair<Int32, Device> item in this.Devices) {
item.Value.Poll();
}
Thread.Sleep(1000);
}
}
public ICommandClass GetCommandClass(Int32 deviceid, Int32 instanceid, Int32 classid) {
if(this.Devices.ContainsKey(deviceid) && this.Devices[deviceid].Instances.ContainsKey(instanceid) && this.Devices[deviceid].Instances[instanceid].CommandClasses.ContainsKey(classid)) {
return this.Devices[deviceid].Instances[instanceid].CommandClasses[classid];
}
return null;
}
public ICommandClass GetCommandClassSub(Int32 deviceid, Int32 instanceid, Int32 classid, Int32 subcid) {
ICommandClass comandclass = this.GetCommandClass(deviceid, instanceid, classid);
if (comandclass != null) {
foreach (PropertyInfo item in comandclass.GetType().GetProperties()) {
if (item.Name == "Sub") {
if (comandclass.GetType().GetProperty("Sub").PropertyType == typeof(ReadOnlyDictionary<Int32, CommandClassSub>)) {
ReadOnlyDictionary<Int32, CommandClassSub> commandclasssub = (ReadOnlyDictionary<Int32, CommandClassSub>)comandclass.GetType().GetProperty("Sub").GetValue(comandclass);
if (commandclasssub.ContainsKey(subcid)) {
return commandclasssub[subcid];
}
}
}
}
}
return null;
}
private void Updater() {
CultureInfo info = new CultureInfo("de-DE");
info.NumberFormat.NumberDecimalSeparator = ".";
@@ -79,8 +113,8 @@ namespace BlubbFish.IoT.Zway {
Int32 deviceid = Int32.Parse(match.Groups[1].Value);
Int32 instanceid = Int32.Parse(match.Groups[2].Value);
Int32 commandid = Int32.Parse(match.Groups[3].Value);
if (this.devices.ContainsKey(deviceid) && this.devices[deviceid].Instances.ContainsKey(instanceid) && this.devices[deviceid].Instances[instanceid].CommandClasses.ContainsKey(commandid)) {
this.devices[deviceid].Instances[instanceid].CommandClasses[commandid].SetUpdate(notifications[item], match);
if (this.Devices.ContainsKey(deviceid) && this.Devices[deviceid].Instances.ContainsKey(instanceid) && this.Devices[deviceid].Instances[instanceid].CommandClasses.ContainsKey(commandid)) {
this.Devices[deviceid].Instances[instanceid].CommandClasses[commandid].SetUpdate(notifications[item], match);
}
} /*else {
if(item.StartsWith("controller.")) { }
@@ -95,8 +129,8 @@ namespace BlubbFish.IoT.Zway {
match = new Regex("^devices\\.([0-9]+)\\.(data.*)", RegexOptions.IgnoreCase).Match(item);
if(match.Success) {
Int32 deviceid = Int32.Parse(match.Groups[1].Value);
if (this.devices.ContainsKey(deviceid)) {
this.devices[deviceid].SetUpdate(notifications[item], match);
if (this.Devices.ContainsKey(deviceid)) {
this.Devices[deviceid].SetUpdate(notifications[item], match);
}
}
}
@@ -104,22 +138,20 @@ namespace BlubbFish.IoT.Zway {
}
}
private void ParseNamespace(JsonData json) {
private void CreateDevices(JsonData json) {
Dictionary<Int32, Device> devices = new Dictionary<Int32, Device>();
foreach (String deviceid in json.Keys) {
this.CreateDevices(Int32.Parse(deviceid), json[deviceid]);
}
}
private void CreateDevices(Int32 deviceid, JsonData json) {
Device d = Device.CreateDevice(json, new Tuple<Int32>(deviceid), this.http);
if(d != null) {
this.devices.Add(deviceid, d);
Device d = Device.CreateDevice(json[deviceid], new Tuple<Int32>(Int32.Parse(deviceid)), this.http, this.polling);
if (d != null) {
devices.Add(Int32.Parse(deviceid), d);
}
}
this.Devices = new ReadOnlyDictionary<Int32, Device>(devices);
}
public List<T> GetCommandClasses<T>() {
List<T> ret = new List<T>();
foreach (KeyValuePair<Int32, Device> device in this.devices) {
foreach (KeyValuePair<Int32, Device> device in this.Devices) {
foreach (KeyValuePair<Int32, Instance> instance in device.Value.Instances) {
foreach (KeyValuePair<Int32, CommandClass> commandclass in instance.Value.CommandClasses) {
if (commandclass.Value is T) {
@@ -142,7 +174,7 @@ namespace BlubbFish.IoT.Zway {
}
this.updatethread = null;
this.pollthread = null;
this.devices.Clear();
this.Devices = null;
this.disposedValue = true;
}
}
@@ -151,7 +183,7 @@ namespace BlubbFish.IoT.Zway {
Dispose(false);
}
void IDisposable.Dispose() {
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}