sprinklers3/common/sprinklersRpc/ConnectionState.ts

82 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-06-16 23:54:03 -06:00
import { computed, observable } from "mobx";
export class ConnectionState {
2018-09-02 02:57:55 -06:00
/**
* Represents if a client is connected to the sprinklers3 server (eg. via websocket)
* Can be null if there is no client involved
*/
@observable
clientToServer: boolean | null = null;
2018-06-16 23:54:03 -06:00
2018-09-02 02:57:55 -06:00
/**
* Represents if the sprinklers3 server is connected to the broker (eg. via mqtt)
* Can be null if there is no broker involved
*/
@observable
serverToBroker: boolean | null = null;
2018-06-16 23:54:03 -06:00
2018-09-02 02:57:55 -06:00
/**
* Represents if the device is connected to the broker and we can communicate with it (eg. via mqtt)
* Can be null if there is no device involved
*/
@observable
brokerToDevice: boolean | null = null;
2018-06-16 23:54:03 -06:00
2018-09-02 02:57:55 -06:00
/**
* Represents if whoever is trying to access this device has permission to access it.
* Is null if there is no concept of access involved.
*/
@observable
hasPermission: boolean | null = null;
2018-06-29 18:16:06 -06:00
2018-09-02 02:57:55 -06:00
@computed
get noPermission() {
return this.hasPermission === false;
}
2018-06-29 18:16:06 -06:00
2018-09-02 02:57:55 -06:00
@computed
get isAvailable(): boolean {
if (this.hasPermission === false) {
return false;
}
if (this.brokerToDevice != null) {
return true;
}
if (this.serverToBroker != null) {
return this.serverToBroker;
}
if (this.clientToServer != null) {
return this.clientToServer;
2018-06-16 23:54:03 -06:00
}
2018-09-02 02:57:55 -06:00
return false;
}
2018-06-29 18:16:06 -06:00
2018-09-02 02:57:55 -06:00
@computed
get isDeviceConnected(): boolean | null {
if (this.hasPermission === false) {
return false;
}
2018-09-02 02:57:55 -06:00
if (this.serverToBroker === false || this.clientToServer === false) {
return null;
}
if (this.brokerToDevice != null) {
return this.brokerToDevice;
}
return null;
}
2018-09-02 02:57:55 -06:00
@computed
get isServerConnected(): boolean | null {
if (this.hasPermission === false) {
return false;
}
if (this.serverToBroker != null) {
return this.serverToBroker;
}
if (this.clientToServer != null) {
return this.brokerToDevice;
2018-06-29 18:16:06 -06:00
}
2018-09-02 02:57:55 -06:00
return null;
}
2018-06-16 23:54:03 -06:00
}