Пример #1
0
        public async Task <IEnumerable <TOut> > List()
        {
            var switches = new List <TOut>();

            foreach (var node in await zwave.GetNodes())
            {
                var protocolInfo = await node.GetProtocolInfo();

                switch (protocolInfo.GenericType)
                {
                case GenericType.SwitchMultiLevel:
                case GenericType.SwitchBinary:
                    switches.Add(new TOut()
                    {
                        Id        = node.NodeID.ToString(),
                        Value     = "",
                        Bridge    = config.ComPort,
                        Subsystem = SubsystemName,
                        Name      = $"{SubsystemName} Node {node.NodeID}"
                    });
                    break;
                }
            }

            return(switches);
        }
Пример #2
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var controller = new ZWaveController("COM5");

            controller.Open();

            controller.Channel.Log = Console.Out;

            var setLearnMode = await controller.SetLearnMode(true);

            var result = await controller.GetNodes();

            var wallPlug = new WallPlug(result.Single(a => a.NodeID == 3));
            await wallPlug.SwitchOn();

            await Task.Delay(500);

            await wallPlug.SwitchOff();


            await Task.Delay(500);

            await wallPlug.SwitchOn();

            Console.ReadLine();
        }
Пример #3
0
        public async Task Start()
        {
            if (nodeStates.Count == 0)
            {
                Console.WriteLine("Looks like we are initialising for the first time, this might take a while...");
                Console.WriteLine();
            }

            var homeId = await z.GetHomeID();

            var nodeId = await z.GetNodeID();

            Console.WriteLine($"Home ID: {homeId:x8}, Controller ID: {nodeId:D3}");
            Console.WriteLine();

            nodes = await z.GetNodes();
            await ListNodes(nodes);
            await QueryNodes(nodes);

            SaveState();
            SubscribeAll(nodes);

            HealthCheckLoop().DoNotAwait();

            Console.WriteLine("Startup finished");
        }
Пример #4
0
        static private async Task Run(ZWaveController controller)
        {
            LogMessage($"Version: {await controller.GetVersion()}");
            LogMessage($"HomeID: {await controller.GetHomeID():X}");

            var controllerNodeID = await controller.GetNodeID();

            LogMessage($"ControllerID: {controllerNodeID:D3}");

            var nodes = await controller.GetNodes();

            foreach (var node in nodes)
            {
                var protocolInfo = await node.GetProtocolInfo();

                LogMessage($"Node: {node}, Generic = {protocolInfo.GenericType}, Basic = {protocolInfo.BasicType}, Listening = {protocolInfo.IsListening} ");

                var neighbours = await node.GetNeighbours();

                LogMessage($"Node: {node}, Neighbours = {string.Join(", ", neighbours.Cast<object>().ToArray())}");

                // subcribe to changes
                Subscribe(node);
            }

            Console.ReadLine();
        }
Пример #5
0
        public async Task SensorAlarm()
        {
            // the nodeID of the motion sensor
            byte motionSensorID = 5;

            // create the controller
            var controller = new ZWaveController("COM1");

            // open the controller
            controller.Open();

            // get the included nodes
            var nodes = await controller.GetNodes();

            // get the motionSensor
            var motionSensor = nodes[motionSensorID];

            // get the SensorAlarm commandclass
            var sensorAlarm = motionSensor.GetCommandClass <SensorAlarm>();

            // subscribe to alarm event
            sensorAlarm.Changed += (s, e) => Console.WriteLine("Alarm");

            // wait
            Console.ReadLine();

            // close the controller
            controller.Close();
        }
Пример #6
0
        public async Task TurnWallPlugOn()
        {
            // the nodeID of the wallplug
            byte wallPlugNodeID = 3;

            // create the controller
            var controller = new ZWaveController("COM1");

            // open the controller
            controller.Open();

            // get the included nodes
            var nodes = await controller.GetNodes();

            // get the wallplug
            var wallPlug = nodes[wallPlugNodeID];

            // get the SwitchBinary commandclass
            var switchBinary = wallPlug.GetCommandClass <SwitchBinary>();

            // turn wallplug on
            await switchBinary.Set(true);

            // close the controller
            controller.Close();
        }
        public async Task <IEnumerable <TOut> > List()
        {
            var results = new List <TOut>();

            foreach (var node in await zwave.GetNodes())
            {
                var protocolInfo = await node.GetProtocolInfo();

                switch (protocolInfo.GenericType)
                {
                case GenericType.Thermostat:
                    var stat = await Get(config.ComPort, node.NodeID.ToString());

                    results.Add(stat);
                    break;
                }
            }

            return(results);
        }
