Skip to content

Commit 83a6e60

Browse files
committed
Implemented refreshing token.
1 parent 7544ea6 commit 83a6e60

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/platform.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ export class HomebridgePrincessHeaterPlatform implements DynamicPlatformPlugin {
6666

6767
const httpAPIClient = new HttpAPIClient(this.log, authorizationHeaderValue);
6868

69-
const token = await httpAPIClient.getToken();
70-
71-
const wsAPIClient = new WsAPIClient(this.log, token);
69+
const wsAPIClient = new WsAPIClient(this.log, httpAPIClient);
7270

7371
const devices = await httpAPIClient.getDevices();
7472

src/ws/index.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {HelloWsOutgoingMessage, ResponseWsIncomingMessage, WsIncomingMessage, Ws
33
import {MessageType, WS_URL} from './const';
44
import {EventEmitter} from 'events';
55
import {Logger} from 'homebridge';
6+
import {HttpAPIClient} from '../http';
67

78
const TIMEOUT = 60 * 1000; // 1 minute
89

@@ -14,7 +15,7 @@ export class WsAPIClient extends EventEmitter {
1415

1516
constructor(
1617
private readonly log: Logger,
17-
private readonly token: string,
18+
private readonly httpApiClient: HttpAPIClient,
1819
) {
1920
super();
2021
}
@@ -24,7 +25,20 @@ export class WsAPIClient extends EventEmitter {
2425
): Promise<ResponseWsIncomingMessage> {
2526

2627
const ws = await this._getWebSocket();
27-
return this._send<M>(message, ws);
28+
29+
return this._send<M>(message, ws)
30+
.catch(err => {
31+
if (err.status === 401) {
32+
this.log.warn('Error code 401. Might mean we need to refresh the token ->', message, err);
33+
return this._handshake(ws).then(ws => this._send<M>(message, ws));
34+
} else {
35+
throw err;
36+
}
37+
})
38+
.catch(err => {
39+
this.log.error('Failed to send WS message ->', message, err);
40+
throw err;
41+
});
2842
}
2943

3044
private _getWebSocket(): Promise<WebSocket> {
@@ -82,14 +96,17 @@ export class WsAPIClient extends EventEmitter {
8296
});
8397
}
8498

85-
private _handshake(ws: WebSocket): Promise<WebSocket> {
99+
private async _handshake(ws: WebSocket): Promise<WebSocket> {
100+
101+
const token = await this.httpApiClient.getToken();
102+
86103
return this._send<HelloWsOutgoingMessage>({
87104
type: MessageType.Hello,
88105
version: '2.4.0',
89106
os: 'ios',
90107
source: 'climate',
91108
compatibility: 3,
92-
token: this.token,
109+
token,
93110
}, ws).then(() => {
94111
this.log.debug('WS handshake successful');
95112
return ws;
@@ -112,7 +129,7 @@ export class WsAPIClient extends EventEmitter {
112129
JSON.stringify(fullMessage),
113130
(err) => {
114131
if (err) {
115-
this.log.warn('Failed to send message ->', fullMessage, err);
132+
this.log.warn('Failed to send WS message ->', fullMessage, err);
116133
rej(err);
117134
} else {
118135
this.log.debug('WS message sent ->', fullMessage);

0 commit comments

Comments
 (0)