import { computed, observable } from "mobx"; import { ConnectionState } from "./ConnectionState"; import { Program } from "./Program"; import * as requests from "./requests"; import { Section } from "./Section"; import { SectionRunner } from "./SectionRunner"; export abstract class SprinklersDevice { @observable connectionState: ConnectionState = new ConnectionState(); @observable sections: Section[] = []; @observable programs: Program[] = []; @observable sectionRunner: SectionRunner; @computed get connected(): boolean { return this.connectionState.isConnected; } sectionConstructor: typeof Section = Section; sectionRunnerConstructor: typeof SectionRunner = SectionRunner; programConstructor: typeof Program = Program; constructor() { this.sectionRunner = new (this.sectionRunnerConstructor)(this); } abstract get id(): string; abstract makeRequest(request: requests.Request): Promise; runProgram(opts: requests.WithProgram) { return this.makeRequest({ ...opts, type: "runProgram" }); } cancelProgram(opts: requests.WithProgram) { return this.makeRequest({ ...opts, type: "cancelProgram" }); } updateProgram(opts: requests.UpdateProgramData): Promise { return this.makeRequest({ ...opts, type: "updateProgram" }) as Promise; } runSection(opts: requests.RunSectionData): Promise { return this.makeRequest({ ...opts, type: "runSection" }) as Promise; } cancelSection(opts: requests.WithSection) { return this.makeRequest({ ...opts, type: "cancelSection" }); } cancelSectionRunId(opts: requests.CancelSectionRunIdData) { return this.makeRequest({ ...opts, type: "cancelSectionRunId" }); } pauseSectionRunner(opts: requests.PauseSectionRunnerData) { return this.makeRequest({ ...opts, type: "pauseSectionRunner" }); } toString(): string { return `SprinklersDevice{id="${this.id}", connected=${this.connected}, ` + `sections=[${this.sections}], ` + `programs=[${this.programs}], ` + `sectionRunner=${this.sectionRunner} }`; } }