2017-09-26 09:40:55 -06:00
|
|
|
import { isUndefined } from "lodash";
|
2019-02-04 13:54:17 -07:00
|
|
|
import moment from "moment";
|
2017-09-26 13:05:01 -06:00
|
|
|
import { StatusRowProps } from "./connectivity_row";
|
2017-09-29 13:46:08 -06:00
|
|
|
import { ConnectionStatus } from "../../connectivity/interfaces";
|
2019-04-02 13:59:37 -06:00
|
|
|
import { t } from "../../i18next_wrapper";
|
2019-07-15 16:53:28 -06:00
|
|
|
import {
|
|
|
|
getBoardCategory, isKnownBoard
|
|
|
|
} from "../components/firmware_hardware_support";
|
2017-09-26 09:40:55 -06:00
|
|
|
|
|
|
|
const HOUR = 1000 * 60 * 60;
|
2017-10-03 14:01:29 -06:00
|
|
|
|
2017-09-26 10:49:11 -06:00
|
|
|
const SIX_HOURS = HOUR * 6;
|
2017-09-26 09:40:55 -06:00
|
|
|
|
2018-02-26 06:50:41 -07:00
|
|
|
const NOT_SEEN = t("No messages seen yet.");
|
2017-10-03 14:01:29 -06:00
|
|
|
|
2019-09-05 09:16:25 -06:00
|
|
|
export function ago(input: number) {
|
2017-10-03 14:01:29 -06:00
|
|
|
return moment(new Date(input)).fromNow();
|
|
|
|
}
|
|
|
|
|
|
|
|
function lastSeen(stat: ConnectionStatus | undefined): string {
|
2018-03-30 08:59:28 -06:00
|
|
|
return stat ? t("Last message seen ") + `${ago(stat.at)}.` : NOT_SEEN;
|
2017-10-03 14:01:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function statusOf(stat: ConnectionStatus | undefined): boolean | undefined {
|
2017-10-03 14:59:39 -06:00
|
|
|
return (stat && stat.state == "up");
|
2017-10-03 14:01:29 -06:00
|
|
|
}
|
|
|
|
|
2017-10-03 14:59:39 -06:00
|
|
|
export function botToAPI(stat: string | undefined,
|
2019-09-05 09:16:25 -06:00
|
|
|
now = (new Date).getTime()): StatusRowProps {
|
|
|
|
const connectionStatus =
|
|
|
|
(stat ? ((now - new Date(stat).getTime()) < SIX_HOURS) : false);
|
2017-10-03 14:59:39 -06:00
|
|
|
return {
|
2017-09-30 22:06:29 -06:00
|
|
|
connectionName: "botAPI",
|
2017-10-03 23:17:02 -06:00
|
|
|
from: "FarmBot",
|
2017-09-27 20:27:56 -06:00
|
|
|
to: "Web App",
|
2019-09-05 09:16:25 -06:00
|
|
|
connectionStatus,
|
2019-09-23 12:56:35 -06:00
|
|
|
children: stat
|
|
|
|
? t("Last message seen ") + `${ago(new Date(stat).getTime())}.`
|
|
|
|
: NOT_SEEN
|
2017-09-26 09:40:55 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-02 08:58:03 -06:00
|
|
|
export function botToMQTT(stat: ConnectionStatus | undefined): StatusRowProps {
|
|
|
|
return {
|
2017-09-30 22:06:29 -06:00
|
|
|
connectionName: "botMQTT",
|
2017-10-03 23:17:02 -06:00
|
|
|
from: "FarmBot",
|
2018-02-26 06:50:41 -07:00
|
|
|
to: t("Message Broker"),
|
2017-10-03 14:01:29 -06:00
|
|
|
connectionStatus: statusOf(stat),
|
2018-01-25 13:10:50 -07:00
|
|
|
children: (stat && stat.state === "up")
|
|
|
|
? lastSeen(stat)
|
2018-02-26 06:50:41 -07:00
|
|
|
: t("No recent messages.")
|
2017-09-26 09:40:55 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-03 14:01:29 -06:00
|
|
|
export function browserToMQTT(status:
|
|
|
|
ConnectionStatus | undefined): StatusRowProps {
|
|
|
|
|
2017-09-26 09:40:55 -06:00
|
|
|
return {
|
2017-09-30 22:06:29 -06:00
|
|
|
connectionName: "browserMQTT",
|
2018-02-26 06:50:41 -07:00
|
|
|
from: t("Browser"),
|
|
|
|
to: t("Message Broker"),
|
2017-10-03 14:01:29 -06:00
|
|
|
children: lastSeen(status),
|
|
|
|
connectionStatus: statusOf(status)
|
2017-09-26 09:40:55 -06:00
|
|
|
};
|
|
|
|
}
|
2017-09-27 21:41:34 -06:00
|
|
|
|
|
|
|
export function botToFirmware(version: string | undefined): StatusRowProps {
|
2018-08-16 07:09:03 -06:00
|
|
|
const connection = (): { status: boolean | undefined, msg: string } => {
|
2019-07-15 16:53:28 -06:00
|
|
|
const status = isKnownBoard(version);
|
2018-08-16 07:09:03 -06:00
|
|
|
if (isUndefined(version)) {
|
|
|
|
return { status: undefined, msg: t("Unknown.") };
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status, msg: status
|
|
|
|
? t("Connected.")
|
|
|
|
: t("Disconnected.")
|
|
|
|
};
|
|
|
|
};
|
2017-09-27 21:41:34 -06:00
|
|
|
return {
|
2017-09-30 22:06:29 -06:00
|
|
|
connectionName: "botFirmware",
|
2017-09-27 21:41:34 -06:00
|
|
|
from: "Raspberry Pi",
|
2019-07-15 16:53:28 -06:00
|
|
|
to: getBoardCategory(version),
|
2018-08-16 07:09:03 -06:00
|
|
|
children: connection().msg,
|
|
|
|
connectionStatus: connection().status
|
2017-09-27 21:41:34 -06:00
|
|
|
};
|
|
|
|
}
|
2017-09-28 10:15:55 -06:00
|
|
|
|
2017-09-29 15:16:53 -06:00
|
|
|
export function browserToAPI(status?: ConnectionStatus | undefined): StatusRowProps {
|
2017-09-28 10:15:55 -06:00
|
|
|
return {
|
2017-09-30 22:06:29 -06:00
|
|
|
connectionName: "browserAPI",
|
2018-02-26 06:50:41 -07:00
|
|
|
from: t("Browser"),
|
|
|
|
to: t("Internet"),
|
2017-10-03 14:01:29 -06:00
|
|
|
children: lastSeen(status),
|
|
|
|
connectionStatus: statusOf(status)
|
2017-09-28 10:15:55 -06:00
|
|
|
};
|
|
|
|
}
|