Cleaned up mobx and ts stuff
This commit is contained in:
parent
111fc0c85d
commit
52ba25c038
@ -5,9 +5,9 @@ import { SprinklersDevice } from "./SprinklersDevice";
|
||||
|
||||
export class ProgramItem {
|
||||
// the section number
|
||||
section: number;
|
||||
readonly section: number;
|
||||
// duration of the run
|
||||
duration: Duration;
|
||||
readonly duration: Duration;
|
||||
|
||||
constructor(section: number, duration: Duration) {
|
||||
this.section = section;
|
||||
@ -16,24 +16,18 @@ export class ProgramItem {
|
||||
}
|
||||
|
||||
export class Program {
|
||||
device: SprinklersDevice;
|
||||
readonly device: SprinklersDevice;
|
||||
readonly id: number;
|
||||
|
||||
@observable
|
||||
name: string = "";
|
||||
@observable
|
||||
enabled: boolean = false;
|
||||
@observable name: string = "";
|
||||
@observable enabled: boolean = false;
|
||||
@observable schedule: Schedule = new Schedule();
|
||||
@observable.shallow sequence: ProgramItem[] = [];
|
||||
@observable running: boolean = false;
|
||||
|
||||
@observable
|
||||
schedule: Schedule = new Schedule();
|
||||
|
||||
@observable
|
||||
sequence: ProgramItem[] = [];
|
||||
|
||||
@observable
|
||||
running: boolean = false;
|
||||
|
||||
constructor(device: SprinklersDevice) {
|
||||
constructor(device: SprinklersDevice, id: number) {
|
||||
this.device = device;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
run() {
|
||||
|
@ -3,16 +3,15 @@ import { Duration } from "./Duration";
|
||||
import { SprinklersDevice } from "./SprinklersDevice";
|
||||
|
||||
export class Section {
|
||||
device: SprinklersDevice;
|
||||
readonly device: SprinklersDevice;
|
||||
readonly id: number;
|
||||
|
||||
@observable
|
||||
name: string = "";
|
||||
@observable name: string = "";
|
||||
@observable state: boolean = false;
|
||||
|
||||
@observable
|
||||
state: boolean = false;
|
||||
|
||||
constructor(device: SprinklersDevice) {
|
||||
constructor(device: SprinklersDevice, id: number) {
|
||||
this.device = device;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
run(duration: Duration) {
|
||||
@ -20,6 +19,6 @@ export class Section {
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `Section{name="${this.name}", state=${this.state}}`;
|
||||
return `Section{id=${this.id}, name="${this.name}", state=${this.state}}`;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import { Duration } from "./Duration";
|
||||
import { SprinklersDevice } from "./SprinklersDevice";
|
||||
|
||||
export class SectionRun {
|
||||
id: number;
|
||||
readonly id: number;
|
||||
section: number;
|
||||
duration: Duration;
|
||||
startTime: Date | null;
|
||||
@ -24,16 +24,11 @@ export class SectionRun {
|
||||
}
|
||||
|
||||
export class SectionRunner {
|
||||
device: SprinklersDevice;
|
||||
readonly device: SprinklersDevice;
|
||||
|
||||
@observable
|
||||
queue: IObservableArray<SectionRun> = observable([]);
|
||||
|
||||
@observable
|
||||
current: SectionRun | null = null;
|
||||
|
||||
@observable
|
||||
paused: boolean = false;
|
||||
@observable queue: SectionRun[] = [];
|
||||
@observable current: SectionRun | null = null;
|
||||
@observable paused: boolean = false;
|
||||
|
||||
constructor(device: SprinklersDevice) {
|
||||
this.device = device;
|
||||
|
@ -5,28 +5,16 @@ import { Section } from "./Section";
|
||||
import { SectionRunner } from "./SectionRunner";
|
||||
|
||||
export abstract class SprinklersDevice {
|
||||
@observable
|
||||
connected: boolean = false;
|
||||
|
||||
@observable
|
||||
sections: IObservableArray<Section> = observable.array<Section>();
|
||||
|
||||
@observable
|
||||
programs: IObservableArray<Program> = observable.array<Program>();
|
||||
|
||||
@observable
|
||||
sectionRunner: SectionRunner;
|
||||
@observable connected: boolean = false;
|
||||
@observable sections: Section[] = [];
|
||||
@observable programs: Program[] = [];
|
||||
@observable sectionRunner: SectionRunner;
|
||||
|
||||
abstract get id(): string;
|
||||
|
||||
abstract runSection(section: number | Section, duration: Duration): Promise<{}>;
|
||||
|
||||
abstract runProgram(program: number | Program): Promise<{}>;
|
||||
|
||||
abstract cancelSectionRunById(id: number): Promise<{}>;
|
||||
|
||||
abstract pauseSectionRunner(): Promise<{}>;
|
||||
|
||||
abstract unpauseSectionRunner(): Promise<{}>;
|
||||
|
||||
toString(): string {
|
||||
|
@ -66,9 +66,9 @@ export function programItemToJSON(programItem: s.ProgramItem): IProgramItemJSON
|
||||
};
|
||||
}
|
||||
|
||||
export function programItemFromJSON(programItem: s.ProgramItem, json: IProgramItemJSON) {
|
||||
assign(programItem, pick(json, programItemProps));
|
||||
programItem.duration = s.Duration.fromSeconds(json.duration);
|
||||
export function programItemFromJSON(json: IProgramItemJSON): s.ProgramItem {
|
||||
const duration = s.Duration.fromSeconds(json.duration);
|
||||
return new s.ProgramItem(json.section, duration);
|
||||
}
|
||||
|
||||
export interface IProgramJSON {
|
||||
@ -90,9 +90,8 @@ export function programToJSON(program: s.Program): IProgramJSON {
|
||||
|
||||
export function programFromJSON(program: s.Program, json: IProgramJSON) {
|
||||
assign(program, pick(json, programProps));
|
||||
program.sequence.length = json.sequence.length;
|
||||
program.sequence.forEach((programItem, i) =>
|
||||
programItemFromJSON(programItem, json.sequence[i]));
|
||||
program.sequence = json.sequence.map((programItemJson) =>
|
||||
programItemFromJSON(programItemJson));
|
||||
scheduleFromJSON(program.schedule, json.schedule);
|
||||
}
|
||||
|
||||
|
@ -161,7 +161,7 @@ class MqttSprinklersDevice extends SprinklersDevice {
|
||||
const secNum = Number(secStr);
|
||||
let section = this.sections[secNum];
|
||||
if (!section) {
|
||||
this.sections[secNum] = section = new MqttSection(this);
|
||||
this.sections[secNum] = section = new MqttSection(this, secNum);
|
||||
}
|
||||
(section as MqttSection).onMessage(subTopic, payload);
|
||||
}
|
||||
@ -179,7 +179,7 @@ class MqttSprinklersDevice extends SprinklersDevice {
|
||||
const progNum = Number(progStr);
|
||||
let program = this.programs[progNum];
|
||||
if (!program) {
|
||||
this.programs[progNum] = program = new MqttProgram(this);
|
||||
this.programs[progNum] = program = new MqttProgram(this, progNum);
|
||||
}
|
||||
(program as MqttProgram).onMessage(subTopic, payload);
|
||||
}
|
||||
@ -361,8 +361,7 @@ export interface ISectionRunJSON {
|
||||
}
|
||||
|
||||
function sectionRunFromJSON(json: ISectionRunJSON) {
|
||||
const run = new SectionRun();
|
||||
run.id = json.id;
|
||||
const run = new SectionRun(json.id);
|
||||
run.section = json.section;
|
||||
run.duration = Duration.fromSeconds(json.duration);
|
||||
run.startTime = json.startTime == null ? null : new Date(json.startTime);
|
||||
@ -383,11 +382,11 @@ class MqttSectionRunner extends SectionRunner {
|
||||
}
|
||||
|
||||
updateFromJSON(json: ISectionRunnerJSON) {
|
||||
if (!json.queue || !json.queue.length) { // null means empty queue
|
||||
this.queue.clear();
|
||||
} else {
|
||||
this.queue.replace(json.queue.map(sectionRunFromJSON));
|
||||
this.queue.length = 0;
|
||||
if (json.queue && json.queue.length) { // null means empty queue
|
||||
this.queue.push.apply(this.queue, json.queue.map(sectionRunFromJSON));
|
||||
}
|
||||
this.current = json.current == null ? null : sectionRunFromJSON(json.current);
|
||||
this.paused = json.paused;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { observable } from "mobx";
|
||||
|
||||
export class TimeOfDay {
|
||||
hour: number;
|
||||
minute: number;
|
||||
second: number;
|
||||
millisecond: number;
|
||||
readonly hour: number;
|
||||
readonly minute: number;
|
||||
readonly second: number;
|
||||
readonly millisecond: number;
|
||||
|
||||
constructor(hour: number, minute: number = 0, second: number = 0, millisecond: number = 0) {
|
||||
this.hour = hour;
|
||||
@ -21,8 +23,8 @@ export enum Weekday {
|
||||
}
|
||||
|
||||
export class Schedule {
|
||||
times: TimeOfDay[] = [];
|
||||
weekdays: Weekday[] = [];
|
||||
from: Date | null = null;
|
||||
to: Date | null = null;
|
||||
@observable times: TimeOfDay[] = [];
|
||||
@observable weekdays: Weekday[] = [];
|
||||
@observable from: Date | null = null;
|
||||
@observable to: Date | null = null;
|
||||
}
|
||||
|
@ -1,6 +1,26 @@
|
||||
{
|
||||
"experimentalDecorators": true,
|
||||
"compilerOptions": {
|
||||
"target": "es5"
|
||||
}
|
||||
"experimentalDecorators": true,
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"types": [
|
||||
"node"
|
||||
],
|
||||
"strict": true,
|
||||
"allowJs": true,
|
||||
"baseUrl": "..",
|
||||
"paths": {
|
||||
"@common/*": [
|
||||
"./common/*"
|
||||
],
|
||||
"@app/*": [
|
||||
"./app/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"./**/*.ts"
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user