Example Client Code

This is an example of using the WZWave library in a Maven-ized Java project.

The POM

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>wzwave-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
      <dependency>
        <groupId>com.whizzosoftware</groupId>
        <artifactId>wzwave</artifactId>
        <version>0.0.3</version>
      </dependency>
      <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.21.Final</version>
        <scope>runtime</scope>
      </dependency>
      <dependency>
        <groupId>org.rxtx</groupId>
        <artifactId>rxtx</artifactId>
        <version>2.1.7</version>
        <scope>runtime</scope>
      </dependency>
    </dependencies>
</project>

The Test Class

The test class will do nothing more than print what devices have been discovered on the Z-Wave network as well as when they report an update.

package com.test;
import com.whizzosoftware.wzwave.controller.ZWaveController;
import com.whizzosoftware.wzwave.controller.ZWaveControllerListener;
import com.whizzosoftware.wzwave.controller.netty.NettyZWaveController;
import com.whizzosoftware.wzwave.node.ZWaveEndpoint;
public class WZWaveTest implements ZWaveControllerListener {
private ZWaveController controller;
   public WZWaveTest() {
controller = new NettyZWaveController("/dev/tty.SLAB_USBtoUART");
controller.setListener(this);
controller.start();
}
   public void onZWaveNodeAdded(ZWaveEndpoint node) {
System.out.println("Z-Wave node added: " + node.getNodeId());
}
   public void onZWaveNodeUpdated(ZWaveEndpoint node) {
System.out.println("Z-Wave node updated: " + node.getNodeId());
}
   public void onZWaveConnectionFailure(Throwable t) {
System.out.println("Something bad happened: " + t);
}
   public static void main(String[] args) {
new WZWaveTest();
}
}

Getting Command Class Values

Devices on a Z-Wave network are represented as nodes. Each Z-Wave node has a set of command classes that represent their features and current state. By retrieving the Z-Wave command class(s) from a ZWaveEndpoint object, you can access this information. All Z-Wave nodes are supposed to have the Basic Command Class which is a sort of catch-all variable that means different things to different devices. For example, a light switch would most likely use the Basic Command Class to represent whether it was on or off (values: 0xFF or 0x00).

To get the current value for a node's Basic Command Class:

ZWaveEndpoint node = ...
BasicCommandClass bcc = (BasicCommandClass)node.getCommandClass(BasicCommandClass.ID);
System.out.println(bcc.getValue());

Setting Command Class Values

In order to set the value for a node, you create a DataFrame object using a specific CommandClass class. You then send this DataFrame to the Z-Wave network via the ZWaveController object.

For example, let's say we have a light switch on our Z-Wave network as node 2 and we want to turn it on:

ZWaveController controller = ...
controller.sendDataFrame(BasicCommandClass.createSetv1((byte)0x02, (byte)0xFF));

Note that command classes are versioned and a device may support more than one version of the same command class. Thus, we call the createSetv1 method to indicate we want to create a DataFrame for version 1 of the Basic Command Class's set function.