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

173 lines
5.8 KiB
TypeScript
Raw Normal View History

2018-07-10 20:37:50 -06:00
import * as React from "react";
import { Saucer } from "../../../ui/index";
2019-04-02 13:59:37 -06:00
2018-07-10 20:37:50 -06:00
import { ToggleButton } from "../../../controls/toggle_button";
import { updateConfig } from "../../actions";
2018-09-10 16:27:41 -06:00
import { last, isNumber } from "lodash";
2018-07-10 20:37:50 -06:00
import { Content } from "../../../constants";
import { FbosDetailsProps } from "./interfaces";
2018-11-26 20:39:35 -07:00
import { SourceFbosConfig, ShouldDisplay, Feature } from "../../interfaces";
import { ConfigurationName } from "farmbot";
2019-04-02 13:59:37 -06:00
import { t } from "../../../i18next_wrapper";
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";
}
};
2018-09-12 19:01:59 -06:00
/** RPI CPU temperature display row: label, temperature, indicator. */
2018-07-20 14:23:31 -06:00
export function ChipTemperatureDisplay({ chip, temperature }: {
chip?: string, temperature: number | undefined
2018-07-10 20:37:50 -06:00
}): JSX.Element {
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>
{<Saucer color={colorFromTemp(temperature)} className={"small-inline"} />}
</div>;
}
2018-09-12 19:01:59 -06:00
/** WiFi signal strength display row: label, strength, indicator. */
2018-07-20 14:23:31 -06:00
export function WiFiStrengthDisplay({ wifiStrength }: {
2018-07-10 20:37:50 -06:00
wifiStrength: number | undefined
}): JSX.Element {
const percent = wifiStrength
? Math.round(-0.0154 * wifiStrength ** 2 - 0.4 * wifiStrength + 98)
: 0;
const dbString = `${wifiStrength || 0}dBm`;
const percentString = `${percent}%`;
return <div className="wifi-strength-display">
<p>
<b>{t("WiFi Strength")}: </b>
{wifiStrength ? dbString : "N/A"}
</p>
{wifiStrength &&
<div className="percent-bar">
<div
className="percent-bar-fill"
style={{ width: percentString }}
title={dbString} />
</div>}
</div>;
}
2018-09-12 19:01:59 -06:00
/** Get the first 8 characters of a commit. */
const shortenCommit = (longCommit: string) => (longCommit || "").slice(0, 8);
/** GitHub commit display row: label, commit link. */
const CommitDisplay = ({ title, repo, commit }: {
title: string, repo: string, commit: string
}): JSX.Element => {
const shortCommit = shortenCommit(commit);
return <p>
<b>{title}: </b>
{shortCommit === "---"
? shortCommit
: <a
href={`https://github.com/FarmBot/${repo}/tree/${shortCommit}`}
target="_blank">
{shortCommit}
</a>}
</p>;
};
/** FBOS uptime display row: label and uptime in relevant unit. */
const UptimeDisplay = ({ uptime_sec }: { uptime_sec: number }): JSX.Element => {
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>;
};
2018-11-26 20:39:35 -07:00
export const betaReleaseOptIn = ({ sourceFbosConfig, shouldDisplay }: {
sourceFbosConfig: SourceFbosConfig, shouldDisplay: ShouldDisplay
}) => {
if (shouldDisplay(Feature.use_update_channel)) {
const betaOptIn = sourceFbosConfig("update_channel" as ConfigurationName);
const betaOptInValue = betaOptIn.value !== "stable";
return {
betaOptIn: { value: betaOptInValue, consistent: true }, betaOptInValue,
update: { update_channel: betaOptInValue ? "stable" : "beta" }
};
} else {
const betaOptIn = sourceFbosConfig("beta_opt_in");
const betaOptInValue = betaOptIn.value;
return {
betaOptIn, betaOptInValue,
update: { beta_opt_in: !betaOptInValue }
};
}
2018-09-12 19:01:59 -06:00
};
2018-11-26 20:39:35 -07:00
/** Label and toggle button for opting in to FBOS beta releases. */
const BetaReleaseOptInButton =
({ dispatch, sourceFbosConfig, shouldDisplay }: {
dispatch: Function,
sourceFbosConfig: SourceFbosConfig,
shouldDisplay: ShouldDisplay,
}): JSX.Element => {
const { betaOptIn, betaOptInValue, update } =
betaReleaseOptIn({ sourceFbosConfig, shouldDisplay });
return <fieldset>
<label style={{ marginTop: "0.75rem" }}>
{t("Beta release Opt-In")}
</label>
<ToggleButton
toggleValue={betaOptInValue}
dim={!betaOptIn.consistent}
toggleAction={() =>
(betaOptInValue || confirm(Content.OS_BETA_RELEASES)) &&
dispatch(updateConfig(update))} />
</fieldset>;
};
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,
2018-09-10 14:51:40 -06:00
soc_temp, wifi_level, uptime, memory_usage, disk_usage
2018-09-12 19:01:59 -06:00
} = props.botInfoSettings;
2018-07-10 20:37:50 -06:00
return <div>
<p><b>Environment: </b>{env}</p>
2018-09-12 19:01:59 -06:00
<CommitDisplay title={"Commit"} repo={"farmbot_os"} commit={commit} />
2018-07-10 20:37:50 -06:00
<p><b>Target: </b>{target}</p>
<p><b>Node name: </b>{last((node_name || "").split("@"))}</p>
<p><b>Firmware: </b>{firmware_version}</p>
2018-09-12 19:01:59 -06:00
<CommitDisplay title={"Firmware commit"}
repo={"farmbot-arduino-firmware"} commit={firmware_commit} />
{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>}
2018-07-10 20:37:50 -06:00
<ChipTemperatureDisplay chip={target} temperature={soc_temp} />
<WiFiStrengthDisplay wifiStrength={wifi_level} />
2018-09-12 19:01:59 -06:00
<BetaReleaseOptInButton
dispatch={props.dispatch}
2018-11-26 20:39:35 -07:00
shouldDisplay={props.shouldDisplay}
2018-09-12 19:01:59 -06:00
sourceFbosConfig={props.sourceFbosConfig} />
2018-07-10 20:37:50 -06:00
</div>;
}