Skip to content

Commit

Permalink
Example app
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Stene committed Sep 20, 2017
1 parent e738bb8 commit 4b51d3f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 115 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 .
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
109 changes: 2 additions & 107 deletions webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4b51d3f

Please sign in to comment.