2022-03-04 02:38:52 +01:00
|
|
|
const logger = require('./logger.js');
|
2022-03-08 00:51:58 +01:00
|
|
|
const { CONTROL_BYPASS } = require('./constants.js');
|
2022-03-04 02:38:52 +01:00
|
|
|
const { httpPOST } = require('./util.js');
|
|
|
|
|
|
|
|
let enabled;
|
|
|
|
|
|
|
|
async function initialize() {
|
|
|
|
enabled = global.config.blinky !== undefined &&
|
|
|
|
global.config.blinky.enabled &&
|
|
|
|
global.config.blinky.host !== undefined &&
|
2022-03-08 00:51:58 +01:00
|
|
|
global.config.blinky.port !== undefined;
|
2022-03-04 02:38:52 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
async function setActive(active) {
|
|
|
|
if (!enabled) {
|
2022-03-04 02:38:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-03-08 00:51:58 +01:00
|
|
|
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(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setPedalStatus(pedal) {
|
|
|
|
if (!enabled || pedal === undefined || pedal.controls === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let index = 0; index < pedal.controls.length; index++) {
|
|
|
|
const bypass = pedal.controls[index];
|
|
|
|
if (bypass.name !== CONTROL_BYPASS) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-14 22:37:42 +01:00
|
|
|
return httpPOST(
|
|
|
|
global.config.blinky.host,
|
|
|
|
global.config.blinky.port,
|
|
|
|
'set',
|
|
|
|
{
|
|
|
|
blinkstick: "strip",
|
|
|
|
index: pedal.id,
|
|
|
|
color: getPedalColor(pedal.name, (bypass.value === undefined || bypass.value === 0))
|
|
|
|
}
|
|
|
|
);
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setStatus(pedals) {
|
|
|
|
if (!enabled) {
|
2022-03-04 02:38:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2022-03-08 00:51:58 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-03-04 02:38:52 +01:00
|
|
|
} catch (err) {
|
2022-03-08 00:51:58 +01:00
|
|
|
logger.error(err);
|
2022-03-04 02:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
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(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 22:37:42 +01:00
|
|
|
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;
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 02:38:52 +01:00
|
|
|
module.exports = {
|
|
|
|
initialize,
|
2022-03-08 00:51:58 +01:00
|
|
|
setActive,
|
|
|
|
setBypass,
|
|
|
|
setStatus,
|
|
|
|
setPedalStatus
|
2022-03-04 02:38:52 +01:00
|
|
|
}
|