Ejemplo n.º 1
0
 public Gateway(string instance, string serialPort)
 {
     InstanceId = instance;
     Controller = new ZWaveController(serialPort);
     Controller.ControllerStatusChanged += OnControllerStatusChanged;
     Controller.DiscoveryProgress       += OnDiscoveryStatusChanged;
 }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            var portName = System.IO.Ports.SerialPort.GetPortNames().Where(element => element != "COM1").First();

            var controller = new ZWaveController(portName);

            //controller.Channel.Log = Console.Out;

            controller.Open();
            try
            {
                Run(controller).Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var inner in ex.InnerExceptions)
                {
                    LogMessage($"{inner}");
                }
            }
            catch (Exception ex)
            {
                LogMessage($"{ex}");
            }
            finally
            {
                Console.ReadLine();
                controller.Close();
            }
        }
Ejemplo n.º 7
0
        public void ZWaveControllerTryStartPairingAlreadyPairing()
        {
            ZWaveController controller = SetupInitializedController();

            _configManager.Raise(s => s.InitializationComplete += null, EventArgs.Empty);

            VerifyInitializedController(controller);

            ZWValueID valueId = new ZWValueID(0, 1, ZWValueID.ValueGenre.Basic, 2, 3, 4, ZWValueID.ValueType.Bool, 5);

            RaiseNotificationNoNodes(ZWNotification.Type.DriverReady, valueId);

            _zWaveManager.Setup(s => s.BeginControllerCommand(2, ZWControllerCommand.AddDevice, false, 0)).Returns(true);

            Mock <IZWavePairingMonitor> monitor = new Mock <IZWavePairingMonitor>(MockBehavior.Strict);

            monitor.Setup(m => m.PairingStarted());

            MockZWaveNode node = new MockZWaveNode();

            Assert.IsTrue(controller.TryStartPairing(node, monitor.Object));

            MockZWaveNode node2 = new MockZWaveNode();

            Assert.IsFalse(controller.TryStartPairing(node2, monitor.Object));

            Assert.IsFalse(controller.IsInitialized);
            _configManager.Verify(s => s.FindAllComponentsOfTypeEvenInvalid <ZWaveNode>(), Times.Exactly(1));
            _zWaveManager.Verify(s => s.BeginControllerCommand(2, ZWControllerCommand.AddDevice, false, 0), Times.Exactly(1));
            monitor.Verify(m => m.PairingStarted(), Times.Exactly(1));
        }
Ejemplo n.º 8
0
        public static async Task Main(string[] args)
        {
            Logging.Factory.Subscribe((message) => WriteConsole(message));

            var port       = new SerialPort(SerialPort.GetPortNames().Where(element => element != "COM1").First());
            var controller = new ZWaveController(port);

            try
            {
                await controller.Open();

                WriteInfo($"Controller Version: {controller.Version}");
                WriteInfo($"Controller ChipType: {controller.ChipType}");
                WriteInfo($"Controller HomeID: {controller.HomeID:X}");
                WriteInfo($"Controller NodeID: {controller.NodeID}");
                WriteLine();
            }
            catch (Exception ex)
            {
                WriteError(ex);
            }
            finally
            {
                await controller.Close();
            }
            Console.ReadLine();
        }
Ejemplo n.º 9
0
        public void Connect()
        {
            try
            {
                if (ZWaveController.IsConnected)
                {
                    return;
                }

                ZWaveController.Connect();
                //TODO: clean this up
                Name    = "ZWave" + ZWaveController.HomeID.ToString();
                Address = Name;
            }
            catch (Exception e)
            {
                if (e.Message.Contains("not licensed"))
                {
                    throw new SdkNotLicencedException();
                }
                throw new HomeAutomationException("Error while connecting to the Z-Wave controller: " + e.Message);
            }

            ScanForDevices();

            //TODO: should this be elsewhere?
            Load();

            Connected();
        }
Ejemplo n.º 10
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();
        }
