Skip to content

Instantly share code, notes, and snippets.

@rossnelson
Created June 28, 2021 20:34
Show Gist options
  • Save rossnelson/72c066119816b3b04375c50780b04187 to your computer and use it in GitHub Desktop.
Save rossnelson/72c066119816b3b04375c50780b04187 to your computer and use it in GitHub Desktop.
const repl = require('repl');
const { Driver } = require("zwave-js");
const networkKey = Buffer.from(process.env.HAB_DEVICE_ID.replace(/-/g, '')
.split('')
.slice(1, 17));
// Tell the driver which serial port to use
const driver = new Driver("/dev/zwaveAeotec", {
networkKey,
logConfig: {
enabled: process.env["ZWAVE_LOGS_ENABLED"]
}
});
// You must add a handler for the error event before starting the driver
driver.on("error", (e) => {
// Do something with it
console.error(e);
});
// Listen for the driver ready event before doing anything with the driver
driver.once("driver ready", () => {
/*
Now the controller interview is complete. This means we know which nodes
are included in the network, but they might not be ready yet.
The node interview will continue in the background.
*/
console.log("driver ready");
driver.controller.on("node added", setupNode)
driver.controller.on("node removed", (node) => {
console.log("node removed", node)
})
driver.controller.on("inclusion started", (n, args) => {
console.log("inclusion started", args)
})
driver.controller.on("exclusion started", (n, args) => {
console.log("exclusion started", args)
})
driver.controller.nodes.forEach(setupNode)
});
const setupNode = (node) => {
node.on("value added", (n, args) => {
console.log("value added", args)
})
node.on("value updated", (n, args) => {
console.log("value updated", args)
})
node.on("value removed", (n, args) => {
console.log("value removed", args)
})
node.on("value notification", (n, args) => {
console.log("value notification", args)
})
node.on("notification", (n, ccid, args) => {
console.log("notification", ccid, args)
})
}
// Start the driver. To await this method, put this line into an async method
driver.start();
const server = repl.start({});
server.context.driver = driver;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment