UNPKG

@isaac-platform/isaac-integration-sdk

Version:

A Typescript SDK for integrating with ISAAC

110 lines (109 loc) 4.24 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as util from "util"; import isaacConnection from "../controller/isaac"; // TODO: Update this to user getters and setters like in events. export class IsaacVariable { constructor(definition) { this.filterUpdatesForChanges = false; /** * Updates the value of a variable internally and publishes to ISAAC * @param value */ this.setValue = (value) => __awaiter(this, void 0, void 0, function* () { const safeValue = this.variableToString(value); try { this.definition = (yield isaacConnection.postRequest(`variables/${this.definition._id}/value`, { 'value': safeValue })); return this.definition; } catch (error) { console.error('ISAAC SDK: Error setting variable value.', error); throw error; } }); this.definition = definition; } /** * Refresh * Retrieves the latest copy of the variable information from ISAAC. * @returns IsaacVariableType */ refresh() { return __awaiter(this, void 0, void 0, function* () { try { this.definition = (yield isaacConnection.getRequest(`variables/${this.definition._id}`)); return this.definition; } catch (error) { console.error('ISAAC SDK: Error refreshing variable.', error); throw error; } }); } /** * Get * @returns IsaacVariableType */ get() { return this.definition; } /** * Disable * Disables the variable in the subsystem. * This is recommended in place of delete as it does not break any user configuration which uses the variable. * @returns IsaacVariableType */ disable() { return __awaiter(this, void 0, void 0, function* () { try { this.definition.availableInSubsystem = false; this.definition = (yield isaacConnection.putRequest(`variables/${this.definition._id}`, this.definition)); return this.definition; } catch (error) { console.error('ISAAC SDK: Error disabling variable.', error); throw error; } }); } /** * Enable * Enables the variable in the subsystem. * @returns IsaacVariableType */ enable() { return __awaiter(this, void 0, void 0, function* () { try { this.definition.availableInSubsystem = true; this.definition = (yield isaacConnection.putRequest(`variables/${this.definition._id}`, this.definition)); return this.definition; } catch (error) { console.error('ISAAC SDK: Error enabling variable.', error); throw error; } }); } // Override inspect behavior for logging. [util.inspect.custom](depth, opts) { return `${this.definition.subsystemDisplayName || this.definition.subsystemId} - ${this.definition.displayName || this.definition.externalRef} = ${this.definition.lastValue}`; } variableToString(value) { if (typeof value === 'boolean') return value ? 'true' : 'false'; if (typeof value === 'number') return value.toString(); if (typeof value === 'object') return JSON.stringify(value); return value; } }