2017-09-28 12:39:56 -06:00
|
|
|
import { store } from "../redux/store";
|
|
|
|
import { networkUp, networkDown } from "./actions";
|
2017-09-29 13:46:08 -06:00
|
|
|
import { Edge } from "./interfaces";
|
2019-09-05 15:44:12 -06:00
|
|
|
import { Actions } from "../constants";
|
2017-09-28 12:39:56 -06:00
|
|
|
|
|
|
|
/* ABOUT THIS FILE: These functions allow us to mark the network as up or
|
2017-09-28 14:51:03 -06:00
|
|
|
down from anywhere within the app (even outside of React-Redux). I usually avoid
|
|
|
|
directly importing `store`, but in this particular instance it might be
|
|
|
|
unavoidable. */
|
2017-09-28 12:39:56 -06:00
|
|
|
|
2017-12-27 08:51:52 -07:00
|
|
|
/** throttle calls to these functions to avoid unnecessary re-paints. */
|
|
|
|
const SLOWDOWN_TIME = 1500;
|
|
|
|
|
2019-09-08 17:03:27 -06:00
|
|
|
export const networkUptimeThrottleStats: Record<Edge, number> = {
|
|
|
|
"user.api": 0,
|
|
|
|
"user.mqtt": 0,
|
|
|
|
"bot.mqtt": 0
|
2017-12-27 08:51:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
function shouldThrottle(edge: Edge, now: number): boolean {
|
2019-09-17 13:26:19 -06:00
|
|
|
if (edge === "bot.mqtt") {
|
2019-09-17 15:45:40 -06:00
|
|
|
return true; // Let PING_OK / PING_NO handle bot.mqtt stuff.
|
2019-09-17 13:26:19 -06:00
|
|
|
}
|
2019-09-08 17:03:27 -06:00
|
|
|
const then = networkUptimeThrottleStats[edge];
|
2019-01-07 22:21:48 -07:00
|
|
|
const diff = now - then;
|
|
|
|
return diff < SLOWDOWN_TIME;
|
2017-12-27 08:51:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function bumpThrottle(edge: Edge, now: number) {
|
2019-09-08 17:03:27 -06:00
|
|
|
networkUptimeThrottleStats[edge] = now;
|
2017-12-27 08:51:52 -07:00
|
|
|
}
|
|
|
|
|
2019-09-05 15:44:12 -06:00
|
|
|
export const dispatchQosStart = (id: string) => {
|
|
|
|
store.dispatch({
|
2019-09-11 15:58:53 -06:00
|
|
|
type: Actions.PING_START,
|
2019-09-05 15:44:12 -06:00
|
|
|
payload: { id }
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-10 12:53:20 -07:00
|
|
|
export const dispatchNetworkUp = (edge: Edge, at: number) => {
|
2019-09-05 09:16:25 -06:00
|
|
|
if (shouldThrottle(edge, at)) { return; }
|
2019-09-11 15:58:53 -06:00
|
|
|
store.dispatch(networkUp(edge, at));
|
2019-09-05 09:16:25 -06:00
|
|
|
bumpThrottle(edge, at);
|
2017-12-27 08:51:52 -07:00
|
|
|
};
|
2017-09-28 12:39:56 -06:00
|
|
|
|
2019-12-10 12:53:20 -07:00
|
|
|
export const dispatchNetworkDown = (edge: Edge, at: number) => {
|
2019-09-05 09:16:25 -06:00
|
|
|
if (shouldThrottle(edge, at)) { return; }
|
2019-09-11 15:58:53 -06:00
|
|
|
store.dispatch(networkDown(edge, at));
|
2019-09-05 09:16:25 -06:00
|
|
|
bumpThrottle(edge, at);
|
2017-12-27 08:51:52 -07:00
|
|
|
};
|
2019-09-11 15:58:53 -06:00
|
|
|
|
|
|
|
export const pingOK = (id: string, at: number) => {
|
|
|
|
const action = { type: Actions.PING_OK, payload: { id, at } };
|
|
|
|
store.dispatch(action);
|
|
|
|
};
|
|
|
|
|
2019-09-13 14:58:58 -06:00
|
|
|
export const pingNO = (id: string, at: number) => {
|
|
|
|
const action = { type: Actions.PING_NO, payload: { id, at } };
|
2019-09-11 15:58:53 -06:00
|
|
|
store.dispatch(action);
|
|
|
|
};
|