node-kobold-control
Version:
Vorwerk Kobold API for VR300
58 lines (51 loc) • 1.86 kB
JavaScript
var api = require(__dirname + '/api');
var robot = require(__dirname + '/robot');
var crypto = require('crypto');
function Client(t) {
this._baseUrl = 'https://beehive.ksecosys.com';
this._token = t;
this._tokenType = 'Token token=';
}
Client.prototype.setToken = function (token) {
this._token = token;
this._tokenType = "Auth0Bearer "
};
Client.prototype.authorize = function (token, callback) {
this._token = token;
this._tokenType = "Auth0Bearer "
callback();
};
Client.prototype.getRobots = function (callback) {
if (this._token) {
api.request(this._baseUrl + '/dashboard', null, 'GET', {Authorization: this._tokenType + this._token}, null, (function (error, body) {
if (!error && body && body.robots) {
var robots = [];
for (var i = 0; i < body.robots.length; i++) {
robots.push(new robot(body.robots[i].name, body.robots[i].serial, body.robots[i].secret_key, this._tokenType + this._token));
}
callback(null, robots);
} else {
if (typeof callback === 'function') {
if (error) {
callback(error);
} else if (body.message) {
callback(body.message);
} else {
callback('unkown error');
}
}
}
}).bind(this));
} else {
if (typeof callback === 'function') {
callback('not authorized');
}
}
};
Client.prototype.reauthorize = function (email, password, callback) {
Client.authorize(email, password, true, callback);
};
function randomValueHex(len) {
return crypto.randomBytes(Math.ceil(len / 2)).toString('hex').slice(0, len);
}
module.exports = Client;