Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
fix issue with sending a command
Browse files Browse the repository at this point in the history
  • Loading branch information
bostaunieux committed May 16, 2020
1 parent 19eaf0e commit 9e670df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 14 additions & 10 deletions controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -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}`);
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 9e670df

Please sign in to comment.