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

125 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-09-20 01:27:37 -06:00
import * as React from "react";
import { Row, Col, DropDownItem, FBSelect } from "../../../ui/index";
2019-04-02 13:59:37 -06:00
2018-01-27 02:29:13 -07:00
import { info } from "farmbot-toastr";
import { FirmwareHardware } from "farmbot";
import { ColWidth } from "../farmbot_os_settings";
2018-01-27 02:29:13 -07:00
import { updateConfig } from "../../actions";
import { BoardTypeProps } from "./interfaces";
2018-03-13 19:32:19 -06:00
import { Feature } from "../../interfaces";
2019-04-02 13:59:37 -06:00
import { t } from "../../../i18next_wrapper";
2017-09-20 01:27:37 -06:00
2018-03-13 19:32:19 -06:00
const ARDUINO = { label: "Arduino/RAMPS (Genesis v1.2)", value: "arduino" };
const FARMDUINO = { label: "Farmduino (Genesis v1.3)", value: "farmduino" };
const FARMDUINO_K14 = {
label: "Farmduino (Genesis v1.4)", value: "farmduino_k14"
};
2017-10-04 00:53:26 -06:00
const FIRMWARE_CHOICES_DDI = {
2018-03-13 19:32:19 -06:00
[ARDUINO.value]: ARDUINO,
[FARMDUINO.value]: FARMDUINO,
[FARMDUINO_K14.value]: FARMDUINO_K14
2017-10-04 00:53:26 -06:00
};
2018-01-27 02:29:13 -07:00
interface BoardTypeState { boardType: string, sending: boolean }
export class BoardType extends React.Component<BoardTypeProps, BoardTypeState> {
state = {
boardType: this.boardType,
sending: this.sending
};
componentWillReceiveProps() {
this.setState({ sending: this.sending });
2018-01-28 21:07:10 -07:00
!["unknown", "Present"].includes(this.boardType) &&
this.setState({ boardType: this.boardType });
2018-01-27 02:29:13 -07:00
}
get sending() {
return !this.props.sourceFbosConfig("firmware_hardware").consistent;
}
2017-09-20 01:27:37 -06:00
2018-03-10 02:09:03 -07:00
get apiValue() {
return this.props.sourceFbosConfig("firmware_hardware").value;
}
2018-03-13 19:32:19 -06:00
get firmwareChoices() {
const { shouldDisplay } = this.props;
return [ARDUINO, FARMDUINO,
...(shouldDisplay(Feature.farmduino_k14) ? [FARMDUINO_K14] : [])
];
}
2018-01-27 02:29:13 -07:00
get boardType() {
2017-09-20 03:15:45 -06:00
if (this.props.firmwareVersion) {
const boardIdentifier = this.props.firmwareVersion.slice(-1);
2017-09-20 01:27:37 -06:00
switch (boardIdentifier) {
case "R":
return "Arduino/RAMPS";
case "F":
return "Farmduino";
2018-03-13 19:32:19 -06:00
case "G":
return "Farmduino k1.4";
2017-09-20 01:27:37 -06:00
default:
2018-03-13 19:32:19 -06:00
return "unknown";
2017-09-20 01:27:37 -06:00
}
} else {
return "unknown";
}
}
2018-01-27 02:29:13 -07:00
get selectedBoard(): DropDownItem | undefined {
switch (this.state.boardType) {
2017-10-04 00:53:26 -06:00
case "Arduino/RAMPS":
return FIRMWARE_CHOICES_DDI["arduino"];
case "Farmduino":
return FIRMWARE_CHOICES_DDI["farmduino"];
2018-03-13 19:32:19 -06:00
case "Farmduino k1.4":
return FIRMWARE_CHOICES_DDI["farmduino_k14"];
2018-03-10 02:09:03 -07:00
case "unknown":
// If unknown/disconnected, display API FirmwareHardware value if valid
return (this.sending && typeof this.apiValue === "string")
? FIRMWARE_CHOICES_DDI[this.apiValue]
: undefined;
2017-10-04 00:53:26 -06:00
default:
return undefined;
}
}
2018-01-27 02:29:13 -07:00
sendOffConfig = (selectedItem: DropDownItem) => {
const isFwHardwareValue = (x?: unknown): x is FirmwareHardware => {
2018-03-13 19:32:19 -06:00
const values: FirmwareHardware[] = [
"arduino", "farmduino", "farmduino_k14"];
return !!values.includes(x as FirmwareHardware);
};
2018-01-27 02:29:13 -07:00
const firmware_hardware = selectedItem.value;
if (selectedItem && isFwHardwareValue(firmware_hardware)) {
2017-10-04 00:53:26 -06:00
info(t("Sending firmware configuration..."), t("Sending"));
2018-01-27 02:29:13 -07:00
this.props.dispatch(updateConfig({ firmware_hardware }));
this.setState({ sending: true });
this.forceUpdate();
2017-10-04 00:53:26 -06:00
}
}
2017-09-20 01:27:37 -06:00
render() {
return <Row>
<Col xs={ColWidth.label}>
2017-09-20 01:27:37 -06:00
<label>
2017-09-20 13:38:17 -06:00
{t("FIRMWARE")}
2017-09-20 01:27:37 -06:00
</label>
</Col>
<Col xs={ColWidth.description}>
2017-10-04 00:53:26 -06:00
<div>
<FBSelect
2018-01-27 02:29:13 -07:00
key={this.state.boardType}
extraClass={this.state.sending ? "dim" : ""}
2018-03-13 19:32:19 -06:00
list={this.firmwareChoices}
2018-01-27 02:29:13 -07:00
selectedItem={this.selectedBoard}
2017-10-04 00:53:26 -06:00
onChange={this.sendOffConfig} />
</div>
2017-09-20 01:27:37 -06:00
</Col>
</Row>;
}
}