From 25b337cf7bc610ddb528a5f9454aa8cd5f2a5df0 Mon Sep 17 00:00:00 2001 From: Olivier Oeuillot Date: Thu, 6 Oct 2016 16:49:12 +0200 Subject: [PATCH] Add sensors() method --- hue-api/commands/sensors-api.js | 53 +++++++++++++++++++++++++++++++++ hue-api/index.js | 19 +++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 hue-api/commands/sensors-api.js diff --git a/hue-api/commands/sensors-api.js b/hue-api/commands/sensors-api.js new file mode 100644 index 0000000..9456a95 --- /dev/null +++ b/hue-api/commands/sensors-api.js @@ -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//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) +}; \ No newline at end of file diff --git a/hue-api/index.js b/hue-api/index.js index 8ca02a9..a160b69 100644 --- a/hue-api/index.js +++ b/hue-api/index.js @@ -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") @@ -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. * @@ -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);