Home

Morten Nielsen edited this page Jun 5, 2017 · 9 revisions
Clone this wiki locally

Welcome to the openzwave-dotnet-uwp wiki!

For more full examples, check out the sample apps.

Compiling from scratch? See Compiling-OpenZWave

Getting Started

The API reference can be found here: (https://openzwave.github.io/openzwave-dotnet-uwp/doc/)

Installing

Package can be referenced via NuGet:

PM> Install-Package OpenZWave -Pre

Note that .NET applications must be set to compile for x86 and not AnyCPU or x64 (this does not apply to UWP apps which also supports x64 and ARM).

Deploying configuration files

If using NuGet, ignore this.

It's important that you deploy all the XML configuration files from \open-zwave\config**. into the /config/ folder of your app. The sample apps uses a DeployConfigFiles.targets file to do this (via msbuild) by including this in your project:

  <Import Project="DeployConfigFiles.targets" />

Note, that if you use the nuget package this will automatically happen, as this .targets file is automatically included.

Initializing the OpenZWave Library

//Initialize
ZWMOptions.Instance.Initialize(); //Configure default options
ZWMOptions.Instance.Lock();       //Options must be locked before using
ZWManager.Instance.Initialize();  //Start up the manager

Opening a serial port

.NET

var availablePorts = System.IO.Ports.SerialPort.GetPortNames();
var serialPort = availablePorts.First().Id; //Adjust to pick the right port for your usb stick
ZWManager.Instance.AddDriver(serialPort); //Add the serial port (you can have multiple!)

UWP

In UWP apps you must first enable the Serial Port capability. To do this, you must add the following section to the Capabilities part of the Package.appxmanifest:

  <Capabilities>
    <DeviceCapability Name="serialcommunication">
      <Device Id="any">
        <Function Type="name:serialPort" />
      </Device>
    </DeviceCapability>
  </Capabilities>

Next is finding the serial port using the DeviceInformation APIs, and then add the driver

//Hook up the serial port
var serialPortSelector = Windows.Devices.SerialCommunication.SerialDevice.GetDeviceSelector();
var devices = await DeviceInformation.FindAllAsync(serialPortSelector);
var serialPort = devices.First().Id; //Adjust to pick the right port for your usb stick
ZWManager.Instance.AddDriver(serialPort); //Add the serial port (you can have multiple!)

Note: If you are running on IoT Core, an OpenZWave-to-AllJoyn bridge is installed by default, and might already be using the serial port. If that is the case, stop the OpenZWave-AllJoyn Bridge service first.

Listening for devices

ZWManager.Instance.NotificationReceived += OnNodeNotification; //Start listening for node events

The rest is in the Notification handler. Every time a node is found, changed, removed etc. an event is reported here, including responses to commands you send. Nodes are identified by the HomeID (one per usb controller connected), and by the NodeID. You use these two values to uniquely identify a node on your network, and can then perform operations like changing properties via the ZWManager instance.

private void OnNodeNotification(ZWManager manager, NotificationReceivedEventArgs e)
{
    var notification = e.Notification;
    var homeID = notification.HomeId;
    var nodeId = notification.NodeId;
    var type = notification.Type;
}

Sending commands

TODO