Browse Source

Added SectionRunner mqtt stuff

update-deps
Alex Mikhalev 8 years ago
parent
commit
d13a0a50e5
  1. 29
      app/script/mqtt.ts
  2. 30
      app/script/sprinklers.ts

29
app/script/mqtt.ts

@ -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;
}
}

30
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 { export class SectionRunner {
device: SprinklersDevice;
@observable
queue: IObservableArray<ISectionRun> = observable([]);
@observable
current: ISectionRun = null;
constructor(device: SprinklersDevice) {
this.device = device;
}
cancelRunById(id: number): Promise<void> {
return this.device.cancelSectionRunById(id);
}
toString(): string {
return `SectionRunner{queue="${this.queue}", current="${this.current}"}`;
}
} }
export abstract class SprinklersDevice { export abstract class SprinklersDevice {
@ -134,10 +159,15 @@ export abstract class SprinklersDevice {
@observable @observable
programs: IObservableArray<Program> = [] as IObservableArray<Program>; programs: IObservableArray<Program> = [] as IObservableArray<Program>;
@observable
sectionRunner: SectionRunner;
abstract runSection(section: number | Section, duration: Duration): Promise<{}>; abstract runSection(section: number | Section, duration: Duration): Promise<{}>;
abstract runProgram(program: number | Program): Promise<{}>; abstract runProgram(program: number | Program): Promise<{}>;
abstract cancelSectionRunById(id: number): Promise<void>;
abstract get id(): string; abstract get id(): string;
} }

Loading…
Cancel
Save