@@ -72,9 +72,9 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
72
72
[ dataType : string ] : NodeJS . Timeout ;
73
73
} = { } ;
74
74
75
- private stationsLoaded = false ;
76
- private devicesLoaded = false ;
77
75
private loadingEmitter = new EventEmitter ( ) ;
76
+ private stationsLoaded ?: Promise < void > ;
77
+ private devicesLoaded ?: Promise < void > ;
78
78
79
79
private constructor ( config : EufySecurityConfig , log : Logger = dummyLogger ) {
80
80
super ( ) ;
@@ -287,8 +287,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
287
287
}
288
288
289
289
private async updateStation ( hub : StationListResponse ) : Promise < void > {
290
- if ( ! this . stationsLoaded )
291
- await waitForEvent ( this . loadingEmitter , "stations loaded" ) ;
290
+ if ( this . stationsLoaded )
291
+ await this . stationsLoaded ;
292
292
if ( Object . keys ( this . stations ) . includes ( hub . station_sn ) ) {
293
293
this . stations [ hub . station_sn ] . update ( hub , this . stations [ hub . station_sn ] !== undefined && ! this . stations [ hub . station_sn ] . isIntegratedDevice ( ) && this . stations [ hub . station_sn ] . isConnected ( ) ) ;
294
294
if ( ! this . stations [ hub . station_sn ] . isConnected ( ) && ! this . stations [ hub . station_sn ] . isEnergySavingDevice ( ) ) {
@@ -337,17 +337,17 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
337
337
}
338
338
339
339
private async updateDevice ( device : DeviceListResponse ) : Promise < void > {
340
- if ( ! this . devicesLoaded )
341
- await waitForEvent ( this . loadingEmitter , "devices loaded" ) ;
340
+ if ( this . devicesLoaded )
341
+ await this . devicesLoaded ;
342
342
if ( Object . keys ( this . devices ) . includes ( device . device_sn ) )
343
343
this . devices [ device . device_sn ] . update ( device , this . stations [ device . station_sn ] !== undefined && ! this . stations [ device . station_sn ] . isIntegratedDevice ( ) && this . stations [ device . station_sn ] . isConnected ( ) )
344
344
else
345
345
this . log . debug ( `Device with this serial ${ device . device_sn } doesn't exists and couldn't be updated!` ) ;
346
346
}
347
347
348
348
public async getDevices ( ) : Promise < Array < Device > > {
349
- if ( ! this . devicesLoaded )
350
- await waitForEvent ( this . loadingEmitter , "devices loaded" ) ;
349
+ if ( this . devicesLoaded )
350
+ await this . devicesLoaded ;
351
351
const arr : Array < Device > = [ ] ;
352
352
Object . keys ( this . devices ) . forEach ( ( serialNumber : string ) => {
353
353
arr . push ( this . devices [ serialNumber ] ) ;
@@ -356,8 +356,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
356
356
}
357
357
358
358
public async getDevicesFromStation ( stationSN : string ) : Promise < Array < Device > > {
359
- if ( ! this . devicesLoaded )
360
- await waitForEvent ( this . loadingEmitter , "devices loaded" ) ;
359
+ if ( this . devicesLoaded )
360
+ await this . devicesLoaded ;
361
361
const arr : Array < Device > = [ ] ;
362
362
Object . keys ( this . devices ) . forEach ( ( serialNumber : string ) => {
363
363
if ( this . devices [ serialNumber ] . getStationSerial ( ) === stationSN )
@@ -367,16 +367,16 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
367
367
}
368
368
369
369
public async getDevice ( deviceSN : string ) : Promise < Device > {
370
- if ( ! this . devicesLoaded )
371
- await waitForEvent ( this . loadingEmitter , "devices loaded" ) ;
370
+ if ( this . devicesLoaded )
371
+ await this . devicesLoaded ;
372
372
if ( Object . keys ( this . devices ) . includes ( deviceSN ) )
373
373
return this . devices [ deviceSN ] ;
374
374
throw new DeviceNotFoundError ( `Device with this serial ${ deviceSN } doesn't exists!` ) ;
375
375
}
376
376
377
377
public async getStationDevice ( stationSN : string , channel : number ) : Promise < Device > {
378
- if ( ! this . devicesLoaded )
379
- await waitForEvent ( this . loadingEmitter , "devices loaded" ) ;
378
+ if ( this . devicesLoaded )
379
+ await this . devicesLoaded ;
380
380
for ( const device of Object . values ( this . devices ) ) {
381
381
if ( ( device . getStationSerial ( ) === stationSN && device . getChannel ( ) === channel ) || ( device . getStationSerial ( ) === stationSN && device . getSerial ( ) === stationSN ) ) {
382
382
return device ;
@@ -386,8 +386,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
386
386
}
387
387
388
388
public async getStations ( ) : Promise < Array < Station > > {
389
- if ( ! this . stationsLoaded )
390
- await waitForEvent ( this . loadingEmitter , "stations loaded" ) ;
389
+ if ( this . stationsLoaded )
390
+ await this . stationsLoaded ;
391
391
const arr : Array < Station > = [ ] ;
392
392
Object . keys ( this . stations ) . forEach ( ( serialNumber : string ) => {
393
393
arr . push ( this . stations [ serialNumber ] ) ;
@@ -396,8 +396,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
396
396
}
397
397
398
398
public async getStation ( stationSN : string ) : Promise < Station > {
399
- if ( ! this . stationsLoaded )
400
- await waitForEvent ( this . loadingEmitter , "stations loaded" ) ;
399
+ if ( this . stationsLoaded )
400
+ await this . stationsLoaded ;
401
401
if ( Object . keys ( this . stations ) . includes ( stationSN ) )
402
402
return this . stations [ stationSN ] ;
403
403
throw new StationNotFoundError ( `No station with serial number: ${ stationSN } !` ) ;
@@ -438,7 +438,7 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
438
438
if ( stationsSNs . includes ( hub . station_sn ) ) {
439
439
this . updateStation ( hub ) ;
440
440
} else {
441
- this . stationsLoaded = false ;
441
+ this . stationsLoaded = waitForEvent < void > ( this . loadingEmitter , "stations loaded" ) ;
442
442
let ipAddress : string | undefined ;
443
443
if ( this . config . stationIPAddresses !== undefined ) {
444
444
ipAddress = this . config . stationIPAddresses [ hub . station_sn ] ;
@@ -499,12 +499,12 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
499
499
}
500
500
}
501
501
Promise . all ( promises ) . then ( ( ) => {
502
- this . stationsLoaded = true ;
503
502
this . loadingEmitter . emit ( "stations loaded" ) ;
503
+ this . stationsLoaded = undefined ;
504
504
} ) ;
505
505
if ( promises . length === 0 ) {
506
- this . stationsLoaded = true ;
507
506
this . loadingEmitter . emit ( "stations loaded" ) ;
507
+ this . stationsLoaded = undefined ;
508
508
}
509
509
for ( const stationSN of stationsSNs ) {
510
510
if ( ! newStationsSNs . includes ( stationSN ) ) {
@@ -565,7 +565,7 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
565
565
if ( deviceSNs . includes ( device . device_sn ) ) {
566
566
this . updateDevice ( device ) ;
567
567
} else {
568
- this . devicesLoaded = false ;
568
+ this . devicesLoaded = waitForEvent < void > ( this . loadingEmitter , "devices loaded" ) ;
569
569
let new_device : Promise < Device > ;
570
570
571
571
if ( Device . isIndoorCamera ( device . device_type ) ) {
@@ -647,12 +647,12 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
647
647
this . log . error ( "Error trying to connect to station afte device loaded" , error ) ;
648
648
} ) ;
649
649
} ) ;
650
- this . devicesLoaded = true ;
651
650
this . loadingEmitter . emit ( "devices loaded" ) ;
651
+ this . devicesLoaded = undefined ;
652
652
} ) ;
653
653
if ( promises . length === 0 ) {
654
- this . devicesLoaded = true ;
655
654
this . loadingEmitter . emit ( "devices loaded" ) ;
655
+ this . devicesLoaded = undefined ;
656
656
}
657
657
for ( const deviceSN of deviceSNs ) {
658
658
if ( ! newDeviceSNs . includes ( deviceSN ) ) {
0 commit comments