fixed a lot of problems

This commit is contained in:
Daniel Sommer 2022-02-28 01:53:13 +01:00
parent 98f4c5e975
commit 2c17326f96
3 changed files with 121 additions and 119 deletions

View file

@ -16,7 +16,7 @@ handleInterrupts();
// start the application // start the application
main() main()
.catch(exit); .catch(util.exit);
async function main() { async function main() {
await setGlobalConfig(); await setGlobalConfig();
@ -44,25 +44,7 @@ async function setGlobalConfig() {
function handleInterrupts() { function handleInterrupts() {
for (let index = 0; index < INTERRUPTS.length; index++) { for (let index = 0; index < INTERRUPTS.length; index++) {
process.once(INTERRUPTS[index], (code) => { process.once(INTERRUPTS[index], (code) => {
exit(null, code); util.exit(undefined, code);
}); });
} }
} }
function exit(err, code) {
if (code === undefined) {
code = 0;
if (err !== undefined) {
code = 1;
}
}
if (err) {
logger.error(err);
logger.error(global.appName + ' ' + global.appVersion + ' ended due to an error');
} else {
logger.info(global.appName + ' ' + global.appVersion + ' shutting down gracefully')
}
process.exit(code);
}

View file

@ -1,4 +1,5 @@
const logger = require('./logger'); const logger = require('./logger.js');
const util = require('./util.js');
const blinkstick = require('blinkstick'); const blinkstick = require('blinkstick');
const ALL = 'all'; const ALL = 'all';
@ -9,8 +10,7 @@ const MODE_BLINK = 'blink';
const MODE_PULSE = 'pulse'; const MODE_PULSE = 'pulse';
const MODE_POWEROFF = 'poweroff'; const MODE_POWEROFF = 'poweroff';
const LEDS_ANIMATED = []; const LEDAnimations = new Map();
const LEDS_STOP = [];
let blinksticks; let blinksticks;
@ -49,13 +49,12 @@ async function findBlinkstick(index, ignoreFilter) {
// simple animation (set the color / morph to color) // simple animation (set the color / morph to color)
async function simple(config) { async function simple(config) {
await stopLedsAccordingly(config); await stopLEDsAccordingly(config);
config.timestamp = new Date().getTime(); config.timestamp = new Date().getTime();
let indices = getIndices(config); let indices = getIndices(config);
for (let index = 0; index < indices.length; index++) { for (let index = 0; index < indices.length; index++) {
setLedAnimated(indices[index]); const tmpConfig = JSON.parse(JSON.stringify(config));
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); await singleAnimation(tmpConfig, indices[index]);
clearLedState(indices[index]);
} }
return { return {
status: 'ok', status: 'ok',
@ -68,38 +67,38 @@ async function simple(config) {
// complex animation (pulse / blink) // complex animation (pulse / blink)
async function complex(config) { async function complex(config) {
if (config.timestamp === undefined) { if (config.timestamp === undefined) {
await stopLedsAccordingly(config); await stopLEDsAccordingly(config);
config.timestamp = new Date().getTime(); config.timestamp = new Date().getTime();
} }
let indices = getIndices(config); let indices = getIndices(config);
for (let index = 0; index < indices.length; index++) { for (let index = 0; index < indices.length; index++) {
if (shouldLedFinish(config)) { if (shouldLEDFinish(config)) {
clearLedState(indices[index]); clearLedState(config.options.index);
return { status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms' }; return { status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms' };
} }
setLedAnimated(indices[index]); const tmpConfig = JSON.parse(JSON.stringify(config));
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); await singleAnimation(tmpConfig, indices[index]);
if (config.options.index === ALL) {
clearLedState(indices[index]);
}
config.repetitions.done++; config.repetitions.done++;
} }
return complex(config); return await complex(config);
} }
// power the blinkstick (or just a specific led) off // power the blinkstick (or just a specific led) off
async function powerOff(config) { async function powerOff(config) {
config.timestamp = new Date().getTime(); config.timestamp = new Date().getTime();
let indices = getIndices(config); let indices = getIndices(config);
await forceStop(config.options.index); if (config.options.index === ALL) {
LEDAnimations.set(ALL, { stop: new Date().getTime() });
}
for (let index = 0; index < indices.length; index++) { for (let index = 0; index < indices.length; index++) {
await stopLEDAnimation(indices[index]);
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
logger.info('led \'' + indices[index] + '\' powered off'); logger.info('led \'' + indices[index] + '\' powered off');
} }
if (config.options.index === ALL) { if (config.options.index === ALL) {
const blinkstick = await findBlinkstick(); const blinkstick = await findBlinkstick();
blinkstick.stop();
blinkstick.turnOff(); blinkstick.turnOff();
LEDAnimations.clear();
logger.info('blinkstick powered off'); logger.info('blinkstick powered off');
} }
return { status: 'ok', indices: indices, time: (new Date().getTime() - config.timestamp) + 'ms' }; return { status: 'ok', indices: indices, time: (new Date().getTime() - config.timestamp) + 'ms' };
@ -110,7 +109,9 @@ async function singleAnimation(config, index) {
config.options.index = index; config.options.index = index;
const blinkstick = await findBlinkstick(); const blinkstick = await findBlinkstick();
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')...'); logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ' | options: ' + JSON.stringify(config.options) + ')...');
setLEDAnimated(config.options.index);
blinkstick.animationsEnabled = true;
switch (config.mode) { switch (config.mode) {
case MODE_MORPH: case MODE_MORPH:
blinkstick.morph(config.color, config.options, callback); blinkstick.morph(config.color, config.options, callback);
@ -127,6 +128,9 @@ async function singleAnimation(config, index) {
} }
function callback(err) { function callback(err) {
if (config.mode !== MODE_BLINK && config.mode !== MODE_PULSE) {
clearLedState(config.options.index);
}
if (err) { if (err) {
reject(new Error('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' encountered an error > ' + err)); reject(new Error('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' encountered an error > ' + err));
} }
@ -136,47 +140,6 @@ async function singleAnimation(config, index) {
}); });
} }
async function stopLedsAccordingly(config) {
if (config.options.index === ALL) {
logger.debug('stopping all leds...');
return await powerOff({
id: Math.random(),
mode: MODE_POWEROFF,
color: '#000000',
options: { index: ALL }
});
}
return stopLedAnimation(config.options.index);
}
async function forceStop(index) {
if (index === ALL) {
LEDS_STOP.push(ALL);
return await new Promise((resolve, reject) => {
waitForAllAnimationsEnd(() => {
resolve();
});
});
}
setLedStopping(index);
return await new Promise((resolve, reject) => {
waitForAnimationEnd(index, () => {
resolve();
});
});
}
function shouldLedFinish(config) {
if (isLedStopping(config.options.index)) {
logger.debug('led \'' + config.options.index + '\' is set to \'stop\' and should finish now');
return true;
}
if (config.mode === MODE_BLINK || config.mode === MODE_PULSE) {
return config.repetitions.max !== 0 && config.repetitions.done >= config.repetitions.max;
}
return false;
}
// led / index helper functions // led / index helper functions
function getIndices(blinkstickConfig) { function getIndices(blinkstickConfig) {
if (blinkstickConfig.options.index === ALL) { if (blinkstickConfig.options.index === ALL) {
@ -185,74 +148,108 @@ function getIndices(blinkstickConfig) {
return [blinkstickConfig.options.index]; return [blinkstickConfig.options.index];
} }
function setLedAnimated(index) { async function stopLEDsAccordingly(config) {
if (isLedAnimated(index)) { if (LEDAnimations.size === 0) {
return; return;
} }
LEDS_ANIMATED.push(index); if (config.options.index === ALL) {
logger.debug('stopping all leds...');
return await powerOff({
id: Math.random(),
mode: MODE_POWEROFF,
color: '#000000',
options: { index: ALL }
});
}
return stopLEDAnimation(config.options.index);
}
function shouldLEDFinish(config) {
if (LEDAnimations.has(ALL) || (isLEDAnimated(config.options.index) && isLEDStopping(config.options.index))) {
logger.debug('led \'' + config.options.index + '\' is set to \'stop\' and should finish now');
return true;
}
if (config.mode === MODE_BLINK || config.mode === MODE_PULSE) {
return config.repetitions.max !== 0 && config.repetitions.done >= config.repetitions.max;
}
return false;
}
function setLEDAnimated(index) {
if (isLEDAnimated(index)) {
return;
}
LEDAnimations.set(index, { start: new Date().getTime() });
logger.debug('led \'' + index + '\ set to \'animated\''); logger.debug('led \'' + index + '\ set to \'animated\'');
} }
function setLedStopping(index) { function setLEDStopping(index) {
if (!isLedAnimated(index) || isLedStopping(index)) { if (!isLEDAnimated(index) || isLEDStopping(index)) {
return; return;
} }
LEDS_STOP.push(index); if (LEDAnimations.has(index)) {
LEDAnimations.get(index).stop = new Date().getTime();
}
logger.debug('led \'' + index + '\ set to \'stop\''); logger.debug('led \'' + index + '\ set to \'stop\'');
} }
function clearLedState(index) { function clearLedState(index) {
if (index === ALL) { if (index === ALL) {
LEDS_ANIMATED.length = 0; LEDAnimations.clear();
logger.debug('cleared animation state of all leds'); logger.debug('cleared animation state of all leds');
return; return;
} }
LEDS_ANIMATED.splice(LEDS_ANIMATED.indexOf(index), 1); LEDAnimations.delete(index);
logger.debug('cleared animation state of led \'' + index + '\''); logger.debug('cleared animation state of led \'' + index + '\'');
} }
function isLedAnimated(index) { function isLEDAnimated(index) {
return LEDS_ANIMATED.includes(index); return LEDAnimations.has(index);
} }
function isLedStopping(index) { function isLEDStopping(index) {
if (LEDS_STOP.includes(ALL)) { if (LEDAnimations.has(ALL) && LEDAnimations.get(ALL).stop !== undefined) {
return true; return true;
} }
return LEDS_STOP.includes(index); if (LEDAnimations.has(index) && LEDAnimations.get(index).stop !== undefined) {
return true;
}
return false;
} }
function stopLedAnimation(index) { async function stopLEDAnimation(index) {
setLedStopping(index); if (index === ALL) {
return new Promise((resolve, reject) => { for (const [key, value] of LEDAnimations) {
waitForAnimationEnd(index, () => { setLEDStopping(key);
resolve(); }
}); await waitForAllAnimationsEnd();
}); return;
}
setLEDStopping(index);
await waitForAnimationEnd(index);
} }
function waitForAnimationEnd(index, callback) { async function waitForAnimationEnd(index, timestamp) {
logger.debug('waiting for animated led \'' + index + '\' to end...'); logger.debug('waiting for animated led \'' + index + '\' to end...');
if (!isLedAnimated(index)) { if (!isLEDAnimated(index)) {
logger.debug('animation of led \'' + index + '\' should have ended now'); logger.debug('animation of led \'' + index + '\' should have ended now');
LEDS_STOP.splice(LEDS_STOP.indexOf(index), 1); return;
return callback();
} }
setTimeout(() => { if (timestamp === undefined) {
waitForAnimationEnd(index, callback); timestamp = new Date().getTime();
}, 100); }
await util.sleep(100);
return await waitForAnimationEnd(index, timestamp);
} }
function waitForAllAnimationsEnd(callback) { async function waitForAllAnimationsEnd(callback, timestamp) {
logger.debug('waiting for all animations to end...'); if (LEDAnimations.size === 0) {
if (LEDS_ANIMATED.length === 0) { return;
logger.debug('all animations should have ended now'); }
LEDS_STOP.length = 0; logger.debug('waiting for all animations to end...');
return callback(); for (const [key, value] of LEDAnimations) {
await waitForAnimationEnd(key);
} }
setTimeout(() => {
waitForAllAnimationsEnd(callback);
}, 100);
} }
function isInfiniteAnimation(config) { function isInfiniteAnimation(config) {

View file

@ -1,6 +1,23 @@
const logger = require('./logger.js');
const realpath = require('fs').realpath; const realpath = require('fs').realpath;
const stat = require('fs').stat; const stat = require('fs').stat;
function exit(err, code) {
if (code === undefined) {
code = 0;
if (err !== undefined) {
code = 1;
}
}
if (err) {
logger.error(err);
logger.error(global.appName + ' ' + global.appVersion + ' ended due to an error');
} else {
logger.info(global.appName + ' ' + global.appVersion + ' shutting down gracefully')
}
process.exit(code);
}
async function fileExists(file) { async function fileExists(file) {
if (file === undefined) { if (file === undefined) {
throw new Error('can not check the existence of an undefined file'); throw new Error('can not check the existence of an undefined file');
@ -11,7 +28,7 @@ async function fileExists(file) {
if (err) { if (err) {
reject(err); reject(err);
} }
resolve({path, stats}); resolve({ path, stats });
}) })
}); });
} }
@ -30,7 +47,13 @@ async function resolvePath(file) {
}); });
} }
async function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
module.exports = { module.exports = {
exit,
fileExists, fileExists,
resolvePath resolvePath,
sleep
}; };