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> events = new Dictionary>(); public override event ModulEvent Update; public Overtaker(ZwayController zway, InIReader settings) : base(zway, settings) { this.ParseIni(); } private void ParseIni() { foreach (String item in this.ini.GetSections()) { String from = this.ini.GetValue(item, "from"); String[] source = from.Split(':'); this.events.Add(source[0], this.ini.GetSection(item)); String[] addr = source[0].Split('-'); ICommandClass c; if (addr.Length == 3 || addr.Length == 4) { Int32 deviceid = Int32.Parse(addr[0]); Int32 instanceid = Int32.Parse(addr[1]); Int32 classid = Int32.Parse(addr[2]); if (addr.Length == 4) { Int32 subcid = Int32.Parse(addr[3]); c = this.zw.GetCommandClassSub(deviceid, instanceid, classid, subcid); } else { c = this.zw.GetCommandClass(deviceid, instanceid, classid); } if (c != null) { c.Polling = true; c.Update += this.ChangedEvent; } } } } private void ChangedEvent(Object sender, DeviceUpdateEvent e) { if(Helper.HasInterface(sender, "ICommandClass")) { if (this.events.ContainsKey(((ICommandClass)sender).Id)) { this.SetValues(sender, ((ICommandClass)sender).Id, this.events[((ICommandClass)sender).Id]); } } } private void SetValues(Object sender, String name, Dictionary dictionary) { String from = dictionary["from"]; String[] source = from.Split(':'); if (source.Length != 2) { return; } String source_value; if (Helper.HasProperty(sender, source[1])) { source_value = Helper.GetProperty(sender, 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]; } } } foreach (String to in dictionary["to"].Split(';')) { String[] target = to.Split(':'); if(target.Length == 2) { String[] addr = target[0].Split('-'); if (addr.Length == 3 || addr.Length == 4) { Int32 deviceid = Int32.Parse(addr[0]); Int32 instanceid = Int32.Parse(addr[1]); Int32 classid = Int32.Parse(addr[2]); ICommandClass c; if (addr.Length == 4) { Int32 subcid = Int32.Parse(addr[3]); c = this.zw.GetCommandClassSub(deviceid, instanceid, classid, subcid); } else { c = this.zw.GetCommandClass(deviceid, instanceid, classid); } if (c != null) { String target_value; if (Helper.HasProperty(c, target[1])) { target_value = Helper.GetProperty(c, target[1]).ToString(); if (target_value != source_value) { Helper.SetProperty(c, target[1], source_value); this.Update?.Invoke(this, new OvertakerEvent(target[0], target[1], source_value)); } } } } } } } #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 } }