Browse Source

Extracted Duration from sprinklers

update-deps
Alex Mikhalev 7 years ago
parent
commit
3ce9bb4116
  1. 2
      app/components/DurationInput.tsx
  2. 11
      app/components/ProgramTable.tsx
  3. 7
      app/components/RunSectionForm.tsx
  4. 0
      common/Duration.ts
  5. 7
      common/sprinklers/Program.ts
  6. 4
      common/sprinklers/Section.ts
  7. 3
      common/sprinklers/SectionRunner.ts
  8. 2
      common/sprinklers/index.ts
  9. 4
      common/sprinklers/requests.ts
  10. 12
      common/sprinklers/schema/common.ts

2
app/components/DurationInput.tsx

@ -1,7 +1,7 @@ @@ -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,

11
app/components/ProgramTable.tsx

@ -2,6 +2,7 @@ import { observer } from "mobx-react"; @@ -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[] @@ -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) => (
<li key={index}>Section {item.section + 1 + ""} for&nbsp;
{item.duration.minutes}M {item.duration.seconds}S</li>
));
const sequenceItems = sequence.map((item, index) => {
const duration = Duration.fromSeconds(item.duration);
return (
<li key={index}>Section {item.section + 1 + ""} for {duration.toString()}</li>
);
});
return [(
<Table.Row key={i}>
<Table.Cell className="program--number">{"" + (i + 1)}</Table.Cell>

7
app/components/RunSectionForm.tsx

@ -3,8 +3,9 @@ import { observer } from "mobx-react"; @@ -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<{ @@ -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<{ @@ -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"));
}

0
common/sprinklers/Duration.ts → common/Duration.ts

7
common/sprinklers/Program.ts

@ -1,15 +1,14 @@ @@ -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;
}

4
common/sprinklers/Section.ts

@ -1,5 +1,4 @@ @@ -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 { @@ -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 });
}

3
common/sprinklers/SectionRunner.ts

@ -1,12 +1,11 @@ @@ -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;

2
common/sprinklers/index.ts

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
export * from "./Duration";
// export * from "./Duration";
export * from "./ISprinklersApi";
export * from "./Program";
export * from "./schedule";

4
common/sprinklers/requests.ts

@ -1,5 +1,3 @@ @@ -1,5 +1,3 @@
import { Duration } from "./Duration";
export interface WithType<Type extends string = string> {
type: Type;
}
@ -15,7 +13,7 @@ export type UpdateProgramResponse = Response<"updateProgram", { data: any }>; @@ -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 }>;

12
common/sprinklers/schema/common.ts

@ -3,17 +3,7 @@ import { @@ -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 ?

Loading…
Cancel
Save