From f017166639ceb123ceb08c76f36accf2eae3f880 Mon Sep 17 00:00:00 2001 From: Tim Hellhake Date: Sun, 30 May 2021 01:17:28 +0200 Subject: [PATCH] Add option to deactivate probing and specify stick manually (#308) --- manifest.json | 25 ++++++++- src/index.js | 145 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 116 insertions(+), 54 deletions(-) diff --git a/manifest.json b/manifest.json index b8e06f1..403f1f4 100644 --- a/manifest.json +++ b/manifest.json @@ -21,7 +21,8 @@ "allowFTDISerial": false, "allowAMASerial": false, "showAging": false, - "debug": "" + "debug": "", + "probing": true }, "schema": { "type": "object", @@ -48,6 +49,28 @@ "title": "Show Aging", "description": "experimental - Creates an additional 'Last seen' property to show when each device was last active on the Zigbee network" }, + "probing": { + "type": "boolean", + "title": "Automatic probing of the serial ports" + }, + "sticks": { + "type": "array", + "title": "List of ZigBee sticks to use", + "items": { + "type": "object", + "title": "ZigBee Stick", + "required": ["type", "port"], + "properties": { + "type": { + "type": "string", + "enum": ["xbee", "conbee", "zstack"] + }, + "port": { + "type": "string" + } + } + } + }, "zigbee2mqtt": { "title": "Zigbee2Mqtt", "type": "object", diff --git a/src/index.js b/src/index.js index 81aec90..e0cdca9 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ const { Database } = require('gateway-addon'); const manifest = require('./manifest.json'); const SerialProber = require('serial-prober'); const { Zigbee2MqttDriver } = require('./zigbee2mqtt/zigbee2mqtt-driver'); +const SerialPort = require('serialport'); const XBEE_FTDI_FILTER = { // Devices like the UartSBee, use a generic FTDI chip and with @@ -225,62 +226,100 @@ async function loadZigbeeAdapters(addonManager, _, errorCallback) { zigbee2mqttConfigured = true; } - const { DEBUG_serialProber } = require('./zb-debug').default; - SerialProber.debug(DEBUG_serialProber); - if (allowFTDISerial) { - xbeeSerialProber.param.filter.push(XBEE_FTDI_FILTER); - } - if (allowAMASerial) { - conbeeSerialProber.param.allowAMASerial = true; - } - SerialProber.probeAll(PROBERS) - .then((matches) => { - if (matches.length == 0) { - SerialProber.listAll() - .then(() => { - if (!zigbee2mqttConfigured) { - errorCallback(manifest.id, 'No Zigbee dongle found'); - } else { - console.debug('No Zigbee dongle found'); - } - }) - .catch((err) => { - if (!zigbee2mqttConfigured) { - errorCallback(manifest.id, err); - } else { - console.debug(`Could not probe serial ports: ${err}`); - } - }); - return; + for (const stick of config.sticks || []) { + console.log(`Creating ${stick.type} driver for ${stick.port}`); + + switch (stick.type) { + case 'xbee': { + const XBeeDriver = require('./driver/xbee'); + const serialPort = new SerialPort(stick.port, { + baudRate: 9600, + lock: true, + }); + new XBeeDriver(addonManager, config, stick.port, serialPort); + break; } - // We put the driver requires here rather than at the top of - // the file so that the debug config gets initialized before we - // import the driver class. - const XBeeDriver = require('./driver/xbee'); - const ConBeeDriver = require('./driver/conbee'); - const ZStackDriver = require('./driver/zstack'); - const driver = { - [xbeeSerialProber.param.name]: XBeeDriver, - [conbeeSerialProber.param.name]: ConBeeDriver, - [cc2531SerialProber.param.name]: ZStackDriver, - [conbeeNewerFirmwareSerialProber.param.name]: ConBeeDriver, - }; - for (const match of matches) { - new driver[match.prober.param.name]( - addonManager, - config, - match.port.path, - match.serialPort - ); + case 'conbee': { + const ConBeeDriver = require('./driver/conbee'); + const serialPort = new SerialPort(stick.port, { + baudRate: 38400, + lock: true, + }); + new ConBeeDriver(addonManager, config, stick.port, serialPort); + break; } - }) - .catch((err) => { - if (!zigbee2mqttConfigured) { - errorCallback(manifest.id, err); - } else { - console.debug(`Could not load serial drivers: ${err}`); + case 'zstack': { + const ZStackDriver = require('./driver/zstack'); + const serialPort = new SerialPort(stick.port, { + baudRate: 115200, + lock: true, + }); + new ZStackDriver(addonManager, config, stick.port, serialPort); + break; } - }); + } + } + + if (config.probing) { + console.log('Probing serial ports'); + + const { DEBUG_serialProber } = require('./zb-debug').default; + SerialProber.debug(DEBUG_serialProber); + if (allowFTDISerial) { + xbeeSerialProber.param.filter.push(XBEE_FTDI_FILTER); + } + if (allowAMASerial) { + conbeeSerialProber.param.allowAMASerial = true; + } + SerialProber.probeAll(PROBERS) + .then((matches) => { + if (matches.length == 0) { + SerialProber.listAll() + .then(() => { + if (!zigbee2mqttConfigured) { + errorCallback(manifest.id, 'No Zigbee dongle found'); + } else { + console.debug('No Zigbee dongle found'); + } + }) + .catch((err) => { + if (!zigbee2mqttConfigured) { + errorCallback(manifest.id, err); + } else { + console.debug(`Could not probe serial ports: ${err}`); + } + }); + return; + } + // We put the driver requires here rather than at the top of + // the file so that the debug config gets initialized before we + // import the driver class. + const XBeeDriver = require('./driver/xbee'); + const ConBeeDriver = require('./driver/conbee'); + const ZStackDriver = require('./driver/zstack'); + const driver = { + [xbeeSerialProber.param.name]: XBeeDriver, + [conbeeSerialProber.param.name]: ConBeeDriver, + [cc2531SerialProber.param.name]: ZStackDriver, + [conbeeNewerFirmwareSerialProber.param.name]: ConBeeDriver, + }; + for (const match of matches) { + new driver[match.prober.param.name]( + addonManager, + config, + match.port.path, + match.serialPort + ); + } + }) + .catch((err) => { + if (!zigbee2mqttConfigured) { + errorCallback(manifest.id, err); + } else { + console.debug(`Could not load serial drivers: ${err}`); + } + }); + } new Zigbee2MqttDriver(addonManager, config); }