2018-06-16 23:54:03 -06:00
|
|
|
import { computed, observable } from "mobx";
|
|
|
|
import { ConnectionState } from "./ConnectionState";
|
2018-06-30 21:18:41 -06:00
|
|
|
import * as req from "./deviceRequests";
|
2017-10-04 13:22:10 -06:00
|
|
|
import { Program } from "./Program";
|
|
|
|
import { Section } from "./Section";
|
|
|
|
import { SectionRunner } from "./SectionRunner";
|
|
|
|
|
|
|
|
export abstract class SprinklersDevice {
|
2018-06-16 23:54:03 -06:00
|
|
|
@observable connectionState: ConnectionState = new ConnectionState();
|
2017-10-05 09:07:07 -06:00
|
|
|
@observable sections: Section[] = [];
|
|
|
|
@observable programs: Program[] = [];
|
|
|
|
@observable sectionRunner: SectionRunner;
|
2017-10-04 13:22:10 -06:00
|
|
|
|
2018-06-16 23:54:03 -06:00
|
|
|
@computed get connected(): boolean {
|
2018-07-13 00:04:59 -06:00
|
|
|
return this.connectionState.isDeviceConnected || false;
|
2018-06-16 23:54:03 -06:00
|
|
|
}
|
|
|
|
|
2017-10-13 16:11:37 -06:00
|
|
|
sectionConstructor: typeof Section = Section;
|
|
|
|
sectionRunnerConstructor: typeof SectionRunner = SectionRunner;
|
|
|
|
programConstructor: typeof Program = Program;
|
|
|
|
|
2018-07-25 12:53:33 -06:00
|
|
|
protected constructor() {
|
2017-10-05 15:15:50 -06:00
|
|
|
this.sectionRunner = new (this.sectionRunnerConstructor)(this);
|
|
|
|
}
|
|
|
|
|
2017-10-04 13:22:10 -06:00
|
|
|
abstract get id(): string;
|
2018-06-16 23:54:03 -06:00
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
abstract makeRequest(request: req.Request): Promise<req.Response>;
|
2017-10-10 16:25:21 -06:00
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
runProgram(opts: req.WithProgram) {
|
2017-10-10 16:25:21 -06:00
|
|
|
return this.makeRequest({ ...opts, type: "runProgram" });
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
cancelProgram(opts: req.WithProgram) {
|
2017-10-10 16:25:21 -06:00
|
|
|
return this.makeRequest({ ...opts, type: "cancelProgram" });
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
updateProgram(opts: req.UpdateProgramData): Promise<req.UpdateProgramResponse> {
|
2017-10-10 16:25:21 -06:00
|
|
|
return this.makeRequest({ ...opts, type: "updateProgram" }) as Promise<any>;
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
runSection(opts: req.RunSectionData): Promise<req.RunSectionResponse> {
|
2017-10-10 16:25:21 -06:00
|
|
|
return this.makeRequest({ ...opts, type: "runSection" }) as Promise<any>;
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
cancelSection(opts: req.WithSection) {
|
2017-10-10 16:25:21 -06:00
|
|
|
return this.makeRequest({ ...opts, type: "cancelSection" });
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
cancelSectionRunId(opts: req.CancelSectionRunIdData) {
|
2017-10-10 16:25:21 -06:00
|
|
|
return this.makeRequest({ ...opts, type: "cancelSectionRunId" });
|
|
|
|
}
|
|
|
|
|
2018-06-30 21:18:41 -06:00
|
|
|
pauseSectionRunner(opts: req.PauseSectionRunnerData) {
|
2017-10-10 16:25:21 -06:00
|
|
|
return this.makeRequest({ ...opts, type: "pauseSectionRunner" });
|
|
|
|
}
|
2017-10-04 13:22:10 -06:00
|
|
|
|
|
|
|
toString(): string {
|
2017-10-04 13:51:06 -06:00
|
|
|
return `SprinklersDevice{id="${this.id}", connected=${this.connected}, ` +
|
|
|
|
`sections=[${this.sections}], ` +
|
|
|
|
`programs=[${this.programs}], ` +
|
|
|
|
`sectionRunner=${this.sectionRunner} }`;
|
2017-10-04 13:22:10 -06:00
|
|
|
}
|
|
|
|
}
|