[NF] Erste Funktionierende Version
This commit is contained in:
+140
-4
@@ -1,13 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using BlubbFish.IoT.Zway.Devices;
|
||||
using BlubbFish.IoT.Zway.Events;
|
||||
using BlubbFish.IoT.Zway.lib;
|
||||
using LitJson;
|
||||
|
||||
namespace BlubbFish.IoT.Zway {
|
||||
public class ZwayController {
|
||||
public class ZwayController : IDisposable {
|
||||
private HttpConnection http;
|
||||
private Dictionary<Int32, Device> devices = new Dictionary<Int32, Device>();
|
||||
private Thread updatethread;
|
||||
private Thread pollthread;
|
||||
|
||||
public delegate void DataUpdate(Object sender, DeviceUpdateEvent e);
|
||||
public event DataUpdate Update;
|
||||
|
||||
public ZwayController(String server, String user, String pass) {
|
||||
this.Connect(server, user, pass);
|
||||
@@ -19,6 +28,133 @@ namespace BlubbFish.IoT.Zway {
|
||||
|
||||
private void Connect(String server, String user, String pass) {
|
||||
this.http = new HttpConnection(server, user, pass);
|
||||
CultureInfo info = new CultureInfo("de-DE");
|
||||
info.NumberFormat.NumberDecimalSeparator = ".";
|
||||
CultureInfo.DefaultThreadCurrentCulture = info;
|
||||
CultureInfo.DefaultThreadCurrentUICulture = info;
|
||||
Thread.CurrentThread.CurrentCulture = info;
|
||||
Thread.CurrentThread.CurrentUICulture = info;
|
||||
this.ParseNamespace(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) {
|
||||
item.Value.Update += this.DeviceUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
private void DeviceUpdate(Object sender, DeviceUpdateEvent e) {
|
||||
this.Update?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
private void Poll() {
|
||||
CultureInfo info = new CultureInfo("de-DE");
|
||||
info.NumberFormat.NumberDecimalSeparator = ".";
|
||||
CultureInfo.DefaultThreadCurrentCulture = info;
|
||||
CultureInfo.DefaultThreadCurrentUICulture = info;
|
||||
Thread.CurrentThread.CurrentCulture = info;
|
||||
Thread.CurrentThread.CurrentUICulture = info;
|
||||
while(true) {
|
||||
foreach (KeyValuePair<Int32, Device> item in this.devices) {
|
||||
item.Value.Poll();
|
||||
}
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
private void Updater() {
|
||||
CultureInfo info = new CultureInfo("de-DE");
|
||||
info.NumberFormat.NumberDecimalSeparator = ".";
|
||||
CultureInfo.DefaultThreadCurrentCulture = info;
|
||||
CultureInfo.DefaultThreadCurrentUICulture = info;
|
||||
Thread.CurrentThread.CurrentCulture = info;
|
||||
Thread.CurrentThread.CurrentUICulture = info;
|
||||
while (true) {
|
||||
Int64 date = ((DateTimeOffset)DateTime.Now.AddSeconds(-2)).ToUnixTimeSeconds();
|
||||
JsonData notifications = this.http.GetJson("ZWave.zway/Data/"+date);
|
||||
foreach (String item in notifications.Keys) {
|
||||
Match match = new Regex("^devices\\.([0-9]+)\\.instances\\.([0-9]+)\\.commandClasses\\.([0-9]+)(\\.data\\.([0-9]+)|\\.data\\.[a-z].*|\\.data)$", RegexOptions.IgnoreCase).Match(item);
|
||||
if(match.Success) {
|
||||
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);
|
||||
}
|
||||
} /*else {
|
||||
if(item.StartsWith("controller.")) { }
|
||||
else if(item == "updateTime") { }
|
||||
else {
|
||||
match = new Regex("^devices\\.([0-9]+)\\.(data.*)", RegexOptions.IgnoreCase).Match(item);
|
||||
if(!match.Success) {
|
||||
|
||||
}
|
||||
}
|
||||
}*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Thread.Sleep(1500);
|
||||
}
|
||||
}
|
||||
|
||||
private void ParseNamespace(JsonData json) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public List<T> GetCommandClasses<T>() {
|
||||
List<T> ret = new List<T>();
|
||||
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) {
|
||||
ret.Add((T)Convert.ChangeType(commandclass.Value, typeof(T)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#region IDisposable Support
|
||||
private Boolean disposedValue = false;
|
||||
|
||||
protected virtual void Dispose(Boolean disposing) {
|
||||
if (!this.disposedValue) {
|
||||
if (disposing) {
|
||||
this.updatethread.Abort();
|
||||
this.pollthread.Abort();
|
||||
}
|
||||
this.updatethread = null;
|
||||
this.pollthread = null;
|
||||
this.devices.Clear();
|
||||
this.disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
~ZwayController() {
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
void IDisposable.Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user