Add DRY
parent
88722b49bb
commit
171c335e3c
|
@ -1,7 +1,7 @@
|
|||
import { Everything } from "../interfaces";
|
||||
import {
|
||||
selectAllPeripherals,
|
||||
getFeeds
|
||||
selectAllWebcamFeeds
|
||||
} from "../resources/selectors";
|
||||
import { Props } from "./interfaces";
|
||||
import { maybeFetchUser } from "../resources/selectors";
|
||||
|
@ -14,7 +14,7 @@ export function mapStateToProps(props: Everything): Props {
|
|||
const botToMqttStatus = bot2mqtt ? bot2mqtt.state : "down";
|
||||
|
||||
return {
|
||||
feeds: getFeeds(resources.index),
|
||||
feeds: selectAllWebcamFeeds(resources.index),
|
||||
dispatch: props.dispatch,
|
||||
bot: props.bot,
|
||||
user: maybeFetchUser(props.resources.index),
|
||||
|
|
|
@ -6,9 +6,10 @@ import {
|
|||
indexRegimenById
|
||||
} from "../../../resources/selectors";
|
||||
import { betterCompact } from "../../../util";
|
||||
import { TaggedFarmEvent } from "../../../resources/tagged_resources";
|
||||
|
||||
export function joinFarmEventsToExecutable(input: ResourceIndex): FarmEventWithExecutable[] {
|
||||
const farmEvents = selectAllFarmEvents(input);
|
||||
const farmEvents: TaggedFarmEvent[] = selectAllFarmEvents(input);
|
||||
const sequenceById = indexSequenceById(input);
|
||||
const regimenById = indexRegimenById(input);
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ import {
|
|||
selectAllPlantPointers,
|
||||
selectAllCrops,
|
||||
joinToolsAndSlot,
|
||||
selectAllPeripherals,
|
||||
selectAllImages,
|
||||
maybeGetTimeOffset
|
||||
maybeGetTimeOffset,
|
||||
selectAllPeripherals
|
||||
} from "../resources/selectors";
|
||||
import { StepsPerMmXY } from "../devices/interfaces";
|
||||
import { isNumber } from "lodash";
|
||||
|
|
|
@ -68,7 +68,7 @@ describe("findSlotByToolId", () => {
|
|||
|
||||
describe("getFeeds", () => {
|
||||
it("returns empty array", () => {
|
||||
expect(Selector.getFeeds(emptyState().index).length).toBe(0);
|
||||
expect(Selector.selectAllWebcamFeeds(emptyState().index).length).toBe(0);
|
||||
});
|
||||
|
||||
it("finds the only WebcamFeed", () => {
|
||||
|
@ -80,7 +80,7 @@ describe("getFeeds", () => {
|
|||
data: feed
|
||||
}
|
||||
}].reduce(resourceReducer, emptyState());
|
||||
expect(Selector.getFeeds(state.index)[0].body).toEqual(feed);
|
||||
expect(Selector.selectAllWebcamFeeds(state.index)[0].body).toEqual(feed);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ export let isKind = (name: ResourceName) => (tr: TaggedResource) => tr.kind ===
|
|||
|
||||
export function groupPointsByType(index: ResourceIndex) {
|
||||
return _(selectAllPoints(index))
|
||||
// If this fiails to compile....
|
||||
// If this fails to compile....
|
||||
.tap(x => x[0].body.pointer_type)
|
||||
// ... this line must be updated:
|
||||
.groupBy("body.pointer_type")
|
||||
|
@ -109,7 +109,7 @@ export function findPlant(i: ResourceIndex, uuid: string):
|
|||
TaggedPlantPointer {
|
||||
const point = findPoints(i, uuid);
|
||||
if (point && sanityCheck(point) && point.body.pointer_type === "Plant") {
|
||||
return point;
|
||||
return point as TaggedPlantPointer;
|
||||
} else {
|
||||
throw new Error("That is not a true plant pointer");
|
||||
}
|
||||
|
|
|
@ -19,19 +19,22 @@ import { findAll } from "./selectors";
|
|||
import * as _ from "lodash";
|
||||
|
||||
/** FINDS: all tagged resources with particular ID */
|
||||
export function findAllById(i: ResourceIndex, ids: number[], k: ResourceName) {
|
||||
const output: TaggedResource[] = [];
|
||||
findAll(i, k).map(x => x.kind === k ? output.push(x) : "");
|
||||
return output;
|
||||
}
|
||||
export let byId = <T extends TaggedResource>(name: ResourceName) =>
|
||||
(index: ResourceIndex, id: number): T | undefined => {
|
||||
const tools = findAll(index, name);
|
||||
const f = (x: TaggedResource) => (x.kind === name) && (x.body.id === id);
|
||||
// Maybe we should add a throw here?
|
||||
return tools.filter(f)[0] as T | undefined;
|
||||
export const findAllById =
|
||||
<T extends TaggedResource>(i: ResourceIndex, ids: number[], k: T["kind"]) => {
|
||||
const output: TaggedResource[] = [];
|
||||
findAll<T>(i, k).map(x => x.kind === k ? output.push(x) : "");
|
||||
return output;
|
||||
};
|
||||
|
||||
export let byId =
|
||||
<T extends TaggedResource>(name: T["kind"]) =>
|
||||
(index: ResourceIndex, id: number): T | undefined => {
|
||||
const resources = findAll(index, name);
|
||||
const f = (x: TaggedResource) => (x.kind === name) && (x.body.id === id);
|
||||
// Maybe we should add a throw here?
|
||||
return resources.filter(f)[0] as T | undefined;
|
||||
};
|
||||
|
||||
export let findFarmEventById = (ri: ResourceIndex, fe_id: number) => {
|
||||
const fe = byId("FarmEvent")(ri, fe_id);
|
||||
if (fe && isTaggedFarmEvent(fe) && sanityCheck(fe)) {
|
||||
|
|
|
@ -3,8 +3,6 @@ import {
|
|||
TaggedResource,
|
||||
SpecialStatus,
|
||||
isTaggedResource,
|
||||
ResourceName,
|
||||
TaggedSequence,
|
||||
sanityCheck,
|
||||
TaggedWebcamFeed,
|
||||
TaggedFbosConfig,
|
||||
|
@ -13,31 +11,28 @@ import {
|
|||
TaggedImage,
|
||||
TaggedLog,
|
||||
TaggedTool,
|
||||
TaggedPlantPointer,
|
||||
TaggedFarmEvent,
|
||||
TaggedGenericPointer,
|
||||
TaggedToolSlotPointer
|
||||
TaggedSequence,
|
||||
TaggedPoint,
|
||||
TaggedSensor,
|
||||
TaggedPeripheral,
|
||||
} from "./tagged_resources";
|
||||
import { sortResourcesById, bail } from "../util";
|
||||
import { sortResourcesById } from "../util";
|
||||
import { error } from "farmbot-toastr";
|
||||
import { assertUuid } from "./selectors";
|
||||
|
||||
interface Finder<T> {
|
||||
(i: ResourceIndex, u: string): T;
|
||||
}
|
||||
const isSaved =
|
||||
<T extends TaggedResource>(t: T) => t.specialStatus === SpecialStatus.SAVED;
|
||||
const isSaved = <T extends TaggedResource>(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 find = <T extends TaggedResource>(r: T["kind"]) =>
|
||||
function findResource(i: ResourceIndex, u: string) {
|
||||
const uuidFinder = <T extends TaggedResource>(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 TaggedResource;
|
||||
return result as T;
|
||||
} else {
|
||||
error("Resource error");
|
||||
throw new Error(`Tagged resource ${r} was not found or malformed: ` +
|
||||
|
@ -45,93 +40,62 @@ const find = <T extends TaggedResource>(r: T["kind"]) =>
|
|||
}
|
||||
};
|
||||
|
||||
export let findTool = find("Tool") as Finder<TaggedTool>;
|
||||
export let findSequence = find("Sequence") as Finder<TaggedSequence>;
|
||||
export let findRegimen = find("Regimen") as Finder<TaggedRegimen>;
|
||||
export let findFarmEvent = find("FarmEvent") as Finder<TaggedFarmEvent>;
|
||||
export let findPoints = find("Point") as Finder<TaggedPlantPointer>;
|
||||
|
||||
export function selectAllTools(index: ResourceIndex) {
|
||||
return findAll(index, "Tool") as TaggedTool[];
|
||||
}
|
||||
|
||||
export function selectAllLogs(index: ResourceIndex) {
|
||||
return findAll(index, "Log") as TaggedLog[];
|
||||
}
|
||||
|
||||
export function selectAllImages(index: ResourceIndex) {
|
||||
return findAll(index, "Image") as TaggedImage[];
|
||||
}
|
||||
|
||||
export function selectAllRegimens(index: ResourceIndex) {
|
||||
return findAll(index, "Regimen") as TaggedRegimen[];
|
||||
}
|
||||
|
||||
export function selectAllCrops(index: ResourceIndex) {
|
||||
return findAll(index, "Crop") as TaggedCrop[];
|
||||
}
|
||||
|
||||
export const selectAllPeripherals = getAllPeripherals;
|
||||
|
||||
export function getAllSensors(input: ResourceIndex) {
|
||||
return input
|
||||
.byKind
|
||||
.Sensor
|
||||
.map(x => input.references[x])
|
||||
.map(x => (x && (x.kind == "Sensor")) ? x : bail("Never"));
|
||||
}
|
||||
|
||||
export const getAllSavedPeripherals =
|
||||
(input: ResourceIndex) => getAllPeripherals(input).filter(isSaved);
|
||||
export const getAllSavedSensors =
|
||||
(input: ResourceIndex) => getAllSensors(input).filter(isSaved);
|
||||
export function findAll(index: ResourceIndex, kind: ResourceName) {
|
||||
const results: TaggedResource[] = [];
|
||||
export function findAll<T extends TaggedResource>(index: ResourceIndex, kind: T["kind"]): T[] {
|
||||
const results: T[] = [];
|
||||
|
||||
index.byKind[kind].map(function (uuid) {
|
||||
const item = index.references[uuid];
|
||||
(item && isTaggedResource(item) && results.push(item));
|
||||
});
|
||||
return sortResourcesById(results);
|
||||
}
|
||||
|
||||
export function getAllPeripherals(input: ResourceIndex) {
|
||||
return input
|
||||
.byKind
|
||||
.Peripheral
|
||||
.map(x => input.references[x])
|
||||
.map(x => (x && (x.kind == "Peripheral")) ? x : bail("Never"));
|
||||
}
|
||||
|
||||
export function getFbosConfig(i: ResourceIndex): TaggedFbosConfig | undefined {
|
||||
const conf = i.references[i.byKind.FbosConfig[0] || "NO"];
|
||||
if (conf && conf.kind === "FbosConfig") {
|
||||
return conf;
|
||||
}
|
||||
}
|
||||
|
||||
export function getFeeds(index: ResourceIndex): TaggedWebcamFeed[] {
|
||||
const list = index.byKind.WebcamFeed;
|
||||
const output: TaggedWebcamFeed[] = [];
|
||||
list.forEach(y => {
|
||||
const x = index.references[y];
|
||||
if (x && x.kind === "WebcamFeed") {
|
||||
sanityCheck(x);
|
||||
output.push(x);
|
||||
if (item && isTaggedResource(item) && (item.kind === kind)) {
|
||||
results.push(item as T);
|
||||
}
|
||||
});
|
||||
return output;
|
||||
return sortResourcesById(results) as T[];
|
||||
}
|
||||
|
||||
export function selectAllSequences(index: ResourceIndex) {
|
||||
return findAll(index, "Sequence") as TaggedSequence[];
|
||||
}
|
||||
export let findTool = uuidFinder<TaggedTool>("Tool");
|
||||
export let findSequence = uuidFinder<TaggedSequence>("Sequence");
|
||||
export let findRegimen = uuidFinder<TaggedRegimen>("Regimen");
|
||||
export let findFarmEvent = uuidFinder<TaggedFarmEvent>("FarmEvent");
|
||||
export let findPoints = uuidFinder<TaggedPoint>("Point");
|
||||
|
||||
export function selectAllFarmEvents(index: ResourceIndex) {
|
||||
return findAll(index, "FarmEvent") as TaggedFarmEvent[];
|
||||
}
|
||||
export const selectAllCrops =
|
||||
(i: ResourceIndex) => findAll<TaggedCrop>(i, "Crop");
|
||||
|
||||
export function selectAllPoints(index: ResourceIndex) {
|
||||
return findAll(index, "Point") as
|
||||
(TaggedGenericPointer | TaggedPlantPointer | TaggedToolSlotPointer)[];
|
||||
}
|
||||
export const selectAllFarmEvents =
|
||||
(i: ResourceIndex) => findAll<TaggedFarmEvent>(i, "FarmEvent");
|
||||
|
||||
export const selectAllImages =
|
||||
(i: ResourceIndex) => findAll<TaggedImage>(i, "Image");
|
||||
|
||||
export const selectAllLogs =
|
||||
(i: ResourceIndex) => findAll<TaggedLog>(i, "Log");
|
||||
|
||||
export const selectAllPeripherals =
|
||||
(i: ResourceIndex) => findAll<TaggedPeripheral>(i, "Peripheral");
|
||||
|
||||
export const selectAllPoints =
|
||||
(i: ResourceIndex) => findAll<TaggedPoint>(i, "Point");
|
||||
|
||||
export const selectAllRegimens =
|
||||
(i: ResourceIndex) => findAll<TaggedRegimen>(i, "Regimen");
|
||||
|
||||
export const selectAllSensors =
|
||||
(i: ResourceIndex) => findAll<TaggedSensor>(i, "Sensor");
|
||||
|
||||
export const selectAllSequences =
|
||||
(i: ResourceIndex) => findAll<TaggedSequence>(i, "Sequence");
|
||||
|
||||
export const selectAllTools =
|
||||
(i: ResourceIndex) => findAll<TaggedTool>(i, "Tool");
|
||||
|
||||
export const selectAllWebcamFeeds =
|
||||
(i: ResourceIndex) => findAll<TaggedWebcamFeed>(i, "WebcamFeed");
|
||||
|
||||
export const getAllSavedPeripherals =
|
||||
(input: ResourceIndex) => selectAllPeripherals(input).filter(isSaved);
|
||||
|
||||
export const getAllSavedSensors =
|
||||
(input: ResourceIndex) => selectAllSensors(input).filter(isSaved);
|
||||
|
||||
export const getFbosConfig =
|
||||
(i: ResourceIndex): TaggedFbosConfig | undefined => findAll<TaggedFbosConfig>(i, "FbosConfig")[0];
|
||||
|
|
Loading…
Reference in New Issue