commit 011ab550cf105a6c6721b1f582cd2283935cf058 Author: manalejandro Date: Sun Jul 1 13:57:22 2018 +0200 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..bf92ab8 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# TP-Link Bulb Web + +## Install + + $ npm install + or + $ yarn + +## Start + + You can provide the listening port and bulb ip address, if not are defaults + + $ npm start 3000 10.0.0.130 + or + $ yarn start 3000 10.0.0.130 + +![](screen.png) + +## License + +MIT \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..0117385 --- /dev/null +++ b/index.js @@ -0,0 +1,59 @@ +'use strict' + +const TPLSmartDevice = require('tplink-lightbulb'), + express = require('express'), + app = express(), + morgan = require('morgan'), + rfs = require('rotating-file-stream'), + colorsys = require('colorsys'), + accessLogStream = rfs('access.log', { interval: '1d', path: __dirname + '/logs' }), + router = express.Router(), + ip = process.argv[3] || '10.0.0.130', + bulb = new TPLSmartDevice(ip) + +app.use(morgan('combined', { stream: accessLogStream })) + .use('/api', router) + .use(express.static(__dirname + '/public')) + .listen(process.argv[2] || 3000) + +router.use('/on/:brightness', (req, res, next) => { + bulb.power(true, 0, { brightness: parseInt(req.params.brightness) }) + .then(r => res.json(r)) + .catch(console.error) +}) + .use('/on', (req, res, next) => { + bulb.power(true, 0, { brightness: 100 }) + .then(r => res.json(r)) + .catch(console.error) + }) + .use('/off', (req, res, next) => { + bulb.power(false, 0) + .then(r => res.json(r)) + .catch(console.error) + }) + .use('/color/hex/:color', (req, res, next) => { + let color = colorsys.hexToHsl(req.params.color) + bulb.power(true, 0, { + hue: color.h, + saturation: color.s, + brightness: color.l, + color_temp: 0 + }) + .then(r => res.json(r)) + .catch(console.error) + }) + .use('/color/chsb/:colortemp/:hue/:saturation/:brightness', (req, res, next) => { + bulb.power(true, 0, { + color_temp: parseInt(req.params.colortemp), + hue: parseInt(req.params.hue), + saturation: parseInt(req.params.saturation), + brightness: parseInt(req.params.brightness) + }) + .then(r => res.json(r)) + .catch(console.error) + }) + .use('/info', (req, res, next) => { + bulb.info() + .then(r => res.json(r.light_state)) + .catch(console.error) + }) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..5b51521 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "tplink-bulb-web", + "version": "1.0.0", + "private": true, + "readme": "README.md", + "description": "TP-Link Bulb Web", + "main": "index.js", + "scripts": { + "install": "cp node_modules/jquery/dist/jquery.min.js public/assets/js/ && cp node_modules/bootstrap/dist/js/bootstrap.min.js public/assets/js/ && cp node_modules/bootstrap/dist/css/bootstrap.min.css public/assets/css/ && cp node_modules/bootstrap/dist/css/bootstrap.min.css.map public/assets/css/", + "start": "node index.js" + }, + "repository": { + "type": "git", + "url": "git+https://gitlab.com/manalejandro/tplink-bulb-web" + }, + "bugs": { + "url": "https://gitlab.com/manalejandro/tplink-bulb-web/issues" + }, + "author": "manalejandro@gmail.com", + "license": "MIT", + "dependencies": { + "bootstrap": "3.3.7", + "colorsys": "^1.0.21", + "express": "^4.16.3", + "jquery": "^3.3.1", + "morgan": "^1.9.0", + "rotating-file-stream": "^1.3.6", + "tplink-lightbulb": "^1.4.2" + } +} diff --git a/public/assets/css/.gitignore b/public/assets/css/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/public/assets/images/lb130-off.jpg b/public/assets/images/lb130-off.jpg new file mode 100644 index 0000000..49efac5 Binary files /dev/null and b/public/assets/images/lb130-off.jpg differ diff --git a/public/assets/images/lb130.jpg b/public/assets/images/lb130.jpg new file mode 100644 index 0000000..7b132e4 Binary files /dev/null and b/public/assets/images/lb130.jpg differ diff --git a/public/assets/js/main.js b/public/assets/js/main.js new file mode 100644 index 0000000..b438e4a --- /dev/null +++ b/public/assets/js/main.js @@ -0,0 +1,41 @@ +$(document).ready(function () { + function setData(data) { + if (data.on_off) + $('.bulb').attr('src', 'assets/images/lb130.jpg') + else + $('.bulb').attr('src', 'assets/images/lb130-off.jpg') + $('.brightness').text(data.brightness || 0) + $('.hue').text(data.hue || 0) + $('.saturation').text(data.saturation || 0) + $('.colortemp').text(data.color_temp || 0) + $('#brightness').val(data.brightness || 0) + $('#hue').val(data.hue || 0) + $('#saturation').val(data.saturation || 0) + $('#colortemp').val(data.color_temp || 0) + } + function setCHSB() { + $.getJSON('/api/color/chsb/' + $('#colortemp').val() + '/' + $('#hue').val() + '/' + $('#saturation').val() + '/' + $('#brightness').val(), setData) + } + function setColor() { + $.getJSON('/api/color/hex/' + $('#color').val().replace('#', '')) + } + $('.on').on('click', function () { + if (parseInt($('.brightness').text()) > 0) + url = '/api/on/' + $('.brightness').text() + else + url = '/api/on' + $.getJSON(url, setData) + }) + $('.off').on('click', function () { + $.getJSON('/api/off', setData) + }) + $('form#chsb').on('submit', function (event) { + event.preventDefault() + setCHSB() + }) + $('form#colorform').on('submit', function (event) { + event.preventDefault() + setColor() + }) + $.getJSON('/api/info', setData) +}) \ No newline at end of file diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..b941e18 --- /dev/null +++ b/public/index.html @@ -0,0 +1,79 @@ + + + + + TP-Link Bulb Web + + + + + + + +

TP-Link Bulb Web

+
+

+ +

+

+ + +

+

+ Brightness: + + +
+ Hue: + + +
+ Saturation: + + +
+ Color Temp: + + +

+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ +
+

 

+
+
+ +
+ +
+
+ +
+
+ + + \ No newline at end of file diff --git a/screen.png b/screen.png new file mode 100644 index 0000000..4ecd5ee Binary files /dev/null and b/screen.png differ