@@ -5,6 +5,7 @@ import { Sensors } from './types/planetWatch/sensors';
55import { DateTime } from 'luxon' ;
66import { AwairToPlanetWatchMapper } from './mappers/AwairToPlanetWatchMapper' ;
77import constants from './constants' ;
8+ import { authorization } from './utils/authorization' ;
89export interface AwairAccount {
910 jwt : string ;
1011 devices : AwairDevice [ ] ;
@@ -168,8 +169,9 @@ export class Scheduler {
168169 }
169170
170171 private async fetchAndSendData ( accounts : AwairAccounts ) {
172+ await authorization . refreshToken ( ) ;
171173 // Grab all the data from awair and save them in a structure
172- accounts . forEach ( ( account ) => {
174+ accounts . forEach ( ( account ) => {
173175 const jwt = account . jwt ;
174176
175177 account . devices . forEach ( ( device ) => {
@@ -182,14 +184,24 @@ export class Scheduler {
182184
183185 AwairController . getLatestData ( jwt , deviceType , deviceId )
184186 . then ( ( res ) => {
185- PlanetWatchService . sendData ( AwairToPlanetWatchMapper . map ( device . deviceUUID , res ) )
186- . catch ( ( err ) => console . log ( err ) ) ;
187187 this . statuses [ device . deviceUUID ] . receivedAt = DateTime . now ( ) . toISO ( ) ;
188188 this . statusSubscribers . forEach ( ( callback ) => callback ( { ...this . statuses } ) ) ;
189- this . logs . push ( DateTime . now ( ) . toFormat ( '[HH:mm:ss]' ) + ' - Retrieving data from ' + deviceId + '\n' ) ;
189+ this . logs . push ( DateTime . now ( ) . toFormat ( '[HH:mm:ss]' ) + ' - Retrieved data from ' + deviceId + '\n' ) ;
190190 this . logSubscribers . forEach ( ( callback ) => callback ( [ ...this . logs ] ) ) ;
191+ PlanetWatchService . sendData ( AwairToPlanetWatchMapper . map ( device . deviceUUID , res ) )
192+ . then ( ( ) => {
193+ this . logs . push ( DateTime . now ( ) . toFormat ( '[HH:mm:ss]' ) + ' - Successfully sent data to PW servers ' + deviceId + '\n' ) ;
194+ this . logSubscribers . forEach ( ( callback ) => callback ( [ ...this . logs ] ) ) ;
195+ } )
196+ . catch ( ( err ) => {
197+ this . logs . push ( DateTime . now ( ) . toFormat ( '[HH:mm:ss]' ) + ' - Error while sending data to PW servers of ' + deviceId + ' ' + err . response . data . errorMessage + '\n' ) ;
198+ this . logSubscribers . forEach ( ( callback ) => callback ( [ ...this . logs ] ) ) ;
199+ console . log ( err . response . data . errorMessage ) ;
200+ } ) ;
191201 } )
192202 . catch ( ( error ) => {
203+ this . logs . push ( DateTime . now ( ) . toFormat ( '[HH:mm:ss]' ) + ' - Error while retrieving data from ' + deviceId + ' ' + error . message + '\n' ) ;
204+ this . logSubscribers . forEach ( ( callback ) => callback ( [ ...this . logs ] ) ) ;
193205 console . log ( error ) ;
194206 } ) ;
195207 } ) ;
0 commit comments