import * as React from "react"; import { connect } from "react-redux"; import { Page, Row, LeftPanel, CenterPanel, RightPanel, DocSlug, } from "../ui/index"; import { mapStateToProps, isPendingInstallation } from "./state_to_props"; import { Photos } from "./images/photos"; import { CameraCalibration } from "./camera_calibration/camera_calibration"; import { FarmwareProps } from "../devices/interfaces"; import { WeedDetector } from "./weed_detector/index"; import { envGet } from "./weed_detector/remote_env/selectors"; import { setActiveFarmwareByName } from "./set_active_farmware_by_name"; import { FarmwareList } from "./farmware_list"; import { FarmwareForm, needsFarmwareForm, farmwareHelpText, } from "./farmware_forms"; import { urlFriendly } from "../util"; import { ToolTips, Actions } from "../constants"; import { FarmwareInfo } from "./farmware_info"; import { Farmwares, FarmwareManifestInfo } from "./interfaces"; import { commandErr } from "../devices/actions"; import { getDevice } from "../device"; import { t } from "../i18next_wrapper"; import { isBotOnline } from "../devices/must_be_online"; import { BooleanSetting } from "../session_keys"; import { Dictionary } from "farmbot"; import { WDENVKey } from "./weed_detector/remote_env/interfaces"; /** Get the correct help text for the provided Farmware. */ const getToolTipByFarmware = (farmwares: Farmwares, farmwareName: string | undefined) => { if (farmwareName) { switch (urlFriendly(farmwareName).replace("-", "_")) { case "take_photo": case "photos": return ToolTips.PHOTOS; case "camera_calibration": return ToolTips.CAMERA_CALIBRATION; case "plant_detection": case "weed_detector": return ToolTips.WEED_DETECTOR; default: return farmwareHelpText(getFarmwareByName(farmwares, farmwareName)); } } else { return ""; } }; /** Get a documentation link for the provided Farmware if one exists. */ const getDocLinkByFarmware = (farmwareName: string | undefined): DocSlug | undefined => { if (farmwareName) { switch (urlFriendly(farmwareName).replace("-", "_")) { case "camera_calibration": return "camera-calibration"; case "plant_detection": case "weed_detector": return "weed-detection"; } } }; /** Get Farmware details for the provided Farmware name. */ const getFarmwareByName = (farmwares: Farmwares, farmwareName: string | undefined) => { switch (farmwareName) { case "Photos": return farmwares["take-photo"]; case "Camera Calibration": return farmwares["camera-calibration"]; case "Weed Detector": return farmwares["plant-detection"]; default: return farmwares[farmwareName || ""]; } }; const FARMWARE_NAMES_1ST_PARTY: Dictionary = { "take-photo": "Photos", "camera-calibration": "Camera Calibration", "plant-detection": "Weed Detector", }; export const getFormattedFarmwareName = (farmwareName: string) => FARMWARE_NAMES_1ST_PARTY[farmwareName] || farmwareName; export const farmwareUrlFriendly = (farmwareName: string) => urlFriendly(farmwareName).replace(/-/g, "_"); /** Execute a Farmware. */ const run = (farmwareName: string) => () => { getDevice().execScript(farmwareName) .then(() => { }, commandErr("Farmware execution")); }; interface BasicFarmwarePageProps { farmwareName: string; farmware: FarmwareManifestInfo | undefined; botOnline: boolean; } export const BasicFarmwarePage = ({ farmwareName, farmware, botOnline }: BasicFarmwarePageProps) =>

{isPendingInstallation(farmware) ? t("Pending installation.") : t("No inputs provided.")}

; export class RawFarmwarePage extends React.Component { get current() { return this.props.currentFarmware; } get botOnline() { return isBotOnline(this.props.syncStatus, this.props.botToMqttStatus); } componentDidMount() { if (window.innerWidth > 450) { this.props.dispatch({ type: Actions.SELECT_FARMWARE, payload: "Photos" }); } const farmwareNames = Object.values(this.props.farmwares).map(x => x.name) .concat(Object.keys(FARMWARE_NAMES_1ST_PARTY)); setActiveFarmwareByName(farmwareNames); } /** Load Farmware input panel contents for 1st & 3rd party Farmware. */ getPanelByFarmware(farmwareName: string) { const wDEnvGet = (key: WDENVKey) => envGet(key, this.props.wDEnv); switch (farmwareUrlFriendly(farmwareName)) { case "take_photo": case "photos": return ; case "camera_calibration": return ; case "plant_detection": case "weed_detector": return ; default: const farmware = getFarmwareByName(this.props.farmwares, farmwareName); return farmware && needsFarmwareForm(farmware) ? : ; } } FarmwareBackButton = (props: { className: string }) => { const infoOpen = props.className.includes("farmware-info-open"); return infoOpen ? this.props.dispatch({ type: Actions.SET_FARMWARE_INFO_STATE, payload: false }) : this.props.dispatch({ type: Actions.SELECT_FARMWARE, payload: undefined })} title={infoOpen ? t("back to farmware") : t("back to farmware list")} />; }; FarmwareInfoButton = (props: { className: string, online: boolean }) => render() { const farmware = getFarmwareByName( this.props.farmwares, this.current || "take-photo"); const farmwareOpen = this.current ? "open" : ""; const online = this.props.botToMqttStatus === "up"; const infoOpen = (this.props.infoOpen && online) ? "farmware-info-open" : ""; const activeClasses = [farmwareOpen, infoOpen].join(" "); const showFirstParty = !!this.props.getConfigValue(BooleanSetting.show_first_party_farmware); return } title={getFormattedFarmwareName(this.current || "Photos")}> {
{this.getPanelByFarmware(this.current ? this.current : "photos")}
}
} title={t("Information")} helpText={getToolTipByFarmware(this.props.farmwares, this.current) || ToolTips.PHOTOS} docPage={getDocLinkByFarmware(this.current)} show={!!farmware}>
; } } export const FarmwarePage = connect(mapStateToProps)(RawFarmwarePage);