improved logging

This commit is contained in:
Daniel Sommer 2022-02-25 22:59:01 +01:00
parent 714a89fb67
commit 98f4c5e975

View file

@ -53,9 +53,9 @@ async function simple(config) {
config.timestamp = new Date().getTime();
let indices = getIndices(config);
for (let index = 0; index < indices.length; index++) {
await setLedAnimated(indices[index]);
setLedAnimated(indices[index]);
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
await clearLedState(indices[index]);
clearLedState(indices[index]);
}
return {
status: 'ok',
@ -74,13 +74,13 @@ async function complex(config) {
let indices = getIndices(config);
for (let index = 0; index < indices.length; index++) {
if (shouldLedFinish(config)) {
await clearLedState(indices[index]);
return {status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms'};
clearLedState(indices[index]);
return { status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms' };
}
await setLedAnimated(indices[index]);
setLedAnimated(indices[index]);
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
if (config.options.index === ALL) {
await clearLedState(indices[index]);
clearLedState(indices[index]);
}
config.repetitions.done++;
}
@ -102,7 +102,7 @@ async function powerOff(config) {
blinkstick.turnOff();
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' };
}
// animations
@ -130,7 +130,7 @@ async function singleAnimation(config, index) {
if (err) {
reject(new Error('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' encountered an error > ' + err));
}
logger.debug('changed color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')');
logger.info('changed color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')');
resolve();
}
});
@ -138,11 +138,12 @@ 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}
options: { index: ALL }
});
}
return stopLedAnimation(config.options.index);
@ -167,6 +168,7 @@ async function forceStop(index) {
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) {
@ -188,6 +190,7 @@ function setLedAnimated(index) {
return;
}
LEDS_ANIMATED.push(index);
logger.debug('led \'' + index + '\ set to \'animated\'');
}
function setLedStopping(index) {
@ -195,14 +198,17 @@ function setLedStopping(index) {
return;
}
LEDS_STOP.push(index);
logger.debug('led \'' + index + '\ set to \'stop\'');
}
function clearLedState(index) {
if (index === ALL) {
LEDS_ANIMATED.length = 0;
logger.debug('cleared animation state of all leds');
return;
}
LEDS_ANIMATED.splice(LEDS_ANIMATED.indexOf(index), 1);
logger.debug('cleared animation state of led \'' + index + '\'');
}
function isLedAnimated(index) {
@ -226,7 +232,9 @@ function stopLedAnimation(index) {
}
function waitForAnimationEnd(index, callback) {
logger.debug('waiting for animated led \'' + index + '\' to end...');
if (!isLedAnimated(index)) {
logger.debug('animation of led \'' + index + '\' should have ended now');
LEDS_STOP.splice(LEDS_STOP.indexOf(index), 1);
return callback();
}
@ -236,7 +244,9 @@ function waitForAnimationEnd(index, callback) {
}
function waitForAllAnimationsEnd(callback) {
logger.debug('waiting for all animations to end...');
if (LEDS_ANIMATED.length === 0) {
logger.debug('all animations should have ended now');
LEDS_STOP.length = 0;
return callback();
}