import { ResourceIndex } from "./interfaces"; import { TaggedResource, SpecialStatus, TaggedWebcamFeed, TaggedCrop, TaggedRegimen, TaggedImage, TaggedLog, TaggedTool, TaggedFarmEvent, TaggedSequence, TaggedPoint, TaggedSensor, TaggedPeripheral, TaggedPinBinding, TaggedDiagnosticDump, TaggedSensorReading, TaggedSavedGarden, TaggedPlantTemplate, TaggedFarmwareEnv, TaggedFarmwareInstallation, TaggedAlert, } from "farmbot"; import { isTaggedResource, sanityCheck, } from "./tagged_resources"; import { bail } from "../util"; import { error } from "../toast/toast"; import { assertUuid } from "./util"; import { joinKindAndId } from "./reducer_support"; import { findAll } from "./find_all"; const isSaved = (t: T) => t.specialStatus === SpecialStatus.SAVED; /** 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! */ const uuidFinder = (r: T["kind"]) => function findResource(i: ResourceIndex, u: string): T { assertUuid(r, u); const result = i.references[u]; if (result && isTaggedResource(result) && sanityCheck(result)) { return result as T; } else { error("Resource error"); throw new Error(`Tagged resource ${r} was not found or malformed: ` + JSON.stringify(result)); } }; export let findTool = uuidFinder("Tool"); export let findSequence = uuidFinder("Sequence"); export let findRegimen = uuidFinder("Regimen"); export let findFarmEvent = uuidFinder("FarmEvent"); export let findPoints = uuidFinder("Point"); export const selectAllCrops = (i: ResourceIndex) => findAll(i, "Crop"); export const selectAllSavedGardens = (i: ResourceIndex) => findAll(i, "SavedGarden"); export const selectAllPlantTemplates = (i: ResourceIndex) => findAll(i, "PlantTemplate"); export const selectAllFarmEvents = (i: ResourceIndex) => findAll(i, "FarmEvent"); export const selectAllImages = (i: ResourceIndex) => findAll(i, "Image"); export const selectAllLogs = (i: ResourceIndex) => findAll(i, "Log"); export const selectAllPeripherals = (i: ResourceIndex) => findAll(i, "Peripheral"); export const selectAllPoints = (i: ResourceIndex) => findAll(i, "Point"); export const selectAllActivePoints = (input: ResourceIndex) => selectAllPoints(input).filter(x => !x.body.discarded_at); export const selectAllDiagnosticDumps = (i: ResourceIndex) => findAll(i, "DiagnosticDump"); export const selectAllFarmwareEnvs = (i: ResourceIndex) => findAll(i, "FarmwareEnv"); export const selectAllFarmwareInstallations = (i: ResourceIndex) => findAll(i, "FarmwareInstallation"); export const selectAllRegimens = (i: ResourceIndex) => findAll(i, "Regimen"); export const selectAllSensors = (i: ResourceIndex) => findAll(i, "Sensor"); export const selectAllPinBindings = (i: ResourceIndex) => findAll(i, "PinBinding"); export const selectAllSequences = (i: ResourceIndex) => findAll(i, "Sequence"); export const selectAllSensorReadings = (i: ResourceIndex) => findAll(i, "SensorReading"); export const selectAllTools = (i: ResourceIndex) => findAll(i, "Tool"); export const selectAllSavedSensors = (input: ResourceIndex) => selectAllSensors(input).filter(isSaved); export const selectAllWebcamFeeds = (i: ResourceIndex) => findAll(i, "WebcamFeed"); export const selectAllSavedPeripherals = (input: ResourceIndex) => selectAllPeripherals(input).filter(isSaved); export const selectAllAlerts = (i: ResourceIndex) => findAll(i, "Alert"); export const findByKindAndId = ( 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); } };