Пример #8
0
        static private async Task Run(ZWaveController controller)
        {
            LogMessage($"Version: {await controller.GetVersion()}");
            LogMessage($"HomeID: {await controller.GetHomeID():X}");

            var controllerNodeID = await controller.GetNodeID();

            LogMessage($"ControllerID: {controllerNodeID:D3}");

            var nodes = await controller.GetNodes();

            foreach (var node in nodes)
            {
                var protocolInfo = await node.GetProtocolInfo();

                LogMessage($"Node: {node}, Generic = {protocolInfo.GenericType}, Basic = {protocolInfo.BasicType}, Listening = {protocolInfo.IsListening} ");

                var neighbours = await node.GetNeighbours();

                LogMessage($"Node: {node}, Neighbours = {string.Join(", ", neighbours.Cast<object>().ToArray())}");

                // subcribe to changes
                Subscribe(node);
            }


            //await InitializeWallPlug(nodes[2]);
            //await InitializeWallPlug(nodes[3]);
            //await InitializeShockSensor(nodes[4]);
            //await InitializeGarageDoorSensor(nodes[5]);
            //await InitializeThermostat(nodes[6]);
            //await InitializeMultiSensor(nodes[18]);
            //await InitializeDoorSensor(nodes[10]);
            //await InitializePowerSwitch(nodes[19]);
            //await InitializePowerSwitch(nodes[20]);
            //await InitializePowerSwitch(nodes[24]);
            //await InitializePowerSwitch(nodes[25]);

            //await InitializeMultiSensor(nodes[26]);
            await InitializeShutter(nodes[27]);

            //var neighborUpdateStatus = await nodes[19].RequestNeighborUpdate((status) => LogMessage($"RequestNeighborUpdate: {status} "));

            Console.ReadLine();
        }
Пример #9
0
 private async Task <Node> GetNode(byte nodeId)
 {
     return((await zwave.GetNodes()).First(n => n.NodeID == nodeId));
 }
Пример #10
0
        public async Task <IEnumerable <CommandClassReportView> > GetSupportedCommandClasses(byte id)
        {
            var nodes = await zwave.GetNodes();

            var supported = await nodes[id].GetSupportedCommandClasses();

            return(supported.Select(i => new CommandClassReportView()
            {
                Class = i.Class,
                Version = i.Version,
                NodeId = i.Node.NodeID
            }));
        }
Пример #11
0
        public async Task <Message> Invoke(string nodeID, string commandClassName, string operationName)
        {
            var context = WebOperationContext.Current;

            // Example: http://localhost:80/api/v1.0/controller/nodes/19/switchbinary/set/?value=true
            // nodeID = 19, commandClassName = switchbinary, operationName = set, query = "value=true"

            // get query: ["value"] = "true"
            var queryParameters = context.IncomingRequest.UriTemplateMatch.QueryParameters;

            // we need to execute: Controller.GetNodes()[nodeID].GetCommandClass<commandClassName>().operationName(p1, p2, p3, ...);
            // so lets have fun with reflection

            // get all the nodes
            var nodes = await Controller.GetNodes();

            // get the node
            var node = nodes[byte.Parse(nodeID)];

            if (node == null)
            {
                return(CreateErrorResponse(context, $"Node: {nodeID} not found."));
            }


            // get the commandclasstype (case insensitive match)
            var commandClassType = typeof(CommandClassBase).Assembly.GetExportedTypes().FirstOrDefault(element => element.IsSubclassOf(typeof(CommandClassBase)) && string.Compare(element.Name, commandClassName, true) == 0);

            if (commandClassType == null)
            {
                return(CreateErrorResponse(context, $"CommandClass: {commandClassName} not found."));
            }

            // get the method: GetCommandClass<commandClassName>()
            var getCommandClassMethod = typeof(Node).GetMethod(nameof(Node.GetCommandClass)).MakeGenericMethod(commandClassType);

            if (getCommandClassMethod == null)
            {
                return(CreateErrorResponse(context, $"Node: {nodeID} does not support CommandClass: {commandClassName}."));
            }

            // invoke method to get the commandclass instance
            var commandClass = getCommandClassMethod.Invoke(node, null);

            // get the commandClassName.operationName method
            var invokeMethod = commandClass.GetType().GetMethods().FirstOrDefault(element => string.Compare(element.Name, operationName, true) == 0);

            // process arguments
            var argumentValues = new List <object>();

            // get the parameters of the method
            foreach (var parameter in invokeMethod.GetParameters())
            {
                // find matching parameter in querey
                var queryParameterName = queryParameters.Keys.Cast <string>().FirstOrDefault(element => string.Compare(element, parameter.Name, true) == 0);

                // found?
                if (queryParameterName != null)
                {
                    // yes, so get value
                    var queryParameterValue = queryParameters[queryParameterName];

                    // is it a enum?
                    if (parameter.ParameterType.IsEnum)
                    {
                        // yes, so convert string to enum
                        var enumValue = Enum.Parse(parameter.ParameterType, queryParameterValue, true);

                        // and add typed parameter to collection
                        argumentValues.Add(enumValue);

                        // done
                        continue;
                    }

                    // convert string to typed parameter
                    var argumentValue = Convert.ChangeType(queryParameterValue, parameter.ParameterType, CultureInfo.InvariantCulture);

                    // and add typed parameter to collection
                    argumentValues.Add(argumentValue);
                }
            }

            // invoke het commandclass method: commandClass.operationName(p1, p2, p3, ...)
            var returnValue = invokeMethod.Invoke(commandClass, argumentValues.ToArray());

            // do we have a return value
            if (returnValue != null)
            {
                // is the return value a Task
                if (returnValue is Task)
                {
                    // yes, so await for the task to complete
                    await((Task)returnValue);

                    // if the task is a generic Task<T> then the task has a result
                    if (invokeMethod.ReturnType.IsGenericType)
                    {
                        // get the result property
                        var resultProperty = returnValue.GetType().GetProperty(nameof(Task <object> .Result));
                        // get the result value
                        var resultValue = resultProperty.GetValue(returnValue);

                        // return response
                        return(CreateValidResponse(context, resultValue));
                    }

                    // return response
                    return(CreateNullResponse(context));
                }

                // return response
                return(CreateValidResponse(context, returnValue));
            }

            return(CreateNullResponse(context));
        }
