Farmbot-Web-App/frontend/devices/components/fbos_settings/fbos_details.tsx

305 lines
11 KiB
TypeScript
Raw Normal View History

2018-07-10 20:37:50 -06:00
import * as React from "react";
2019-11-01 15:25:39 -06:00
import { Saucer, FBSelect } from "../../../ui";
2018-07-10 20:37:50 -06:00
import { updateConfig } from "../../actions";
2019-09-24 15:50:32 -06:00
import { last, isNumber, isString } from "lodash";
2018-07-10 20:37:50 -06:00
import { Content } from "../../../constants";
import { FbosDetailsProps } from "./interfaces";
2019-11-01 15:25:39 -06:00
import { SourceFbosConfig } from "../../interfaces";
2018-11-26 20:39:35 -07:00
import { ConfigurationName } from "farmbot";
2019-04-02 13:59:37 -06:00
import { t } from "../../../i18next_wrapper";
2019-06-07 18:26:12 -06:00
import { LastSeen } from "./last_seen_row";
2019-07-16 13:07:29 -06:00
import { Popover } from "@blueprintjs/core";
2019-09-24 15:50:32 -06:00
import moment from "moment";
import { timeFormatString } from "../../../util";
import { TimeSettings } from "../../../interfaces";
2019-11-01 15:25:39 -06:00
import { StringConfigKey } from "farmbot/dist/resources/configs/fbos";
2020-02-07 16:06:40 -07:00
import { boardType, FIRMWARE_CHOICES_DDI } from "../firmware_hardware_support";
2020-02-15 11:30:23 -07:00
import { ExternalUrl, FarmBotRepo } from "../../../external_urls";
2018-07-10 20:37:50 -06:00
2018-09-12 19:01:59 -06:00
/** Return an indicator color for the given temperature (C). */
2018-07-10 20:37:50 -06:00
export const colorFromTemp = (temp: number | undefined): string => {
if (!temp) {
return "gray";
}
if (temp < 0) {
return "lightblue";
} else if (temp < 10) {
return "blue";
} else if (temp > 75) {
return "red";
} else if (temp > 60) {
return "yellow";
} else {
return "green";
}
};
2019-07-16 13:07:29 -06:00
interface ChipTemperatureDisplayProps {
chip?: string;
temperature: number | undefined;
}
2018-09-12 19:01:59 -06:00
/** RPI CPU temperature display row: label, temperature, indicator. */
2019-07-16 13:07:29 -06:00
export function ChipTemperatureDisplay(
2020-02-28 09:35:32 -07:00
{ chip, temperature }: ChipTemperatureDisplayProps,
2019-07-16 13:07:29 -06:00
): JSX.Element {
2018-07-10 20:37:50 -06:00
return <div className="chip-temp-display">
<p>
2018-07-20 14:23:31 -06:00
<b>{chip && chip.toUpperCase()} {t("CPU temperature")}: </b>
{temperature ? <span>{temperature}&deg;C</span> : t("unknown")}
2018-07-10 20:37:50 -06:00
</p>
2019-04-24 17:27:04 -06:00
<Saucer color={colorFromTemp(temperature)} className={"small-inline"} />
2018-07-10 20:37:50 -06:00
</div>;
}
2019-07-16 13:07:29 -06:00
interface WiFiStrengthDisplayProps {
wifiStrength: number | undefined;
wifiStrengthPercent?: number | undefined;
2020-02-24 09:55:57 -07:00
extraInfo?: boolean;
2019-07-16 13:07:29 -06:00
}
2018-09-12 19:01:59 -06:00
/** WiFi signal strength display row: label, strength, indicator. */
2019-07-16 13:07:29 -06:00
export function WiFiStrengthDisplay(
2020-02-28 09:35:32 -07:00
{ wifiStrength, wifiStrengthPercent, extraInfo }: WiFiStrengthDisplayProps,
2019-07-16 13:07:29 -06:00
): JSX.Element {
2018-07-10 20:37:50 -06:00
const percent = wifiStrength
? Math.round(-0.0154 * wifiStrength ** 2 - 0.4 * wifiStrength + 98)
: 0;
const dbString = `${wifiStrength || 0}dBm`;
2019-07-16 13:07:29 -06:00
const percentString = `${wifiStrengthPercent || percent}%`;
2020-02-24 09:55:57 -07:00
const numberDisplay =
extraInfo ? `${percentString} (${dbString})` : percentString;
2018-07-10 20:37:50 -06:00
return <div className="wifi-strength-display">
<p>
2019-09-06 07:23:32 -06:00
<b>{t("WiFi strength")}: </b>
2020-02-24 09:55:57 -07:00
{wifiStrength ? numberDisplay : "N/A"}
2018-07-10 20:37:50 -06:00
</p>
{wifiStrength &&
<div className="percent-bar">
<div
className="percent-bar-fill"
style={{ width: percentString }}
title={dbString} />
</div>}
</div>;
}
2019-04-24 17:27:04 -06:00
/** Available throttle info. */
export enum ThrottleType {
UnderVoltage = "UnderVoltage",
ArmFrequencyCapped = "ArmFrequencyCapped",
Throttled = "Throttled",
SoftTempLimit = "SoftTempLimit",
}
/** Bit positions for throttle flags. */
const THROTTLE_BIT_LOOKUP:
Record<ThrottleType, Record<"active" | "occurred", number>> = {
[ThrottleType.UnderVoltage]: { active: 0, occurred: 16 },
[ThrottleType.ArmFrequencyCapped]: { active: 1, occurred: 17 },
[ThrottleType.Throttled]: { active: 2, occurred: 18 },
[ThrottleType.SoftTempLimit]: { active: 3, occurred: 19 },
};
/** Return a color based on throttle flag states. */
export const colorFromThrottle =
(throttled: string, throttleType: ThrottleType) => {
const throttleCode = parseInt(throttled, 16);
const bit = THROTTLE_BIT_LOOKUP[throttleType];
// tslint:disable-next-line:no-bitwise
const active = throttleCode & (1 << bit.active);
// tslint:disable-next-line:no-bitwise
const occurred = throttleCode & (1 << bit.occurred);
if (active) {
return "red";
} else if (occurred) {
return "yellow";
} else {
return "green";
}
};
2019-07-16 13:07:29 -06:00
const THROTTLE_COLOR_KEY = () => ({
red: t("active"),
yellow: t("occurred"),
2019-08-26 12:20:46 -06:00
green: t("ok")
2019-07-16 13:07:29 -06:00
});
interface ThrottleIndicatorProps {
throttleDataString: string;
throttleType: ThrottleType;
}
/** Saucer with color and title indicating throttle state. */
const ThrottleIndicator = (props: ThrottleIndicatorProps) => {
const { throttleDataString, throttleType } = props;
const throttleColor = colorFromThrottle(throttleDataString, throttleType);
return <Saucer className={"small-inline"}
title={THROTTLE_COLOR_KEY()[throttleColor]}
color={throttleColor} />;
};
/** Visual representation of throttle state. */
const ThrottleDisplay = (dataString: string) =>
<div className="throttle-display">
{Object.keys(THROTTLE_BIT_LOOKUP).map((key: ThrottleType) =>
<div className="throttle-row" key={key}>
<ThrottleIndicator throttleDataString={dataString} throttleType={key} />
<p>{key}</p>
</div>)}
</div>;
2019-04-24 17:27:04 -06:00
interface VoltageDisplayProps {
chip?: string;
throttled: string | undefined;
}
/** RPI throttle state display row: label, indicator. */
export const VoltageDisplay = ({ chip, throttled }: VoltageDisplayProps) =>
throttled
? <div className="voltage-display">
<p>
<b>{chip && chip.toUpperCase()} {t("Voltage")}: </b>
</p>
2019-07-16 13:07:29 -06:00
<Popover usePortal={false}>
<ThrottleIndicator
throttleDataString={throttled}
throttleType={ThrottleType.UnderVoltage} />
{ThrottleDisplay(throttled)}
</Popover>
2019-04-24 17:27:04 -06:00
</div> : <div className="voltage-display" />;
2018-09-12 19:01:59 -06:00
/** Get the first 8 characters of a commit. */
const shortenCommit = (longCommit: string) => (longCommit || "").slice(0, 8);
2019-07-16 13:07:29 -06:00
interface CommitDisplayProps {
title: string;
2020-02-15 11:30:23 -07:00
repo: FarmBotRepo;
2019-07-16 13:07:29 -06:00
commit: string;
}
2018-09-12 19:01:59 -06:00
/** GitHub commit display row: label, commit link. */
2019-07-16 13:07:29 -06:00
const CommitDisplay = (
2020-02-28 09:35:32 -07:00
{ title, repo, commit }: CommitDisplayProps,
2019-07-16 13:07:29 -06:00
): JSX.Element => {
2018-09-12 19:01:59 -06:00
const shortCommit = shortenCommit(commit);
return <p>
<b>{title}: </b>
{shortCommit === "---"
? shortCommit
: <a
2020-02-15 11:30:23 -07:00
href={`${ExternalUrl.gitHubFarmBot}/${repo}/tree/${shortCommit}`}
2018-09-12 19:01:59 -06:00
target="_blank">
{shortCommit}
</a>}
</p>;
};
2019-07-16 13:07:29 -06:00
interface UptimeDisplayProps {
uptime_sec: number;
}
2018-09-12 19:01:59 -06:00
/** FBOS uptime display row: label and uptime in relevant unit. */
2019-07-16 13:07:29 -06:00
const UptimeDisplay = ({ uptime_sec }: UptimeDisplayProps): JSX.Element => {
2018-09-12 19:01:59 -06:00
const convertUptime = (seconds: number) => {
if (seconds >= 172800) {
return `${Math.round(seconds / 86400)} ${t("days")}`;
} else if (seconds >= 7200) {
return `${Math.round(seconds / 3600)} ${t("hours")}`;
} else if (seconds >= 120) {
return `${Math.round(seconds / 60)} ${t("minutes")}`;
} else {
return `${seconds} ${t("seconds")}`;
}
};
return <p><b>{t("Uptime")}: </b>{convertUptime(uptime_sec)}</p>;
};
2019-11-01 15:25:39 -06:00
export interface BetaReleaseOptInButtonProps {
2019-07-16 13:07:29 -06:00
dispatch: Function;
sourceFbosConfig: SourceFbosConfig;
}
2018-11-26 20:39:35 -07:00
/** Label and toggle button for opting in to FBOS beta releases. */
2019-11-01 15:25:39 -06:00
export const BetaReleaseOptIn = (
2020-02-28 09:35:32 -07:00
{ dispatch, sourceFbosConfig }: BetaReleaseOptInButtonProps,
2019-07-16 13:07:29 -06:00
): JSX.Element => {
2019-11-01 15:25:39 -06:00
const betaOptIn = sourceFbosConfig("update_channel" as ConfigurationName).value;
return <fieldset className={"os-release-channel"}>
<label>
{t("OS release channel")}
2019-07-16 13:07:29 -06:00
</label>
2019-11-01 15:25:39 -06:00
<FBSelect
selectedItem={{ label: t("" + betaOptIn), value: "" + betaOptIn }}
onChange={ddi =>
(ddi.value == "stable" || confirm(Content.OS_BETA_RELEASES)) &&
dispatch(updateConfig({ ["update_channel" as StringConfigKey]: ddi.value }))}
list={[
{ label: t("stable"), value: "stable" },
{ label: t("beta"), value: "beta" },
{ label: t("staging"), value: "staging" },
{ label: t("qa"), value: "qa" },
]} />
2019-07-16 13:07:29 -06:00
</fieldset>;
};
2018-11-26 20:39:35 -07:00
2019-09-24 15:50:32 -06:00
/** Format datetime string for display. */
const reformatDatetime = (datetime: string, timeSettings: TimeSettings) =>
moment(datetime)
.utcOffset(timeSettings.utcOffset)
.format(`MMMM D, ${timeFormatString(timeSettings)}`);
2020-02-07 16:06:40 -07:00
const reformatFwVersion = (firmwareVersion: string | undefined): string => {
const version = firmwareVersion ?
firmwareVersion.split(".").slice(0, 3).join(".") : "none";
const board = FIRMWARE_CHOICES_DDI[boardType(firmwareVersion)]?.label || "";
return version == "none" ? "---" : `${version} ${board}`;
};
2018-09-12 19:01:59 -06:00
/** Current technical information about FarmBot OS running on the device. */
2018-07-10 20:37:50 -06:00
export function FbosDetails(props: FbosDetailsProps) {
const {
env, commit, target, node_name, firmware_version, firmware_commit,
2019-07-16 13:07:29 -06:00
soc_temp, wifi_level, uptime, memory_usage, disk_usage, throttled,
2019-09-24 15:50:32 -06:00
wifi_level_percent, cpu_usage, private_ip,
2019-09-25 09:25:18 -06:00
} = props.botInfoSettings;
2020-04-23 16:58:33 -06:00
const { last_ota, last_ota_checkup, fbos_version } = props.deviceAccount.body;
2020-02-20 19:38:50 -07:00
const infoFwCommit = firmware_version?.includes(".") ? firmware_commit : "---";
const firmwareCommit = firmware_version?.split("-")[1] || infoFwCommit;
2018-09-12 19:01:59 -06:00
2020-02-28 09:34:28 -07:00
return <div className={"farmbot-os-details"}>
2019-06-07 18:26:12 -06:00
<LastSeen
dispatch={props.dispatch}
botToMqttLastSeen={props.botToMqttLastSeen}
timeSettings={props.timeSettings}
device={props.deviceAccount} />
2020-04-23 16:58:33 -06:00
<p><b>{t("Version last seen")}: </b>{fbos_version}</p>
2019-09-11 11:50:48 -06:00
<p><b>{t("Environment")}: </b>{env}</p>
2020-02-15 11:30:23 -07:00
<CommitDisplay title={t("Commit")}
repo={FarmBotRepo.FarmBotOS} commit={commit} />
2019-09-11 11:50:48 -06:00
<p><b>{t("Target")}: </b>{target}</p>
<p><b>{t("Node name")}: </b>{last((node_name || "").split("@"))}</p>
2019-09-24 15:50:32 -06:00
<p><b>{t("Device ID")}: </b>{props.deviceAccount.body.id}</p>
{isString(private_ip) && <p><b>{t("Local IP address")}: </b>{private_ip}</p>}
2020-02-07 16:06:40 -07:00
<p><b>{t("Firmware")}: </b>{reformatFwVersion(firmware_version)}</p>
2019-09-11 11:50:48 -06:00
<CommitDisplay title={t("Firmware commit")}
2020-02-15 11:30:23 -07:00
repo={FarmBotRepo.FarmBotArduinoFirmware} commit={firmwareCommit} />
2020-02-07 16:06:40 -07:00
<p><b>{t("Firmware code")}: </b>{firmware_version}</p>
2018-09-12 19:01:59 -06:00
{isNumber(uptime) && <UptimeDisplay uptime_sec={uptime} />}
2018-11-29 12:54:53 -07:00
{isNumber(memory_usage) &&
<p><b>{t("Memory usage")}: </b>{memory_usage}MB</p>}
{isNumber(disk_usage) && <p><b>{t("Disk usage")}: </b>{disk_usage}%</p>}
2019-09-24 15:50:32 -06:00
{isNumber(cpu_usage) && <p><b>{t("CPU usage")}: </b>{cpu_usage}%</p>}
2018-07-10 20:37:50 -06:00
<ChipTemperatureDisplay chip={target} temperature={soc_temp} />
2020-02-24 09:55:57 -07:00
<WiFiStrengthDisplay extraInfo={true}
2019-07-16 13:07:29 -06:00
wifiStrength={wifi_level} wifiStrengthPercent={wifi_level_percent} />
2019-04-24 17:27:04 -06:00
<VoltageDisplay chip={target} throttled={throttled} />
2019-11-01 15:25:39 -06:00
<BetaReleaseOptIn
dispatch={props.dispatch} sourceFbosConfig={props.sourceFbosConfig} />
2019-09-24 15:50:32 -06:00
{last_ota_checkup && <p><b>{t("Last checked for updates")}: </b>
{reformatDatetime(last_ota_checkup, props.timeSettings)}</p>}
{last_ota && <p><b>{t("Last updated")}: </b>
{reformatDatetime(last_ota, props.timeSettings)}</p>}
2018-07-10 20:37:50 -06:00
</div>;
}