Farmbot-Web-App/frontend/connectivity/maybe_negate_status.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

import { SyncStatus } from "farmbot";
/** There are a bunch of ways we need to handle data consistency management
* depending on a number of factors. */
export enum SyncStrat {
2017-11-21 05:00:30 -07:00
/** Auto sync is enabled by user. */
AUTO,
2017-11-21 05:00:30 -07:00
/** Auto sync is not enabled by user*/
MANUAL,
/** Not enough info to say. */
OFFLINE
}
2017-11-21 05:00:30 -07:00
/** "Hints" for figuring out which of the 4 strategies is appropriate. */
interface StratHints {
2017-11-21 05:00:30 -07:00
/** Not always available if device is offline. */
fbosVersion?: string;
autoSync: boolean;
}
export function determineStrategy(x: StratHints): SyncStrat {
const { fbosVersion, autoSync } = x;
2017-11-21 05:00:30 -07:00
/** First pass: Is it even on right now? Don't investigate further if so. */
if (!fbosVersion) {
return SyncStrat.OFFLINE;
}
2018-05-01 20:35:23 -06:00
/** Second pass: Is auto_sync enabled? */
const strat = autoSync ? "AUTO" : "MANUAL";
return SyncStrat[strat];
}
export interface OverrideHints {
consistent: boolean;
syncStatus: SyncStatus | undefined;
fbosVersion: string | undefined;
autoSync: boolean;
}
/** Sometimes we can't trust what FBOS tells us. */
export function maybeNegateStatus(x: OverrideHints): SyncStatus | undefined {
const {
consistent,
/** The bot's __CURRENT__ sync status. */
syncStatus,
fbosVersion,
autoSync
} = x;
/** No need to override if data is consistent. */
2017-11-21 09:43:15 -07:00
if (consistent) { return syncStatus; }
switch (determineStrategy({ autoSync, fbosVersion })) {
case SyncStrat.AUTO:
2017-11-21 09:43:15 -07:00
return "syncing";
case SyncStrat.MANUAL:
2017-11-21 09:43:15 -07:00
return "sync_now";
case SyncStrat.OFFLINE:
return "unknown";
}
}