sprinklers3/app/script/sprinklers.ts

180 lines
3.8 KiB
TypeScript
Raw Normal View History

import { observable, IObservableArray } from "mobx";
2017-05-27 14:03:13 -06:00
export abstract class Section {
2017-06-21 19:04:15 -06:00
device: SprinklersDevice;
2017-05-27 14:03:13 -06:00
@observable
2017-06-21 19:04:15 -06:00
name: string = "";
@observable
2017-06-21 19:04:15 -06:00
state: boolean = false;
2017-05-27 14:03:13 -06:00
constructor(device: SprinklersDevice) {
this.device = device;
}
2017-06-21 19:04:15 -06:00
run(duration: Duration) {
2017-05-30 16:45:25 -06:00
return this.device.runSection(this, duration);
2017-05-27 14:03:13 -06:00
}
2017-06-21 19:04:15 -06:00
toString(): string {
2017-05-27 14:03:13 -06:00
return `Section{name="${this.name}", state=${this.state}}`;
}
}
2017-05-06 15:39:25 -06:00
export interface ITimeOfDay {
hour: number;
minute: number;
second: number;
millisecond: number;
}
2017-05-06 15:39:25 -06:00
export enum Weekday {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
}
2017-05-06 15:39:25 -06:00
export class Schedule {
2017-06-21 19:04:15 -06:00
times: ITimeOfDay[] = [];
weekdays: Weekday[] = [];
from?: Date = null;
to?: Date = null;
}
export class Duration {
2017-06-21 19:04:15 -06:00
static fromSeconds(seconds: number): Duration {
return new Duration(Math.floor(seconds / 60), seconds % 60);
}
2017-06-21 19:04:15 -06:00
minutes: number = 0;
seconds: number = 0;
constructor(minutes: number, seconds: number) {
this.minutes = minutes;
this.seconds = seconds;
}
2017-06-21 19:04:15 -06:00
toSeconds(): number {
return this.minutes * 60 + this.seconds;
}
2017-05-27 14:03:13 -06:00
2017-06-21 19:04:15 -06:00
withSeconds(newSeconds: number): Duration {
2017-05-27 14:03:13 -06:00
let newMinutes = this.minutes;
if (newSeconds >= 60) {
newMinutes++;
newSeconds = 0;
}
if (newSeconds < 0) {
newMinutes = Math.max(0, newMinutes - 1);
newSeconds = 59;
}
return new Duration(newMinutes, newSeconds);
}
2017-06-21 19:04:15 -06:00
withMinutes(newMinutes: number): Duration {
2017-05-27 14:03:13 -06:00
if (newMinutes < 0) {
newMinutes = 0;
}
return new Duration(newMinutes, this.seconds);
}
2017-06-21 19:04:15 -06:00
toString(): string {
2017-05-27 14:03:13 -06:00
return `${this.minutes}M ${this.seconds}S`;
}
}
2017-05-06 15:39:25 -06:00
export interface IProgramItem {
// the section number
section: number;
// duration of the run
duration: Duration;
}
2017-05-03 23:00:10 -06:00
export class Program {
2017-06-21 19:04:15 -06:00
device: SprinklersDevice;
2017-05-30 16:45:25 -06:00
@observable
2017-06-21 19:04:15 -06:00
name: string = "";
@observable
2017-06-21 19:04:15 -06:00
enabled: boolean = false;
@observable
2017-06-21 19:04:15 -06:00
schedule: Schedule = new Schedule();
@observable
2017-06-21 19:04:15 -06:00
sequence: IProgramItem[] = [];
2017-05-03 23:00:10 -06:00
@observable
2017-06-21 19:04:15 -06:00
running: boolean = false;
constructor(device: SprinklersDevice) {
this.device = device;
}
2017-05-30 16:45:25 -06:00
2017-06-21 19:04:15 -06:00
run() {
2017-05-30 16:45:25 -06:00
return this.device.runProgram(this);
}
2017-06-21 19:04:15 -06:00
toString(): string {
2017-05-30 16:45:25 -06:00
return `Program{name="${this.name}", enabled=${this.enabled}, schedule=${this.schedule},
sequence=${this.sequence}, running=${this.running}}`;
}
}
2017-06-30 00:09:25 -06:00
export interface ISectionRun {
id: number;
section: number;
duration: number;
startTime?: Date;
}
export class SectionRunner {
2017-06-30 00:09:25 -06:00
device: SprinklersDevice;
@observable
queue: IObservableArray<ISectionRun> = observable([]);
@observable
current: ISectionRun = null;
2017-06-30 00:09:25 -06:00
constructor(device: SprinklersDevice) {
this.device = device;
}
2017-06-30 00:30:41 -06:00
cancelRunById(id: number): Promise<{}> {
2017-06-30 00:09:25 -06:00
return this.device.cancelSectionRunById(id);
}
toString(): string {
return `SectionRunner{queue="${this.queue}", current="${this.current}"}`;
}
}
export abstract class SprinklersDevice {
@observable
2017-06-21 19:04:15 -06:00
connected: boolean = false;
@observable
2017-06-21 19:04:15 -06:00
sections: IObservableArray<Section> = [] as IObservableArray<Section>;
@observable
2017-06-21 19:04:15 -06:00
programs: IObservableArray<Program> = [] as IObservableArray<Program>;
2017-06-30 00:09:25 -06:00
@observable
sectionRunner: SectionRunner;
2017-06-21 19:04:15 -06:00
abstract runSection(section: number | Section, duration: Duration): Promise<{}>;
2017-05-30 16:45:25 -06:00
2017-06-21 19:04:15 -06:00
abstract runProgram(program: number | Program): Promise<{}>;
2017-05-27 14:03:13 -06:00
2017-06-30 00:30:41 -06:00
abstract cancelSectionRunById(id: number): Promise<{}>;
2017-06-30 00:09:25 -06:00
abstract get id(): string;
}
2017-05-06 15:39:25 -06:00
export interface ISprinklersApi {
start();
2017-05-06 15:39:25 -06:00
getDevice(id: string): SprinklersDevice;
2017-05-06 15:39:25 -06:00
removeDevice(id: string);
}