import * as React from "react"; import { connect } from "react-redux"; import { Navbar } from "../nav/navbar"; import { addDevice, changeSettingsBuffer, commitSettingsChanges, settingToggle, changeDevice } from "../devices/actions"; import { convertFormToObject } from "../util"; import { ToggleButton } from "../controls/toggle_button"; import { devices } from "../device"; import { BotLog } from "../devices/interfaces"; import * as moment from "moment"; import { Everything } from "../interfaces"; import { saveAccountChanges } from "./actions"; import { t } from "i18next"; export class SettingsInputBox extends React.Component { bot() { // Dumb hacks for impossible bugs. // This is probably what"s causing the bug return this.props.bot; } primary() { return this.props.bot.settingsBuffer[this.props.setting]; } secondary() { let num = this.props.bot.hardware[this.props.setting]; if (_.isNumber(num)) { return String(num); // Prevent 0 from being falsy. } else { return num; } } style() { return { border: (this.primary()) ? "1px solid red" : "" }; } change(key: any, dispatch: Function) { return function(event: React.FormEvent) { dispatch(changeSettingsBuffer(key, (event.target as any).value)); }; } render() { return( ); } } // TODO HACK : This is the biggest refactor target in the app right now. // Numerous issues: uses local variables instead of component state, references // Farmbot object and Redux .bot property (redundant). class DevicesPage extends React.Component { constructor() { // DELETE THIS! super(); this.state = {bot: devices.current}; } updateBot(e: React.MouseEvent) { this.props.dispatch(saveAccountChanges); } changeBot(e: React.MouseEvent) { // THIS IS THE CAUSE OF THE "STALE DATA" BUG: Fix me! e.preventDefault(); let updates: any = _.object([[(e.target as any).name, (e.target as any).value]]); // {name: "value"} this.props.dispatch(changeDevice(updates)); } saveBot(e: React.MouseEvent) { // THIS IS THE CAUSE OF THE "STALE DATA" BUG: Fix me! e.preventDefault(); this.props.dispatch(addDevice(convertFormToObject(e.target as any))); } render() { return (
{t("DEVICE")}
{t("This widget shows device information. Note: not all features work.")}

{t("MQTT: {{mqtt_server}}", {mqtt_server: this.props.auth.mqtt})}

0.0.0.0

0.0.0.0

farmbot-raspberry-pi-controller

{t("Version")} { String(this.props.bot.hardware.param_version) || "loading" }

{t("This will restart FarmBot's Raspberry Pi and controller software (coming soon)")}

{t("This will shutdown FarmBot's Raspberry Pi. To turn it back on, unplug FarmBot and plug it back in. (coming soon)")}

Hardware
{t(`Change settings of your FarmBot hardware with the fields below. Caution: Changing these settings to extreme values can cause hardware malfunction. Make sure to test any new settings before letting your FarmBot use them unsupervised. Tip: Recalibrate FarmBot after changing settings and test a few sequences to verify that everything works as expected. Note: Currently not all settings can be changed.`)}
this.props.dispatch( settingToggle("movement_invert_endpoints_x", this.props.bot)) } /> this.props.dispatch( settingToggle("movement_invert_endpoints_y", this.props.bot)) } /> this.props.dispatch( settingToggle("movement_invert_endpoints_z", this.props.bot)) } />
this.props.dispatch(settingToggle("movement_invert_motor_x", this.props.bot)) } /> this.props.dispatch(settingToggle("movement_invert_motor_y", this.props.bot)) } /> this.props.dispatch(settingToggle("movement_invert_motor_z", this.props.bot)) } />
this.props.dispatch(settingToggle("movement_home_up_x", this.props.bot)) } /> this.props.dispatch(settingToggle("movement_home_up_y", this.props.bot)) } /> this.props.dispatch(settingToggle("movement_home_up_z", this.props.bot)) } />
Logs
{t(`All messages from your FarmBot are shown in these logs. Note: these are not currently saved anywhere so if you refresh the app this table will be cleared.`)}
); } }; interface LogsProps { logs: BotLog[]; } function Logs({logs}: LogsProps) { function HasLogs(_: any) { function displayTime(t: number): string { return moment.unix(t).format("D MMM h:mma"); } function displayCoordinates(log: BotLog) { // Stringify coords bcuz 0 is falsy in JS. let [x, y, z] = [log.status.X, log.status.Y, log.status.Z].map((i) => String(i)); if (x && y && z) { return `${x}, ${y}, ${z}`; } else { return "Unknown"; } } return { logs.map((log, i) => { displayTime(log.time) } { log.data } { displayCoordinates(log) } ) } ; } function NoLogs(_: any) { return

{t("No logs yet.")}

; } return (logs.length ? : ) ; } export let Devices = connect(state => state)(DevicesPage);