Ejemplo n.º 11
0
 public void Start()
 {
     _controller = new ZWaveController(new SerialPort(ZWavePortName));
     _controller.ChannelClosed += LogChannelClosed;
     _controller.Error         += LogError;
     _controller.Open();
 }
Ejemplo n.º 12
0
        public void ZWaveControllerInitializationCalledTwice()
        {
            ZWaveController controller = SetupInitializedController();

            _configManager.Raise(s => s.InitializationComplete += null, EventArgs.Empty);

            VerifyInitializedController(controller);
        }
Ejemplo n.º 13
0
        public void ZWaveControllerConstructor()
        {
            Mock <IInternalConfigurationManager> configManager = new Mock <IInternalConfigurationManager>(MockBehavior.Strict);

            ZWaveController controller = new ZWaveController(configManager.Object);

            Assert.IsFalse(controller.IsInitialized);
        }
Ejemplo n.º 14
0
 public ZWave()
 {
     controller = new ZWaveController();
     controller.ControllerStatusChanged += Controller_ControllerStatusChanged;
     controller.DiscoveryProgress       += Controller_DiscoveryProgress;
     controller.HealProgress            += Controller_HealProgress;
     controller.NodeOperationProgress   += Controller_NodeOperationProgress;
     controller.NodeUpdated             += Controller_NodeUpdated;
 }
Ejemplo n.º 15
0
        public void TestMethod1()
        {
            var controller = new ZWaveController("COM3");

            controller.Open();
            var version = controller.GetVersion().Result;

            controller.Close();
        }
Ejemplo n.º 16
0
        private void VerifyInitializedController(ZWaveController controller)
        {
            Assert.IsFalse(controller.IsInitialized);

            _zWaveOptions.Verify(s => s.Create(ConfigPath, ConfigPath, ""), Times.Exactly(1));
            _zWaveOptions.Verify(s => s.Lock(), Times.Exactly(1));
            _zWaveManager.Verify(m => m.Create(), Times.Exactly(1));
            _zWaveManager.Verify(m => m.AddDriver(TestCOMPort, ZWControllerInterface.Serial), Times.Exactly(1));
            _configManager.Verify(c => c.ZWaveCOM, Times.Exactly(1));
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Invoked when the application is launched normally by the end user.  Other entry points
 /// will be used such as when the application is launched to open a specific file.
 /// </summary>
 /// <param name="e">Details about the launch request and process.</param>
 protected override void OnLaunched(LaunchActivatedEventArgs e)
 {
     Task.Run(() =>
     {
         var controller = new ZWaveController(0x0658, 0x0200);
         controller.Open();
         var version = controller.GetVersion().Result;
         controller.Close();
     });
 }
Ejemplo n.º 18
0
        private static ZWaveController CreateController(string portName)
        {
            var zwaveController = new ZWaveController(portName);

            //zwave.Channel.Log = new System.IO.StreamWriter(Console.OpenStandardOutput());
            zwaveController.Channel.MaxRetryCount   = 1;
            zwaveController.Channel.ResponseTimeout = TimeSpan.FromSeconds(10);
            zwaveController.Channel.ReceiveTimeout  = TimeSpan.FromSeconds(10);
            zwaveController.Open();
            return(zwaveController);
        }
Ejemplo n.º 19
0
        private static Task Dump(ZWaveController controller)
        {
            WriteInfo($"Controller");
            WriteSeparator();
            WriteInfo($"Version: {controller.Version}");
            WriteInfo($"ChipType: {controller.ChipType}");
            WriteInfo($"HomeID: {controller.HomeID:X}");
            WriteInfo($"NodeID: {controller.NodeID}");

            return(Task.CompletedTask);
        }
Ejemplo n.º 20
0
        public static void SetSerialPortName(ZWaveController controller, string com_port)
        {
            var port = com_port;

            if (!String.IsNullOrWhiteSpace(port))
            {
                serialPortName      = port;
                controller.PortName = serialPortName;
                controller.Connect();
            }
        }
Ejemplo n.º 21
0
        private static void SetSerialPortName(ZWaveController controller)
        {
            Console.WriteLine("Enter the serial port name (eg. COM7 or /dev/ttyUSB0):");
            var port = Console.ReadLine().Trim();

            if (!String.IsNullOrWhiteSpace(port))
            {
                serialPortName      = port;
                controller.PortName = serialPortName;
                controller.Connect();
            }
        }
Ejemplo n.º 22
0
        public CommandClassService(byte nodeID, byte endpointID, ZWaveController controller, CommandClass commandClass)
        {
            if (nodeID == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(nodeID), nodeID, "nodeID must be greater than 0");
            }

            _nodeID      = nodeID;
            _endpointID  = endpointID;
            Controller   = controller ?? throw new ArgumentNullException(nameof(controller));
            CommandClass = commandClass;
        }
