private static void RunStressTest(ZWaveController controller) { ToggleDebug(true); // loop 10 times for (var x = 0; x < 10; x++) { foreach (var node in controller.Nodes) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("\nNode {0} Controller.GetNodeInformationFrame", node.Id); Console.ForegroundColor = ConsoleColor.White; controller.GetNodeInformationFrame(node.Id); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("\nNode {0} Controller.GetNeighborsRoutingInfo", node.Id); Console.ForegroundColor = ConsoleColor.White; controller.GetNeighborsRoutingInfo(node.Id); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("\nNode {0} CommandClass.ManufacturerSpecific.Get", node.Id); Console.ForegroundColor = ConsoleColor.White; ManufacturerSpecific.Get(node); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("\nNode {0} CommandClass.Basic.Get", node.Id); Console.ForegroundColor = ConsoleColor.White; Basic.Get(node); } // Pause 2 secods between each test pass Thread.Sleep(2000); } ToggleDebug(false); }
public object InterfaceControl(MIGInterfaceCommand request) { string returnvalue = ""; bool raisepropchanged = false; string parampath = "Status.Level"; string raiseparam = ""; // string nodeid = request.nodeid; Command command = (Command)request.command; ////---------------------- try { if (command == Command.CONTROLLER_DISCOVERY) { _controller.Discovery(); } //--------------////---------------------- DEPRECATE THESE ---------------------------- //else if (command == Command.CONTROLLER_AUTOREPORTSET) //{ // _controller.AutoReportSet((byte)int.Parse(request.GetOption(0)), (byte)int.Parse(request.GetOption(1))); //} //else if (command == Command.BASIC_REPORT || command.ToString() == "Meter.Get") // TODO .. FIX: this is not basic report, it's a meter get { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.RequestMeterReport(); } else if (command == Command.MULTILEVEL_REPORT) // TODO this one call for having SwitchMultiLevel and SensorMultiLevel reports { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.RequestMultiLevelReport(); } //--------------////-------------------------------------------------------------------- else if (command == Command.CONTROLLER_NODEADD) { _lastnodeadded = 0; byte addcid = _controller.BeginNodeAdd(); for (int i = 0; i < 20; i++) { if (_lastnodeadded > 0) { break; } Thread.Sleep(500); } _controller.StopNodeAdd(); // returnvalue = _lastnodeadded.ToString(); } else if (command == Command.CONTROLLER_NODEREMOVE) { _lastnoderemoved = 0; byte remcid = _controller.BeginNodeRemove(); for (int i = 0; i < 20; i++) { if (_lastnoderemoved > 0) { break; } Thread.Sleep(500); } _controller.StopNodeRemove(); // returnvalue = _lastnoderemoved.ToString(); } ////---------------------- else if (command == Command.BASIC_SET) { raisepropchanged = true; double raiseval = double.Parse(request.GetOption(0)) / 100; if (raiseval > 1) { raiseval = 1; } raiseparam = raiseval.ToString(System.Globalization.CultureInfo.InvariantCulture); // ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.Basic_Set((byte)int.Parse(request.GetOption(0))); } else if (command == Command.BASIC_GET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.Basic_Get(); } ////----------------------- else if (command == Command.MULTIINSTANCE_GETCOUNT) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); // switch (request.GetOption(0)) { case "Switch.Binary": node.MultiInstance_GetCount((byte)ZWaveLib.CommandClass.COMMAND_CLASS_SWITCH_BINARY); break; case "Switch.MultiLevel": node.MultiInstance_GetCount((byte)ZWaveLib.CommandClass.COMMAND_CLASS_SWITCH_MULTILEVEL); break; case "Sensor.Binary": node.MultiInstance_GetCount((byte)ZWaveLib.CommandClass.COMMAND_CLASS_SENSOR_BINARY); break; case "Sensor.MultiLevel": node.MultiInstance_GetCount((byte)ZWaveLib.CommandClass.COMMAND_CLASS_SENSOR_MULTILEVEL); break; } } else if (command == Command.MULTIINSTANCE_GET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); byte instance = (byte)int.Parse(request.GetOption(1)); // parameter index // switch (request.GetOption(0)) { case "Switch.Binary": node.MultiInstance_SwitchBinaryGet(instance); break; case "Switch.MultiLevel": node.MultiInstance_SwitchMultiLevelGet(instance); break; case "Sensor.Binary": node.MultiInstance_SensorBinaryGet(instance); break; case "Sensor.MultiLevel": node.MultiInstance_SensorMultiLevelGet(instance); break; } } else if (command == Command.MULTIINSTANCE_SET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); byte instance = (byte)int.Parse(request.GetOption(1)); // parameter index int value = int.Parse(request.GetOption(2)); // //raisepropchanged = true; //parampath += "." + instance; // Status.Level.<instance> // switch (request.GetOption(0)) { case "Switch.Binary": node.MultiInstance_SwitchBinarySet(instance, value); //raiseparam = (double.Parse(request.GetOption(2)) / 255).ToString(); break; case "Switch.MultiLevel": node.MultiInstance_SwitchMultiLevelSet(instance, value); //raiseparam = (double.Parse(request.GetOption(2)) / 100).ToString(); // TODO: should it be 99 ? break; } } else if (command == Command.NODEINFO_GET) { ZWaveController.GetNodeInformationFrame((byte)int.Parse(nodeid)); } ////----------------------- else if (command == Command.BATTERY_GET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.Battery_Get(); } ////----------------------- else if (command == Command.ASSOCIATION_SET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.Association_Set((byte)int.Parse(request.GetOption(0)), (byte)int.Parse(request.GetOption(1))); } else if (command == Command.ASSOCIATION_GET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.Association_Get((byte)int.Parse(request.GetOption(0))); // groupid } else if (command == Command.ASSOCIATION_REMOVE) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.Association_Remove((byte)int.Parse(request.GetOption(0)), (byte)int.Parse(request.GetOption(1))); // groupid } ////----------------------- else if (command == Command.MANUFACTURERSPECIFIC_GET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.ManufacturerSpecific_Get(); } ////------------------ else if (command == Command.CONFIG_PARAMETERSET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); //byte[] value = new byte[] { (byte)int.Parse(option1) };//BitConverter.GetBytes(Int16.Parse(option1)); //Array.Reverse(value); node.ConfigParameterSet((byte)int.Parse(request.GetOption(0)), int.Parse(request.GetOption(1))); } else if (command == Command.CONFIG_PARAMETERGET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.ConfigParameterGet((byte)int.Parse(request.GetOption(0))); } ////------------------ else if (command == Command.WAKEUP_GET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.WakeUpGetInterval(); } else if (command == Command.WAKEUP_SET) { ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); node.WakeUpSetInterval(uint.Parse(request.GetOption(0))); } ////------------------ else if (command == Command.CONTROL_ON) { raisepropchanged = true; raiseparam = "1"; // // Basic.Set 0xFF ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); ((ZWaveLib.Devices.ProductHandlers.Generic.Switch)node.DeviceHandler).On(); } else if (command == Command.CONTROL_OFF) { raisepropchanged = true; raiseparam = "0"; // // Basic.Set 0x00 ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); ((ZWaveLib.Devices.ProductHandlers.Generic.Switch)node.DeviceHandler).Off(); } else if (command == Command.CONTROL_LEVEL) { raisepropchanged = true; raiseparam = (double.Parse(request.GetOption(0)) / 100).ToString(); // // Basic.Set <level> ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); ((ZWaveLib.Devices.ProductHandlers.Generic.Dimmer)node.DeviceHandler).Level = int.Parse(request.GetOption(0)); } else if (command == Command.CONTROL_TOGGLE) { raisepropchanged = true; // ZWaveNode node = _controller.GetDevice((byte)int.Parse(nodeid)); if (((ZWaveLib.Devices.ProductHandlers.Generic.Switch)node.DeviceHandler).Level == 0) { raiseparam = "1"; // Basic.Set 0xFF ((ZWaveLib.Devices.ProductHandlers.Generic.Switch)node.DeviceHandler).On(); } else { raiseparam = "0"; // Basic.Set 0x00 ((ZWaveLib.Devices.ProductHandlers.Generic.Switch)node.DeviceHandler).Off(); } } } catch { if (raiseparam != "") { raisepropchanged = true; } } // if (raisepropchanged && InterfacePropertyChangedAction != null) { try { //ZWaveNode node = _controller.GetDevice ((byte)int.Parse (nodeid)); InterfacePropertyChangedAction(new InterfacePropertyChangedAction() { Domain = this.Domain, SourceId = nodeid, SourceType = "ZWave Node", Path = parampath, Value = raiseparam }); } catch { } } // return(returnvalue); }
public object InterfaceControl(MigInterfaceCommand request) { ResponseText returnValue = new ResponseText("OK"); bool raiseEvent = false; string eventParameter = ModuleEvents.Status_Level; string eventValue = ""; string nodeId = request.Address; Commands command; Enum.TryParse <Commands>(request.Command.Replace(".", "_"), out command); ZWaveNode node = null; byte nodeNumber = 0; if (byte.TryParse(nodeId, out nodeNumber)) { if (nodeNumber > 0) { node = controller.GetNode(nodeNumber); } switch (command) { case Commands.Controller_Discovery: controller.Discovery(); break; case Commands.Controller_SoftReset: controller.SoftReset(); break; case Commands.Controller_HardReset: controller.HardReset(); controller.Discovery(); break; case Commands.Controller_HealNetwork: controller.HealNetwork(); break; case Commands.Controller_NodeNeighborUpdate: controller.RequestNeighborsUpdateOptions(nodeNumber); controller.RequestNeighborsUpdate(nodeNumber); controller.GetNeighborsRoutingInfo(nodeNumber); returnValue = GetResponseValue(nodeNumber, EventPath_RoutingInfo); break; case Commands.Controller_NodeRoutingInfo: controller.GetNeighborsRoutingInfo(nodeNumber); returnValue = GetResponseValue(nodeNumber, EventPath_RoutingInfo); break; case Commands.Controller_NodeAdd: lastAddedNode = 0; controller.BeginNodeAdd(); for (int i = 0; i < 20; i++) { if (lastAddedNode > 0) { break; } Thread.Sleep(500); } controller.StopNodeAdd(); returnValue = new ResponseText(lastAddedNode.ToString()); break; case Commands.Controller_NodeRemove: lastRemovedNode = 0; controller.BeginNodeRemove(); for (int i = 0; i < 20; i++) { if (lastRemovedNode > 0) { break; } Thread.Sleep(500); } controller.StopNodeRemove(); returnValue = new ResponseText(lastRemovedNode.ToString()); break; case Commands.Basic_Set: { raiseEvent = true; var level = int.Parse(request.GetOption(0)); eventValue = level.ToString(CultureInfo.InvariantCulture); Basic.Set(node, (byte)level); } break; case Commands.Basic_Get: Basic.Get(node); returnValue = GetResponseValue(nodeNumber, EventPath_Basic); break; case Commands.SwitchBinary_Set: { raiseEvent = true; var level = int.Parse(request.GetOption(0)); eventValue = level.ToString(CultureInfo.InvariantCulture); SwitchBinary.Set(node, (byte)level); } break; case Commands.SwitchBinary_Get: SwitchBinary.Get(node); returnValue = GetResponseValue(nodeNumber, EventPath_SwitchBinary); break; case Commands.SwitchMultilevel_Set: { raiseEvent = true; var level = int.Parse(request.GetOption(0)); eventValue = level.ToString(CultureInfo.InvariantCulture); SwitchMultilevel.Set(node, (byte)level); } break; case Commands.SwitchMultilevel_Get: SwitchMultilevel.Get(node); returnValue = GetResponseValue(nodeNumber, EventPath_SwitchMultilevel); break; case Commands.MultiInstance_GetCount: { string commandType = request.GetOption(0).Replace(".", ""); switch (commandType) { case "SwitchBinary": MultiInstance.GetCount(node, (byte)ZWaveLib.CommandClass.SwitchBinary); break; case "SwitchMultiLevel": MultiInstance.GetCount(node, (byte)ZWaveLib.CommandClass.SwitchMultilevel); break; case "SensorBinary": MultiInstance.GetCount(node, (byte)ZWaveLib.CommandClass.SensorBinary); break; case "SensorMultiLevel": MultiInstance.GetCount(node, (byte)ZWaveLib.CommandClass.SensorMultilevel); break; } returnValue = GetResponseValue(nodeNumber, EventPath_MultiInstance + "." + commandType + ".Count"); } break; case Commands.MultiInstance_Get: { byte instance = (byte)int.Parse(request.GetOption(1)); string commandType = request.GetOption(0).Replace(".", ""); switch (commandType) { case "SwitchBinary": MultiInstance.SwitchBinaryGet(node, instance); break; case "SwitchMultiLevel": MultiInstance.SwitchMultiLevelGet(node, instance); break; case "SensorBinary": MultiInstance.SensorBinaryGet(node, instance); break; case "SensorMultiLevel": MultiInstance.SensorMultiLevelGet(node, instance); break; } returnValue = GetResponseValue(nodeNumber, EventPath_MultiInstance + "." + commandType + "." + instance); } break; case Commands.MultiInstance_Set: { byte instance = (byte)int.Parse(request.GetOption(1)); int value = int.Parse(request.GetOption(2)); // //raisepropchanged = true; //parampath += "." + instance; // Status.Level.<instance> // switch (request.GetOption(0)) { case "Switch.Binary": MultiInstance.SwitchBinarySet(node, instance, value); //raiseparam = (double.Parse(request.GetOption(2)) / 255).ToString(); break; case "Switch.MultiLevel": MultiInstance.SwitchMultiLevelSet(node, instance, value); //raiseparam = (double.Parse(request.GetOption(2)) / 100).ToString(); // TODO: should it be 99 ? break; } } break; case Commands.SensorBinary_Get: SensorBinary.Get(node); break; case Commands.SensorMultiLevel_Get: SensorMultilevel.Get(node); break; case Commands.Meter_Get: // see ZWaveLib Sensor.cs for EnergyMeterScale options int scaleType = 0; int.TryParse(request.GetOption(0), out scaleType); Meter.Get(node, (byte)(scaleType << 0x03)); break; case Commands.Meter_SupportedGet: Meter.GetSupported(node); break; case Commands.Meter_Reset: Meter.Reset(node); break; case Commands.NodeInfo_Get: controller.GetNodeInformationFrame(nodeNumber); returnValue = GetResponseValue(nodeNumber, EventPath_NodeInfo); break; case Commands.Battery_Get: Battery.Get(node); returnValue = GetResponseValue(nodeNumber, EventPath_Battery); break; case Commands.Association_Set: Association.Set(node, (byte)int.Parse(request.GetOption(0)), (byte)int.Parse(request.GetOption(1))); break; case Commands.Association_Get: byte group = (byte)int.Parse(request.GetOption(0)); Association.Get(node, group); returnValue = GetResponseValue(nodeNumber, EventPath_Associations + "." + group); break; case Commands.Association_Remove: Association.Remove(node, (byte)int.Parse(request.GetOption(0)), (byte)int.Parse(request.GetOption(1))); break; case Commands.ManufacturerSpecific_Get: ManufacturerSpecific.Get(node); returnValue = GetResponseValue(nodeNumber, EventPath_ManufacturerSpecific); break; case Commands.Config_ParameterSet: Configuration.Set(node, (byte)int.Parse(request.GetOption(0)), int.Parse(request.GetOption(1))); break; case Commands.Config_ParameterGet: byte position = (byte)int.Parse(request.GetOption(0)); Configuration.Get(node, position); returnValue = GetResponseValue(nodeNumber, EventPath_ConfigVariables + "." + position); break; case Commands.WakeUp_Get: WakeUp.Get(node); returnValue = GetResponseValue(nodeNumber, EventPath_WakeUpInterval); break; case Commands.WakeUp_Set: WakeUp.Set(node, uint.Parse(request.GetOption(0))); break; case Commands.WakeUp_SendToSleep: WakeUp.SendToSleep(node); break; case Commands.WakeUp_GetAlwaysAwake: returnValue = new ResponseText(WakeUp.GetAlwaysAwake(node) ? "1" : "0"); break; case Commands.WakeUp_SetAlwaysAwake: WakeUp.SetAlwaysAwake(node, uint.Parse(request.GetOption(0)) == 1 ? true : false); break; case Commands.Version_Get: returnValue = new ResponseText("ERROR"); CommandClass cclass; Enum.TryParse <CommandClass>(request.GetOption(0), out cclass); if (cclass != CommandClass.NotSet) { var nodeCclass = node.GetCommandClass(cclass); if (nodeCclass != null && nodeCclass.Version != 0) { returnValue = new ResponseText(nodeCclass.Version.ToString()); } else { ZWaveLib.CommandClasses.Version.Get(node, cclass); returnValue = GetResponseValue(nodeNumber, "ZWaveNode.Version." + cclass); } } break; case Commands.Version_GetAll: controller.GetNodeCcsVersion(node); break; case Commands.Control_On: raiseEvent = true; double lastLevel = GetNormalizedValue((double)GetNodeLastLevel(node)); eventValue = lastLevel > 0 ? lastLevel.ToString(CultureInfo.InvariantCulture) : "1"; if (node.SupportCommandClass(CommandClass.SwitchMultilevel)) { SwitchMultilevel.Set(node, 0xFF); } else if (node.SupportCommandClass(CommandClass.SwitchBinary)) { SwitchBinary.Set(node, 0xFF); } else { Basic.Set(node, 0xFF); } SetNodeLevel(node, 0xFF); break; case Commands.Control_Off: raiseEvent = true; eventValue = "0"; if (node.SupportCommandClass(CommandClass.SwitchMultilevel)) { SwitchMultilevel.Set(node, 0x00); } else if (node.SupportCommandClass(CommandClass.SwitchBinary)) { SwitchBinary.Set(node, 0x00); } else { Basic.Set(node, 0x00); } SetNodeLevel(node, 0x00); break; case Commands.Control_Level: { raiseEvent = true; var level = int.Parse(request.GetOption(0)); eventValue = Math.Round(level / 100D, 2).ToString(CultureInfo.InvariantCulture); // the max value should be obtained from node parameters specifications, // here we assume that the commonly used interval is [0-99] for most multilevel switches if (level >= 100) { level = 99; } if (node.SupportCommandClass(CommandClass.SwitchMultilevel)) { SwitchMultilevel.Set(node, (byte)level); } else { Basic.Set(node, (byte)level); } SetNodeLevel(node, (byte)level); } break; case Commands.Control_Toggle: raiseEvent = true; if (GetNodeLevel(node) == 0) { double lastOnLevel = GetNormalizedValue((double)GetNodeLastLevel(node)); eventValue = lastOnLevel > 0 ? lastOnLevel.ToString(CultureInfo.InvariantCulture) : "1"; if (node.SupportCommandClass(CommandClass.SwitchMultilevel)) { SwitchMultilevel.Set(node, 0xFF); } else if (node.SupportCommandClass(CommandClass.SwitchBinary)) { SwitchBinary.Set(node, 0xFF); } else { Basic.Set(node, 0xFF); } SetNodeLevel(node, 0xFF); } else { eventValue = "0"; if (node.SupportCommandClass(CommandClass.SwitchMultilevel)) { SwitchMultilevel.Set(node, 0x00); } else if (node.SupportCommandClass(CommandClass.SwitchBinary)) { SwitchBinary.Set(node, 0x00); } else { Basic.Set(node, 0x00); } SetNodeLevel(node, 0x00); } break; case Commands.Thermostat_ModeGet: ThermostatMode.Get(node); break; case Commands.Thermostat_ModeSet: { ThermostatMode.Value mode = (ThermostatMode.Value)Enum.Parse(typeof(ThermostatMode.Value), request.GetOption(0)); // raiseEvent = true; eventParameter = "Thermostat.Mode"; eventValue = request.GetOption(0); // ThermostatMode.Set(node, mode); } break; case Commands.Thermostat_SetPointGet: { ThermostatSetPoint.Value mode = (ThermostatSetPoint.Value)Enum.Parse(typeof(ThermostatSetPoint.Value), request.GetOption(0)); ThermostatSetPoint.Get(node, mode); } break; case Commands.Thermostat_SetPointSet: { ThermostatSetPoint.Value mode = (ThermostatSetPoint.Value)Enum.Parse(typeof(ThermostatSetPoint.Value), request.GetOption(0)); double temperature = double.Parse(request.GetOption(1).Replace(',', '.'), CultureInfo.InvariantCulture); // raiseEvent = true; eventParameter = "Thermostat.SetPoint." + request.GetOption(0); eventValue = temperature.ToString(CultureInfo.InvariantCulture); // ThermostatSetPoint.Set(node, mode, temperature); } break; case Commands.Thermostat_FanModeGet: ThermostatFanMode.Get(node); break; case Commands.Thermostat_FanModeSet: { ThermostatFanMode.Value mode = (ThermostatFanMode.Value)Enum.Parse(typeof(ThermostatFanMode.Value), request.GetOption(0)); // raiseEvent = true; eventParameter = "Thermostat.FanMode"; eventValue = request.GetOption(0); // ThermostatFanMode.Set(node, mode); } break; case Commands.Thermostat_FanStateGet: ThermostatFanState.Get(node); break; case Commands.Thermostat_OperatingStateGet: ThermostatOperatingState.GetOperatingState(node); break; case Commands.UserCode_Set: byte userId = byte.Parse(request.GetOption(0)); byte userIdStatus = byte.Parse(request.GetOption(1)); byte[] tagCode = ZWaveLib.Utility.HexStringToByteArray(request.GetOption(2)); UserCode.Set(node, new ZWaveLib.Values.UserCodeValue(userId, userIdStatus, tagCode)); break; case Commands.DoorLock_Get: DoorLock.Get(node); returnValue = GetResponseValue(nodeNumber, ModuleEvents.Status_DoorLock); break; case Commands.DoorLock_Set: { DoorLock.Value mode = (DoorLock.Value)Enum.Parse(typeof(DoorLock.Value), request.GetOption(0)); DoorLock.Set(node, mode); } break; } } if (raiseEvent) { //ZWaveNode node = _controller.GetDevice ((byte)int.Parse (nodeid)); OnInterfacePropertyChanged(this.GetDomain(), nodeId, "ZWave Node", eventParameter, eventValue); } // return(returnValue); }