Skip to content

Commit

Permalink
Merge pull request #95 from oeuillot/master
Browse files Browse the repository at this point in the history
Basic sensor support
  • Loading branch information
peter-murray authored Oct 10, 2016
2 parents 4820f9c + 25b337c commit c3f3727
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
53 changes: 53 additions & 0 deletions hue-api/commands/sensors-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

//
// The Documented Phillips Hue Bridge API for lights http://developers.meethue.com/1_lightsapi.html
//
// This module wraps up all the functionality for the definition and basic processing of the parameters for the API
// so that it can be called from the httpPromise module.
//
// The benefits of keeping all this code here is that it is much simpler to update the keep in step with the Phillips
// Hue API documentation, than having it scatter piece meal through various other classes and functions.
//

var Trait = require("traits").Trait
, deepExtend = require("deep-extend")
, tApiMethod = require("./traits/tApiMethod")
, tDescription = require("./traits/tDescription")
, tBodyArguments = require("./traits/tBodyArguments")
, tLightStateBody = require("./traits/tLightStateBody")
, tPostProcessing = require("./traits/tPostProcessing")
, ApiError = require("../errors").ApiError
, utils = require("../utils")
;

var apiTraits = {};


//TODO tie this into the API definition as a post processing step, then apply it via the http.invoke()
function buildSensorsResult(result) {
var sensors = [];

if (result) {
Object.keys(result).forEach(function (id) {
sensors.push(deepExtend({id: id}, result[id]));
});
}
return {"sensors": sensors};
}

apiTraits.getAllSensors = Trait.compose(
tApiMethod(
"/api/<username>/sensors",
"GET",
"1.0",
"Whitelist"
),
tDescription("Gets a list of all sensors that have been discovered by the bridge."),
tPostProcessing(buildSensorsResult)
);


module.exports = {
"getAllSensors": Trait.create(Object.prototype, apiTraits.getAllSensors)
};
19 changes: 18 additions & 1 deletion hue-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Q = require("q")
, ApiError = require("./errors").ApiError
, utils = require("./utils")
, lightsApi = require("./commands/lights-api")
, sensorsApi = require("./commands/sensors-api")
, groupsApi = require("./commands/groups-api")
, schedulesApi = require("./commands/schedules-api")
, scenesApi = require("./commands/scenes-api")
Expand Down Expand Up @@ -211,6 +212,22 @@ HueApi.prototype.registeredUsers = function (cb) {
HueApi.prototype.getRegisteredUsers = HueApi.prototype.registeredUsers;


/**
* Obtains the details of the individual sensors that are attached to the Philips Hue.
*
* @param cb An optional callback function to use if you do not want a promise returned.
* @return A promise that will be provided with the lights object, or {null} if a callback function was provided.
*/
HueApi.prototype.sensors = function (cb) {
var options = this._defaultOptions(),
promise;

promise = http.invoke(sensorsApi.getAllSensors, options);

return utils.promiseOrCallback(promise, cb);
};
HueApi.prototype.getSensors = HueApi.prototype.sensors;

/**
* Obtains the details of the individual lights that are attached to the Philips Hue.
*
Expand All @@ -219,7 +236,7 @@ HueApi.prototype.getRegisteredUsers = HueApi.prototype.registeredUsers;
*/
HueApi.prototype.lights = function (cb) {
var options = this._defaultOptions(),
promise;
promise;

promise = http.invoke(lightsApi.getAllLights, options);

Expand Down

0 comments on commit c3f3727

Please sign in to comment.