Ejemplo n.º 23
0
        public void ZWaveControllerAllQueriesCompleteSomeDead()
        {
            ZWaveController controller = SetupInitializedController();

            _configManager.Raise(s => s.InitializationComplete += null, EventArgs.Empty);

            VerifyInitializedController(controller);

            RaiseNotificationNoNodes(ZWNotification.Type.AllNodesQueriedSomeDead, new ZWValueID(0, 1, ZWValueID.ValueGenre.Basic, 2, 3, 4, ZWValueID.ValueType.Bool, 5));

            Assert.IsTrue(controller.IsInitialized);
            _configManager.Verify(s => s.FindAllComponentsOfTypeEvenInvalid <ZWaveNode>(), Times.Exactly(1));
        }
Ejemplo n.º 24
0
        public override Device AddDevice()
        {
            var backingDevice = ZWaveController.AddDevice();

            if (backingDevice == null)
            {
                return(null); //TODO: throw exception?
            }
            var device = new ZWaveDevice(this, backingDevice);

            Devices.Add(device);
            return(device);
        }
Ejemplo n.º 25
0
        public override void RemoveDevice(Device device)
        {
            var zWaveDevice = device as ZWaveDevice;

            var result = ZWaveController.RemoveFailedDevice(zWaveDevice.BackingObject);

            if (!result)
            {
                throw new HomeAutomationException("Failed to remove device " + device);
            }

            Devices.Remove(device);
        }
Ejemplo n.º 26
0
 private static void HealNetwork(ZWaveController controller)
 {
     ToggleDebug(true);
     foreach (var node in controller.Nodes)
     {
         Console.ForegroundColor = ConsoleColor.Cyan;
         Console.WriteLine("\nHealing Node {0}", node.Id);
         Console.ForegroundColor = ConsoleColor.White;
         controller.RequestNeighborsUpdateOptions(node.Id);
         controller.RequestNeighborsUpdate(node.Id);
         controller.GetNeighborsRoutingInfo(node.Id);
     }
     ToggleDebug(false);
 }
