Skip to content

Commit c9bc446

Browse files
committed
Add functionality for controlling each light individually (#69)
1 parent af6c345 commit c9bc446

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

scripts/lights.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,57 @@ const fetch = require('node-fetch');
1313
const openFaas = require('../lib/openfaas');
1414
const logger = require('../lib/log');
1515

16-
function sendCommand(command) {
16+
function sendCommand(command, light = '') {
1717
payload = {
1818
command,
19+
light,
1920
};
2021

2122
return openFaas('office-lights-api', {
2223
method: 'POST',
24+
headers: { 'Content-Type': 'application/json' },
2325
body: JSON.stringify(payload),
2426
}).then((response) => {
2527
return;
2628
});
2729
}
2830

2931
module.exports = (robot) => {
30-
robot.respond(/lights off/i, (msg) => {
32+
robot.respond(/lights off( .+)?/i, (msg) => {
3133
logger.log(msg);
3234
const send = msg.send.bind(msg);
33-
sendCommand('force_power_off').catch((error) => send(error.message));
35+
const light = msg.match[1] ? msg.match[1].trim() : '';
36+
sendCommand('force_power_off', light).catch((error) => send(error.message));
3437
robot.adapter.client.web.reactions.add('mobile_phone_off', {
3538
channel: msg.message.room,
3639
timestamp: msg.message.id,
3740
});
3841
});
39-
robot.respond(/lights on/i, (msg) => {
42+
robot.respond(/lights on( .+)?/i, (msg) => {
4043
logger.log(msg);
4144
const send = msg.send.bind(msg);
42-
sendCommand('force_power_on').catch((error) => send(error.message));
45+
const light = msg.match[1] ? msg.match[1].trim() : '';
46+
sendCommand('force_power_on', light).catch((error) => send(error.message));
4347
robot.adapter.client.web.reactions.add('bulb', {
4448
channel: msg.message.room,
4549
timestamp: msg.message.id,
4650
});
4751
});
48-
robot.respond(/lights lock/i, (msg) => {
52+
robot.respond(/lights lock( .+)?/i, (msg) => {
4953
logger.log(msg);
5054
const send = msg.send.bind(msg);
51-
sendCommand('power_lock').catch((error) => send(error.message));
55+
const light = msg.match[1] ? msg.match[1].trim() : '';
56+
sendCommand('power_lock', light).catch((error) => send(error.message));
5257
robot.adapter.client.web.reactions.add('lock', {
5358
channel: msg.message.room,
5459
timestamp: msg.message.id,
5560
});
5661
});
57-
robot.respond(/lights unlock/i, (msg) => {
62+
robot.respond(/lights unlock( .+)?/i, (msg) => {
5863
logger.log(msg);
5964
const send = msg.send.bind(msg);
60-
sendCommand('power_unlock').catch((error) => send(error.message));
65+
const light = msg.match[1] ? msg.match[1].trim() : '';
66+
sendCommand('power_unlock', light).catch((error) => send(error.message));
6167
robot.adapter.client.web.reactions.add('unlock', {
6268
channel: msg.message.room,
6369
timestamp: msg.message.id,

0 commit comments

Comments
 (0)