2017-08-24 17:55:35 -06:00
|
|
|
import * as React from "react";
|
2018-01-20 07:46:44 -07:00
|
|
|
import { DropDownItem, Row, Col, FBSelect } from "../../../ui/index";
|
2017-12-07 21:05:22 -07:00
|
|
|
import {
|
|
|
|
CameraSelectionProps, CameraSelectionState
|
2018-03-10 02:42:15 -07:00
|
|
|
} from "./interfaces";
|
2019-06-24 15:39:49 -06:00
|
|
|
import { info, success, error } from "../../../toast/toast";
|
2017-12-07 21:05:22 -07:00
|
|
|
import { getDevice } from "../../../device";
|
|
|
|
import { ColWidth } from "../farmbot_os_settings";
|
2019-12-27 11:37:54 -07:00
|
|
|
import { Feature, UserEnv } from "../../interfaces";
|
2019-04-02 13:59:37 -06:00
|
|
|
import { t } from "../../../i18next_wrapper";
|
2019-12-27 11:37:54 -07:00
|
|
|
import { Content, ToolTips } from "../../../constants";
|
|
|
|
|
|
|
|
/** Check if the camera has been disabled. */
|
|
|
|
export const cameraDisabled = (env: UserEnv): boolean =>
|
|
|
|
parseCameraSelection(env) === Camera.NONE;
|
|
|
|
|
|
|
|
/** `disabled` and `title` props for buttons with actions that use the camera. */
|
|
|
|
export const cameraBtnProps = (env: UserEnv) => {
|
|
|
|
const disabled = cameraDisabled(env);
|
|
|
|
return disabled
|
|
|
|
? {
|
|
|
|
class: "pseudo-disabled",
|
|
|
|
click: () =>
|
|
|
|
error(t(ToolTips.SELECT_A_CAMERA), t(Content.NO_CAMERA_SELECTED)),
|
|
|
|
title: t(Content.NO_CAMERA_SELECTED)
|
|
|
|
}
|
|
|
|
: { class: "", click: undefined, title: "" };
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Camera {
|
|
|
|
USB = "USB",
|
|
|
|
RPI = "RPI",
|
|
|
|
NONE = "NONE",
|
|
|
|
}
|
|
|
|
|
|
|
|
const parseCameraSelection = (env: UserEnv): Camera => {
|
|
|
|
const camera = env["camera"]?.toUpperCase();
|
|
|
|
if (camera?.includes(Camera.NONE)) {
|
|
|
|
return Camera.NONE;
|
|
|
|
} else if (camera?.includes(Camera.RPI)) {
|
|
|
|
return Camera.RPI;
|
|
|
|
} else {
|
|
|
|
return Camera.USB;
|
|
|
|
}
|
|
|
|
};
|
2017-08-24 17:55:35 -06:00
|
|
|
|
2019-04-09 23:44:58 -06:00
|
|
|
const CAMERA_CHOICES = () => ([
|
2019-12-27 11:37:54 -07:00
|
|
|
{ label: t("USB Camera"), value: Camera.USB },
|
|
|
|
{ label: t("Raspberry Pi Camera"), value: Camera.RPI },
|
|
|
|
{ label: t("None"), value: Camera.NONE },
|
2019-04-09 23:44:58 -06:00
|
|
|
]);
|
2017-08-24 17:55:35 -06:00
|
|
|
|
2019-04-09 23:44:58 -06:00
|
|
|
const CAMERA_CHOICES_DDI = () => {
|
|
|
|
const CHOICES = CAMERA_CHOICES();
|
|
|
|
return {
|
2019-12-27 11:37:54 -07:00
|
|
|
[CHOICES[0].value]: { label: CHOICES[0].label, value: CHOICES[0].value },
|
|
|
|
[CHOICES[1].value]: { label: CHOICES[1].label, value: CHOICES[1].value },
|
|
|
|
[CHOICES[2].value]: { label: CHOICES[2].label, value: CHOICES[2].value },
|
2019-04-09 23:44:58 -06:00
|
|
|
};
|
2017-08-24 17:55:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
export class CameraSelection
|
|
|
|
extends React.Component<CameraSelectionProps, CameraSelectionState> {
|
|
|
|
|
|
|
|
state: CameraSelectionState = {
|
|
|
|
cameraStatus: ""
|
|
|
|
};
|
|
|
|
|
2019-12-27 11:37:54 -07:00
|
|
|
selectedCamera = (): DropDownItem =>
|
|
|
|
CAMERA_CHOICES_DDI()[parseCameraSelection(this.props.env)]
|
2017-08-24 17:55:35 -06:00
|
|
|
|
|
|
|
sendOffConfig = (selectedCamera: DropDownItem) => {
|
2018-11-01 11:17:18 -06:00
|
|
|
const { props } = this;
|
|
|
|
const configKey = "camera";
|
|
|
|
const config = { [configKey]: JSON.stringify(selectedCamera.value) };
|
2017-08-24 17:55:35 -06:00
|
|
|
info(t("Sending camera configuration..."), t("Sending"));
|
2018-11-01 11:17:18 -06:00
|
|
|
props.shouldDisplay(Feature.api_farmware_env)
|
|
|
|
? props.dispatch(props.saveFarmwareEnv(configKey, config[configKey]))
|
|
|
|
: getDevice()
|
|
|
|
.setUserEnv(config)
|
2019-07-02 11:03:16 -06:00
|
|
|
.then(() => success(t("Successfully configured camera!")))
|
2018-11-01 11:17:18 -06:00
|
|
|
.catch(() => error(t("An error occurred during configuration.")));
|
2017-08-24 17:55:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <Row>
|
2017-12-07 21:05:22 -07:00
|
|
|
<Col xs={ColWidth.label}>
|
2017-08-24 17:55:35 -06:00
|
|
|
<label>
|
|
|
|
{t("CAMERA")}
|
|
|
|
</label>
|
|
|
|
</Col>
|
2017-12-07 21:05:22 -07:00
|
|
|
<Col xs={ColWidth.description}>
|
2017-08-24 17:55:35 -06:00
|
|
|
<div>
|
|
|
|
<FBSelect
|
2017-12-07 18:33:28 -07:00
|
|
|
allowEmpty={false}
|
2019-04-09 23:44:58 -06:00
|
|
|
list={CAMERA_CHOICES()}
|
2017-08-24 17:55:35 -06:00
|
|
|
selectedItem={this.selectedCamera()}
|
2018-03-10 02:42:15 -07:00
|
|
|
onChange={this.sendOffConfig}
|
|
|
|
extraClass={this.props.botOnline ? "" : "disabled"} />
|
2017-08-24 17:55:35 -06:00
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Row>;
|
|
|
|
}
|
|
|
|
}
|