You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
5.0 KiB
218 lines
5.0 KiB
7 years ago
|
import { IObservableArray, observable } from "mobx";
|
||
8 years ago
|
|
||
8 years ago
|
export abstract class Section {
|
||
8 years ago
|
device: SprinklersDevice;
|
||
8 years ago
|
|
||
8 years ago
|
@observable
|
||
8 years ago
|
name: string = "";
|
||
8 years ago
|
|
||
|
@observable
|
||
8 years ago
|
state: boolean = false;
|
||
8 years ago
|
|
||
|
constructor(device: SprinklersDevice) {
|
||
|
this.device = device;
|
||
|
}
|
||
|
|
||
8 years ago
|
run(duration: Duration) {
|
||
8 years ago
|
return this.device.runSection(this, duration);
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
toString(): string {
|
||
8 years ago
|
return `Section{name="${this.name}", state=${this.state}}`;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
export class TimeOfDay {
|
||
8 years ago
|
hour: number;
|
||
|
minute: number;
|
||
|
second: number;
|
||
|
millisecond: number;
|
||
7 years ago
|
|
||
|
constructor(hour: number, minute: number = 0, second: number = 0, millisecond: number = 0) {
|
||
|
this.hour = hour;
|
||
|
this.minute = minute;
|
||
|
this.second = second;
|
||
|
this.millisecond = millisecond;
|
||
|
}
|
||
|
|
||
|
static fromDate(date: Date): TimeOfDay {
|
||
|
return new TimeOfDay(date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
export enum Weekday {
|
||
|
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
export class Schedule {
|
||
7 years ago
|
times: TimeOfDay[] = [];
|
||
8 years ago
|
weekdays: Weekday[] = [];
|
||
7 years ago
|
from: Date | null = null;
|
||
|
to: Date | null = null;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
export class Duration {
|
||
8 years ago
|
minutes: number = 0;
|
||
|
seconds: number = 0;
|
||
8 years ago
|
|
||
7 years ago
|
constructor(minutes: number = 0, seconds: number = 0) {
|
||
8 years ago
|
this.minutes = minutes;
|
||
|
this.seconds = seconds;
|
||
|
}
|
||
|
|
||
8 years ago
|
static fromSeconds(seconds: number): Duration {
|
||
|
return new Duration(Math.floor(seconds / 60), seconds % 60);
|
||
|
}
|
||
|
|
||
8 years ago
|
toSeconds(): number {
|
||
8 years ago
|
return this.minutes * 60 + this.seconds;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
withSeconds(newSeconds: number): Duration {
|
||
8 years ago
|
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);
|
||
|
}
|
||
|
|
||
8 years ago
|
withMinutes(newMinutes: number): Duration {
|
||
8 years ago
|
if (newMinutes < 0) {
|
||
|
newMinutes = 0;
|
||
|
}
|
||
|
return new Duration(newMinutes, this.seconds);
|
||
|
}
|
||
|
|
||
8 years ago
|
toString(): string {
|
||
8 years ago
|
return `${this.minutes}M ${this.seconds}S`;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
export class ProgramItem {
|
||
8 years ago
|
// the section number
|
||
|
section: number;
|
||
8 years ago
|
// duration of the run
|
||
|
duration: Duration;
|
||
7 years ago
|
|
||
|
constructor(section: number, duration: Duration) {
|
||
|
this.section = section;
|
||
|
this.duration = duration;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
export class Program {
|
||
8 years ago
|
device: SprinklersDevice;
|
||
8 years ago
|
|
||
8 years ago
|
@observable
|
||
8 years ago
|
name: string = "";
|
||
8 years ago
|
@observable
|
||
8 years ago
|
enabled: boolean = false;
|
||
8 years ago
|
|
||
|
@observable
|
||
8 years ago
|
schedule: Schedule = new Schedule();
|
||
8 years ago
|
|
||
|
@observable
|
||
7 years ago
|
sequence: ProgramItem[] = [];
|
||
8 years ago
|
|
||
|
@observable
|
||
8 years ago
|
running: boolean = false;
|
||
|
|
||
|
constructor(device: SprinklersDevice) {
|
||
|
this.device = device;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
run() {
|
||
8 years ago
|
return this.device.runProgram(this);
|
||
|
}
|
||
|
|
||
8 years ago
|
toString(): string {
|
||
8 years ago
|
return `Program{name="${this.name}", enabled=${this.enabled}, schedule=${this.schedule},
|
||
|
sequence=${this.sequence}, running=${this.running}}`;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
export class SectionRun {
|
||
8 years ago
|
id: number;
|
||
|
section: number;
|
||
7 years ago
|
duration: Duration;
|
||
|
startTime: Date | null;
|
||
|
pauseTime: Date | null;
|
||
|
|
||
|
constructor(id: number = 0, section: number = 0, duration: Duration = new Duration()) {
|
||
|
this.id = id;
|
||
|
this.section = section;
|
||
|
this.duration = duration;
|
||
|
this.startTime = null;
|
||
|
this.pauseTime = null;
|
||
|
}
|
||
|
|
||
|
toString() {
|
||
|
return `SectionRun{id=${this.id}, section=${this.section}, duration=${this.duration},` +
|
||
|
` startTime=${this.startTime}, pauseTime=${this.pauseTime}}`;
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
export class SectionRunner {
|
||
8 years ago
|
device: SprinklersDevice;
|
||
|
|
||
|
@observable
|
||
7 years ago
|
queue: IObservableArray<SectionRun> = observable([]);
|
||
8 years ago
|
|
||
|
@observable
|
||
7 years ago
|
current: SectionRun | null = null;
|
||
|
|
||
|
@observable
|
||
|
paused: boolean = false;
|
||
8 years ago
|
|
||
8 years ago
|
constructor(device: SprinklersDevice) {
|
||
|
this.device = device;
|
||
|
}
|
||
|
|
||
8 years ago
|
cancelRunById(id: number): Promise<{}> {
|
||
8 years ago
|
return this.device.cancelSectionRunById(id);
|
||
|
}
|
||
|
|
||
|
toString(): string {
|
||
7 years ago
|
return `SectionRunner{queue="${this.queue}", current="${this.current}", paused=${this.paused}}`;
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
export abstract class SprinklersDevice {
|
||
|
@observable
|
||
8 years ago
|
connected: boolean = false;
|
||
8 years ago
|
|
||
|
@observable
|
||
7 years ago
|
sections: IObservableArray<Section> = observable.array<Section>();
|
||
8 years ago
|
|
||
|
@observable
|
||
7 years ago
|
programs: IObservableArray<Program> = observable.array<Program>();
|
||
8 years ago
|
|
||
8 years ago
|
@observable
|
||
|
sectionRunner: SectionRunner;
|
||
|
|
||
8 years ago
|
abstract get id(): string;
|
||
|
|
||
8 years ago
|
abstract runSection(section: number | Section, duration: Duration): Promise<{}>;
|
||
8 years ago
|
|
||
8 years ago
|
abstract runProgram(program: number | Program): Promise<{}>;
|
||
8 years ago
|
|
||
8 years ago
|
abstract cancelSectionRunById(id: number): Promise<{}>;
|
||
7 years ago
|
|
||
|
abstract pauseSectionRunner(): Promise<{}>;
|
||
|
|
||
|
abstract unpauseSectionRunner(): Promise<{}>;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
export interface ISprinklersApi {
|
||
7 years ago
|
start(): void;
|
||
8 years ago
|
|
||
8 years ago
|
getDevice(id: string): SprinklersDevice;
|
||
8 years ago
|
|
||
7 years ago
|
removeDevice(id: string): void;
|
||
8 years ago
|
}
|