Skip to content

Instantly share code, notes, and snippets.

@artoliukkonen
Last active April 21, 2020 12:11
Show Gist options
  • Save artoliukkonen/7a17a1b9c0c487505f3effe1339f4b9b to your computer and use it in GitHub Desktop.
Save artoliukkonen/7a17a1b9c0c487505f3effe1339f4b9b to your computer and use it in GitHub Desktop.
var cors = require('cors')
var fs = require('fs')
var bodyParser = require('body-parser')
var express = require('express'),
app = express(),
port = process.env.PORT || 8080;
app.use(cors())
app.use(bodyParser.json());
app.listen(port)
app.put('/brightness', async (req, res) => {
const { device, brightness } = req.body;
const light = devices[device].lightList[0];
light.setBrightness(brightness, 2);
res.send('ok');
});
app.put('/toggle', async (req, res) => {
const { device } = req.body;
const light = devices[device].lightList ? devices[device].lightList[0] : devices[device].plugList[0];
light.toggle();
res.send('ok');
});
app.put('/on', async (req, res) => {
const { device } = req.body;
const light = devices[device].lightList ? devices[device].lightList[0] : devices[device].plugList[0];
light.turnOn();
res.send('ok');
});
app.put('/off', async (req, res) => {
const { device } = req.body;
const light = devices[device].lightList ? devices[device].lightList[0] : devices[device].plugList[0];
light.turnOff();
res.send('ok');
});
app.get('/list', async (req, res) => {
res.send({ lightbulbs: { ...lightbulbs }, plugs: { ...plugs } });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment