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";
|
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
|
|
|
|
2018-06-18 15:12:00 -06:00
|
|
|
export function ago(input: string) {
|
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,
|
2017-09-26 09:40:55 -06:00
|
|
|
now = moment()): StatusRowProps {
|
2017-09-27 07:16:42 -06:00
|
|
|
|
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",
|
2017-10-03 17:01:11 -06:00
|
|
|
connectionStatus: stat ? (now.diff(moment(stat)) < SIX_HOURS) : false,
|
2018-03-30 12:33:38 -06:00
|
|
|
children: stat ? t("Last message seen ") + `${ago(stat)}.` : 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 {
|
|
|
|
const boardIdentifier = version ? version.slice(-1) : "undefined";
|
2018-08-16 07:09:03 -06:00
|
|
|
const connection = (): { status: boolean | undefined, msg: string } => {
|
|
|
|
const status = ["R", "F", "G"].includes(boardIdentifier);
|
|
|
|
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",
|
2018-03-13 19:32:19 -06:00
|
|
|
to: ["F", "G"].includes(boardIdentifier) ? "Farmduino" : "Arduino",
|
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
|
|
|
};
|
|
|
|
}
|