Skip to content

Commit

Permalink
Added support of state GET calls to API (/api/:username)
Browse files Browse the repository at this point in the history
  • Loading branch information
datech committed Jan 13, 2019
1 parent 0788cd7 commit da71618
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
8 changes: 4 additions & 4 deletions api/hue/templates/lights/all.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
{{#.}}
{{#lights}}
"{{ id }}": {
"state": {
"on": {{ on }},
Expand All @@ -19,7 +19,7 @@
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2018-01-02T19:24:20"
"lastinstall": "{{ date }}"
},
"type": "Extended color light",
"name": "{{ name }}",
Expand Down Expand Up @@ -61,9 +61,9 @@
"function": "mixed",
"direction": "omnidirectional"
},
"uniqueid": "00:17:88:01:00:bd:c7:b9-0b",
"uniqueid": "00:11:22:33:44:55:66:77-88",
"swversion": "5.105.0.21169"
},
{{/.}}
{{/lights}}
"last": {}
}
2 changes: 1 addition & 1 deletion api/hue/templates/lights/get-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2018-01-02T19:24:20"
"lastinstall": "{{ date }}"
},
"type": "Extended color light",
"name": "{{ name }}",
Expand Down
47 changes: 47 additions & 0 deletions api/hue/templates/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"lights": {{> lightsTemplate }},
"groups":{
},
"config":{
"name":"Philips hue",
"mac":"00:00:00:aa:bb:cc",
"dhcp":true,
"ipaddress":"{{ address }}",
"netmask":"0.0.0.0",
"gateway":"0.0.0.0",
"proxyaddress":"",
"proxyport":0,
"UTC":"{{ date }}",
"whitelist":{
"{{ username }}":{
"last use date":"{{ date }}",
"create date":"{{ date }}",
"name":"Echo"
}
},
"swversion":"01003372",
"swupdate":{
"updatestate":0,
"url":"",
"text":"",
"notify":false
},
"linkbutton":false,
"portalservices":false
},
"swupdate2":{
"checkforupdate":false,
"lastchange":"{{ date }}",
"bridge":{
"state":"noupdates",
"lastinstall":"{{ date }}"
},
"state":"noupdates",
"autoinstall":{
"updatetime":"T14:00:00",
"on":false
}
},
"schedules":{
}
}
48 changes: 33 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ module.exports = function(RED) {

hubNode.on('input', function(msg) {

if (config.enableinput && "deviceid" in msg.payload && msg.payload.deviceid !== null){
if (config.enableinput && "nodeid" in msg.payload && msg.payload.nodeid !== null){
msg.payload.deviceid = formatUUID(msg.payload.nodeid);
delete msg.payload["nodeid"];
setDeviceAttributes(msg.payload.deviceid, msg.payload, hubNode.context());
payloadHandler(hubNode, msg.payload.deviceid);
}
Expand Down Expand Up @@ -128,28 +130,30 @@ module.exports = function(RED) {
res.json(output);
});

app.get('/api/c6260f982b43a226b5542b967f612ce', function (req, res) {
var template = fs.readFileSync(__dirname + '/api/hue/templates/registration.json', 'utf8').toString();
app.get('/api/:username', function (req, res) {
var lightsTemplate = fs.readFileSync(__dirname + '/api/hue/templates/lights/all.json', 'utf8').toString();
var template = fs.readFileSync(__dirname + '/api/hue/templates/state.json', 'utf8').toString();

var data = {
username: "c6260f982b43a226b5542b967f612ce"
};
lights: getDevicesAttributes(hubNode.context()),
address: req.hostname,
username: req.params.username,
date: new Date().toISOString().split('.').shift()
}

var output = Mustache.render(template, data);
var output = Mustache.render(template, data, {lightsTemplate: lightsTemplate});
output = JSON.parse(output);
delete output.lights.last;

res.json(output);
});

app.get('/api/c6260f982b43a226b5542b967f612ce/lights', function (req, res) {
app.get('/api/:username/lights', function (req, res) {
var template = fs.readFileSync(__dirname + '/api/hue/templates/lights/all.json', 'utf8').toString();

var data = [];
var devices = getDevices();

for (var key in devices) {
var attributes = getDeviceAttributes(devices[key].id, hubNode.context());
data.push(Object.assign({}, attributes, devices[key]));
var data = {
lights: getDevicesAttributes(hubNode.context()),
date: new Date().toISOString().split('.').shift()
}

var output = Mustache.render(template, data);
Expand All @@ -159,7 +163,7 @@ module.exports = function(RED) {
res.json(output);
});

app.get('/api/c6260f982b43a226b5542b967f612ce/lights/:id', function (req, res) {
app.get('/api/:username/lights/:id', function (req, res) {
var template = fs.readFileSync(__dirname + '/api/hue/templates/lights/get-state.json', 'utf8').toString();

var deviceName = "";
Expand All @@ -171,14 +175,15 @@ module.exports = function(RED) {

var data = getDeviceAttributes(req.params.id, hubNode.context());
data.name = deviceName;
data.date = new Date().toISOString().split('.').shift();

var output = Mustache.render(template, data);
output = JSON.parse(output);

res.json(output);
});

app.put('/api/c6260f982b43a226b5542b967f612ce/lights/:id/state', function (req, res) {
app.put('/api/:username/lights/:id/state', function (req, res) {

setDeviceAttributes(req.params.id, req.body, hubNode.context());

Expand Down Expand Up @@ -320,6 +325,19 @@ module.exports = function(RED) {
return getOrDefault(id, defaultAttributes, context);
}

function getDevicesAttributes(context) {

var devices = getDevices();
var devicesAttributes = [];

for (var key in devices) {
var attributes = getDeviceAttributes(devices[key].id, context);
devicesAttributes.push(Object.assign({}, attributes, devices[key]));
}

return devicesAttributes;
}

function setDeviceAttributes(id, attributes, context) {

var currentAttributes = getDeviceAttributes(id, context);
Expand Down

0 comments on commit da71618

Please sign in to comment.