const logger = require('./logger.js'); const { CONTROL_BYPASS } = require('./constants.js'); const { httpPOST } = require('./util.js'); let enabled; let isBypassActive; async function initialize() { enabled = global.config.blinky !== undefined && global.config.blinky.enabled && global.config.blinky.host !== undefined && global.config.blinky.port !== undefined; } async function setActive(active) { if (!enabled) { return; } logger.info('handling blinky event \'setActive\' (argument: \'' + active + '\')...'); try { if (active) { await httpPOST( global.config.blinky.host, global.config.blinky.port, '/set', { blinkstick: 'square', color: '#0f0f0f' } ); return; } let mode = '/poweroff'; await httpPOST( global.config.blinky.host, global.config.blinky.port, mode, { blinkstick: 'square' } ); await httpPOST( global.config.blinky.host, global.config.blinky.port, mode, { blinkstick: 'strip' } ); } catch (err) { logger.error('[BLINKY] ' + err); } } async function setPedalStatus(pedal) { try { if (!enabled || pedal === undefined || pedal.controls === undefined) { return; } if (isBypassActive === undefined) { isBypassActive = 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: 'strip', index: pedal.id, color: getPedalColor(pedal.name, isBypassActive(bypass)) } ); } } catch (err) { logger.error('[BLINKY] ' + err); } } async function setStatus(pedals) { if (!enabled) { return; } try { await httpPOST(global.config.blinky.host, global.config.blinky.port, '/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 setBypass(bypassActive) { if (!enabled) { return; } try { if (!bypassActive) { return await setActive(true); } await httpPOST(global.config.blinky.host, global.config.blinky.port, 'pulse', { blinkstick: 'square', color: '#660000' }); } catch (err) { logger.error('[BLINKY] ' + err); } } function getPedalColor(name, bypassed) { let color; if (bypassed) { color = global.config.blinky?.colors[name]?.bypassed || global.config.blinky?.colors['Bypass'] || '#333333'; } else { color = global.config.blinky?.colors[name]?.enabled || 'random'; } return color; } module.exports = { initialize, setActive, setBypass, setStatus, setPedalStatus }