|
|
@ -1,21 +1,22 @@ |
|
|
|
import "paho-mqtt"; |
|
|
|
|
|
|
|
import MQTT = Paho.MQTT; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import {EventEmitter} from "events"; |
|
|
|
import {EventEmitter} from "events"; |
|
|
|
|
|
|
|
import "paho-mqtt"; |
|
|
|
import { |
|
|
|
import { |
|
|
|
SprinklersDevice, ISprinklersApi, Section, Program, Schedule, ITimeOfDay, Duration, SectionRunner, ISectionRun, |
|
|
|
Duration, |
|
|
|
|
|
|
|
ISectionRun, |
|
|
|
|
|
|
|
ISprinklersApi, |
|
|
|
|
|
|
|
ITimeOfDay, |
|
|
|
|
|
|
|
Program, |
|
|
|
|
|
|
|
Schedule, |
|
|
|
|
|
|
|
Section, |
|
|
|
|
|
|
|
SectionRunner, |
|
|
|
|
|
|
|
SprinklersDevice, |
|
|
|
} from "./sprinklers"; |
|
|
|
} from "./sprinklers"; |
|
|
|
import {checkedIndexOf} from "./utils"; |
|
|
|
import {checkedIndexOf} from "./utils"; |
|
|
|
|
|
|
|
import MQTT = Paho.MQTT; |
|
|
|
|
|
|
|
|
|
|
|
export class MqttApiClient extends EventEmitter implements ISprinklersApi { |
|
|
|
export class MqttApiClient extends EventEmitter implements ISprinklersApi { |
|
|
|
private static newClientId() { |
|
|
|
|
|
|
|
return "sprinklers3-MqttApiClient-" + Math.round(Math.random() * 1000); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client: MQTT.Client; |
|
|
|
client: MQTT.Client; |
|
|
|
|
|
|
|
|
|
|
|
connected: boolean; |
|
|
|
connected: boolean; |
|
|
|
|
|
|
|
|
|
|
|
devices: { [prefix: string]: MqttSprinklersDevice } = {}; |
|
|
|
devices: { [prefix: string]: MqttSprinklersDevice } = {}; |
|
|
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
constructor() { |
|
|
@ -26,6 +27,10 @@ export class MqttApiClient extends EventEmitter implements ISprinklersApi { |
|
|
|
// (this.client as any).trace = (m => console.log(m));
|
|
|
|
// (this.client as any).trace = (m => console.log(m));
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static newClientId() { |
|
|
|
|
|
|
|
return "sprinklers3-MqttApiClient-" + Math.round(Math.random() * 1000); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
start() { |
|
|
|
start() { |
|
|
|
console.log("connecting to mqtt with client id %s", this.client.clientId); |
|
|
|
console.log("connecting to mqtt with client id %s", this.client.clientId); |
|
|
|
this.client.connect({ |
|
|
|
this.client.connect({ |
|
|
@ -106,6 +111,22 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
this.sectionRunner = new MqttSectionRunner(this); |
|
|
|
this.sectionRunner = new MqttSectionRunner(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get id(): string { |
|
|
|
|
|
|
|
return this.prefix; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private get subscriptions() { |
|
|
|
|
|
|
|
return [ |
|
|
|
|
|
|
|
`${this.prefix}/connected`, |
|
|
|
|
|
|
|
`${this.prefix}/sections`, |
|
|
|
|
|
|
|
`${this.prefix}/sections/+/#`, |
|
|
|
|
|
|
|
`${this.prefix}/programs`, |
|
|
|
|
|
|
|
`${this.prefix}/programs/+/#`, |
|
|
|
|
|
|
|
`${this.prefix}/responses/+`, |
|
|
|
|
|
|
|
`${this.prefix}/section_runner`, |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
doSubscribe() { |
|
|
|
doSubscribe() { |
|
|
|
const c = this.apiClient.client; |
|
|
|
const c = this.apiClient.client; |
|
|
|
this.subscriptions |
|
|
|
this.subscriptions |
|
|
@ -184,10 +205,6 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
console.warn(`MqttSprinklersDevice recieved invalid topic: ${topic}`); |
|
|
|
console.warn(`MqttSprinklersDevice recieved invalid topic: ${topic}`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get id(): string { |
|
|
|
|
|
|
|
return this.prefix; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
runSection(section: Section | number, duration: Duration) { |
|
|
|
runSection(section: Section | number, duration: Duration) { |
|
|
|
const sectionNum = checkedIndexOf(section, this.sections, "Section"); |
|
|
|
const sectionNum = checkedIndexOf(section, this.sections, "Section"); |
|
|
|
const payload: IRunSectionJSON = { |
|
|
|
const payload: IRunSectionJSON = { |
|
|
@ -202,7 +219,7 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cancelSectionRunById(id: number) { |
|
|
|
cancelSectionRunById(id: number) { |
|
|
|
return this.makeRequest(`section_runner/cancel_id`, { id }); |
|
|
|
return this.makeRequest(`section_runner/cancel_id`, {id}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//noinspection JSMethodCanBeStatic
|
|
|
|
//noinspection JSMethodCanBeStatic
|
|
|
@ -228,23 +245,12 @@ class MqttSprinklersDevice extends SprinklersDevice { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private get subscriptions() { |
|
|
|
|
|
|
|
return [ |
|
|
|
|
|
|
|
`${this.prefix}/connected`, |
|
|
|
|
|
|
|
`${this.prefix}/sections`, |
|
|
|
|
|
|
|
`${this.prefix}/sections/+/#`, |
|
|
|
|
|
|
|
`${this.prefix}/programs`, |
|
|
|
|
|
|
|
`${this.prefix}/programs/+/#`, |
|
|
|
|
|
|
|
`${this.prefix}/responses/+`, |
|
|
|
|
|
|
|
`${this.prefix}/section_runner`, |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
interface IResponseData { |
|
|
|
interface IResponseData { |
|
|
|
reqTopic: string; |
|
|
|
reqTopic: string; |
|
|
|
error?: string; |
|
|
|
error?: string; |
|
|
|
|
|
|
|
|
|
|
|
[key: string]: any; |
|
|
|
[key: string]: any; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|