示例#1
0
        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);
        }