From 4b51d3f1931fcaacc5da2dd6b33f3af526dca9b4 Mon Sep 17 00:00:00 2001 From: Henrik Stene Date: Wed, 20 Sep 2017 17:10:52 +0200 Subject: [PATCH] Example app --- README.md | 4 +- package.json | 11 +++--- webapp.js | 109 +-------------------------------------------------- 3 files changed, 9 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 4fa2f08..1eaeb20 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# hue-service -Service to control hue functionality +# example-service +Service skeleton # Build docker build -t home-server-[c86|ARM] -f [x86|ARM]/Dockerfile . diff --git a/package.json b/package.json index e334629..32afd23 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,24 @@ { - "name": "hue-service", + "name": "example-service", "version": "1.0.0", - "description": "Service app to run hue api services", + "description": "Service app to run example services", "main": "webapp.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "git+https://github.com/steam0/hue-service.git" + "url": "git+https://github.com/steam0/example-service.git" }, "author": "Henrik Stene", "license": "ISC", "bugs": { - "url": "https://github.com/steam0/hue-service/issues" + "url": "https://github.com/steam0/example-service/issues" }, - "homepage": "https://github.com/steam0/hue-service#readme", + "homepage": "https://github.com/steam0/example-service#readme", "dependencies": { "cors": "^2.7.1", "express": "^4.3.2", - "node-hue-api": "2.4.2", "path": "^0.12.7" } } diff --git a/webapp.js b/webapp.js index 18cc9ef..0ad3940 100644 --- a/webapp.js +++ b/webapp.js @@ -11,112 +11,7 @@ var server = app.listen(3000, function() { console.log('Server is listening at http://%s:%s', host, port); }); -/* Hue API */ -var hue = require('node-hue-api'); -var HueApi = hue.HueApi; - -/* Hue API Keys */ -var hueAPIkey = "352b12401b73b8af87f56902124609f" -var internalip = "10.0.1.3" -var externalip = "84.208.132.246:9050" -var hueip = internalip - -// Use this -var api = new HueApi(hueip, hueAPIkey); - -/* Hue REST API */ - -/** - * GET all groups - */ -app.get('/groups', function (request, response) { - console.log("GET all groups"); - console.log(request.query); - - api.groups(function(err, result) { - if (err) hue_error(err); - - // Debug - console.log(result) - response.status(200) - response.end(JSON.stringify(result)) - }); -}); - -/** - * GET a single group - * { - * id: 1 - * } - */ -app.get('/group', function (request, response) { - console.log("GET single group"); +app.get('/test', function (request, response) { + console.log("Testing auth"); console.log(request.query); - - var id = request.query.id - - api.getGroup(id, function(err, result) { - if (err) hue_error(err); - - response.status(200) - response.end(JSON.stringify(result)) - }); }); - -/** - * POST a state to a specific group - * { - * id: 1, - * state: 0, - * brightness: 255 - * } - */ -app.put('/group', function (request, response) { - console.log(request.query) - - var id = request.query.id; - var brightness = request.query.brightness; - - if (request.query.on === "true") { - var state = hue.lightState.create().on().brightness(brightness); - } else { - var state = hue.lightState.create().off().brightness(brightness); - } - - console.log(state); - - api.setGroupLightState(id, state, function (err, lights) { - if (err) { - hue_error(response, 404); - } - - response.status(200) - response.end(JSON.stringify(lights)) - }); -}); - - -/* - * What we need: - * - Get all lights - * + Get all groups - * + Get single group - * - Set state of light - * + Set state of group - * - Schedule light - * - Schedule group - * - Add new light - * - Remove light - */ - - function hue_error(response, errorCode) { - var error = "Unknown error"; - - if (errorCode == 500) { - error = "Internal Server Error - Request received but Hue Hub is not reachable" - } else if (errorCode == 404) { - error = "Not found - The requested element was not found" - } - console.log(errorCode + ": " + error) - response.status(errorCode).send(error); - }