|
|
@ -3,7 +3,7 @@ import MQTT = Paho.MQTT; |
|
|
|
|
|
|
|
|
|
|
|
import {EventEmitter} from "events"; |
|
|
|
import {EventEmitter} from "events"; |
|
|
|
import { |
|
|
|
import { |
|
|
|
SprinklersDevice, ISprinklersApi, Section, Program, Schedule, ITimeOfDay, Duration, |
|
|
|
SprinklersDevice, ISprinklersApi, Section, Program, Schedule, ITimeOfDay, Duration, SectionRunner, ISectionRun, |
|
|
|
} from "./sprinklers"; |
|
|
|
} from "./sprinklers"; |
|
|
|
import {checkedIndexOf} from "./utils"; |
|
|
|
import {checkedIndexOf} from "./utils"; |
|
|
|
import * as Promise from "bluebird"; |
|
|
|
import * as Promise from "bluebird"; |
|
|
@ -104,6 +104,7 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
super(); |
|
|
|
super(); |
|
|
|
this.apiClient = apiClient; |
|
|
|
this.apiClient = apiClient; |
|
|
|
this.prefix = prefix; |
|
|
|
this.prefix = prefix; |
|
|
|
|
|
|
|
this.sectionRunner = new MqttSectionRunner(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
doSubscribe() { |
|
|
|
doSubscribe() { |
|
|
@ -163,6 +164,10 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
matches = topic.match(/^section_runner$/); |
|
|
|
|
|
|
|
if (matches != null) { |
|
|
|
|
|
|
|
(this.sectionRunner as MqttSectionRunner).onMessage(null, payload); |
|
|
|
|
|
|
|
} |
|
|
|
matches = topic.match(/^responses\/(\d+)$/); |
|
|
|
matches = topic.match(/^responses\/(\d+)$/); |
|
|
|
if (matches != null) { |
|
|
|
if (matches != null) { |
|
|
|
//noinspection JSUnusedLocalSymbols
|
|
|
|
//noinspection JSUnusedLocalSymbols
|
|
|
@ -196,6 +201,10 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
return this.makeRequest(`programs/${programNum}/run`, {}); |
|
|
|
return this.makeRequest(`programs/${programNum}/run`, {}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cancelSectionRunById(id: number) { |
|
|
|
|
|
|
|
return this.makeRequest(`section_runner/cancel_id`, { id }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//noinspection JSMethodCanBeStatic
|
|
|
|
//noinspection JSMethodCanBeStatic
|
|
|
|
private nextRequestId(): number { |
|
|
|
private nextRequestId(): number { |
|
|
|
return Math.floor(Math.random() * 1000000000); |
|
|
|
return Math.floor(Math.random() * 1000000000); |
|
|
@ -228,6 +237,7 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
`${this.prefix}/programs`, |
|
|
|
`${this.prefix}/programs`, |
|
|
|
`${this.prefix}/programs/+/#`, |
|
|
|
`${this.prefix}/programs/+/#`, |
|
|
|
`${this.prefix}/responses/+`, |
|
|
|
`${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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|