Farmbot-Web-App/frontend/farm_designer/state_to_props.ts

151 lines
5.3 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import { Everything } from "../interfaces";
import {
selectAllGenericPointers,
selectAllPlantPointers,
selectAllCrops,
2017-11-01 02:07:02 -06:00
joinToolsAndSlot,
2018-02-15 03:51:38 -07:00
selectAllImages,
2018-03-09 02:34:24 -07:00
selectAllPeripherals,
2018-10-15 17:32:23 -06:00
selectAllPlantTemplates,
selectAllSensorReadings,
2018-11-01 11:17:18 -06:00
selectAllSensors,
2020-02-07 16:05:16 -07:00
maybeGetTimeSettings,
selectAllPoints,
2020-02-20 19:38:50 -07:00
selectAllPointGroups,
getDeviceAccountSettings,
2020-02-28 09:35:32 -07:00
maybeFindToolById,
selectAllWeedPointers,
2017-06-29 12:54:02 -06:00
} from "../resources/selectors";
2019-12-27 11:37:54 -07:00
import { validBotLocationData, validFwConfig, unpackUUID } from "../util";
2018-02-15 03:51:38 -07:00
import { getWebAppConfigValue } from "../config_storage/actions";
2018-09-13 16:00:14 -06:00
import { Props } from "./interfaces";
2020-04-13 13:24:38 -06:00
import { TaggedPlant, BotSize } from "./map/interfaces";
2018-09-13 16:00:14 -06:00
import { RestResources } from "../resources/interfaces";
2019-02-04 07:32:26 -07:00
import { isString, uniq, chain } from "lodash";
2018-09-20 13:59:13 -06:00
import { BooleanSetting } from "../session_keys";
2019-12-27 11:37:54 -07:00
import { getEnv, getShouldDisplayFn } from "../farmware/state_to_props";
import { getFirmwareConfig } from "../resources/getters";
2019-04-16 11:01:27 -06:00
import { calcMicrostepsPerMm } from "../controls/move/direction_axes_props";
2020-04-13 13:24:38 -06:00
import { getBotSize } from "./map/util";
import { getDefaultAxisLength } from ".";
2017-06-29 12:54:02 -06:00
2018-09-13 16:00:14 -06:00
const plantFinder = (plants: TaggedPlant[]) =>
(uuid: string | undefined): TaggedPlant =>
plants.filter(x => x.uuid === uuid)[0];
2017-06-29 12:54:02 -06:00
2018-09-13 16:00:14 -06:00
export const getPlants = (resources: RestResources) => {
const onlyPlants = selectAllPlantPointers(resources.index);
const plantTemplates = selectAllPlantTemplates(resources.index);
const { openedSavedGarden } = resources.consumers.farm_designer;
return isString(openedSavedGarden)
? plantTemplates.filter(x =>
x.body.saved_garden_id === unpackUUID(openedSavedGarden).remoteId)
: onlyPlants;
};
export function mapStateToProps(props: Everything): Props {
const plants = getPlants(props.resources);
const findPlant = plantFinder(plants);
const { selectedPoints } = props.resources.consumers.farm_designer;
const selectedPlant = selectedPoints ? findPlant(selectedPoints[0]) : undefined;
2018-09-13 16:00:14 -06:00
const { plantUUID } = props.resources.consumers.farm_designer.hoveredPlant;
2018-09-13 16:00:14 -06:00
const hoveredPlant = findPlant(plantUUID);
2017-06-29 12:54:02 -06:00
2018-09-20 13:59:13 -06:00
const getConfigValue = getWebAppConfigValue(() => props);
2020-02-07 16:05:16 -07:00
const allGenericPoints = selectAllGenericPointers(props.resources.index);
const genericPoints = getConfigValue(BooleanSetting.show_historic_points)
? allGenericPoints
2020-05-08 14:27:15 -06:00
: allGenericPoints.filter(x => x);
const weeds = selectAllWeedPointers(props.resources.index);
2018-09-20 13:59:13 -06:00
2018-03-09 02:34:24 -07:00
const fwConfig = validFwConfig(getFirmwareConfig(props.resources.index));
2018-04-28 01:04:39 -06:00
const { mcu_params } = props.bot.hardware;
2018-03-09 02:34:24 -07:00
const firmwareSettings = fwConfig || mcu_params;
2020-02-20 19:38:50 -07:00
const mountedToolId =
getDeviceAccountSettings(props.resources.index).body.mounted_tool_id;
const mountedToolName =
maybeFindToolById(props.resources.index, mountedToolId)?.body.name;
2019-02-04 07:32:26 -07:00
const peripherals = uniq(selectAllPeripherals(props.resources.index))
2017-11-01 02:07:02 -06:00
.map(x => {
const label = x.body.label;
const pinStatus = x.body.pin
? props.bot.hardware.pins[x.body.pin]
: undefined;
const value = pinStatus ? pinStatus.value > 0 : false;
2017-11-01 02:07:02 -06:00
return { label, value };
});
2019-02-04 07:32:26 -07:00
const latestImages = chain(selectAllImages(props.resources.index))
2018-02-11 19:51:49 -07:00
.sortBy(x => x.body.id)
.reverse()
2018-02-11 19:52:44 -07:00
.value();
2018-02-11 19:51:49 -07:00
2019-12-27 11:37:54 -07:00
const shouldDisplay = getShouldDisplayFn(props.resources.index, props.bot);
const env = getEnv(props.resources.index, shouldDisplay, props.bot);
2018-11-01 11:17:18 -06:00
2018-02-11 19:51:49 -07:00
const cameraCalibrationData = {
2018-11-01 11:17:18 -06:00
scale: env["CAMERA_CALIBRATION_coord_scale"],
rotation: env["CAMERA_CALIBRATION_total_rotation_angle"],
2018-02-11 19:51:49 -07:00
offset: {
2018-11-01 11:17:18 -06:00
x: env["CAMERA_CALIBRATION_camera_offset_x"],
y: env["CAMERA_CALIBRATION_camera_offset_y"]
2018-02-11 19:51:49 -07:00
},
2018-11-01 11:17:18 -06:00
origin: env["CAMERA_CALIBRATION_image_bot_origin_location"],
calibrationZ: env["CAMERA_CALIBRATION_camera_z"],
2018-02-11 19:51:49 -07:00
};
2019-02-04 07:32:26 -07:00
const sensorReadings = chain(selectAllSensorReadings(props.resources.index))
2018-10-15 17:32:23 -06:00
.sortBy(x => x.body.created_at)
.reverse()
.take(500)
.reverse()
.value();
2017-06-29 12:54:02 -06:00
return {
crops: selectAllCrops(props.resources.index),
dispatch: props.dispatch,
selectedPlant,
designer: props.resources.consumers.farm_designer,
2020-02-07 16:05:16 -07:00
genericPoints,
weeds,
2020-02-07 16:05:16 -07:00
allPoints: selectAllPoints(props.resources.index),
2017-06-29 12:54:02 -06:00
toolSlots: joinToolsAndSlot(props.resources.index),
hoveredPlant,
plants,
2018-01-23 16:21:44 -07:00
botLocationData: validBotLocationData(props.bot.hardware.location_data),
2018-03-09 02:34:24 -07:00
botMcuParams: firmwareSettings,
2020-04-13 13:24:38 -06:00
botSize: botSize(props),
peripherals,
2018-02-11 19:51:49 -07:00
eStopStatus: props.bot.hardware.informational_settings.locked,
2018-02-11 19:52:44 -07:00
latestImages,
2018-02-11 19:51:49 -07:00
cameraCalibrationData,
2019-04-09 23:17:03 -06:00
timeSettings: maybeGetTimeSettings(props.resources.index),
2018-09-20 13:59:13 -06:00
getConfigValue,
2018-10-15 17:32:23 -06:00
sensorReadings,
sensors: selectAllSensors(props.resources.index),
2020-02-07 16:05:16 -07:00
groups: selectAllPointGroups(props.resources.index),
shouldDisplay,
2020-02-20 19:38:50 -07:00
mountedToolName,
2017-06-29 12:54:02 -06:00
};
}
2020-04-13 13:24:38 -06:00
export const botSize = (props: Everything): BotSize => {
const getConfigValue = getWebAppConfigValue(() => props);
const fwConfig = validFwConfig(getFirmwareConfig(props.resources.index));
const { mcu_params } = props.bot.hardware;
const firmwareSettings = fwConfig || mcu_params;
const fw = firmwareSettings;
const stepsPerMmXY = {
x: calcMicrostepsPerMm(fw.movement_step_per_mm_x, fw.movement_microsteps_x),
y: calcMicrostepsPerMm(fw.movement_step_per_mm_y, fw.movement_microsteps_y),
};
return getBotSize(
firmwareSettings,
stepsPerMmXY,
getDefaultAxisLength(getConfigValue));
};