From 9e670dfbc0549d00865f2f112fbbc2fceb3750cc Mon Sep 17 00:00:00 2001 From: Damien Angelos Date: Sat, 16 May 2020 17:32:17 -0400 Subject: [PATCH] fix issue with sending a command --- api.js | 2 +- controller.js | 24 ++++++++++++++---------- package.json | 2 +- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/api.js b/api.js index e8a5bd0..b8e25e7 100644 --- a/api.js +++ b/api.js @@ -136,7 +136,7 @@ module.exports = class Api { * @param {string} token - Auth token * @param {string} switchId - Id of switch to receive action * @param {string} action - Command action - * @param {string} longPress -Is action a long press + * @param {string} longPress - Is action a long press */ async requestSendCommand({token, switchId, action, longPress}) { const requestId = uuidv4().toUpperCase(); diff --git a/controller.js b/controller.js index 12c5dcd..9d4104a 100644 --- a/controller.js +++ b/controller.js @@ -3,7 +3,7 @@ const snakeCase = require('lodash/snakeCase'); const mqtt = require('mqtt'); const Api = require('./api'); -const appConfig = require('/config/config.json'); +const appConfig = require('./config.json'); const client = mqtt.connect(process.env.MQTT_HOST, { username: process.env.MQTT_USER, @@ -30,7 +30,7 @@ client.on('connect', () => { client.publish('caavo/availability', 'online', {qos: 1, retain: true}); client.subscribe('caavo/living_room/update_state'); - client.subscribe('caavo/living_room/command/*', {qos: 2}); + client.subscribe('caavo/living_room/command', {qos: 2}); }); client.on('message', async (topic, message) => { @@ -39,8 +39,8 @@ client.on('message', async (topic, message) => { case 'caavo/living_room/update_state': return await fetchHubState({switchId: appConfig.switches['living_room']}); case 'caavo/living_room/command': - const command = JSON.parse(+(message.toString())); - return await sendCommand(command); + const {action} = JSON.parse(message.toString()); + return await sendCommand({action, switchId: appConfig.switches['living_room']}); } console.warn('No handler for topic: %s', topic); @@ -59,12 +59,16 @@ const fetchHubState = throttle(async ({switchId}) => { * * @param {{action: {string}}}} request */ -const sendCommand = async ({action}) => { - console.info('Processing request to send command'); - - const response = await api.sendCommand({action}); - - response && notifyStateChange(response); +const sendCommand = async ({action, switchId}) => { + console.info(`Processing request to send command with action: ${action}`); + + try { + await api.sendCommand({action, switchId}); + setTimeout(() => fetchHubState({switchId}), 5000); + setTimeout(() => fetchHubState({switchId}), 10000); + } catch (error) { + console.error(`Unable to perform command, action: ${action}, error: ${error}`); + } }; /** diff --git a/package.json b/package.json index b0091ef..a6e4603 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bostaunieux/caavo-mqtt-bridge", - "version": "0.2.0", + "version": "0.3.0", "description": "Node service for controlling a caavo hub", "author": "bostaunieux", "license": "ISC",