75 lines
1.7 KiB
TypeScript
Raw Normal View History

import { observable } from "mobx";
import { serialize } from "serializr";
import { Schedule } from "./schedule";
import * as schema from "./schema";
import { SprinklersDevice } from "./SprinklersDevice";
export class ProgramItem {
2018-09-02 02:57:55 -06:00
// the section number
readonly section!: number;
// duration of the run, in seconds
readonly duration!: number;
2018-09-02 02:57:55 -06:00
constructor(data?: Partial<ProgramItem>) {
if (data) {
Object.assign(this, data);
}
2018-09-02 02:57:55 -06:00
}
}
export class Program {
2018-09-02 02:57:55 -06:00
readonly device: SprinklersDevice;
readonly id: number;
2018-09-02 02:57:55 -06:00
@observable
name: string = "";
@observable
enabled: boolean = false;
@observable
schedule: Schedule = new Schedule();
@observable.shallow
sequence: ProgramItem[] = [];
@observable
running: boolean = false;
2018-09-02 02:57:55 -06:00
constructor(device: SprinklersDevice, id: number, data?: Partial<Program>) {
this.device = device;
this.id = id;
if (data) {
Object.assign(this, data);
}
2018-09-02 02:57:55 -06:00
}
2018-09-02 02:57:55 -06:00
run() {
return this.device.runProgram({ programId: this.id });
}
2018-09-02 02:57:55 -06:00
cancel() {
return this.device.cancelProgram({ programId: this.id });
}
2018-09-02 02:57:55 -06:00
update() {
const data = serialize(schema.program, this);
return this.device.updateProgram({ programId: this.id, data });
}
2018-09-02 02:57:55 -06:00
clone(): Program {
return new Program(this.device, this.id, {
name: this.name,
enabled: this.enabled,
running: this.running,
schedule: this.schedule.clone(),
sequence: this.sequence.slice()
});
}
2018-08-07 11:55:15 +03:00
2018-09-02 02:57:55 -06:00
toString(): string {
return (
`Program{name="${this.name}", enabled=${this.enabled}, schedule=${
this.schedule
}, ` + `sequence=${this.sequence}, running=${this.running}}`
);
}
}