Ejemplo n.º 27
0
 private static void ListNodes(ZWaveController controller)
 {
     foreach (var node in controller.Nodes)
     {
         Console.ForegroundColor = ConsoleColor.Cyan;
         Console.WriteLine("\nNode {0}", node.Id);
         Console.ForegroundColor = ConsoleColor.White;
         var mspecs = node.ManufacturerSpecific;
         Console.WriteLine("    Manufacturer Specific {0}-{1}-{2}", mspecs.ManufacturerId, mspecs.TypeId, mspecs.ProductId);
         Console.WriteLine("    Basic Type {0}", (GenericType)node.ProtocolInfo.BasicType);
         Console.WriteLine("    Generic Type {0}", (GenericType)node.ProtocolInfo.GenericType);
         Console.WriteLine("    Specific Type {0}", node.ProtocolInfo.SpecificType);
         Console.WriteLine("    Secure Info Frame {0}", BitConverter.ToString(node.SecuredNodeInformationFrame));
         Console.WriteLine("    Info Frame {0}", BitConverter.ToString(node.NodeInformationFrame));
         foreach (var nodeCmdClass in node.CommandClasses)
         {
             var versionInfo = "";
             // TODO: GetCmdClassVersion version is not currently working
             if (node.SupportCommandClass(CommandClass.Version))
             {
                 versionInfo = String.Format("(version {0})", nodeCmdClass.Version);
             }
             if (!Enum.IsDefined(typeof(CommandClass), nodeCmdClass.Id))
             {
                 versionInfo            += " [UNSUPPORTED]";
                 Console.ForegroundColor = ConsoleColor.Red;
             }
             else
             {
                 Console.ForegroundColor = ConsoleColor.White;
             }
             Console.WriteLine("        {0} {1}", nodeCmdClass.CommandClass, versionInfo);
         }
         Console.ForegroundColor = ConsoleColor.White;
         if (node.Version != null)
         {
             Console.WriteLine("    Node Version info:");
             Console.WriteLine("        LibraryType {0}", (node.Version.LibraryType));
             Console.WriteLine("        ProtocolVersion {0}", (node.Version.ProtocolVersion));
             Console.WriteLine("        ProtocolSubVersion {0}", (node.Version.ProtocolSubVersion));
             Console.WriteLine("        ApplicationVersion {0}", (node.Version.ApplicationVersion));
             Console.WriteLine("        ApplicationSubVersion {0}", (node.Version.ApplicationSubVersion));
         }
         if (node.GetData("RoutingInfo") != null)
         {
             Console.WriteLine("    Routing Info {0}", BitConverter.ToString((byte[])node.GetData("RoutingInfo").Value));
         }
     }
     Console.WriteLine("\n");
 }
Ejemplo n.º 28
0
        public void ZWaveControllerNodeRemoved()
        {
            ZWaveController controller = SetupInitializedController();

            _configManager.Raise(s => s.InitializationComplete += null, EventArgs.Empty);

            VerifyInitializedController(controller);

            MockZWaveNode node = RaiseNotification(ZWNotification.Type.NodeRemoved, true, new ZWValueID(0, 1, ZWValueID.ValueGenre.Basic, 2, 3, 4, ZWValueID.ValueType.Bool, 5));

            Assert.IsFalse(controller.IsInitialized);
            Assert.IsFalse(node.IsConnected);
            _configManager.Verify(s => s.FindAllComponentsOfTypeEvenInvalid <ZWaveNode>(), Times.Exactly(1));
        }
Ejemplo n.º 29
0
        public void ZWaveControllerSetByteValueId()
        {
            ZWaveController controller = SetupInitializedController();

            _configManager.Raise(s => s.InitializationComplete += null, EventArgs.Empty);

            VerifyInitializedController(controller);

            ZWValueID valueId = new ZWValueID(1, 2, ZWValueID.ValueGenre.Basic, 3, 4, 5, ZWValueID.ValueType.Byte, 6);

            _zWaveManager.Setup(s => s.SetValue(valueId, 0x25));
            controller.SetValue(valueId, 0x25);
            _zWaveManager.Verify(s => s.SetValue(valueId, 0x25), Times.Exactly(1));
        }
Ejemplo n.º 30
0
        private void TestNoChangeAfterNotification(ZWNotification.Type notificationType)
        {
            ZWaveController controller = SetupInitializedController();

            _configManager.Raise(s => s.InitializationComplete += null, EventArgs.Empty);

            VerifyInitializedController(controller);

            ZWValueID valueId = new ZWValueID(0, 1, ZWValueID.ValueGenre.Basic, 2, 3, 4, ZWValueID.ValueType.Bool, 5);

            RaiseNotificationNoNodes(ZWNotification.Type.ValueChanged, valueId);

            Assert.IsFalse(controller.IsInitialized);
            _configManager.Verify(s => s.FindAllComponentsOfTypeEvenInvalid <ZWaveNode>(), Times.Exactly(1));
        }