move some more code from promises to async/await
This commit is contained in:
parent
4dd916997e
commit
9ceb501265
1 changed files with 61 additions and 76 deletions
|
@ -31,53 +31,49 @@ async function findBlinkstick(index) {
|
|||
|
||||
// simple animation (set the color / morph to color)
|
||||
async function simple(config) {
|
||||
try {
|
||||
await forceStop();
|
||||
config.timestamp = new Date().getTime();
|
||||
let indices = getIndices(config);
|
||||
for (let index = 0; index < indices.length; index++) {
|
||||
try {
|
||||
await setLedState(indices[index], LED_ANIMATED);
|
||||
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
|
||||
} finally {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// complex animation (pulse / blink)
|
||||
async function complex(config) {
|
||||
try {
|
||||
if (config.timestamp === undefined) {
|
||||
await powerOff({id: Math.random(), mode: MODE_POWEROFF, color: '#000000', options: {index: LEDS_ALL}});
|
||||
config.timestamp = new Date().getTime();
|
||||
}
|
||||
let indices = getIndices(config);
|
||||
for (let index = 0; index < indices.length; index++) {
|
||||
try {
|
||||
if (shouldLedFinish(config)) {
|
||||
return {status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms'};
|
||||
}
|
||||
await setLedState(indices[index], LED_ANIMATED);
|
||||
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
|
||||
await clearLedState(indices[index]);
|
||||
config.repetitions.done++;
|
||||
} finally {
|
||||
await clearLedState(indices[index]);
|
||||
}
|
||||
}
|
||||
return complex(config);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
// power the blinkstick (or just a specific led) off
|
||||
async function powerOff(config) {
|
||||
try {
|
||||
await forceStop();
|
||||
config.timestamp = new Date().getTime();
|
||||
let indices = getIndices(config);
|
||||
|
@ -92,15 +88,10 @@ async function powerOff(config) {
|
|||
logger.info('blinkstick powered off');
|
||||
}
|
||||
return {status: 'ok', indices: indices, time: (new Date().getTime() - config.timestamp) + 'ms'};
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// animations
|
||||
async function singleAnimation(config, index) {
|
||||
try {
|
||||
config.options.index = index;
|
||||
const blinkstick = await findBlinkstick();
|
||||
return await new Promise((resolve, reject) => {
|
||||
|
@ -122,15 +113,12 @@ async function singleAnimation(config, index) {
|
|||
|
||||
function callback(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();
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async function forceStop() {
|
||||
|
@ -207,10 +195,7 @@ function isInfiniteAnimation(config) {
|
|||
if (config.mode !== MODE_BLINK && config.mode !== MODE_PULSE) {
|
||||
return false;
|
||||
}
|
||||
if (config.options.index === LEDS_ALL) {
|
||||
return config.repetitions.max === 0;
|
||||
}
|
||||
return config.options.repeats === 0;
|
||||
}
|
||||
|
||||
// exports
|
||||
|
|
Loading…
Reference in a new issue