From 52ba25c03872bacbb28c829cbedcc7368376e73a Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Thu, 5 Oct 2017 09:07:07 -0600 Subject: [PATCH] Cleaned up mobx and ts stuff --- common/sprinklers/Program.ts | 28 +++++++++++---------------- common/sprinklers/Section.ts | 15 +++++++------- common/sprinklers/SectionRunner.ts | 15 +++++--------- common/sprinklers/SprinklersDevice.ts | 20 ++++--------------- common/sprinklers/json/index.ts | 11 +++++------ common/sprinklers/mqtt/index.ts | 15 +++++++------- common/sprinklers/schedule.ts | 18 +++++++++-------- common/tsconfig.json | 26 ++++++++++++++++++++++--- 8 files changed, 72 insertions(+), 76 deletions(-) diff --git a/common/sprinklers/Program.ts b/common/sprinklers/Program.ts index c822562..cc61ea9 100644 --- a/common/sprinklers/Program.ts +++ b/common/sprinklers/Program.ts @@ -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() { diff --git a/common/sprinklers/Section.ts b/common/sprinklers/Section.ts index 854ebad..d7969ff 100644 --- a/common/sprinklers/Section.ts +++ b/common/sprinklers/Section.ts @@ -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}}`; } } diff --git a/common/sprinklers/SectionRunner.ts b/common/sprinklers/SectionRunner.ts index e79d30b..3aa8099 100644 --- a/common/sprinklers/SectionRunner.ts +++ b/common/sprinklers/SectionRunner.ts @@ -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 = 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; diff --git a/common/sprinklers/SprinklersDevice.ts b/common/sprinklers/SprinklersDevice.ts index e159c9c..6d82a15 100644 --- a/common/sprinklers/SprinklersDevice.ts +++ b/common/sprinklers/SprinklersDevice.ts @@ -5,28 +5,16 @@ import { Section } from "./Section"; import { SectionRunner } from "./SectionRunner"; export abstract class SprinklersDevice { - @observable - connected: boolean = false; - - @observable - sections: IObservableArray
= observable.array
(); - - @observable - programs: IObservableArray = observable.array(); - - @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 { diff --git a/common/sprinklers/json/index.ts b/common/sprinklers/json/index.ts index 8112524..cdc05d5 100644 --- a/common/sprinklers/json/index.ts +++ b/common/sprinklers/json/index.ts @@ -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); } diff --git a/common/sprinklers/mqtt/index.ts b/common/sprinklers/mqtt/index.ts index 91e01e8..d08eeb9 100644 --- a/common/sprinklers/mqtt/index.ts +++ b/common/sprinklers/mqtt/index.ts @@ -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; } } diff --git a/common/sprinklers/schedule.ts b/common/sprinklers/schedule.ts index b13973b..39e5583 100644 --- a/common/sprinklers/schedule.ts +++ b/common/sprinklers/schedule.ts @@ -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; } diff --git a/common/tsconfig.json b/common/tsconfig.json index 97737e9..774f8f5 100644 --- a/common/tsconfig.json +++ b/common/tsconfig.json @@ -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" + ] } \ No newline at end of file