"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = require("axios"); const constants_1 = require("./constants"); const Robot_1 = require("./Robot"); function isBypassEnabled() { const raw = process.env.JIBO_BYPASS_PORTAL || ""; return raw === "1" || raw.toLowerCase() === "true"; } function getOfflineSerials() { const raw = process.env.JIBO_OFFLINE_SERIALS || process.env.JIBO_OFFLINE_SERIAL || ""; const serials = raw.split(",").map(s => s.trim()).filter(Boolean); return serials.length ? serials : ["offline-jibo"]; } function buildOfflineConnectionConfig() { return { ip: process.env.JIBO_OFFLINE_IP, certPath: process.env.JIBO_OFFLINE_CERT_PATH, certPem: process.env.JIBO_OFFLINE_CERT_PEM, keyPath: process.env.JIBO_OFFLINE_KEY_PATH, keyPem: process.env.JIBO_OFFLINE_KEY_PEM, fingerprint: process.env.JIBO_OFFLINE_FINGERPRINT, skipTls: process.env.JIBO_OFFLINE_SKIP_TLS === '1' || process.env.JIBO_OFFLINE_SKIP_TLS === 'true', }; } /** * @description A reference to a Jibo account. Log in here to get an API handle * to a Jibo robot or robots. * @class Account */ class Account { constructor(creds) { this.clientId = creds.clientId; this._clientSecret = creds.clientSecret; this.email = creds.email; this._password = creds.password; } /** * Log into the account with the credentials provided in the * {@link AccountCreds} * @method Account#login */ async login() { if (isBypassEnabled()) { console.log("[JIBO_BYPASS] login(): bypassing portal.jibo.com token request"); this._accessToken = process.env.JIBO_OFFLINE_ACCESS_TOKEN || "offline-token"; this._tokenType = process.env.JIBO_OFFLINE_TOKEN_TYPE || "Bearer"; return; } if (this._accessToken) { this.logout(); } const getTokenUri = `https://${constants_1.ENDPOINT}/token`; const body = { grant_type: "password", client_id: this.clientId, client_secret: this._clientSecret, username: this.email, password: this._password, }; return axios_1.default.post(getTokenUri, body).then(res => { this._accessToken = res.data.access_token; this._tokenType = res.data.token_type; }).catch(err => { throw new Error(err.message); }); } /** * Get an API handle for each robot associated with the account * @method Account#getRobots * @returns {Promise} */ async getRobots() { if (isBypassEnabled()) { console.log("[JIBO_BYPASS] getRobots(): returning offline robot handles"); const tokenType = this._tokenType || process.env.JIBO_OFFLINE_TOKEN_TYPE || "Bearer"; const accessToken = this._accessToken || process.env.JIBO_OFFLINE_ACCESS_TOKEN || "offline-token"; const config = buildOfflineConnectionConfig(); return getOfflineSerials().map(serial => { const robot = new Robot_1.Robot(serial, tokenType, accessToken); if (typeof robot.configureDirectConnection === "function") { robot.configureDirectConnection(config); } return robot; }); } if (!this._accessToken) { throw new Error('Not logged in'); } const getRobotListUri = `https://${constants_1.ENDPOINT}/rom/v1/robots`; return axios_1.default.get(getRobotListUri, { headers: { "Authorization": `${this._tokenType} ${this._accessToken}` }, }) .then(res => res.data.data.map(data => new Robot_1.Robot(data.robotName, this._tokenType, this._accessToken))) .catch(err => { throw new Error(err.message); }); } /** * Log out from the account * @method Account#logout */ logout() { // Don't throw an error if this is called in a late cleanup and this // falls out of scope if (this) { this._accessToken = null; this._tokenType = null; } } } exports.Account = Account; //# sourceMappingURL=Account.js.map