Skip to content

Commit

Permalink
Add option to deactivate probing and specify stick manually (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hellhake committed May 29, 2021
1 parent 31e3a7e commit f017166
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 54 deletions.
25 changes: 24 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"allowFTDISerial": false,
"allowAMASerial": false,
"showAging": false,
"debug": ""
"debug": "",
"probing": true
},
"schema": {
"type": "object",
Expand All @@ -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",
Expand Down
145 changes: 92 additions & 53 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit f017166

Please sign in to comment.