move some more code from promises to async/await

This commit is contained in:
Daniel Sommer 2022-02-24 11:52:16 +01:00
parent 4dd916997e
commit 9ceb501265

View file

@ -31,106 +31,94 @@ async function findBlinkstick(index) {
// simple animation (set the color / morph to color) // simple animation (set the color / morph to color)
async function simple(config) { async function simple(config) {
try { await forceStop();
await forceStop(); 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++) { try {
await setLedState(indices[index], LED_ANIMATED); await setLedState(indices[index], LED_ANIMATED);
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
} finally {
await clearLedState(indices[index]); await clearLedState(indices[index]);
} }
return {
status: 'ok',
color: config.color,
indices: indices,
time: (new Date().getTime() - config.timestamp) + 'ms'
};
} catch (err) {
logger.error(err);
return err;
} }
return {
status: 'ok',
color: config.color,
indices: indices,
time: (new Date().getTime() - config.timestamp) + 'ms'
};
} }
// complex animation (pulse / blink) // complex animation (pulse / blink)
async function complex(config) { async function complex(config) {
try { if (config.timestamp === undefined) {
if (config.timestamp === undefined) { await powerOff({id: Math.random(), mode: MODE_POWEROFF, color: '#000000', options: {index: LEDS_ALL}});
await powerOff({id: Math.random(), mode: MODE_POWEROFF, color: '#000000', options: {index: LEDS_ALL}}); 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++) { try {
if (shouldLedFinish(config)) { if (shouldLedFinish(config)) {
return {status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms'}; return {status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms'};
} }
await setLedState(indices[index], LED_ANIMATED); await setLedState(indices[index], LED_ANIMATED);
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
await clearLedState(indices[index]);
config.repetitions.done++; config.repetitions.done++;
} finally {
await clearLedState(indices[index]);
} }
return complex(config);
} catch (err) {
logger.error(err);
} }
return 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) {
try { await forceStop();
await forceStop(); 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++) { 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 === LEDS_ALL) {
const blinkstick = await findBlinkstick();
blinkstick.stop();
blinkstick.turnOff();
logger.info('blinkstick powered off');
}
return {status: 'ok', indices: indices, time: (new Date().getTime() - config.timestamp) + 'ms'};
} catch (err) {
logger.error(err);
throw err;
} }
if (config.options.index === LEDS_ALL) {
const blinkstick = await findBlinkstick();
blinkstick.stop();
blinkstick.turnOff();
logger.info('blinkstick powered off');
}
return {status: 'ok', indices: indices, time: (new Date().getTime() - config.timestamp) + 'ms'};
} }
// animations // animations
async function singleAnimation(config, index) { async function singleAnimation(config, index) {
try { 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 + ')...'); 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); break;
break; case MODE_BLINK:
case MODE_BLINK: blinkstick.blink(config.color, config.options, callback);
blinkstick.blink(config.color, config.options, callback); break;
break; case MODE_PULSE:
case MODE_PULSE: blinkstick.pulse(config.color, config.options, callback);
blinkstick.pulse(config.color, config.options, callback); break;
break; default:
default: blinkstick.setColor(config.color, config.options, callback);
blinkstick.setColor(config.color, config.options, callback); break;
break; }
}
function callback(err) { function callback(err) {
if (err) { if (err) {
reject('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));
}
logger.debug('changed color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')');
resolve();
} }
}); logger.debug('changed color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')');
} catch (err) { resolve();
throw err; }
} });
} }
async function forceStop() { async function forceStop() {
@ -207,10 +195,7 @@ function isInfiniteAnimation(config) {
if (config.mode !== MODE_BLINK && config.mode !== MODE_PULSE) { if (config.mode !== MODE_BLINK && config.mode !== MODE_PULSE) {
return false; return false;
} }
if (config.options.index === LEDS_ALL) { return config.repetitions.max === 0;
return config.repetitions.max === 0;
}
return config.options.repeats === 0;
} }
// exports // exports