UNPKG

nanoleaf-cove

Version:
160 lines (159 loc) 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var axios_1 = tslib_1.__importDefault(require("axios")); var utils_1 = tslib_1.__importDefault(require("./utils")); var fetch_cove_1 = require("fetch-cove"); var NanoleafApi = (function () { function NanoleafApi(config) { this._config = { host: "", authToken: null, timeout: 10000, httpTimeout: 4000, }; this.authorize = function (host) { return new Promise(function (resolve, reject) { host = utils_1.default.checkHost(host); if (host) this._config.host = host; this.axiosInstance .post(this._config.host + "/api/v1/new") .then(function (res) { if (res.status === 200 && res.data && res.data.auth_token) { this._config.authToken = res.data.auth_token; resolve(res.data.auth_token); } else { reject("Could not authorize", res); } }.bind(this)) .catch(function (err) { reject(err); }); }.bind(this)); }; this.updateNanoleafApi = function (host, timeout) { host = utils_1.default.checkHost(host); if (host) this._config.host = host; if (timeout) this._config.timeout = timeout; return this; }; this.unAuthorize = function (host, authToken) { host = utils_1.default.checkHost(host); if (host) this._config.host = host; return this.axiosInstance.delete(this._config.host + "/api/v1/" + authToken); }; this.getAllInfo = function () { return this.axiosInstance.get(this._config.host + "/api/v1/" + this._config.authToken); }; this.setLightState = function (state, fetchConfig) { if (fetchConfig === void 0) { fetchConfig = null; } if (state && state._values) state = state._values; var shouldWait = false; if (fetchConfig && fetchConfig.shouldWait) { shouldWait = true; } return fetch_cove_1.fetchWork({ url: this._config.host + "/api/v1/" + this._config.authToken + "/state", data: { method: "PUT", body: state, }, shouldWait: shouldWait, }); }; this.getAllEffects = function () { return this.axiosInstance.get(this._config.host + "/api/v1/" + this._config.authToken + "/effects/effectsList"); }; this.getAllScenes = this.getAllEffects; this.activateEffect = function (effect, fetchConfig) { if (fetchConfig === void 0) { fetchConfig = null; } var payload = { select: effect }; var shouldWait = false; if (fetchConfig && fetchConfig.shouldWait) { shouldWait = true; } return fetch_cove_1.fetchWork({ url: this._config.host + "/api/v1/" + this._config.authToken + "/effects", data: { method: "PUT", body: payload, }, shouldWait: shouldWait, }); }; this.activateScene = this.activateEffect; this.write = function (payload, fetchConfig) { if (fetchConfig === void 0) { fetchConfig = null; } var shouldWait = false; if (fetchConfig && fetchConfig.shouldWait) { shouldWait = true; } return fetch_cove_1.fetchWork({ url: this._config.host + "/api/v1/" + this._config.authToken + "/effects", data: { method: "PUT", body: { write: payload }, }, shouldWait: shouldWait, }); }; this.setStreamingMode = function () { return utils_1.default.setStreamingMode(this._config.host, this._config.username); }; this.writeCustomLights = function (config) { var packed = []; for (var _i = 0, _a = config.panels; _i < _a.length; _i++) { var panel = _a[_i]; var brightness = utils_1.default.isTruly(panel.brightness) ? panel.brightness : utils_1.default.isTruly(config.brightness) ? config.brightness : 100; var color = panel.color ? panel.color : config.defaultColor ? config.defaultColor : [0, 0, 0]; if (!Array.isArray(color)) { color = [color.r, color.g, color.b]; } color = utils_1.default.brightnessConvert(color, brightness); var transition = utils_1.default.isTruly(panel.transition) ? panel.transition : config.transition ? config.transition : 0; transition = transition / 100; var id = parseInt(typeof panel === "object" ? panel.id : panel, 10); packed = tslib_1.__spreadArrays(packed, [id, 1, color[0], color[1], color[2], 0, transition]); } packed = tslib_1.__spreadArrays([config.panels.length], packed); return this.write({ command: "display", version: "1.0", animType: "static", animData: packed.join(" "), loop: false, palette: [], colorType: "HSB", }); }; if (config === null || config === void 0 ? void 0 : config.host) { config.host = utils_1.default.checkHost(config.host); } this.axiosInstance = axios_1.default.create(); this.axiosInstance.defaults.timeout = (config === null || config === void 0 ? void 0 : config.httpTimeout) || 4000; this._config = tslib_1.__assign(tslib_1.__assign({}, this._config), config); } return NanoleafApi; }()); exports.default = NanoleafApi;