const logger = require('./logger.js'); const { CONTROL_BYPASS, SYSTEMD_STATE_ACTIVE } = require('./constants.js'); const { httpPOST, httpGET } = require('./util.js'); const BLINKSTICK_SQUARE = 'square'; const BLINKSTICK_STRIP = 'strip'; const COLOR_RANDOM = 'random'; const COLOR_DEFAULT = '#333333'; let enabled; let fnIsBypassActive; let animationsInProgress = {}; async function initialize() { enabled = global.config.blinky !== undefined && global.config.blinky.enabled && global.config.blinky.host !== undefined && global.config.blinky.port !== undefined; } async function getColor() { if (!enabled) { return; } try { return await httpGET( global.config.blinky.host, global.config.blinky.port, 'color' ); } catch (err) { logger.error('[blinky] ' + err); } } async function poweroff(...blinksticks) { if (!enabled) { return; } if (blinksticks === undefined || blinksticks.length === 0) { blinksticks = [BLINKSTICK_SQUARE, BLINKSTICK_STRIP]; } for (let index = 0; index < blinksticks.length; index++) { try { await httpPOST( global.config.blinky.host, global.config.blinky.port, 'poweroff', { blinkstick: blinksticks[index] } ); logger.debug('[blinky] powered off blinkstick \'' + blinksticks[index]); } catch (err) { logger.error('[blinky] ' + err); } } } async function setPedalboardStatus(pedalboard) { try { if (!enabled || pedalboard === undefined) { return; } await httpPOST( global.config.blinky.host, global.config.blinky.port, 'set', { blinkstick: BLINKSTICK_SQUARE, color: getPedalboardColor(pedalboard.title) } ); logger.debug('[blinky] set status for pedalboard \'' + pedalboard.title + '\''); } catch (err) { logger.error('[blinky] ' + err); } } async function setPedalStatus(pedal) { try { if (!enabled || pedal === undefined || pedal.controls === undefined) { return; } if (fnIsBypassActive === undefined) { fnIsBypassActive = require('./modep.js').isBypassActive; } for (let index = 0; index < pedal.controls.length; index++) { const bypass = pedal.controls[index]; if (bypass.name !== CONTROL_BYPASS) { continue; } await httpPOST( global.config.blinky.host, global.config.blinky.port, 'set', { blinkstick: BLINKSTICK_STRIP, index: pedal.id, color: getPedalColor(pedal.name, fnIsBypassActive(bypass)) } ); logger.debug('[blinky] set status for pedal \'' + pedal.name + '\''); } } catch (err) { logger.error('[blinky] ' + err); } } async function setStatus(pedalboard, pedals) { if (!enabled) { return; } try { setPedalboardStatus(pedalboard); await poweroff([BLINKSTICK_STRIP]); if (pedals === undefined || pedals.length === 0) { return; } const promises = []; for (let index = 0; index < pedals.length; index++) { if (pedals[index]?.controls === undefined || pedals[index]?.controls.length === 0) { continue; } promises.push(setPedalStatus(pedals[index])); } const results = await Promise.allSettled(promises); for (let index = 0; index < results.length; index++) { const result = results[index]; if (result.status !== 'fulfilled') { logger.error(result.reason); } } } catch (err) { logger.error('[blinky] ' + err); } } async function setSystemdStatus(name, state) { if (name === undefined || state === undefined) { return; } let mode = 'set'; let options = { blinkstick: BLINKSTICK_STRIP, color: animationsInProgress[name] || COLOR_DEFAULT }; if (state === SYSTEMD_STATE_ACTIVE) { const result = await getColor([BLINKSTICK_SQUARE]); for (let index = 0; index < result.length; index++) { if (result[index].blinkstick !== options.blinkstick) { continue; } animationsInProgress[name] = result[index].leds[0]?.color || COLOR_RANDOM; mode = 'pulse'; options.color = getSystemdColor(name); break; } } return await httpPOST( global.config.blinky.host, global.config.blinky.port, mode, options ); } function getPedalboardColor(name) { return global.config?.blinky?.colors?.pedalboards[name] || COLOR_RANDOM; } function getPedalColor(name, bypassed) { if (bypassed) { return global.config?.blinky?.colors?.pedals[name]?.bypassed || COLOR_DEFAULT; } return global.config?.blinky?.colors?.pedals[name]?.enabled || COLOR_RANDOM; } function getSystemdColor(name) { return global.config?.blinky?.colors?.systemd[name] || COLOR_RANDOM; } module.exports = { initialize, poweroff, setStatus, setPedalStatus, setSystemdStatus }