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

141 lines
5.1 KiB
TypeScript
Raw Permalink Normal View History

2017-06-29 12:54:02 -06:00
import { Everything } from "../interfaces";
2018-11-01 11:17:18 -06:00
import {
2020-02-28 09:35:32 -07:00
selectAllImages, maybeGetDevice, maybeGetTimeSettings,
2018-11-01 11:17:18 -06:00
} from "../resources/selectors";
2018-11-05 18:37:09 -07:00
import {
2020-02-28 09:35:32 -07:00
FarmwareProps, Feature, SaveFarmwareEnv, UserEnv, ShouldDisplay, BotState,
2018-11-05 18:37:09 -07:00
} from "../devices/interfaces";
2017-07-28 15:24:21 -06:00
import { prepopulateEnv } from "./weed_detector/remote_env/selectors";
2018-11-01 11:17:18 -06:00
import {
2020-02-28 09:35:32 -07:00
selectAllFarmwareEnvs, selectAllFarmwareInstallations,
2018-11-01 11:17:18 -06:00
} from "../resources/selectors_by_kind";
import {
determineInstalledOsVersion,
2019-12-27 11:37:54 -07:00
createShouldDisplayFn,
2020-02-28 09:35:32 -07:00
betterCompact,
2018-11-01 11:17:18 -06:00
} from "../util";
import { ResourceIndex } from "../resources/interfaces";
2019-03-20 14:18:19 -06:00
import { TaggedFarmwareEnv, JobProgress } from "farmbot";
2018-11-01 11:17:18 -06:00
import { save, edit, initSave } from "../api/crud";
2019-03-20 14:18:19 -06:00
import { chain } from "lodash";
import { FarmwareManifestInfo, Farmwares } from "./interfaces";
import { manifestInfo, manifestInfoPending } from "./generate_manifest_info";
2019-04-02 13:59:37 -06:00
import { t } from "../i18next_wrapper";
2019-04-09 19:15:50 -06:00
import { getStatus } from "../connectivity/reducer_support";
2019-04-09 19:29:25 -06:00
import { DevSettings } from "../account/dev/dev_support";
2019-04-09 23:17:03 -06:00
import { getWebAppConfigValue } from "../config_storage/actions";
2018-11-01 11:17:18 -06:00
/** Edit an existing Farmware env variable or add a new one. */
export const saveOrEditFarmwareEnv = (ri: ResourceIndex): SaveFarmwareEnv =>
(key: string, value: string) => (dispatch: Function) => {
const fwEnvLookup: Record<string, TaggedFarmwareEnv> = {};
selectAllFarmwareEnvs(ri)
.map(x => { fwEnvLookup[x.body.key] = x; });
if (Object.keys(fwEnvLookup).includes(key)) {
const fwEnv = fwEnvLookup[key];
dispatch(edit(fwEnv, { value }));
dispatch(save(fwEnv.uuid));
} else {
dispatch(initSave("FarmwareEnv", { key, value }));
}
};
2019-03-20 14:18:19 -06:00
export const isPendingInstallation = (farmware: FarmwareManifestInfo | undefined) =>
!farmware || farmware.installation_pending;
2018-11-09 13:40:36 -07:00
2018-11-01 11:17:18 -06:00
export const reduceFarmwareEnv =
(ri: ResourceIndex): UserEnv => {
const farmwareEnv: UserEnv = {};
selectAllFarmwareEnvs(ri)
.map(x => { farmwareEnv[x.body.key] = "" + x.body.value; });
return farmwareEnv;
};
2017-06-29 12:54:02 -06:00
2019-12-27 11:37:54 -07:00
export const getEnv =
(ri: ResourceIndex, shouldDisplay: ShouldDisplay, bot: BotState) =>
shouldDisplay(Feature.api_farmware_env)
? reduceFarmwareEnv(ri)
: bot.hardware.user_env;
export const getShouldDisplayFn = (ri: ResourceIndex, bot: BotState) => {
const lookupData = bot.minOsFeatureData;
const installed = determineInstalledOsVersion(bot, maybeGetDevice(ri));
const override = DevSettings.overriddenFbosVersion();
const shouldDisplay = createShouldDisplayFn(installed, lookupData, override);
return shouldDisplay;
};
2017-06-29 12:54:02 -06:00
export function mapStateToProps(props: Everything): FarmwareProps {
2019-02-04 07:32:26 -07:00
const images = chain(selectAllImages(props.resources.index))
2017-06-29 12:54:02 -06:00
.sortBy(x => x.body.id)
.reverse()
.value();
2017-10-04 21:01:16 -06:00
const firstImage = images[0];
2017-08-28 05:49:13 -06:00
const currentImage = images
2017-10-04 21:01:16 -06:00
.filter(i => i.uuid === props.resources.consumers.farmware.currentImage)[0]
|| firstImage;
2019-03-20 14:18:19 -06:00
const botStateFarmwares = props.bot.hardware.process_info.farmwares;
2019-04-09 19:45:59 -06:00
const { currentFarmware, firstPartyFarmwareNames, infoOpen } =
2018-11-01 11:17:18 -06:00
props.resources.consumers.farmware;
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-11-05 18:37:09 -07:00
const taggedFarmwareInstallations =
selectAllFarmwareInstallations(props.resources.index);
2019-01-09 19:29:01 -07:00
const namePendingInstall =
(packageName: string | undefined, id: number | undefined): string => {
const nameBase = packageName || `${t("Unknown Farmware")} ${id}`;
const pendingInstall = ` (${t("pending install")}...)`;
return nameBase + pendingInstall;
};
2019-03-20 14:18:19 -06:00
const farmwares: Farmwares = {};
Object.values(botStateFarmwares).map((fm: unknown) => {
const info = manifestInfo(fm);
farmwares[info.name] = manifestInfo(fm);
});
2018-11-09 13:40:36 -07:00
shouldDisplay(Feature.api_farmware_installations) &&
taggedFarmwareInstallations.map(x => {
2020-02-28 09:34:28 -07:00
const n = namePendingInstall(x.body.package, x.body.id);
const alreadyAdded = Object.keys(farmwares).includes(x.body.package || n);
2019-03-20 14:18:19 -06:00
const alreadyInstalled = Object.values(farmwares)
.map(fw => fw.url).includes(x.body.url);
if (x.body.id && !alreadyAdded && !alreadyInstalled) {
2020-02-28 09:34:28 -07:00
farmwares[n] = manifestInfoPending(n, x.body.url);
2018-11-09 13:40:36 -07:00
}
});
2019-01-04 12:26:31 -07:00
const jobs = props.bot.hardware.jobs || {};
const imageJobNames = Object.keys(jobs).filter(x => x != "FBOS_OTA");
const imageJobs: JobProgress[] =
2019-02-04 07:32:26 -07:00
chain(betterCompact(imageJobNames.map(x => jobs[x])))
.sortBy("time")
.reverse()
.value();
const botToMqttStatus = getStatus(props.bot.connectivity.uptime["bot.mqtt"]);
2019-02-10 22:10:58 -07:00
const syncStatus = props.bot.hardware.informational_settings.sync_status;
2017-06-29 12:54:02 -06:00
return {
2019-04-09 23:17:03 -06:00
timeSettings: maybeGetTimeSettings(props.resources.index),
2018-06-21 15:04:21 -06:00
currentFarmware,
2017-06-29 12:54:02 -06:00
farmwares,
2019-02-10 22:10:58 -07:00
botToMqttStatus,
2019-12-27 11:37:54 -07:00
wDEnv: prepopulateEnv(env),
env,
2017-06-29 12:54:02 -06:00
dispatch: props.dispatch,
currentImage,
2018-01-19 10:49:07 -07:00
images,
2019-02-10 22:10:58 -07:00
syncStatus,
2019-04-09 23:17:03 -06:00
getConfigValue: getWebAppConfigValue(() => props),
2018-11-01 11:17:18 -06:00
firstPartyFarmwareNames,
shouldDisplay,
saveFarmwareEnv: saveOrEditFarmwareEnv(props.resources.index),
2018-11-05 18:37:09 -07:00
taggedFarmwareInstallations,
imageJobs,
2019-04-09 19:45:59 -06:00
infoOpen,
2017-06-29 12:54:02 -06:00
};
}