Пример #12
0
        static async Task Main(string[] args)
        {
            // create the controller
            var controller = new ZWaveController("COM3");

            // open the controller
            controller.Open();

            foreach (var node in await controller.GetNodes())
            {
                Console.WriteLine(node.NodeID);

                var protocolInfo = await node.GetProtocolInfo();

                Console.WriteLine(protocolInfo.BasicType);
                Console.WriteLine(protocolInfo.GenericType);

                var cces = await node.GetSupportedCommandClasses();

                foreach (var cc in cces)
                {
                    Console.WriteLine($"{cc.Class} {cc.Version}");
                }

                if (protocolInfo.GenericType == GenericType.Thermostat)
                {
                    var mode = node.GetCommandClass <ThermostatMode>();
                    //await mode.Set(ThermostatModeValue.Auto);
                    var modeReport = await mode.Get();

                    Console.WriteLine($"Thermostat Mode: {modeReport.Mode}");

                    var fanMode       = node.GetCommandClass <ThermostatFanMode>();
                    var fanModeReport = await fanMode.Get();

                    Console.WriteLine($"Fan Mode: {fanModeReport.Mode}");

                    var setpoint = node.GetCommandClass <ThermostatSetpoint>();
                    //await setpoint.Set(ThermostatSetpointType.Cooling, FloatToCentigrade(85));
                    var coolingReport = await setpoint.Get(ThermostatSetpointType.Cooling);

                    Console.WriteLine($"Cooling Setpoint: {coolingReport.Value}");
                    //await setpoint.Set(ThermostatSetpointType.Heating, FloatToCentigrade(65));
                    var heatingReport = await setpoint.Get(ThermostatSetpointType.Heating);

                    Console.WriteLine($"Heating Setpoint: {heatingReport.Value}");

                    var battery       = node.GetCommandClass <Battery>();
                    var batteryReport = await battery.Get();

                    Console.WriteLine($"Battery: {batteryReport.Value}");

                    var sensor = node.GetCommandClass <SensorMultiLevel>();
                    var supportedSensorReport = await sensor.GetSupportedSensors();

                    foreach (var sensorInfo in supportedSensorReport.SupportedSensorTypes)
                    {
                        var value = await sensor.Get(sensorInfo);

                        Console.WriteLine($"{sensorInfo}: {value.Value} {value.Unit}");
                    }

                    var operatingState       = node.GetCommandClass <ThermostatOperatingState>();
                    var operatingStateReport = await operatingState.Get();

                    Console.WriteLine($"Operating State: {operatingStateReport.Value}");

                    var fanState       = node.GetCommandClass <ThermostatFanState>();
                    var fanStateReport = await fanState.Get();

                    Console.WriteLine($"Fan State: {fanStateReport.Value}");
                }

                if (protocolInfo.GenericType == GenericType.SwitchBinary)
                {
                    var setpoint = node.GetCommandClass <SwitchBinary>();
                    //await setpoint.Set(false);
                    var setpointReport = await setpoint.Get();

                    Console.WriteLine(setpointReport.Value);
                }

                Console.WriteLine();
            }
        }