diff --git a/MIG.HomeAutomation/Commons/ModuleTypes.cs b/MIG.HomeAutomation/Commons/ModuleTypes.cs index 250aaec..a648c7f 100644 --- a/MIG.HomeAutomation/Commons/ModuleTypes.cs +++ b/MIG.HomeAutomation/Commons/ModuleTypes.cs @@ -1,6 +1,6 @@ namespace MIG.Interfaces.HomeAutomation.Commons { - public enum ModuleTypes_To_Refactor + public enum ModuleTypes { Generic = -1, Program, diff --git a/MIG.HomeAutomation/MIG.HomeAutomation.csproj b/MIG.HomeAutomation/MIG.HomeAutomation.csproj index 52bb83c..bd93372 100644 --- a/MIG.HomeAutomation/MIG.HomeAutomation.csproj +++ b/MIG.HomeAutomation/MIG.HomeAutomation.csproj @@ -19,7 +19,7 @@ - + diff --git a/MIG.HomeAutomation/X10.cs b/MIG.HomeAutomation/X10.cs index f1f9afa..9d835de 100644 --- a/MIG.HomeAutomation/X10.cs +++ b/MIG.HomeAutomation/X10.cs @@ -34,14 +34,12 @@ limitations under the License. using System.Threading; using System.Xml.Serialization; -using MIG.Interfaces.HomeAutomation.Commons; using MIG.Config; +using MIG.Interfaces.HomeAutomation.Commons; using CM19Lib; using CM19Lib.X10; -using Newtonsoft.Json; - using XTenLib; using RfCommandReceivedEventArgs = CM19Lib.Events.RfCommandReceivedEventArgs; @@ -134,8 +132,11 @@ public void OnSetOption(Option option) module.Domain = this.GetDomain(); module.Address = m.Value.Code; module.Description = m.Value.Description; - module.ModuleType = ModuleTypes.Switch; - module.CustomData = m.Value.Level; + module.CustomData = new X10ModuleData() + { + Level = m.Value.Level, + Type = ModuleTypes.Switch + }; standardModules.Add(module); } OnInterfaceModulesChanged(this.GetDomain()); @@ -161,7 +162,10 @@ public List GetModules() { module.Description = "CM19 Transceiver"; } - module.ModuleType = ModuleTypes.Sensor; + module.CustomData = new X10ModuleData() + { + Type = ModuleTypes.Sensor + }; modules.Add(module); } // Standard X10 modules @@ -170,7 +174,10 @@ public List GetModules() module = new InterfaceModule(); module.Domain = this.GetDomain(); module.Address = kv.Value.Code; - module.ModuleType = ModuleTypes.Switch; + module.CustomData = new X10ModuleData() + { + Type = ModuleTypes.Switch + }; module.Description = "X10 Module"; modules.Add(module); } @@ -263,28 +270,28 @@ public object InterfaceControl(MigInterfaceCommand request) { case Commands.Control_On: cm19Lib.UnitOn(houseCode, unitCode); - module.CustomData = 1D; + module.CustomData.Level = 1D; UpdateModuleLevel(module); break; case Commands.Control_Off: cm19Lib.UnitOff(houseCode, unitCode); - module.CustomData = 0D; + module.CustomData.Level = 0D; UpdateModuleLevel(module); break; case Commands.Control_Bright: cm19Lib.Bright(houseCode); - module.CustomData = module.CustomData + (5 / 100D); - if (module.CustomData > 1) module.CustomData = 1D; + module.CustomData.Level += (5 / 100D); + if (module.CustomData.Level > 1) module.CustomData.Level = 1D; UpdateModuleLevel(module); break; case Commands.Control_Dim: cm19Lib.Dim(houseCode); - module.CustomData = module.CustomData - (5 / 100D); - if (module.CustomData < 0) module.CustomData = 0D; + module.CustomData.Level -= (5 / 100D); + if (module.CustomData.Level < 0) module.CustomData.Level = 0D; UpdateModuleLevel(module); break; case Commands.Control_Level: - int dimValue = (int.Parse(option) - (int) (module.CustomData * 100.0)) / 5; + int dimValue = (int.Parse(option) - (int) (module.CustomData.Level * 100.0)) / 5; if (dimValue > 0) { cm19Lib.Bright(houseCode); @@ -301,11 +308,11 @@ public object InterfaceControl(MigInterfaceCommand request) cm19Lib.Dim(houseCode); } } - module.CustomData = module.CustomData + (dimValue * 5 / 100D); + module.CustomData.Level += (dimValue * 5 / 100D); UpdateModuleLevel(module); break; case Commands.Control_Toggle: - if (module.CustomData == 0D) + if (module.CustomData.Level == 0D) { cm19Lib.UnitOn(houseCode, unitCode); UpdateModuleLevel(module); @@ -504,8 +511,11 @@ private InterfaceModule AddSecurityModule(ModuleTypes moduleType, string address module.Domain = this.GetDomain(); module.Address = address; module.Description = "X10 Security"; - module.ModuleType = moduleType; - module.CustomData = 0D; + module.CustomData = new X10ModuleData() + { + Level = 0D, + Type = moduleType + }; securityModules.Add(module); SerializeModules(SecurityModulesDb, securityModules); OnInterfacePropertyChanged(module.Domain, "RF", "X10 RF Receiver", ModuleEvents.Receiver_Status, "Added module " + address + " (" + moduleType + ")"); @@ -654,21 +664,21 @@ private void Cm19LibOnRfCommandReceived(object sender, RfCommandReceivedEventArg switch (args.Command) { case Function.On: - module.CustomData = 1D; + module.CustomData.Level = 1D; UpdateModuleLevel(module); break; case Function.Off: - module.CustomData = 0D; + module.CustomData.Level = 0D; UpdateModuleLevel(module); break; case Function.Bright: - module.CustomData = module.CustomData + 1D/22D; - if (module.CustomData > 1) module.CustomData = 1D; + module.CustomData.Level += 1D/22D; + if (module.CustomData.Level > 1) module.CustomData.Level = 1D; UpdateModuleLevel(module); break; case Function.Dim: - module.CustomData = module.CustomData - 1D/22D; - if (module.CustomData < 0) module.CustomData = 0D; + module.CustomData.Level -= 1D/22D; + if (module.CustomData.Level < 0) module.CustomData.Level = 0D; UpdateModuleLevel(module); break; case Function.AllLightsOn: @@ -782,4 +792,23 @@ protected virtual void OnInterfacePropertyChanged(string domain, string source, #endregion } + + public class X10ModuleData + { + private double level = 0; + public double Level { + get => level; + set + { + level = value; + if (level != 0) + { + LastLevel = level; + } + } + } + public double LastLevel; + public ModuleTypes Type = ModuleTypes.Generic; + } + } diff --git a/MIG.HomeAutomation/ZWave.cs b/MIG.HomeAutomation/ZWave.cs index 7646966..c6fdcb6 100644 --- a/MIG.HomeAutomation/ZWave.cs +++ b/MIG.HomeAutomation/ZWave.cs @@ -191,54 +191,57 @@ public List GetModules() module.Domain = this.GetDomain(); module.Address = node.Id.ToString(); //module.Description = "ZWave Node"; - module.ModuleType = ModuleTypes.Generic; + module.CustomData = new ZWaveNodeData() + { + Type = ModuleTypes.Generic + }; if (node.ProtocolInfo.GenericType != (byte)GenericType.None) { switch (node.ProtocolInfo.GenericType) { case (byte)GenericType.StaticController: module.Description = "Static Controller"; - module.ModuleType = ModuleTypes.Generic; + module.CustomData.Type = ModuleTypes.Generic; break; case (byte)GenericType.SwitchBinary: module.Description = "Binary Switch"; - module.ModuleType = ModuleTypes.Switch; + module.CustomData.Type = ModuleTypes.Switch; break; case (byte)GenericType.SwitchMultilevel: module.Description = "Multilevel Switch"; - module.ModuleType = ModuleTypes.Dimmer; + module.CustomData.Type = ModuleTypes.Dimmer; break; case (byte)GenericType.Thermostat: module.Description = "Thermostat"; - module.ModuleType = ModuleTypes.Thermostat; + module.CustomData.Type = ModuleTypes.Thermostat; break; case (byte)GenericType.SensorAlarm: module.Description = "Alarm Sensor"; - module.ModuleType = ModuleTypes.Sensor; + module.CustomData.Type = ModuleTypes.Sensor; break; case (byte)GenericType.SensorBinary: module.Description = "Binary Sensor"; - module.ModuleType = ModuleTypes.Sensor; + module.CustomData.Type = ModuleTypes.Sensor; break; case (byte)GenericType.SensorMultilevel: module.Description = "Multilevel Sensor"; - module.ModuleType = ModuleTypes.Sensor; + module.CustomData.Type = ModuleTypes.Sensor; break; case (byte)GenericType.Meter: module.Description = "ZWave Meter"; - module.ModuleType = ModuleTypes.Sensor; + module.CustomData.Type = ModuleTypes.Sensor; break; case (byte)GenericType.EntryControl: module.Description = "ZWave Door Lock"; - module.ModuleType = ModuleTypes.DoorLock; + module.CustomData.Type = ModuleTypes.DoorLock; break; } @@ -1241,4 +1244,22 @@ protected virtual void OnInterfacePropertyChanged(string domain, string source, #endregion } + + public class ZWaveNodeData + { + private double level = 0; + public double Level { + get => level; + set + { + level = value; + if (level != 0) + { + LastLevel = level; + } + } + } + public double LastLevel; + public ModuleTypes Type = ModuleTypes.Generic; + } } diff --git a/MIG.HomeAutomation/ZigBee.cs b/MIG.HomeAutomation/ZigBee.cs index 86a72a3..426f9df 100644 --- a/MIG.HomeAutomation/ZigBee.cs +++ b/MIG.HomeAutomation/ZigBee.cs @@ -376,9 +376,11 @@ public void AddNode(ZigBeeNode node) { Domain = "HomeAutomation.ZigBee", Address = address, - // TODO: get device type - ModuleType = ModuleTypes.Dimmer, CustomData = new ZigBeeNodeData() + { + // TODO: get type from device node + Type = ModuleTypes.Dimmer + } }); lastAddedNode = node.NetworkAddress; OnInterfacePropertyChanged(this.GetDomain(), "0", "ZigBee Controller", "Controller.Status", "Added node " + node.NetworkAddress); @@ -517,7 +519,6 @@ private async Task DiscoverAttributes(ZigBeeEndpoint endpoint) } - public class ZigBeeNodeData { private double level = 0; @@ -534,6 +535,7 @@ public double Level { } public double LastLevel; public ushort Transition = 4; // 400ms + public ModuleTypes Type = ModuleTypes.Generic; } public class ConsoleCommandListener : IZigBeeCommandListener