node-hue-api
Version:
Philips Hue API Library for Node.js
98 lines (97 loc) • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Users = void 0;
const ApiDefinition_1 = require("./http/ApiDefinition");
const configuration_1 = require("./http/endpoints/configuration");
class Users extends ApiDefinition_1.ApiDefinition {
constructor(hueApi) {
super(hueApi);
}
getAll() {
return getAllUsersAsArray(this);
}
getUser(username) {
return this.getAll()
.then(users => {
if (users.length > 0) {
const filtered = users.filter(user => {
return user.username === username;
});
if (filtered) {
return filtered[0];
}
}
return undefined;
});
}
// /**
// * @deprecated Use getUserByName(username) instead
// * @param username {string}
// */
// get(username) {
// util.deprecatedFunction('5.x', 'users.get(username)', 'Use users.getUser(username) instead.');
// return this.getUser(username);
// }
getUserByName(appName, deviceName) {
let nameToMatch;
if (deviceName) {
nameToMatch = `${appName}#${deviceName}`;
}
else {
nameToMatch = appName;
}
return getAllUsersAsArray(this)
.then(users => {
return users.filter(user => user.name === nameToMatch);
});
}
// /**
// * @deprecated use getUserByName(appName, deviceName) instead.
// * @param appName {string}
// * @param deviceName {string}
// * @returns {Promise<Object[]>}
// */
// getByName(appName, deviceName) {
// util.deprecatedFunction('5.x', 'users.getByName(appName, deviceName)', 'Use users.getUserByName(appName, deviceName) instead.');
// return this.getUserByName(appName, deviceName);
// }
createUser(appName, deviceName) {
return this.hueApi.getCachedState()
.then((state) => {
//TODO may need to combine the modelid and API version, but am assuming that all newer bridges are kept up to
// date, as do not know the specific version number of the introduction of the generateclientkey parameter.
// Default to always reporting an old bridge if we cannot get the state
let oldBridge = true;
if (state) {
oldBridge = state.modelid === 'BSB001';
}
return this.execute(configuration_1.configurationApi.createUser, {
appName: appName,
deviceName: deviceName,
generateKey: !oldBridge
});
});
}
deleteUser(username) {
return this.execute(configuration_1.configurationApi.deleteUser, { element: username });
}
}
exports.Users = Users;
function getAllUsers(api) {
return api.execute(configuration_1.configurationApi.getConfiguration)
.then((config) => {
return config.whitelist || undefined;
});
}
function getAllUsersAsArray(api) {
return getAllUsers(api)
.then((users) => {
const results = [];
if (users) {
Object.keys(users).forEach(username => {
results.push(Object.assign({ username: username }, users[username]));
});
}
return results;
});
}