[NF] v1.2.0.0 Change classid to enum, so its more readable [NF] v1.1.0.0 Rename a lot of internal functions
79 lines
3.1 KiB
C#
79 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using BlubbFish.IoT.Zway.Devices.CommandClasses;
|
|
using BlubbFish.IoT.Zway.Events;
|
|
using BlubbFish.IoT.Zway.Interfaces;
|
|
using BlubbFish.IoT.Zway.lib;
|
|
using LitJson;
|
|
|
|
namespace BlubbFish.IoT.Zway.Devices {
|
|
public class Instance {
|
|
private DateTime nextwakeup;
|
|
private Boolean polling;
|
|
|
|
public delegate void UpdatedInstance(Object sender, DeviceUpdateEvent e);
|
|
public event UpdatedInstance Update;
|
|
|
|
public Int32 DeviceId { get; }
|
|
public Int32 InstanceId { get; }
|
|
public ReadOnlyDictionary<ACommandClass.Classes, ACommandClass> CommandClasses { get; private set; }
|
|
|
|
private Instance(JsonData json, Tuple<Int32, Int32> id, HttpConnection http, Boolean polling) {
|
|
this.DeviceId = id.Item1;
|
|
this.InstanceId = id.Item2;
|
|
this.polling = polling;
|
|
this.CreateInstances(json["commandClasses"], http);
|
|
foreach (KeyValuePair<ACommandClass.Classes, ACommandClass> item in this.CommandClasses) {
|
|
item.Value.Update += this.ClassUpdate;
|
|
}
|
|
this.MakePolltimer();
|
|
}
|
|
|
|
private void MakePolltimer() {
|
|
if(this.CommandClasses.ContainsKey(ACommandClass.Classes.Wakeup)) {
|
|
this.nextwakeup = ((Wakeup)this.CommandClasses[ACommandClass.Classes.Wakeup]).LastWakeup.AddSeconds(((Wakeup)this.CommandClasses[ACommandClass.Classes.Wakeup]).Interval).AddSeconds(-20);
|
|
} else {
|
|
this.nextwakeup = DateTime.Now.AddSeconds(60);
|
|
}
|
|
}
|
|
|
|
private void ClassUpdate(Object sender, DeviceUpdateEvent e) {
|
|
this.Update?.Invoke(this, e);
|
|
}
|
|
|
|
private void CreateInstances(JsonData json, HttpConnection http) {
|
|
Dictionary<ACommandClass.Classes, ACommandClass> commands = new Dictionary<ACommandClass.Classes, ACommandClass>();
|
|
foreach (String commandid in json.Keys) {
|
|
ACommandClass c = ACommandClass.CreateInstance(json[commandid], new Tuple<Int32, Int32, ACommandClass.Classes>(this.DeviceId, this.InstanceId, (ACommandClass.Classes)Int32.Parse(commandid)), http, this.polling);
|
|
if (c != null) {
|
|
commands.Add((ACommandClass.Classes)Int32.Parse(commandid), c);
|
|
}
|
|
}
|
|
this.CommandClasses = new ReadOnlyDictionary<ACommandClass.Classes, ACommandClass>(commands);
|
|
}
|
|
|
|
internal static Instance CreateInstance(JsonData json, Tuple<Int32, Int32> id, HttpConnection http, Boolean polling) {
|
|
if (json.Keys.Contains("commandClasses") &&
|
|
json["commandClasses"].Count > 0 &&
|
|
json.Keys.Contains("data")) {
|
|
return new Instance(json, id, http, polling);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public override String ToString() {
|
|
return "Instance: " + this.DeviceId + "-" + this.InstanceId + " [" + this.CommandClasses.Count + "]";
|
|
}
|
|
|
|
internal void Poll() {
|
|
if(DateTime.Now > this.nextwakeup) {
|
|
this.MakePolltimer();
|
|
foreach (KeyValuePair<ACommandClass.Classes, ACommandClass> item in this.CommandClasses) {
|
|
item.Value.Poll();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|