diff --git a/app/components/DurationInput.tsx b/app/components/DurationInput.tsx index 45c514c..83db646 100644 --- a/app/components/DurationInput.tsx +++ b/app/components/DurationInput.tsx @@ -1,7 +1,7 @@ import * as React from "react"; import { Input, InputProps } from "semantic-ui-react"; -import { Duration } from "@common/sprinklers"; +import { Duration } from "@common/Duration"; export default class DurationInput extends React.Component<{ duration: Duration, diff --git a/app/components/ProgramTable.tsx b/app/components/ProgramTable.tsx index 8571bb7..0e290e5 100644 --- a/app/components/ProgramTable.tsx +++ b/app/components/ProgramTable.tsx @@ -2,6 +2,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import { Table } from "semantic-ui-react"; +import { Duration } from "@common/Duration"; import { Program, Schedule } from "@common/sprinklers"; @observer @@ -20,10 +21,12 @@ export default class ProgramTable extends React.Component<{ programs: Program[] return null; } const { name, running, enabled, schedule, sequence } = program; - const sequenceItems = sequence.map((item, index) => ( -
  • Section {item.section + 1 + ""} for  - {item.duration.minutes}M {item.duration.seconds}S
  • - )); + const sequenceItems = sequence.map((item, index) => { + const duration = Duration.fromSeconds(item.duration); + return ( +
  • Section {item.section + 1 + ""} for {duration.toString()}
  • + ); + }); return [( {"" + (i + 1)} diff --git a/app/components/RunSectionForm.tsx b/app/components/RunSectionForm.tsx index 9db0e48..a4de427 100644 --- a/app/components/RunSectionForm.tsx +++ b/app/components/RunSectionForm.tsx @@ -3,8 +3,9 @@ import { observer } from "mobx-react"; import * as React from "react"; import { DropdownItemProps, DropdownProps, Form, Header, Segment } from "semantic-ui-react"; +import { Duration } from "@common/Duration"; import log from "@common/logger"; -import { Duration, Section } from "@common/sprinklers"; +import { Section } from "@common/sprinklers"; import DurationInput from "./DurationInput"; @observer @@ -17,7 +18,7 @@ export default class RunSectionForm extends React.Component<{ constructor() { super(); this.state = { - duration: new Duration(1, 1), + duration: new Duration(0, 0), section: "", }; } @@ -67,7 +68,7 @@ export default class RunSectionForm extends React.Component<{ } const section: Section = this.props.sections[this.state.section]; const { duration } = this.state; - section.run(duration) + section.run(duration.toSeconds()) .then((result) => log.debug({ result }, "requested section run")) .catch((err) => log.error(err, "error running section")); } diff --git a/common/sprinklers/Duration.ts b/common/Duration.ts similarity index 100% rename from common/sprinklers/Duration.ts rename to common/Duration.ts diff --git a/common/sprinklers/Program.ts b/common/sprinklers/Program.ts index 8675d54..a832ece 100644 --- a/common/sprinklers/Program.ts +++ b/common/sprinklers/Program.ts @@ -1,15 +1,14 @@ import { observable } from "mobx"; -import { Duration } from "./Duration"; import { Schedule } from "./schedule"; import { SprinklersDevice } from "./SprinklersDevice"; export class ProgramItem { // the section number readonly section: number; - // duration of the run - readonly duration: Duration; + // duration of the run, in seconds + readonly duration: number; - constructor(section: number = 0, duration: Duration = new Duration()) { + constructor(section: number = 0, duration: number = 0) { this.section = section; this.duration = duration; } diff --git a/common/sprinklers/Section.ts b/common/sprinklers/Section.ts index 2d6df89..3f8c710 100644 --- a/common/sprinklers/Section.ts +++ b/common/sprinklers/Section.ts @@ -1,5 +1,4 @@ import { observable } from "mobx"; -import { Duration } from "./Duration"; import { SprinklersDevice } from "./SprinklersDevice"; export class Section { @@ -14,7 +13,8 @@ export class Section { this.id = id; } - run(duration: Duration) { + /** duration is in seconds */ + run(duration: number) { return this.device.runSection({ sectionId: this.id, duration }); } diff --git a/common/sprinklers/SectionRunner.ts b/common/sprinklers/SectionRunner.ts index f6fa969..e4d2d56 100644 --- a/common/sprinklers/SectionRunner.ts +++ b/common/sprinklers/SectionRunner.ts @@ -1,12 +1,11 @@ import { observable } from "mobx"; -import { Duration } from "./Duration"; import { SprinklersDevice } from "./SprinklersDevice"; export class SectionRun { readonly sectionRunner: SectionRunner; readonly id: number; section: number; - duration: Duration = new Duration(); + duration: number = 0; startTime: Date | null = null; pauseTime: Date | null = null; diff --git a/common/sprinklers/index.ts b/common/sprinklers/index.ts index 8aad53d..b6d7175 100644 --- a/common/sprinklers/index.ts +++ b/common/sprinklers/index.ts @@ -1,4 +1,4 @@ -export * from "./Duration"; +// export * from "./Duration"; export * from "./ISprinklersApi"; export * from "./Program"; export * from "./schedule"; diff --git a/common/sprinklers/requests.ts b/common/sprinklers/requests.ts index f7c5c10..c688e45 100644 --- a/common/sprinklers/requests.ts +++ b/common/sprinklers/requests.ts @@ -1,5 +1,3 @@ -import { Duration } from "./Duration"; - export interface WithType { type: Type; } @@ -15,7 +13,7 @@ export type UpdateProgramResponse = Response<"updateProgram", { data: any }>; export interface WithSection { sectionId: number; } -export type RunSectionData = WithSection & { duration: Duration }; +export type RunSectionData = WithSection & { duration: number }; export type RunSectionReqeust = RunSectionData & WithType<"runSection">; export type RunSectionResponse = Response<"runSection", { runId: number }>; diff --git a/common/sprinklers/schema/common.ts b/common/sprinklers/schema/common.ts index 04624c2..82ca2cb 100644 --- a/common/sprinklers/schema/common.ts +++ b/common/sprinklers/schema/common.ts @@ -3,17 +3,7 @@ import { } from "serializr"; import * as s from ".."; -export const duration: PropSchema = { - serializer: (d: s.Duration | null) => - d != null ? d.toSeconds() : null, - deserializer: (json: any, done) => { - if (typeof json === "number") { - done(null, s.Duration.fromSeconds(json)); - } else { - done(new Error(`Duration expects a number, not ${json}`), undefined); - } - }, -}; +export const duration: PropSchema = primitive(); export const date: PropSchema = { serializer: (jsDate: Date | null) => jsDate != null ?