homey-api 3.0.23 → 3.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -480,11 +480,17 @@ class Manager extends EventEmitter {
|
|
|
480
480
|
this.__debug(`onDisconnect Reason:${reason}`);
|
|
481
481
|
this.__connected = false;
|
|
482
482
|
|
|
483
|
+
// Disable for now. We should probably only set the cache to invalid.
|
|
484
|
+
|
|
483
485
|
// Clear CRUD Item cache
|
|
484
|
-
for (const itemId of Object.keys(this.__cache)) {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
}
|
|
486
|
+
// for (const itemId of Object.keys(this.__cache)) {
|
|
487
|
+
// this.__cache[itemId] = {};
|
|
488
|
+
// this.__cacheAllComplete[itemId] = false;
|
|
489
|
+
// }
|
|
490
|
+
},
|
|
491
|
+
onReconnect: () => {
|
|
492
|
+
this.__debug(`onReconnect`);
|
|
493
|
+
this.__connected = true;
|
|
488
494
|
},
|
|
489
495
|
onEvent: (event, data) => {
|
|
490
496
|
this.__debug('onEvent', event);
|
|
@@ -552,7 +558,9 @@ class Manager extends EventEmitter {
|
|
|
552
558
|
|
|
553
559
|
// Delete the connecting Promise
|
|
554
560
|
this.__connectPromise
|
|
555
|
-
.catch(() => {
|
|
561
|
+
.catch(() => {
|
|
562
|
+
delete this.io;
|
|
563
|
+
})
|
|
556
564
|
.finally(() => {
|
|
557
565
|
delete this.__connectPromise;
|
|
558
566
|
});
|
|
@@ -566,7 +566,7 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
566
566
|
this.__debug('subscribe', uri);
|
|
567
567
|
|
|
568
568
|
await this.connect();
|
|
569
|
-
await new Promise((resolve, reject) => {
|
|
569
|
+
await Util.timeout(new Promise((resolve, reject) => {
|
|
570
570
|
this.__homeySocket.once('disconnect', (reason) => {
|
|
571
571
|
reject(new Error(reason));
|
|
572
572
|
});
|
|
@@ -580,7 +580,7 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
580
580
|
this.__debug('subscribed', uri);
|
|
581
581
|
return resolve();
|
|
582
582
|
});
|
|
583
|
-
});
|
|
583
|
+
}), 5000, `Failed to subscribe to ${uri} (Timeout after 5000ms).`);
|
|
584
584
|
|
|
585
585
|
// On Connect
|
|
586
586
|
const __onEvent = (event, data) => {
|
|
@@ -600,16 +600,21 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
600
600
|
const __onReconnect = () => {
|
|
601
601
|
Promise.resolve().then(async () => {
|
|
602
602
|
await this.connect();
|
|
603
|
-
await new Promise((resolve, reject) => {
|
|
603
|
+
await Util.timeout(new Promise((resolve, reject) => {
|
|
604
|
+
this.__homeySocket.once('disconnect', (reason) => {
|
|
605
|
+
reject(new Error(reason));
|
|
606
|
+
});
|
|
607
|
+
this.__debug('subscribing', uri);
|
|
604
608
|
this.__homeySocket.emit('subscribe', uri, err => {
|
|
605
609
|
if (err) {
|
|
606
610
|
this.__debug('Failed to subscribe', uri, err);
|
|
607
611
|
return reject(err)
|
|
608
612
|
}
|
|
609
|
-
|
|
613
|
+
|
|
614
|
+
this.__debug('subscribed', uri);
|
|
610
615
|
return resolve();
|
|
611
616
|
});
|
|
612
|
-
});
|
|
617
|
+
}), 5000, `Failed to subscribe to ${uri} (Timeout after 5000ms).`);
|
|
613
618
|
|
|
614
619
|
this.__homeySocket.on(uri, __onEvent);
|
|
615
620
|
|
|
@@ -652,23 +657,6 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
652
657
|
|
|
653
658
|
this.__socket.on('disconnect', reason => {
|
|
654
659
|
this.__debug('SocketIOClient.onDisconnect', reason);
|
|
655
|
-
// this.emit('state', {
|
|
656
|
-
// socketConnected: this.__socket.connected,
|
|
657
|
-
// homeySocketConnected: this.__homeySocket && this.__homeySocket.connected
|
|
658
|
-
// });
|
|
659
|
-
|
|
660
|
-
if (this.__homeySocket) {
|
|
661
|
-
this.__homeySocket.disconnect();
|
|
662
|
-
this.__homeySocket.destroy();
|
|
663
|
-
this.__homeySocket.removeAllListeners();
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
if (reason === 'io server disconnect') {
|
|
667
|
-
// The disconnect was initiated by the server.
|
|
668
|
-
// this.__socket.open();
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
reject(new Error('Disconnected'));
|
|
672
660
|
});
|
|
673
661
|
|
|
674
662
|
this.__socket.on('error', err => {
|
|
@@ -677,15 +665,6 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
677
665
|
|
|
678
666
|
this.__socket.on('reconnect', () => {
|
|
679
667
|
this.__debug('SocketIOClient.onReconnect');
|
|
680
|
-
this.__handshakeClient()
|
|
681
|
-
.then(() => {
|
|
682
|
-
this.__debug('SocketIOClient.onReconnect.onHandshakeClientSuccess');
|
|
683
|
-
resolve();
|
|
684
|
-
})
|
|
685
|
-
.catch(err => {
|
|
686
|
-
this.__debug('SocketIOClient.onReconnect.onHandshakeClientError', err.message);
|
|
687
|
-
reject(err);
|
|
688
|
-
});
|
|
689
668
|
});
|
|
690
669
|
|
|
691
670
|
this.__socket.on('reconnect_attempt', () => {
|
|
@@ -706,7 +685,6 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
706
685
|
});
|
|
707
686
|
|
|
708
687
|
this.__socket.on('connect', () => {
|
|
709
|
-
// this.emit('state', { connected: this.__homeySocket?.connected });
|
|
710
688
|
this.__debug('SocketIOClient.onConnect');
|
|
711
689
|
this.__handshakeClient()
|
|
712
690
|
.then(() => {
|
|
@@ -725,6 +703,7 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
725
703
|
|
|
726
704
|
this.__connectPromise.catch(err => {
|
|
727
705
|
this.__debug('SocketIOClient Error', err.message);
|
|
706
|
+
delete this.__connectPromise;
|
|
728
707
|
});
|
|
729
708
|
}
|
|
730
709
|
|
|
@@ -732,6 +711,8 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
732
711
|
}
|
|
733
712
|
|
|
734
713
|
async disconnect() {
|
|
714
|
+
// Should we wait for connect here?
|
|
715
|
+
|
|
735
716
|
if (this.__socket) {
|
|
736
717
|
await new Promise(resolve => {
|
|
737
718
|
this.__socket.once('disconnect', resolve());
|
|
@@ -740,6 +721,7 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
740
721
|
this.__socket = null;
|
|
741
722
|
});
|
|
742
723
|
}
|
|
724
|
+
|
|
743
725
|
// TODO todo what?
|
|
744
726
|
}
|
|
745
727
|
|
|
@@ -753,70 +735,71 @@ class HomeyAPIV3 extends HomeyAPI {
|
|
|
753
735
|
async __handshakeClient() {
|
|
754
736
|
this.__debug('__handshakeClient');
|
|
755
737
|
|
|
756
|
-
|
|
757
|
-
this.
|
|
758
|
-
token: this.__token,
|
|
759
|
-
homeyId: this.id,
|
|
760
|
-
}, (err, result) => {
|
|
761
|
-
if (err) return reject(err);
|
|
762
|
-
return resolve(result);
|
|
763
|
-
});
|
|
764
|
-
})
|
|
765
|
-
.catch(async err => {
|
|
766
|
-
// If token is expired, try to refresh, statusCode is for apiVersion 3 and code for apiVersion 2.
|
|
767
|
-
if (err.statusCode === 401 || err.code === 401) {
|
|
768
|
-
this.__debug('Token expired, refreshing...');
|
|
769
|
-
await this.logout();
|
|
770
|
-
await this.login();
|
|
771
|
-
|
|
772
|
-
return new Promise((resolve, reject) => {
|
|
773
|
-
this.__socket.emit('handshakeClient', {
|
|
774
|
-
token: this.__token,
|
|
775
|
-
homeyId: this.id,
|
|
776
|
-
}, (err, result) => {
|
|
777
|
-
if (err) return reject(err);
|
|
778
|
-
return resolve(result);
|
|
779
|
-
});
|
|
780
|
-
});
|
|
781
|
-
}
|
|
738
|
+
const onResult = ({ namespace }) => {
|
|
739
|
+
this.__debug('SocketIOClient.onHandshakeClientSuccess', `Namespace: ${namespace}`);
|
|
782
740
|
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
.then(({ namespace }) => {
|
|
786
|
-
this.__debug('SocketIOClient.onHandshakeClientSuccess', `Namespace: ${namespace}`);
|
|
741
|
+
return new Promise((resolve, reject) => {
|
|
742
|
+
this.__homeySocket = this.__socket.io.socket(namespace);
|
|
787
743
|
|
|
788
|
-
|
|
789
|
-
this.
|
|
744
|
+
this.__homeySocket.once('connect', () => {
|
|
745
|
+
this.__debug(`SocketIOClient.Namespace[${namespace}].onConnect`);
|
|
746
|
+
resolve();
|
|
747
|
+
});
|
|
790
748
|
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
749
|
+
this.__homeySocket.once('connect_error', err => {
|
|
750
|
+
this.__debug(`SocketIOClient.Namespace[${namespace}].onConnectError`, err.message);
|
|
751
|
+
reject(err);
|
|
752
|
+
});
|
|
795
753
|
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
});
|
|
754
|
+
this.__homeySocket.on('disconnect', reason => {
|
|
755
|
+
this.__debug(`SocketIOClient.Namespace[${namespace}].onDisconnect`, reason);
|
|
756
|
+
});
|
|
800
757
|
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
758
|
+
this.__homeySocket.on('reconnecting', attempt => {
|
|
759
|
+
this.__debug(`SocketIOClient.Namespace[${namespace}].onReconnecting (Attempt #${attempt})`);
|
|
760
|
+
});
|
|
804
761
|
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
762
|
+
this.__homeySocket.on('reconnect', () => {
|
|
763
|
+
this.__debug(`SocketIOClient.Namespace[${namespace}].onReconnect`);
|
|
764
|
+
});
|
|
808
765
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
766
|
+
this.__homeySocket.on('reconnect_error', err => {
|
|
767
|
+
this.__debug(`SocketIOClient.Namespace[${namespace}].onReconnectError`, err.message);
|
|
768
|
+
});
|
|
812
769
|
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
770
|
+
this.__homeySocket.open();
|
|
771
|
+
});
|
|
772
|
+
}
|
|
816
773
|
|
|
817
|
-
|
|
774
|
+
const handshakeClient = async () => {
|
|
775
|
+
return new Promise((resolve, reject) => {
|
|
776
|
+
this.__socket.emit('handshakeClient', {
|
|
777
|
+
token: this.__token,
|
|
778
|
+
homeyId: this.id,
|
|
779
|
+
}, (err, result) => {
|
|
780
|
+
if (err) return reject(err);
|
|
781
|
+
return resolve(result);
|
|
818
782
|
});
|
|
819
783
|
});
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
try {
|
|
787
|
+
const result = await Util.timeout(handshakeClient(), 5000, `Failed to handshake client (Timeout after 5000ms).`);
|
|
788
|
+
|
|
789
|
+
return onResult(result);
|
|
790
|
+
} catch (err) {
|
|
791
|
+
if (err.statusCode === 401 || err.code === 401) {
|
|
792
|
+
this.__debug('Token expired, refreshing...');
|
|
793
|
+
await this.logout();
|
|
794
|
+
await this.login();
|
|
795
|
+
|
|
796
|
+
const result = await Util.timeout(handshakeClient(), 5000, `Failed to handshake client (Timeout after 5000ms).`);
|
|
797
|
+
|
|
798
|
+
return onResult(result);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
throw err;
|
|
802
|
+
}
|
|
820
803
|
}
|
|
821
804
|
|
|
822
805
|
hasScope(scope) {
|