Skip to content

Commit

Permalink
Add functionality for controlling each light individually (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
falbru committed May 10, 2023
1 parent af6c345 commit c9bc446
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions scripts/lights.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,57 @@ const fetch = require('node-fetch');
const openFaas = require('../lib/openfaas');
const logger = require('../lib/log');

function sendCommand(command) {
function sendCommand(command, light = '') {
payload = {
command,
light,
};

return openFaas('office-lights-api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
}).then((response) => {
return;
});
}

module.exports = (robot) => {
robot.respond(/lights off/i, (msg) => {
robot.respond(/lights off( .+)?/i, (msg) => {
logger.log(msg);
const send = msg.send.bind(msg);
sendCommand('force_power_off').catch((error) => send(error.message));
const light = msg.match[1] ? msg.match[1].trim() : '';
sendCommand('force_power_off', light).catch((error) => send(error.message));
robot.adapter.client.web.reactions.add('mobile_phone_off', {
channel: msg.message.room,
timestamp: msg.message.id,
});
});
robot.respond(/lights on/i, (msg) => {
robot.respond(/lights on( .+)?/i, (msg) => {
logger.log(msg);
const send = msg.send.bind(msg);
sendCommand('force_power_on').catch((error) => send(error.message));
const light = msg.match[1] ? msg.match[1].trim() : '';
sendCommand('force_power_on', light).catch((error) => send(error.message));
robot.adapter.client.web.reactions.add('bulb', {
channel: msg.message.room,
timestamp: msg.message.id,
});
});
robot.respond(/lights lock/i, (msg) => {
robot.respond(/lights lock( .+)?/i, (msg) => {
logger.log(msg);
const send = msg.send.bind(msg);
sendCommand('power_lock').catch((error) => send(error.message));
const light = msg.match[1] ? msg.match[1].trim() : '';
sendCommand('power_lock', light).catch((error) => send(error.message));
robot.adapter.client.web.reactions.add('lock', {
channel: msg.message.room,
timestamp: msg.message.id,
});
});
robot.respond(/lights unlock/i, (msg) => {
robot.respond(/lights unlock( .+)?/i, (msg) => {
logger.log(msg);
const send = msg.send.bind(msg);
sendCommand('power_unlock').catch((error) => send(error.message));
const light = msg.match[1] ? msg.match[1].trim() : '';
sendCommand('power_unlock', light).catch((error) => send(error.message));
robot.adapter.client.web.reactions.add('unlock', {
channel: msg.message.room,
timestamp: msg.message.id,
Expand Down

0 comments on commit c9bc446

Please sign in to comment.