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

76 lines
2.2 KiB
TypeScript
Raw Normal View History

import * as React from "react";
import { DropDownItem, Row, Col, FBSelect } from "../../../ui/index";
import { t } from "i18next";
import {
CameraSelectionProps, CameraSelectionState
} from "./interfaces";
2018-11-01 11:17:18 -06:00
import { info, success, error } from "farmbot-toastr";
import { getDevice } from "../../../device";
import { ColWidth } from "../farmbot_os_settings";
2018-11-01 11:17:18 -06:00
import { Feature } from "../../interfaces";
const CAMERA_CHOICES = [
2018-02-26 06:50:41 -07:00
{ label: t("USB Camera"), value: "USB" },
{ label: t("Raspberry Pi Camera"), value: "RPI" }
];
const CAMERA_CHOICES_DDI = {
[CAMERA_CHOICES[0].value]: {
label: CAMERA_CHOICES[0].label,
value: CAMERA_CHOICES[0].value
},
[CAMERA_CHOICES[1].value]: {
label: CAMERA_CHOICES[1].label,
value: CAMERA_CHOICES[1].value
}
};
export class CameraSelection
extends React.Component<CameraSelectionProps, CameraSelectionState> {
state: CameraSelectionState = {
cameraStatus: ""
};
2017-12-07 18:33:28 -07:00
selectedCamera(): DropDownItem {
2017-08-28 05:49:13 -06:00
const camera = this.props.env["camera"];
2017-12-07 18:33:28 -07:00
return camera
? CAMERA_CHOICES_DDI[JSON.parse(camera)]
: CAMERA_CHOICES_DDI["USB"];
}
sendOffConfig = (selectedCamera: DropDownItem) => {
2018-11-01 11:17:18 -06:00
const { props } = this;
const configKey = "camera";
const config = { [configKey]: JSON.stringify(selectedCamera.value) };
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)
.then(() => success(t("Successfully configured camera!"), t("Success")))
.catch(() => error(t("An error occurred during configuration.")));
}
render() {
return <Row>
<Col xs={ColWidth.label}>
<label>
{t("CAMERA")}
</label>
</Col>
<Col xs={ColWidth.description}>
<div>
<FBSelect
2017-12-07 18:33:28 -07:00
allowEmpty={false}
list={CAMERA_CHOICES}
selectedItem={this.selectedCamera()}
placeholder="Select a camera..."
onChange={this.sendOffConfig}
extraClass={this.props.botOnline ? "" : "disabled"} />
</div>
</Col>
</Row>;
}
}