diff --git a/server/src/lib/wifiDevices.js b/server/src/lib/wifiDevices.js index 2618689..d68ace8 100644 --- a/server/src/lib/wifiDevices.js +++ b/server/src/lib/wifiDevices.js @@ -147,58 +147,54 @@ async function updateDB(onlineDevices) { function updateDevicesInfo() { return new Promise(async (resolve, reject) => { - if (working) reject('A última atualização ainda não terminou') - else { - working = true + if (working) return reject('A última atualização ainda não terminou') - const updateTimeout = setTimeout(() => { - reject('A função atingiu seu tempo limite.') - }, TIMEOUT_IN_MILLISECONDS) + working = true - try { - const startTime = performance.now() + const updateTimeout = setTimeout(() => { + reject('A função atingiu seu tempo limite.') + working = false + }, TIMEOUT_IN_MILLISECONDS) - const onlineDevices = await getOnlineDevices() + try { + const startTime = performance.now() - await updateDevicesStatus() + const onlineDevices = await getOnlineDevices() + await updateDevicesStatus() + await updateDB(onlineDevices) - await updateDB(onlineDevices) + const endTime = performance.now() - const endTime = performance.now() + onlineDevices.length > 0 + ? resolve( + `${onlineDevices.length} atualizados em ${Math.floor( + endTime - startTime + )}ms` + ) + : reject('Não há dispositivos conectados no momento.') - onlineDevices.length > 0 - ? resolve( - `${onlineDevices.length} atualizados em ${Math.floor( - endTime - startTime - )}ms` - ) - : reject('Não há dispositivos conectados no momento.') + pubsub.publish(USER_PRESENCE_UPDATED, { + userPresenceUpdated: onlineDevices.length + }) - pubsub.publish(USER_PRESENCE_UPDATED, { - userPresenceUpdated: onlineDevices.length - }) + logSuccess({ + tags: ['wifiDevices'], + message: `Foram atualizados ${ + onlineDevices.length + } dispositivos Wi-Fi em ${((endTime - startTime) / 1000).toFixed(2)}s.`, + data: onlineDevices + }) + } catch (e) { + logError({ + tags: ['wifiDevices'], + message: `Erro atualizando dispositivos Wi-Fi: ${e.message}`, + data: e + }) - logSuccess({ - tags: ['wifiDevices'], - message: `Foram atualizados ${ - onlineDevices.length - } dispositivos Wi-Fi em ${((endTime - startTime) / 1000).toFixed( - 2 - )}s.`, - data: onlineDevices - }) - } catch (e) { - logError({ - tags: ['wifiDevices'], - message: `Erro atualizando dispositivos Wi-Fi: ${e.message}`, - data: e - }) - - reject('Não foi possível atualizar as informações dos dispositivos.') - } finally { - working = false - clearTimeout(updateTimeout) - } + reject('Não foi possível atualizar as informações dos dispositivos.') + } finally { + working = false + clearTimeout(updateTimeout) } }) }