sprinklers3/common/sprinklersRpc/SprinklersDevice.ts

65 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-06-16 23:54:03 -06:00
import { computed, observable } from "mobx";
import { ConnectionState } from "./ConnectionState";
import * as req from "./deviceRequests";
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;
2018-06-16 23:54:03 -06:00
@computed get connected(): boolean {
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);
}
abstract get id(): string;
2018-06-16 23:54:03 -06:00
abstract makeRequest(request: req.Request): Promise<req.Response>;
runProgram(opts: req.WithProgram) {
return this.makeRequest({ ...opts, type: "runProgram" });
}
cancelProgram(opts: req.WithProgram) {
return this.makeRequest({ ...opts, type: "cancelProgram" });
}
updateProgram(opts: req.UpdateProgramData): Promise<req.UpdateProgramResponse> {
return this.makeRequest({ ...opts, type: "updateProgram" }) as Promise<any>;
}
runSection(opts: req.RunSectionData): Promise<req.RunSectionResponse> {
return this.makeRequest({ ...opts, type: "runSection" }) as Promise<any>;
}
cancelSection(opts: req.WithSection) {
return this.makeRequest({ ...opts, type: "cancelSection" });
}
cancelSectionRunId(opts: req.CancelSectionRunIdData) {
return this.makeRequest({ ...opts, type: "cancelSectionRunId" });
}
pauseSectionRunner(opts: req.PauseSectionRunnerData) {
return this.makeRequest({ ...opts, type: "pauseSectionRunner" });
}
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} }`;
}
}