Farmbot-Web-App/frontend/resources/selectors_by_kind.ts

125 lines
4.7 KiB
TypeScript
Raw Normal View History

2018-03-07 09:39:16 -07:00
import { ResourceIndex } from "./interfaces";
import {
TaggedResource,
SpecialStatus,
TaggedWebcamFeed,
TaggedCrop,
TaggedRegimen,
TaggedImage,
TaggedLog,
TaggedTool,
TaggedFarmEvent,
2018-03-07 10:27:19 -07:00
TaggedSequence,
TaggedPoint,
TaggedSensor,
TaggedPeripheral,
2018-03-13 18:34:30 -06:00
TaggedPinBinding,
TaggedDiagnosticDump,
2018-07-19 23:48:32 -06:00
TaggedSensorReading,
2018-08-01 18:13:44 -06:00
TaggedSavedGarden,
TaggedPlantTemplate,
2018-11-01 11:17:18 -06:00
TaggedFarmwareEnv,
2018-11-05 18:37:09 -07:00
TaggedFarmwareInstallation,
2019-04-19 16:04:25 -06:00
TaggedAlert,
TaggedPointGroup,
2019-12-04 08:49:55 -07:00
TaggedFolder,
2018-08-01 07:03:35 -06:00
} from "farmbot";
import {
isTaggedResource,
sanityCheck,
2018-03-07 09:39:16 -07:00
} from "./tagged_resources";
2018-11-26 09:58:17 -07:00
import { bail } from "../util";
2019-06-24 15:39:49 -06:00
import { error } from "../toast/toast";
2018-04-25 11:45:22 -06:00
import { assertUuid } from "./util";
import { joinKindAndId } from "./reducer_support";
import { findAll } from "./find_all";
2018-03-07 09:39:16 -07:00
const isSaved = <T extends TaggedResource>(t: T) =>
t.specialStatus === SpecialStatus.SAVED;
2018-03-07 09:39:16 -07:00
/** Generalized way to stamp out "finder" functions.
* Pass in a `ResourceName` and it will add all the relevant checks.
* WARNING: WILL THROW ERRORS IF RESOURCE NOT FOUND!
*/
2018-03-07 10:27:19 -07:00
const uuidFinder = <T extends TaggedResource>(r: T["kind"]) =>
function findResource(i: ResourceIndex, u: string): T {
2018-03-07 09:39:16 -07:00
assertUuid(r, u);
const result = i.references[u];
if (result && isTaggedResource(result) && sanityCheck(result)) {
2018-03-07 10:27:19 -07:00
return result as T;
2018-03-07 09:39:16 -07:00
} else {
error("Resource error");
throw new Error(`Tagged resource ${r} was not found or malformed: ` +
JSON.stringify(result));
}
};
export const findTool = uuidFinder<TaggedTool>("Tool");
export const findSequence = uuidFinder<TaggedSequence>("Sequence");
export const findRegimen = uuidFinder<TaggedRegimen>("Regimen");
export const findFarmEvent = uuidFinder<TaggedFarmEvent>("FarmEvent");
export const findPoints = uuidFinder<TaggedPoint>("Point");
export const findPointGroup = uuidFinder<TaggedPoint>("Point");
export const findSavedGarden = uuidFinder<TaggedSavedGarden>("SavedGarden");
2018-03-07 09:39:16 -07:00
export const selectAllCrops =
(i: ResourceIndex) => findAll<TaggedCrop>(i, "Crop");
2018-08-01 18:13:44 -06:00
export const selectAllSavedGardens = (i: ResourceIndex) =>
findAll<TaggedSavedGarden>(i, "SavedGarden");
export const selectAllPlantTemplates = (i: ResourceIndex) =>
findAll<TaggedPlantTemplate>(i, "PlantTemplate");
export const selectAllFarmEvents = (i: ResourceIndex) =>
findAll<TaggedFarmEvent>(i, "FarmEvent");
export const selectAllImages =
(i: ResourceIndex) => findAll<TaggedImage>(i, "Image");
2018-03-07 10:34:31 -07:00
export const selectAllLogs = (i: ResourceIndex) => findAll<TaggedLog>(i, "Log");
2018-03-07 10:27:19 -07:00
export const selectAllPeripherals =
(i: ResourceIndex) => findAll<TaggedPeripheral>(i, "Peripheral");
export const selectAllPoints =
(i: ResourceIndex) => findAll<TaggedPoint>(i, "Point");
export const selectAllPointGroups =
(i: ResourceIndex) => findAll<TaggedPointGroup>(i, "PointGroup");
2018-09-20 13:59:13 -06:00
export const selectAllActivePoints = (input: ResourceIndex) =>
selectAllPoints(input).filter(x => !x.body.discarded_at);
2018-03-13 17:39:36 -06:00
export const selectAllDiagnosticDumps =
(i: ResourceIndex) => findAll<TaggedDiagnosticDump>(i, "DiagnosticDump");
2018-11-01 11:17:18 -06:00
export const selectAllFarmwareEnvs =
(i: ResourceIndex) => findAll<TaggedFarmwareEnv>(i, "FarmwareEnv");
2018-11-05 18:37:09 -07:00
export const selectAllFarmwareInstallations = (i: ResourceIndex) =>
findAll<TaggedFarmwareInstallation>(i, "FarmwareInstallation");
export const selectAllRegimens = (i: ResourceIndex) =>
findAll<TaggedRegimen>(i, "Regimen");
export const selectAllSensors =
(i: ResourceIndex) => findAll<TaggedSensor>(i, "Sensor");
2018-03-13 18:34:30 -06:00
export const selectAllPinBindings =
(i: ResourceIndex) => findAll<TaggedPinBinding>(i, "PinBinding");
export const selectAllSequences = (i: ResourceIndex) =>
findAll<TaggedSequence>(i, "Sequence");
2018-07-19 23:48:32 -06:00
export const selectAllSensorReadings = (i: ResourceIndex) =>
findAll<TaggedSensorReading>(i, "SensorReading");
export const selectAllTools =
(i: ResourceIndex) => findAll<TaggedTool>(i, "Tool");
2018-03-07 10:34:31 -07:00
export const selectAllSavedSensors =
(input: ResourceIndex) => selectAllSensors(input).filter(isSaved);
2018-03-07 10:27:19 -07:00
export const selectAllWebcamFeeds =
(i: ResourceIndex) => findAll<TaggedWebcamFeed>(i, "WebcamFeed");
export const selectAllSavedPeripherals =
2018-03-07 10:27:19 -07:00
(input: ResourceIndex) => selectAllPeripherals(input).filter(isSaved);
2019-04-19 16:04:25 -06:00
export const selectAllAlerts =
(i: ResourceIndex) => findAll<TaggedAlert>(i, "Alert");
2019-12-04 08:49:55 -07:00
export const selectAllFolders =
(i: ResourceIndex) => findAll<TaggedFolder>(i, "Folder");
export const findByKindAndId = <T extends TaggedResource>(
i: ResourceIndex, kind: T["kind"], id: number | undefined): T => {
const kni = joinKindAndId(kind, id);
const uuid = i.byKindAndId[kni] || bail("Not found: " + kni);
const resource = i.references[uuid] || bail("Not found uuid: " + uuid);
if (resource.kind === kind) {
return resource as T; // Why `as T`?
} else {
return bail("Impossible! " + uuid);
}
};