2018-04-18 09:25:40 -07:00
|
|
|
const MQTT = require('./mqtt');
|
|
|
|
const Zigbee = require('./zigbee');
|
2020-01-09 13:47:19 -07:00
|
|
|
const EventBus = require('./eventBus');
|
2018-08-05 09:38:33 -07:00
|
|
|
const State = require('./state');
|
2018-04-18 09:25:40 -07:00
|
|
|
const logger = require('./util/logger');
|
2018-04-18 10:09:59 -07:00
|
|
|
const settings = require('./util/settings');
|
2018-11-16 12:23:11 -07:00
|
|
|
const objectAssignDeep = require('object-assign-deep');
|
2019-03-17 12:04:51 -07:00
|
|
|
const utils = require('./util/utils');
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
// Extensions
|
2019-09-09 10:48:09 -07:00
|
|
|
const ExtensionEntityPublish = require('./extension/entityPublish');
|
|
|
|
const ExtensionDeviceReceive = require('./extension/deviceReceive');
|
2018-08-28 12:55:00 -07:00
|
|
|
const ExtensionNetworkMap = require('./extension/networkMap');
|
2018-10-02 12:15:12 -07:00
|
|
|
const ExtensionSoftReset = require('./extension/softReset');
|
2018-11-16 12:23:11 -07:00
|
|
|
const ExtensionHomeAssistant = require('./extension/homeassistant');
|
|
|
|
const ExtensionDeviceConfigure = require('./extension/deviceConfigure');
|
2019-02-18 10:21:54 -07:00
|
|
|
const ExtensionDeviceGroupMembership = require('./extension/deviceGroupMembership');
|
2018-11-16 12:23:11 -07:00
|
|
|
const ExtensionBridgeConfig = require('./extension/bridgeConfig');
|
2018-12-21 16:07:53 -07:00
|
|
|
const ExtensionGroups = require('./extension/groups');
|
2019-02-13 12:55:14 -07:00
|
|
|
const ExtensionDeviceAvailability = require('./extension/deviceAvailability');
|
|
|
|
const ExtensionDeviceBind = require('./extension/deviceBind');
|
|
|
|
const ExtensionDeviceReport = require('./extension/deviceReport');
|
2019-09-19 11:36:05 -07:00
|
|
|
const ExtensionDeviceEvent = require('./extension/deviceEvent');
|
2020-02-08 11:55:27 -07:00
|
|
|
const ExtensionOTAUpdate = require('./extension/otaUpdate');
|
2018-05-28 12:10:58 -07:00
|
|
|
|
2018-05-17 08:20:46 -07:00
|
|
|
class Controller {
|
2018-04-18 09:25:40 -07:00
|
|
|
constructor() {
|
2018-11-16 12:23:11 -07:00
|
|
|
this.zigbee = new Zigbee();
|
2018-04-18 09:25:40 -07:00
|
|
|
this.mqtt = new MQTT();
|
2020-01-09 13:47:19 -07:00
|
|
|
this.eventBus = new EventBus();
|
2019-05-02 11:14:44 -07:00
|
|
|
this.state = new State();
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2019-02-04 10:36:49 -07:00
|
|
|
this.publishEntityState = this.publishEntityState.bind(this);
|
2019-09-09 10:48:09 -07:00
|
|
|
this.onZigbeeAdapterDisconnected = this.onZigbeeAdapterDisconnected.bind(this);
|
2018-06-11 11:31:05 -07:00
|
|
|
|
2018-08-28 12:55:00 -07:00
|
|
|
// Initialize extensions.
|
|
|
|
this.extensions = [
|
2020-01-09 13:47:19 -07:00
|
|
|
new ExtensionEntityPublish(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
|
|
|
new ExtensionDeviceReceive(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
|
|
|
new ExtensionDeviceGroupMembership(
|
|
|
|
this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus,
|
|
|
|
),
|
|
|
|
new ExtensionDeviceConfigure(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
|
|
|
new ExtensionNetworkMap(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
|
|
|
new ExtensionBridgeConfig(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
|
|
|
new ExtensionGroups(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
|
|
|
new ExtensionDeviceBind(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
|
|
|
new ExtensionDeviceEvent(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
2020-02-08 11:55:27 -07:00
|
|
|
new ExtensionOTAUpdate(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus),
|
2018-08-28 12:55:00 -07:00
|
|
|
];
|
|
|
|
|
2019-02-26 12:21:35 -07:00
|
|
|
if (settings.get().advanced.report) {
|
|
|
|
this.extensions.push(new ExtensionDeviceReport(
|
2020-01-09 13:47:19 -07:00
|
|
|
this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus,
|
2019-02-26 12:21:35 -07:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
if (settings.get().homeassistant) {
|
|
|
|
this.extensions.push(new ExtensionHomeAssistant(
|
2020-01-09 13:47:19 -07:00
|
|
|
this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus,
|
2018-11-16 12:23:11 -07:00
|
|
|
));
|
2018-05-16 10:29:47 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
/* istanbul ignore next */
|
2018-11-16 12:23:11 -07:00
|
|
|
if (settings.get().advanced.soft_reset_timeout !== 0) {
|
|
|
|
this.extensions.push(new ExtensionSoftReset(
|
2020-01-09 13:47:19 -07:00
|
|
|
this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus,
|
2018-11-16 12:23:11 -07:00
|
|
|
));
|
2018-05-28 11:40:30 -07:00
|
|
|
}
|
2018-12-29 11:55:59 -07:00
|
|
|
|
2019-01-29 12:17:56 -07:00
|
|
|
if (settings.get().advanced.availability_timeout) {
|
2019-02-13 12:55:14 -07:00
|
|
|
this.extensions.push(new ExtensionDeviceAvailability(
|
2020-01-09 13:47:19 -07:00
|
|
|
this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus,
|
2018-12-29 11:55:59 -07:00
|
|
|
));
|
|
|
|
}
|
2018-05-16 10:29:47 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async start() {
|
2019-09-25 03:08:39 -07:00
|
|
|
const settingsErrors = settings.validate();
|
|
|
|
if (settingsErrors) {
|
|
|
|
logger.error(`Refusing to start, configuration.yaml is not valid, found the following errors:`);
|
|
|
|
for (const error of settingsErrors) {
|
|
|
|
logger.error(`\t - ${error}`);
|
|
|
|
}
|
|
|
|
logger.error(
|
2019-10-26 09:25:51 -07:00
|
|
|
`If you don't know how to solve this, read https://www.zigbee2mqtt.io/configuration/configuration.html`,
|
2019-09-25 03:08:39 -07:00
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
this.state.start();
|
2018-09-20 12:42:50 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
const info = await utils.getZigbee2mqttVersion();
|
|
|
|
logger.info(`Starting zigbee2mqtt version ${info.version} (commit #${info.commitHash})`);
|
|
|
|
|
|
|
|
// Start zigbee
|
|
|
|
try {
|
2020-01-03 15:43:04 -07:00
|
|
|
await this.zigbee.start();
|
|
|
|
this.callExtensionMethod('onZigbeeStarted', []);
|
2019-09-09 10:48:09 -07:00
|
|
|
this.zigbee.on('event', this.onZigbeeEvent.bind(this));
|
|
|
|
this.zigbee.on('adapterDisconnected', this.onZigbeeAdapterDisconnected);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Failed to start zigbee');
|
|
|
|
logger.error('Exiting...');
|
2019-09-10 13:07:31 -07:00
|
|
|
logger.error(error.stack);
|
2019-09-09 10:48:09 -07:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-09-20 12:42:50 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// Log zigbee clients on startup
|
2019-09-23 13:21:27 -07:00
|
|
|
const devices = this.zigbee.getClients();
|
2018-11-16 12:23:11 -07:00
|
|
|
logger.info(`Currently ${devices.length} devices are joined:`);
|
2019-09-09 10:48:09 -07:00
|
|
|
for (const device of devices) {
|
2019-09-23 13:21:27 -07:00
|
|
|
const entity = this.zigbee.resolveEntity(device);
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.info(
|
|
|
|
(entity.settings ? entity.settings.friendlyName : entity.device.ieeeAddr) +
|
|
|
|
` (${entity.device.ieeeAddr}): ` +
|
|
|
|
(entity.mapped ?
|
|
|
|
`${entity.mapped.model} - ${entity.mapped.vendor} ${entity.mapped.description} ` :
|
|
|
|
'Not supported ') +
|
2019-10-26 09:25:51 -07:00
|
|
|
`(${entity.device.type})`,
|
2019-09-09 10:48:09 -07:00
|
|
|
);
|
|
|
|
}
|
2018-05-21 02:49:02 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
// Enable zigbee join.
|
|
|
|
if (settings.get().permit_join) {
|
|
|
|
logger.warn('`permit_join` set to `true` in configuration.yaml.');
|
|
|
|
logger.warn('Allowing new devices to join.');
|
|
|
|
logger.warn('Set `permit_join` to `false` once you joined all devices.');
|
2018-09-10 09:06:29 -07:00
|
|
|
}
|
|
|
|
|
2020-01-31 14:52:39 -07:00
|
|
|
await this.zigbee.permitJoin(settings.get().permit_join);
|
2018-06-25 11:18:39 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// MQTT
|
|
|
|
this.mqtt.on('message', this.onMQTTMessage.bind(this));
|
|
|
|
await this.mqtt.connect();
|
2018-04-23 12:44:06 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// Send all cached states.
|
2019-09-25 04:15:30 -07:00
|
|
|
if (settings.get().advanced.cache_state) {
|
|
|
|
for (const device of this.zigbee.getClients()) {
|
|
|
|
if (this.state.exists(device.ieeeAddr)) {
|
|
|
|
this.publishEntityState(device.ieeeAddr, this.state.get(device.ieeeAddr));
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
// Call extensions
|
2019-09-09 10:48:09 -07:00
|
|
|
await this.callExtensionMethod('onMQTTConnected', []);
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-04-20 10:53:40 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async stop() {
|
2018-11-16 12:23:11 -07:00
|
|
|
// Call extensions
|
2019-09-09 10:48:09 -07:00
|
|
|
await this.callExtensionMethod('stop', []);
|
2018-06-08 11:20:35 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
// Wrap-up
|
2019-05-02 11:14:44 -07:00
|
|
|
this.state.stop();
|
2019-09-09 10:48:09 -07:00
|
|
|
await this.mqtt.disconnect();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.zigbee.stop();
|
|
|
|
process.exit(0);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Failed to stop zigbee');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onZigbeeAdapterDisconnected() {
|
|
|
|
logger.error('Adapter disconnected, stopping');
|
|
|
|
await this.stop();
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async onZigbeeEvent(type, data) {
|
2019-09-23 13:21:27 -07:00
|
|
|
const entity = this.zigbee.resolveEntity(data.device || data.ieeeAddr);
|
2019-09-25 16:14:58 -07:00
|
|
|
if (data.device && !entity.settings) {
|
2019-09-09 10:48:09 -07:00
|
|
|
// Only deviceLeave doesn't have a device (not interesting to add to settings)
|
|
|
|
entity.settings = settings.addDevice(data.device.ieeeAddr);
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-07-24 09:25:16 -07:00
|
|
|
|
2019-09-25 16:14:58 -07:00
|
|
|
const name = entity && entity.settings ? entity.settings.friendlyName : null;
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
if (type === 'message') {
|
|
|
|
logger.debug(
|
2019-10-01 11:58:08 -07:00
|
|
|
`Received Zigbee message from '${name}', type '${data.type}', cluster '${data.cluster}'` +
|
2019-10-01 11:50:05 -07:00
|
|
|
`, data '${JSON.stringify(data.data)}' from endpoint ${data.endpoint.ID}` +
|
2019-10-26 09:25:51 -07:00
|
|
|
(data.hasOwnProperty('groupID') ? ` with groupID ${data.groupID}` : ``),
|
2019-09-09 10:48:09 -07:00
|
|
|
);
|
|
|
|
} else if (type === 'deviceJoined') {
|
2019-09-25 16:14:58 -07:00
|
|
|
logger.info(`Device '${name}' joined`);
|
|
|
|
this.mqtt.log('device_connected', {friendly_name: name});
|
2019-09-09 10:48:09 -07:00
|
|
|
} else if (type === 'deviceInterview') {
|
|
|
|
if (data.status === 'successful') {
|
2019-09-26 00:30:12 -07:00
|
|
|
logger.info(`Successfully interviewed '${name}', device has successfully been paired`);
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
if (entity.mapped) {
|
|
|
|
const {vendor, description, model} = entity.mapped;
|
|
|
|
logger.info(
|
2019-10-26 09:25:51 -07:00
|
|
|
`Device '${name}' is supported, identified as: ${vendor} ${description} (${model})`,
|
2019-09-09 10:48:09 -07:00
|
|
|
);
|
|
|
|
|
2019-09-25 16:14:58 -07:00
|
|
|
const log = {friendly_name: name, model, vendor, description, supported: true};
|
2019-09-09 10:48:09 -07:00
|
|
|
this.mqtt.log('pairing', 'interview_successful', log);
|
|
|
|
} else {
|
|
|
|
logger.warn(
|
2019-09-25 16:14:58 -07:00
|
|
|
`Device '${name}' with Zigbee model '${data.device.modelID}' is NOT supported, ` +
|
2019-10-26 09:25:51 -07:00
|
|
|
`please follow https://www.zigbee2mqtt.io/how_tos/how_to_support_new_devices.html`,
|
2019-09-09 10:48:09 -07:00
|
|
|
);
|
2019-09-25 16:14:58 -07:00
|
|
|
this.mqtt.log('pairing', 'interview_successful', {friendly_name: name, supported: false});
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
} else if (data.status === 'failed') {
|
2019-09-26 00:30:12 -07:00
|
|
|
logger.error(`Failed to interview '${name}', device has not successfully been paired`);
|
2019-09-25 16:14:58 -07:00
|
|
|
this.mqtt.log('pairing', 'interview_failed', {friendly_name: name});
|
2019-09-09 10:48:09 -07:00
|
|
|
} else {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (data.status === 'started') {
|
2019-09-25 16:14:58 -07:00
|
|
|
logger.info(`Starting interview of '${name}'`);
|
|
|
|
this.mqtt.log('pairing', 'interview_started', {friendly_name: name});
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type === 'deviceAnnounce') {
|
2019-09-25 16:14:58 -07:00
|
|
|
logger.debug(`Device '${name}' announced itself`);
|
2020-03-11 10:21:06 -07:00
|
|
|
this.mqtt.log('device_announced', 'announce', {friendly_name: name});
|
2019-09-09 10:48:09 -07:00
|
|
|
} else {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (type === 'deviceLeave') {
|
2019-09-25 16:14:58 -07:00
|
|
|
logger.warn(`Device '${name || data.ieeeAddr}' left the network`);
|
|
|
|
this.mqtt.log('device_removed', 'left_network', {friendly_name: name || data.ieeeAddr});
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-07-24 09:25:16 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// Call extensions
|
2019-09-25 16:14:58 -07:00
|
|
|
this.callExtensionMethod(
|
|
|
|
'onZigbeeEvent',
|
2019-10-26 09:25:51 -07:00
|
|
|
[type, data, entity ? entity.mapped : null, entity ? entity.settings : null],
|
2019-09-25 16:14:58 -07:00
|
|
|
);
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-07-24 09:25:16 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
onMQTTMessage(payload) {
|
|
|
|
const {topic, message} = payload;
|
|
|
|
logger.debug(`Received MQTT message on '${topic}' with data '${message}'`);
|
|
|
|
|
|
|
|
// Call extensions
|
|
|
|
this.callExtensionMethod('onMQTTMessage', [topic, message]);
|
2018-04-25 11:54:41 -07:00
|
|
|
}
|
|
|
|
|
2019-09-29 05:35:05 -07:00
|
|
|
async publishEntityState(IDorName, payload, stateChangeReason=null) {
|
2019-09-23 13:21:27 -07:00
|
|
|
const entity = this.zigbee.resolveEntity(IDorName);
|
2019-09-12 13:48:23 -07:00
|
|
|
if (!entity || !entity.settings) {
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.error(`'${IDorName}' does not exist, skipping publish`);
|
|
|
|
return;
|
|
|
|
}
|
2018-09-20 12:42:50 -07:00
|
|
|
|
2019-10-23 22:46:19 -07:00
|
|
|
if (entity.type === 'device' && settings.get().advanced.last_seen !== 'disable' && entity.device.lastSeen) {
|
|
|
|
payload.last_seen = utils.formatDate(entity.device.lastSeen, settings.get().advanced.last_seen);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
let messagePayload = {...payload};
|
2019-09-12 13:48:23 -07:00
|
|
|
const currentState = this.state.exists(entity.settings.ID) ? this.state.get(entity.settings.ID) : {};
|
2019-05-02 11:14:44 -07:00
|
|
|
const newState = objectAssignDeep.noMutate(currentState, payload);
|
|
|
|
|
|
|
|
// Update state cache with new state.
|
2019-09-29 05:35:05 -07:00
|
|
|
this.state.set(entity.settings.ID, newState, stateChangeReason);
|
2018-04-21 00:13:14 -07:00
|
|
|
|
2019-05-02 11:14:44 -07:00
|
|
|
if (settings.get().advanced.cache_state) {
|
|
|
|
// Add cached state to payload
|
|
|
|
messagePayload = newState;
|
2018-05-11 10:14:18 -07:00
|
|
|
}
|
2018-04-21 00:13:14 -07:00
|
|
|
|
2020-02-27 13:06:27 -07:00
|
|
|
const deviceOptions = settings.get().device_options;
|
2018-05-15 09:42:26 -07:00
|
|
|
const options = {
|
2020-02-27 13:06:27 -07:00
|
|
|
retain: utils.getObjectsProperty([entity.settings, deviceOptions], 'retain', false),
|
|
|
|
qos: utils.getObjectsProperty([entity.settings, deviceOptions], 'qos', 0),
|
2018-05-15 09:42:26 -07:00
|
|
|
};
|
|
|
|
|
2020-03-12 12:25:37 -07:00
|
|
|
const retention = utils.getObjectsProperty([entity.settings, deviceOptions], 'retention', false);
|
|
|
|
if (retention !== false) {
|
|
|
|
options.properties = {messageExpiryInterval: retention};
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
if (entity.type === 'device' && settings.get().mqtt.include_device_information) {
|
2019-09-23 13:21:27 -07:00
|
|
|
const device = this.zigbee.getDeviceByIeeeAddr(entity.device.ieeeAddr);
|
2019-09-09 10:48:09 -07:00
|
|
|
const attributes = [
|
|
|
|
'ieeeAddr', 'networkAddress', 'type', 'manufacturerID', 'manufacturerName', 'powerSource',
|
|
|
|
'applicationVersion', 'stackVersion', 'zclVersion', 'hardwareVersion', 'dateCode', 'softwareBuildID',
|
|
|
|
];
|
|
|
|
|
2019-12-11 12:15:42 -07:00
|
|
|
messagePayload.device = {
|
|
|
|
friendlyName: entity.name,
|
|
|
|
model: entity.mapped ? entity.mapped.model : 'unknown',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
attributes.forEach((a) => messagePayload.device[a] = device[a]);
|
2018-09-20 12:42:50 -07:00
|
|
|
}
|
|
|
|
|
2020-03-02 12:08:51 -07:00
|
|
|
// filter mqtt message attributes
|
2020-03-03 10:30:54 -07:00
|
|
|
if (deviceOptions.filtered_attributes) {
|
|
|
|
deviceOptions.filtered_attributes.forEach((a) => delete messagePayload[a]);
|
|
|
|
}
|
2020-03-02 12:08:51 -07:00
|
|
|
if (entity.settings.filtered_attributes) {
|
|
|
|
entity.settings.filtered_attributes.forEach((a) => delete messagePayload[a]);
|
|
|
|
}
|
|
|
|
|
2020-02-29 10:07:15 -07:00
|
|
|
this.eventBus.emit('publishEntityState', {payload: messagePayload, entity});
|
|
|
|
|
2019-06-19 09:11:35 -07:00
|
|
|
if (Object.entries(messagePayload).length) {
|
2020-01-12 07:07:06 -07:00
|
|
|
if (settings.get().experimental.output === 'attribute_and_json') {
|
|
|
|
await this.mqtt.publish(entity.name, JSON.stringify(messagePayload), options);
|
|
|
|
await this.iteratePayloadAttributeOutput(`${entity.name}/`, messagePayload, options);
|
|
|
|
} else if (settings.get().experimental.output === 'json') {
|
2019-09-12 13:48:23 -07:00
|
|
|
await this.mqtt.publish(entity.name, JSON.stringify(messagePayload), options);
|
2019-09-09 10:48:09 -07:00
|
|
|
} else {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().experimental.output === 'attribute') {
|
2019-09-12 13:48:23 -07:00
|
|
|
await this.iteratePayloadAttributeOutput(`${entity.name}/`, messagePayload, options);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
2019-06-19 09:11:35 -07:00
|
|
|
}
|
2019-03-04 10:13:36 -07:00
|
|
|
}
|
2018-04-21 00:13:14 -07:00
|
|
|
}
|
2018-06-15 08:48:10 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async iteratePayloadAttributeOutput(topicRoot, payload, options) {
|
|
|
|
for (const [key, value] of Object.entries(payload)) {
|
|
|
|
let subPayload = value;
|
2019-05-28 11:01:46 -07:00
|
|
|
let message;
|
|
|
|
|
|
|
|
// Special cases
|
2019-09-09 10:48:09 -07:00
|
|
|
if (key === 'color' && utils.objectHasProperties(subPayload, ['r', 'g', 'b'])) {
|
2019-05-28 11:01:46 -07:00
|
|
|
subPayload = [subPayload.r, subPayload.g, subPayload.b];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check Array first, since it is also an Object
|
|
|
|
if (Array.isArray(subPayload)) {
|
|
|
|
message = subPayload.map((x) => `${x}`).join(',');
|
|
|
|
} else if (typeof subPayload === 'object') {
|
2019-09-09 10:48:09 -07:00
|
|
|
return this.iteratePayloadAttributeOutput(`${topicRoot}${key}-`, subPayload, options);
|
2019-05-28 11:01:46 -07:00
|
|
|
} else {
|
|
|
|
message = typeof subPayload === 'string' ? subPayload : JSON.stringify(subPayload);
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
await this.mqtt.publish(`${topicRoot}${key}`, message, options);
|
|
|
|
}
|
2019-05-28 11:01:46 -07:00
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
async callExtensionMethod(method, parameters) {
|
|
|
|
for (const extension of this.extensions) {
|
|
|
|
if (extension[method]) {
|
|
|
|
try {
|
|
|
|
await extension[method](...parameters);
|
|
|
|
} catch (error) {
|
|
|
|
/* istanbul ignore next */
|
|
|
|
logger.error(`Failed to call '${extension.constructor.name}' '${method}' (${error.stack})`);
|
|
|
|
/* istanbul ignore next */
|
|
|
|
if (process.env.JEST_WORKER_ID !== undefined) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 08:48:10 -07:00
|
|
|
}
|
2018-04-18 09:25:40 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 08:20:46 -07:00
|
|
|
module.exports = Controller;
|