Fixed connection state updating for devices
This commit is contained in:
parent
07a16f79a1
commit
757141c3cb
@ -1,4 +1,4 @@
|
|||||||
import { action, observable, when } from "mobx";
|
import { action, autorun, observable, when } from "mobx";
|
||||||
import { update } from "serializr";
|
import { update } from "serializr";
|
||||||
|
|
||||||
import { TokenStore } from "@app/state/TokenStore";
|
import { TokenStore } from "@app/state/TokenStore";
|
||||||
@ -33,6 +33,11 @@ export class WSSprinklersDevice extends s.SprinklersDevice {
|
|||||||
super();
|
super();
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this._id = id;
|
this._id = id;
|
||||||
|
|
||||||
|
autorun(() => {
|
||||||
|
this.connectionState.clientToServer = this.api.connectionState.clientToServer;
|
||||||
|
this.connectionState.serverToBroker = this.api.connectionState.serverToBroker;
|
||||||
|
});
|
||||||
this.waitSubscribe();
|
this.waitSubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,9 +51,9 @@ export class WSSprinklersDevice extends s.SprinklersDevice {
|
|||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
await this.api.makeRequest("deviceSubscribe", subscribeRequest);
|
await this.api.makeRequest("deviceSubscribe", subscribeRequest);
|
||||||
this.connectionState.serverToBroker = true;
|
this.connectionState.brokerToDevice = true;
|
||||||
this.connectionState.clientToServer = true;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
this.connectionState.brokerToDevice = false;
|
||||||
if ((err as ws.IError).code === ErrorCode.NoPermission) {
|
if ((err as ws.IError).code === ErrorCode.NoPermission) {
|
||||||
this.connectionState.hasPermission = false;
|
this.connectionState.hasPermission = false;
|
||||||
} else {
|
} else {
|
||||||
@ -62,9 +67,9 @@ export class WSSprinklersDevice extends s.SprinklersDevice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
waitSubscribe = () => {
|
waitSubscribe = () => {
|
||||||
when(() => this.api.connected, () => {
|
when(() => this.api.authenticated, () => {
|
||||||
this.subscribe();
|
this.subscribe();
|
||||||
when(() => !this.api.connected, this.waitSubscribe);
|
when(() => !this.api.authenticated, this.waitSubscribe);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,6 +81,9 @@ export class WebSocketRpcClient implements s.SprinklersRPC {
|
|||||||
@observable connectionState: s.ConnectionState = new s.ConnectionState();
|
@observable connectionState: s.ConnectionState = new s.ConnectionState();
|
||||||
socket: WebSocket | null = null;
|
socket: WebSocket | null = null;
|
||||||
|
|
||||||
|
@observable
|
||||||
|
authenticated: boolean = false;
|
||||||
|
|
||||||
tokenStore: TokenStore;
|
tokenStore: TokenStore;
|
||||||
|
|
||||||
private nextRequestId = Math.round(Math.random() * 1000000);
|
private nextRequestId = Math.round(Math.random() * 1000000);
|
||||||
@ -128,8 +136,15 @@ export class WebSocketRpcClient implements s.SprinklersRPC {
|
|||||||
|
|
||||||
async tryAuthenticate() {
|
async tryAuthenticate() {
|
||||||
when(() => this.connectionState.clientToServer === true
|
when(() => this.connectionState.clientToServer === true
|
||||||
&& this.tokenStore.accessToken.isValid, () => {
|
&& this.tokenStore.accessToken.isValid, async () => {
|
||||||
return this.authenticate(this.tokenStore.accessToken.token!);
|
try {
|
||||||
|
await this.authenticate(this.tokenStore.accessToken.token!);
|
||||||
|
this.authenticated = true;
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({ err }, "error authenticating websocket connection");
|
||||||
|
// TODO message?
|
||||||
|
this.authenticated = false;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,6 +227,7 @@ export class WebSocketRpcClient implements s.SprinklersRPC {
|
|||||||
private onOpen() {
|
private onOpen() {
|
||||||
log.info("established websocket connection");
|
log.info("established websocket connection");
|
||||||
this.connectionState.clientToServer = true;
|
this.connectionState.clientToServer = true;
|
||||||
|
this.authenticated = false;
|
||||||
this.tryAuthenticate();
|
this.tryAuthenticate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,6 +235,7 @@ export class WebSocketRpcClient implements s.SprinklersRPC {
|
|||||||
private onDisconnect = action(() => {
|
private onDisconnect = action(() => {
|
||||||
this.connectionState.serverToBroker = null;
|
this.connectionState.serverToBroker = null;
|
||||||
this.connectionState.clientToServer = false;
|
this.connectionState.clientToServer = false;
|
||||||
|
this.authenticated = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
private onClose(event: CloseEvent) {
|
private onClose(event: CloseEvent) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user