diff --git a/app/script/mqtt.ts b/app/script/mqtt.ts index 5fcd2d3..4b095cc 100644 --- a/app/script/mqtt.ts +++ b/app/script/mqtt.ts @@ -3,7 +3,7 @@ import MQTT = Paho.MQTT; import {EventEmitter} from "events"; import { - SprinklersDevice, ISprinklersApi, Section, Program, Schedule, ITimeOfDay, Duration, + SprinklersDevice, ISprinklersApi, Section, Program, Schedule, ITimeOfDay, Duration, SectionRunner, ISectionRun, } from "./sprinklers"; import {checkedIndexOf} from "./utils"; import * as Promise from "bluebird"; @@ -104,6 +104,7 @@ class MqttSprinklersDevice extends SprinklersDevice { super(); this.apiClient = apiClient; this.prefix = prefix; + this.sectionRunner = new MqttSectionRunner(this); } doSubscribe() { @@ -163,6 +164,10 @@ class MqttSprinklersDevice extends SprinklersDevice { } return; } + matches = topic.match(/^section_runner$/); + if (matches != null) { + (this.sectionRunner as MqttSectionRunner).onMessage(null, payload); + } matches = topic.match(/^responses\/(\d+)$/); if (matches != null) { //noinspection JSUnusedLocalSymbols @@ -196,6 +201,10 @@ class MqttSprinklersDevice extends SprinklersDevice { return this.makeRequest(`programs/${programNum}/run`, {}); } + cancelSectionRunById(id: number) { + return this.makeRequest(`section_runner/cancel_id`, { id }); + } + //noinspection JSMethodCanBeStatic private nextRequestId(): number { return Math.floor(Math.random() * 1000000000); @@ -228,6 +237,7 @@ class MqttSprinklersDevice extends SprinklersDevice { `${this.prefix}/programs`, `${this.prefix}/programs/+/#`, `${this.prefix}/responses/+`, + `${this.prefix}/section_runner`, ]; } } @@ -317,3 +327,20 @@ class MqttProgram extends Program { } } } + +interface ISectionRunnerJSON { + queue: ISectionRun[]; + current?: ISectionRun; +} + +class MqttSectionRunner extends SectionRunner { + onMessage(topic: string, payload: string) { + const json = JSON.parse(payload) as ISectionRunnerJSON; + this.updateFromJSON(json); + } + + updateFromJSON(json: ISectionRunnerJSON) { + this.queue.replace(json.queue); + this.current = json.current; + } +} diff --git a/app/script/sprinklers.ts b/app/script/sprinklers.ts index 2ee81d1..de67499 100644 --- a/app/script/sprinklers.ts +++ b/app/script/sprinklers.ts @@ -120,8 +120,33 @@ export class Program { } } +export interface ISectionRun { + id: number; + section: number; + duration: number; + startTime?: Date; +} + export class SectionRunner { + device: SprinklersDevice; + + @observable + queue: IObservableArray = observable([]); + + @observable + current: ISectionRun = null; + constructor(device: SprinklersDevice) { + this.device = device; + } + + cancelRunById(id: number): Promise { + return this.device.cancelSectionRunById(id); + } + + toString(): string { + return `SectionRunner{queue="${this.queue}", current="${this.current}"}`; + } } export abstract class SprinklersDevice { @@ -134,10 +159,15 @@ export abstract class SprinklersDevice { @observable programs: IObservableArray = [] as IObservableArray; + @observable + sectionRunner: SectionRunner; + abstract runSection(section: number | Section, duration: Duration): Promise<{}>; abstract runProgram(program: number | Program): Promise<{}>; + abstract cancelSectionRunById(id: number): Promise; + abstract get id(): string; }