UNPKG

browser-sdk

Version:

javascript SDK for the lightelligence-platform

157 lines (125 loc) 5.65 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _apiService = _interopRequireDefault(require("../tools/apiService")); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } /** * Methods for device operations */ class Device { /** * Creates a new device * @param {object} device * @returns {Promise} */ static createDevice(device) { return _apiService.default.call('/devices', 'POST', device); } /** * Get all devices * @param {object} params * @param {number} [params.page=0] The number of the result page starting with 0 * @param {number} [params.pageSize=10] The number of result per page. Default is 10 * @param {string} [params.tags=undefined] Filter the results to contain only devices having all the comma-separated list of tags provided here. Case-insensitive. Surround the tag with * for substring matching. Example: subString-of-a-tag,fullMatching-Tag * @param {string} [params.name=undefined] Filter the results to contain only devices having the provided name. Case-insensitive. Surround with * for substring matching. Example: &subString* OR fullMatchingName * @param {string} [params.deviceTypeId=undefined] Filter returning devices matching a single device type UUID. * @param {string} [params.connectedBy=undefined] Filter the results to contain only devices connected by a given device UUID. * @returns {Promise} */ static getDevices(params) { var urlParams = new URLSearchParams(params); return _apiService.default.call("/devices?".concat(urlParams.toString())); } /** * Get all device tags * @returns {Promise} */ static getDeviceTags() { return _apiService.default.call('/devices/tags'); } /** * Get a device by its deviceId. * @param {string} deviceId * @returns {Promise} */ static getDevice(deviceId) { return _apiService.default.call("/devices/".concat(deviceId)); } /** * Edit a specific device by id partially. Id is not patchable. You can also only patch a single property from your custom data. To delete submit null. * @param {string} deviceId * @param {object} patch changes to device * @returns {Promise} */ static patchDevice(deviceId, patch) { return _apiService.default.call("/devices/".concat(deviceId), 'PATCH', patch); } /** * Delete a specific device by id. * @param {string} deviceId * @returns {Promise} */ static deleteDevice(deviceId) { return _apiService.default.call("/devices/".concat(deviceId), 'DELETE'); } /** * Trigger device action * @param {string} deviceId * @returns {Promise} */ static createAction(deviceId, action) { return _apiService.default.call("/devices/".concat(deviceId, "/actions"), 'POST', action); } /** * The current state of a a device are the attributes and configurations the device has last sent to the cloud. Newer values for the same attribute and configuration overwrite older on. * @param {string} deviceId * @returns {Promise} */ static getState(deviceId) { return _apiService.default.call("/devices/".concat(deviceId, "/state")); } /** * List device diagnostic messages sorted descending by time for a specific device for the last hour to simplify debugging. * @param {string} deviceId * @returns {Promise} */ static getLastDiagnostics(deviceId) { return _apiService.default.call("/devices/".concat(deviceId, "/last-diagnostics")); } /** * Push data to device * @param {string} deviceId * @param {object} data * @returns {Promise} */ static pushData(deviceId, data) { var post = _objectSpread({}, data, { deviceId }); return _apiService.default.call('/data-ingest', 'POST', post); } /** * Get the online monitoring configuration for a device * @param {string} deviceId * @returns {Promise} */ static getOnlineMonitoringRules(deviceId) { return _apiService.default.call("/devices/".concat(deviceId, "/onlinemonitoring")); } /** * Edit the online monitoring configuration for a device * @param {string} deviceId * @param {object} config changes to device * @param {number|null} config.communicationInterval The expected communication interval in seconds. Special cases: **0** means monitoring is disabled, **null** means inherit from device type * @returns {Promise} */ static patchOnlineMonitoringRules(deviceId, config) { return _apiService.default.call("/devices/".concat(deviceId, "/onlinemonitoring"), 'PATCH', config); } } exports.default = Device;