Zway-Bot/Zway-Bot/Moduls/Overtaker.cs

102 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using BlubbFish.IoT.Zway;
using BlubbFish.IoT.Zway.Events;
using BlubbFish.IoT.Zway.Interfaces;
using BlubbFish.Utils;
namespace ZwayBot.Moduls {
internal class Overtaker : AModul, IDisposable {
private Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>();
public override event ModulEvent Update;
public Overtaker(ZwayController zway, InIReader settings) : base(zway, settings) {
this.ParseIni();
}
private void ParseIni() {
foreach (KeyValuePair<String, Dictionary<String, String>> item in this.config) {
if (item.Value.ContainsKey("from")) {
String from = item.Value["from"];
String[] source = from.Split(':');
this.events.Add(source[0], item.Value);
ACommandClass c = this.zw.GetCommandClass(source[0]);
if (c != null) {
c.Polling = true;
c.Update += this.ChangedEvent;
}
}
}
}
private void ChangedEvent(Object sender, DeviceUpdateEvent e) {
if(sender.HasAbstract(typeof(ACommandClass))) {
if (this.events.ContainsKey(((ACommandClass)sender).Id)) {
this.SetValues(sender, ((ACommandClass)sender).Id, this.events[((ACommandClass)sender).Id]);
}
}
}
private void SetValues(Object sender, String name, Dictionary<String, String> dictionary) {
String from = dictionary["from"];
String[] source = from.Split(':');
if (source.Length != 2) {
return;
}
String source_value;
if (sender.HasProperty(source[1])) {
source_value = sender.GetProperty(source[1]).ToString();
} else {
return;
}
if(dictionary.ContainsKey("convert")) {
foreach (String tuple in dictionary["convert"].Split(';')) {
String[] item = tuple.Split('-');
if(source_value == item[0]) {
source_value = item[1];
}
}
}
if (dictionary.ContainsKey("to")) {
foreach (String to in dictionary["to"].Split(';')) {
String[] target = to.Split(':');
if (target.Length == 2) {
ACommandClass c = this.zw.GetCommandClass(target[0]);
if (c != null) {
if (c.HasProperty(target[1])) {
if (c.GetProperty(target[1]).ToString() != source_value) {
c.SetProperty(target[1], source_value);
this.Update?.Invoke(this, new OvertakerEvent(target[0], target[1], source_value));
}
}
}
}
}
}
}
protected override void UpdateConfig() { }
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
}
this.disposedValue = true;
}
}
~Overtaker() {
Dispose(false);
}
public override void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}