pull/677/head
Rick Carlino 2018-02-22 11:07:21 -06:00
parent 3d9ead929e
commit ce5a1f042f
4 changed files with 79 additions and 72 deletions

View File

@ -4,7 +4,8 @@ import {
TaggedFarmEvent, TaggedSequence, TaggedRegimen, TaggedImage,
TaggedTool, TaggedToolSlotPointer, TaggedUser, TaggedWebcamFeed,
TaggedPlantPointer, TaggedGenericPointer, TaggedPeripheral, TaggedFbosConfig,
TaggedWebAppConfig
TaggedWebAppConfig,
TaggedSensor
} from "../../resources/tagged_resources";
import { ExecutableType } from "../../farm_designer/interfaces";
import { fakeResource } from "../fake_resource";
@ -130,6 +131,14 @@ export function fakeWebcamFeed(): TaggedWebcamFeed {
});
}
export function fakeSensor(): TaggedSensor {
return fakeResource("Sensor", {
id: idCounter++,
label: "Fake Pin",
mode: 0,
pin: 1
});
}
export function fakePeripheral(): TaggedPeripheral {
return fakeResource("Peripheral", {
id: idCounter++,

View File

@ -568,7 +568,15 @@ export function getAllPeripherals(input: ResourceIndex) {
.byKind
.Peripheral
.map(x => input.references[x])
.map(x => (x && (x.kind == "Peripheral")) ? x : bail("Never"))
.map(x => (x && (x.kind == "Peripheral")) ? x : bail("Never"));
}
export function getAllSensors(input: ResourceIndex) {
return input
.byKind
.Sensor
.map(x => input.references[x])
.map(x => (x && (x.kind == "Sensor")) ? x : bail("Never"));
}
const isSaved =
@ -576,3 +584,5 @@ const isSaved =
export const getAllSavedPeripherals =
(input: ResourceIndex) => getAllPeripherals(input).filter(isSaved);
export const getAllSavedSensors =
(input: ResourceIndex) => getAllSensors(input).filter(isSaved);

View File

@ -6,10 +6,14 @@ import {
PIN_RANGE,
PIN_HEADING,
PinGroupName,
peripheralsAsDropDowns
peripheralsAsDropDowns,
PERIPHERAL_HEADING,
SENSOR_HEADING,
sensorsAsDropDowns
} from "../pin_and_peripheral_support";
import * as _ from "lodash";
import { fakePeripheral } from "../../../__test_support__/fake_state/resources";
import { fakePeripheral, fakeSensor } from "../../../__test_support__/fake_state/resources";
import { DropDownItem } from "../../../ui";
describe("Pin and Peripheral support files", () => {
it("has a list of unnamed pins", () => {
@ -17,18 +21,31 @@ describe("Pin and Peripheral support files", () => {
.toBe(PIN_RANGE.length + 1); // 54 pins plus the header.
expect(pinDropdowns[0]).toBe(PIN_HEADING);
// Grab all uniq heading IDs- we expect only 1.
const values = _(pinDropdowns).tail().map(x => x.headingId).uniq().value();
const values = _(pinDropdowns)
.tail()
.map((x: DropDownItem) => x.headingId)
.uniq()
.value();
expect(values).toEqual([PinGroupName.pin]);
});
it("Makes a list of Peripheral drop down selectors", () => {
it("Makes a list of Peripheral drop downs", () => {
const p = fakePeripheral();
p.body.label = "The one";
const ri = buildResourceIndex([p]);
const result = peripheralsAsDropDowns(ri.index);
expect(result.length).toEqual(2); // Heading + 1 peripheral
expect(result[0]).toBe(PERIPHERAL_HEADING);
expect(result[1].label).toEqual(p.body.label);
});
it("Makes a list of Sensor drop down selectors");
it("Makes a list of Sensor drop downs", () => {
const s = fakeSensor();
s.body.label = "The one";
const ri = buildResourceIndex([s]);
const result = sensorsAsDropDowns(ri.index);
expect(result.length).toEqual(2); // Heading + 1 peripheral
expect(result[0]).toBe(SENSOR_HEADING);
expect(result[1].label).toEqual(s.body.label);
});
});

View File

@ -1,59 +1,16 @@
import * as React from "react";
import {
SequenceBodyItem,
ReadPin,
WritePin,
} from "farmbot";
import { TaggedSequence } from "../../resources/tagged_resources";
import { editStep } from "../../api/crud";
import { maybeDetermineUuid, getAllSavedPeripherals } from "../../resources/selectors";
import {
getAllSavedPeripherals,
getAllSavedSensors
} from "../../resources/selectors";
import { ResourceIndex } from "../../resources/interfaces";
import { JSXChildren } from "../../util/index";
import { DropDownItem } from "../../ui";
import { range } from "lodash";
import { bail } from "../../util/errors";
export const EMPTY_READ_PIN: ReadPin = {
kind: "read_pin",
args: { pin_mode: 0, pin_number: 13, label: "" }
};
export const EMPTY_WRITE_PIN: WritePin = {
kind: "write_pin",
args: { pin_number: 13, pin_value: 0, pin_mode: 0 }
};
/** Generates a function that returns a redux action. */
export const changeStep =
/** When put inside a call to `dispatch()`, transforms the provided step from
* one `kind` to another. Ex: Turn `read_pin` to `read_peripheral`. */
(replacement: SequenceBodyItem) =>
(step: Readonly<SequenceBodyItem>,
sequence: Readonly<TaggedSequence>,
index: number) => {
return editStep({
step,
sequence,
index,
executor(c) {
c.kind = replacement.kind;
c.args = replacement.args;
c.body = replacement.body;
}
});
};
export const selectedItem = (id: number, resources: ResourceIndex) => {
const uuid = maybeDetermineUuid(resources, "Peripheral", id) || "_";
const item = resources.references[uuid];
if (item && item.kind === "Peripheral") {
return { label: item.body.label, value: item.body.id || 0 };
}
};
export const getPeripheralId = (step: SequenceBodyItem) => {
throw new Error("TODO");
};
import { TaggedPeripheral, TaggedSensor } from "../../resources/tagged_resources";
interface StepCheckBoxProps {
onClick(): void;
@ -76,25 +33,11 @@ export function StepCheckBox(props: StepCheckBoxProps) {
/** `headingIds` required to group the three kinds of pins. */
export enum PinGroupName { sensor = "👂", peripheral = "🔌", pin = "📌" }
export function sensorsAsDropDowns(input: ResourceIndex): DropDownItem[] {
console.log("TODO");
return [];
}
export const PERIPHERAL_HEADING: DropDownItem =
({ heading: true, label: "Peripherals", value: 0 });
export function peripheralsAsDropDowns(input: ResourceIndex): DropDownItem[] {
const all: DropDownItem[] = getAllSavedPeripherals(input).map(x => ({
label: x.body.label,
value: x.body.id || 0,
headingId: PinGroupName.peripheral
}));
return [PERIPHERAL_HEADING, ...all];
}
/** Number of pins in an Arduino Mega */
export const PIN_RANGE = range(0, 54);
export const SENSOR_HEADING: DropDownItem =
({ heading: true, label: "Sensors", value: 0 });
export const PIN_HEADING: DropDownItem =
({ heading: true, label: "Pins", value: 0 });
@ -103,9 +46,37 @@ export const PIN_HEADING: DropDownItem =
const pinNumber2DropDown =
(n: number) => ({ label: `Pin ${n}`, value: n, headingId: PinGroupName.pin });
const peripheral2DropDown =
(x: TaggedPeripheral): DropDownItem => ({
label: x.body.label,
value: x.body.id || 0,
headingId: PinGroupName.peripheral
});
const sensor2DropDown =
(x: TaggedSensor): DropDownItem => ({
label: x.body.label,
value: x.body.id || 0,
headingId: PinGroupName.peripheral
});
export function peripheralsAsDropDowns(input: ResourceIndex): DropDownItem[] {
return [
PERIPHERAL_HEADING,
...getAllSavedPeripherals(input).map(peripheral2DropDown)
];
}
export function sensorsAsDropDowns(input: ResourceIndex): DropDownItem[] {
return [SENSOR_HEADING, ...getAllSavedSensors(input).map(sensor2DropDown)];
}
/** Number of pins in an Arduino Mega */
export const PIN_RANGE = range(0, 54);
export const pinDropdowns = [PIN_HEADING, ...PIN_RANGE.map(pinNumber2DropDown)];
export const dropdownItemsForPinId = (input: ResourceIndex): DropDownItem[] => [
const dropdownItemsForPinId = (input: ResourceIndex): DropDownItem[] => [
...peripheralsAsDropDowns(input),
...sensorsAsDropDowns(input),
...pinDropdowns,