Skip to content

Commit

Permalink
Upgrade to fbjs8-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed May 17, 2019
1 parent d3f7cc6 commit 290d2fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
15 changes: 12 additions & 3 deletions frontend/connectivity/__tests__/ping_mqtt_test.ts
Expand Up @@ -22,10 +22,10 @@ import {
isInactive,
sendOutboundPing,
startPinging,
PING,
buildPing,
ACTIVE_THRESHOLD
} from "../ping_mqtt";
import { Farmbot } from "farmbot";
import { Farmbot, RpcRequest, RpcRequestBodyItem } from "farmbot";
import { dispatchNetworkDown, dispatchNetworkUp } from "../index";
import { FarmBotInternalConfig } from "farmbot/dist/config";

Expand All @@ -38,6 +38,15 @@ let state: Partial<FarmBotInternalConfig> = {

function fakeBot(): Farmbot {
const fb: Partial<Farmbot> = {
rpcShim: jest.fn((_: RpcRequestBodyItem[]): RpcRequest => {
return {
kind: "rpc_request",
args: {
label: "ping",
priority: 0
}
};
}),
setConfig: jest.fn(),
publish: jest.fn(),
on: jest.fn(),
Expand Down Expand Up @@ -96,7 +105,7 @@ describe("ping util", () => {
const bot = fakeBot();
const oldOutbound = readPing(bot, "out");
sendOutboundPing(bot);
expect(bot.publish).toHaveBeenCalledWith(PING);
expect(bot.publish).toHaveBeenCalledWith(buildPing(bot));
expect(oldOutbound).toBeLessThanOrEqual(readPing(bot, "out") || NaN);
});

Expand Down
16 changes: 9 additions & 7 deletions frontend/connectivity/ping_mqtt.tsx
@@ -1,4 +1,4 @@
import { Farmbot, RpcRequest } from "farmbot";
import { Farmbot } from "farmbot";
import { dispatchNetworkDown, dispatchNetworkUp } from "./index";
import { isNumber } from "lodash";
import axios from "axios";
Expand All @@ -9,12 +9,14 @@ import { FarmBotInternalConfig } from "farmbot/dist/config";
export const PING_INTERVAL = 3000;
export const ACTIVE_THRESHOLD = PING_INTERVAL * 2;

const label = "ping";
const PING_LABEL = "ping";
export const LAST_IN: keyof FarmBotInternalConfig = "LAST_PING_IN";
export const LAST_OUT: keyof FarmBotInternalConfig = "LAST_PING_OUT";
export const PING: Readonly<RpcRequest> = {
kind: "rpc_request", args: { label, priority: 0 }
};
export function buildPing(bot: Farmbot) {
const rpc = bot.rpcShim([]);
rpc.args.label = PING_LABEL;
return rpc;
}

type Direction = "in" | "out";

Expand Down Expand Up @@ -42,7 +44,7 @@ export function isInactive(last: number, now: number): boolean {
}

export function sendOutboundPing(bot: Farmbot) {
bot.publish(PING);
bot.publish(buildPing(bot));
const now = timestamp();
const lastPing = readPing(bot, "in");
lastPing && (isInactive(lastPing, now) ? markStale() : markActive());
Expand All @@ -51,7 +53,7 @@ export function sendOutboundPing(bot: Farmbot) {

export function startPinging(bot: Farmbot) {
setInterval(() => sendOutboundPing(bot), PING_INTERVAL);
bot.on(label, () => writePing(bot, "in"));
bot.on(PING_LABEL, () => writePing(bot, "in"));
}

export function pingAPI() {
Expand Down

0 comments on commit 290d2fd

Please sign in to comment.