UNPKG

@isaac-platform/isaac-node-red

Version:

Set of Node-RED nodes to communicate with an ISAAC system

122 lines (121 loc) 4.67 kB
const axios = require("axios"), utils = require("../../utils.js"); async function getAllPlayables(server) { if (!server.subsystemExternalId) throw new Error("subsystemExternalId must be specified in the configuration node"); const response = await axios.get( `${utils.getApiUrl(server)}/playables?subsystemExternalId=${server.subsystemExternalId}` ); return { payload: response.data, statusCode: response.status }; } async function getPlayable(server, mergedConfig) { if (!mergedConfig.externalRef) throw new Error("externalRef must be specified"); const subsystem = await utils.getSubsystem(server), response = await axios.get(`${utils.getApiUrl(server)}/playables/${subsystem._id}/${mergedConfig.externalRef}`); return { payload: response.data, statusCode: response.status }; } async function upsertPlayable(server, mergedConfig) { if (!server.subsystemExternalId) throw new Error("subsystemExternalId must be specified in the configuration node"); if (!mergedConfig.externalRef) throw new Error("externalRef must be specified"); let existingPlayable; try { ({ payload: existingPlayable } = await getPlayable(server, mergedConfig)); } catch { } let duration; if (typeof mergedConfig.duration == "number" && !Number.isNaN(mergedConfig.duration)) duration = mergedConfig.duration; else if (mergedConfig.duration) { const parsed = parseInt(mergedConfig.duration, 10); Number.isNaN(parsed) || (duration = parsed); } if (existingPlayable) { const payload2 = { ...existingPlayable, displayName: mergedConfig.displayName, description: mergedConfig.description, command: mergedConfig.command, group: mergedConfig.group, durationType: mergedConfig.durationType }; duration !== void 0 && (payload2.duration = duration), mergedConfig.forceCached !== void 0 && (payload2.forceCached = mergedConfig.forceCached || !1); const response2 = await axios.put(`${utils.getApiUrl(server)}/playables/${existingPlayable._id}`, payload2); return { payload: response2.data, statusCode: response2.status }; } const payload = { externalRef: mergedConfig.externalRef, displayName: mergedConfig.displayName, description: mergedConfig.description, command: mergedConfig.command, group: mergedConfig.group, durationType: mergedConfig.durationType, subsystemExternalId: server.subsystemExternalId }; duration !== void 0 && (payload.duration = duration), mergedConfig.forceCached !== void 0 && (payload.forceCached = mergedConfig.forceCached || !1); const response = await axios.post(`${utils.getApiUrl(server)}/playables`, payload); return { payload: response.data, statusCode: response.status }; } async function deletePlayable(server, mergedConfig) { if (!mergedConfig.externalRef) throw new Error("externalRef must be specified"); const subsystem = await utils.getSubsystem(server), response = await axios.delete( `${utils.getApiUrl(server)}/playables/${subsystem._id}/${mergedConfig.externalRef}` ); return { payload: response.data, statusCode: response.status }; } module.exports = (RED) => { function PlayableNode(config) { RED.nodes.createNode(this, config); const node = this, server = RED.nodes.getNode(config.isaacConnection), didAddUsedConnection = utils.incrementConnectionUsage(node, server); this.on("input", async (msg) => { try { if (!server) throw new Error("No ISAAC connection configured"); const mergedConfig = utils.mergeConfig({ config, msg }), action = utils.getAction({ config, msg }); let promise; if (action === "getOne") promise = getPlayable(server, mergedConfig); else if (action === "getAll") promise = getAllPlayables(server); else if (action === "upsert") promise = upsertPlayable(server, mergedConfig); else if (action === "delete") promise = deletePlayable(server, mergedConfig); else throw new Error(`Invalid action "${action}"`); const result = await promise; node.send({ ...msg, ...result }); } catch (e) { const niceMessage = utils.getNiceAxiosErrorMessage(e); if (niceMessage) { const error = new Error(niceMessage); node.error(error, msg); return; } node.error(e, msg); } }), this.on("close", () => { didAddUsedConnection && utils.decrementConnectionUsage(node, server); }); } RED.nodes.registerType("isaac playable", PlayableNode); };