Browse Source

Cleaned up mobx and ts stuff

update-deps
Alex Mikhalev 7 years ago
parent
commit
52ba25c038
  1. 28
      common/sprinklers/Program.ts
  2. 15
      common/sprinklers/Section.ts
  3. 15
      common/sprinklers/SectionRunner.ts
  4. 20
      common/sprinklers/SprinklersDevice.ts
  5. 11
      common/sprinklers/json/index.ts
  6. 15
      common/sprinklers/mqtt/index.ts
  7. 18
      common/sprinklers/schedule.ts
  8. 26
      common/tsconfig.json

28
common/sprinklers/Program.ts

@ -5,9 +5,9 @@ import { SprinklersDevice } from "./SprinklersDevice";
export class ProgramItem { export class ProgramItem {
// the section number // the section number
section: number; readonly section: number;
// duration of the run // duration of the run
duration: Duration; readonly duration: Duration;
constructor(section: number, duration: Duration) { constructor(section: number, duration: Duration) {
this.section = section; this.section = section;
@ -16,24 +16,18 @@ export class ProgramItem {
} }
export class Program { export class Program {
device: SprinklersDevice; readonly device: SprinklersDevice;
readonly id: number;
@observable @observable name: string = "";
name: string = ""; @observable enabled: boolean = false;
@observable @observable schedule: Schedule = new Schedule();
enabled: boolean = false; @observable.shallow sequence: ProgramItem[] = [];
@observable running: boolean = false;
@observable constructor(device: SprinklersDevice, id: number) {
schedule: Schedule = new Schedule();
@observable
sequence: ProgramItem[] = [];
@observable
running: boolean = false;
constructor(device: SprinklersDevice) {
this.device = device; this.device = device;
this.id = id;
} }
run() { run() {

15
common/sprinklers/Section.ts

@ -3,16 +3,15 @@ import { Duration } from "./Duration";
import { SprinklersDevice } from "./SprinklersDevice"; import { SprinklersDevice } from "./SprinklersDevice";
export class Section { export class Section {
device: SprinklersDevice; readonly device: SprinklersDevice;
readonly id: number;
@observable @observable name: string = "";
name: string = ""; @observable state: boolean = false;
@observable constructor(device: SprinklersDevice, id: number) {
state: boolean = false;
constructor(device: SprinklersDevice) {
this.device = device; this.device = device;
this.id = id;
} }
run(duration: Duration) { run(duration: Duration) {
@ -20,6 +19,6 @@ export class Section {
} }
toString(): string { toString(): string {
return `Section{name="${this.name}", state=${this.state}}`; return `Section{id=${this.id}, name="${this.name}", state=${this.state}}`;
} }
} }

15
common/sprinklers/SectionRunner.ts

@ -3,7 +3,7 @@ import { Duration } from "./Duration";
import { SprinklersDevice } from "./SprinklersDevice"; import { SprinklersDevice } from "./SprinklersDevice";
export class SectionRun { export class SectionRun {
id: number; readonly id: number;
section: number; section: number;
duration: Duration; duration: Duration;
startTime: Date | null; startTime: Date | null;
@ -24,16 +24,11 @@ export class SectionRun {
} }
export class SectionRunner { export class SectionRunner {
device: SprinklersDevice; readonly device: SprinklersDevice;
@observable @observable queue: SectionRun[] = [];
queue: IObservableArray<SectionRun> = observable([]); @observable current: SectionRun | null = null;
@observable paused: boolean = false;
@observable
current: SectionRun | null = null;
@observable
paused: boolean = false;
constructor(device: SprinklersDevice) { constructor(device: SprinklersDevice) {
this.device = device; this.device = device;

20
common/sprinklers/SprinklersDevice.ts

@ -5,28 +5,16 @@ import { Section } from "./Section";
import { SectionRunner } from "./SectionRunner"; import { SectionRunner } from "./SectionRunner";
export abstract class SprinklersDevice { export abstract class SprinklersDevice {
@observable @observable connected: boolean = false;
connected: boolean = false; @observable sections: Section[] = [];
@observable programs: Program[] = [];
@observable @observable sectionRunner: SectionRunner;
sections: IObservableArray<Section> = observable.array<Section>();
@observable
programs: IObservableArray<Program> = observable.array<Program>();
@observable
sectionRunner: SectionRunner;
abstract get id(): string; abstract get id(): string;
abstract runSection(section: number | Section, duration: Duration): Promise<{}>; abstract runSection(section: number | Section, duration: Duration): Promise<{}>;
abstract runProgram(program: number | Program): Promise<{}>; abstract runProgram(program: number | Program): Promise<{}>;
abstract cancelSectionRunById(id: number): Promise<{}>; abstract cancelSectionRunById(id: number): Promise<{}>;
abstract pauseSectionRunner(): Promise<{}>; abstract pauseSectionRunner(): Promise<{}>;
abstract unpauseSectionRunner(): Promise<{}>; abstract unpauseSectionRunner(): Promise<{}>;
toString(): string { toString(): string {

11
common/sprinklers/json/index.ts

@ -66,9 +66,9 @@ export function programItemToJSON(programItem: s.ProgramItem): IProgramItemJSON
}; };
} }
export function programItemFromJSON(programItem: s.ProgramItem, json: IProgramItemJSON) { export function programItemFromJSON(json: IProgramItemJSON): s.ProgramItem {
assign(programItem, pick(json, programItemProps)); const duration = s.Duration.fromSeconds(json.duration);
programItem.duration = s.Duration.fromSeconds(json.duration); return new s.ProgramItem(json.section, duration);
} }
export interface IProgramJSON { export interface IProgramJSON {
@ -90,9 +90,8 @@ export function programToJSON(program: s.Program): IProgramJSON {
export function programFromJSON(program: s.Program, json: IProgramJSON) { export function programFromJSON(program: s.Program, json: IProgramJSON) {
assign(program, pick(json, programProps)); assign(program, pick(json, programProps));
program.sequence.length = json.sequence.length; program.sequence = json.sequence.map((programItemJson) =>
program.sequence.forEach((programItem, i) => programItemFromJSON(programItemJson));
programItemFromJSON(programItem, json.sequence[i]));
scheduleFromJSON(program.schedule, json.schedule); scheduleFromJSON(program.schedule, json.schedule);
} }

15
common/sprinklers/mqtt/index.ts

@ -161,7 +161,7 @@ class MqttSprinklersDevice extends SprinklersDevice {
const secNum = Number(secStr); const secNum = Number(secStr);
let section = this.sections[secNum]; let section = this.sections[secNum];
if (!section) { if (!section) {
this.sections[secNum] = section = new MqttSection(this); this.sections[secNum] = section = new MqttSection(this, secNum);
} }
(section as MqttSection).onMessage(subTopic, payload); (section as MqttSection).onMessage(subTopic, payload);
} }
@ -179,7 +179,7 @@ class MqttSprinklersDevice extends SprinklersDevice {
const progNum = Number(progStr); const progNum = Number(progStr);
let program = this.programs[progNum]; let program = this.programs[progNum];
if (!program) { if (!program) {
this.programs[progNum] = program = new MqttProgram(this); this.programs[progNum] = program = new MqttProgram(this, progNum);
} }
(program as MqttProgram).onMessage(subTopic, payload); (program as MqttProgram).onMessage(subTopic, payload);
} }
@ -361,8 +361,7 @@ export interface ISectionRunJSON {
} }
function sectionRunFromJSON(json: ISectionRunJSON) { function sectionRunFromJSON(json: ISectionRunJSON) {
const run = new SectionRun(); const run = new SectionRun(json.id);
run.id = json.id;
run.section = json.section; run.section = json.section;
run.duration = Duration.fromSeconds(json.duration); run.duration = Duration.fromSeconds(json.duration);
run.startTime = json.startTime == null ? null : new Date(json.startTime); run.startTime = json.startTime == null ? null : new Date(json.startTime);
@ -383,11 +382,11 @@ class MqttSectionRunner extends SectionRunner {
} }
updateFromJSON(json: ISectionRunnerJSON) { updateFromJSON(json: ISectionRunnerJSON) {
if (!json.queue || !json.queue.length) { // null means empty queue this.queue.length = 0;
this.queue.clear(); if (json.queue && json.queue.length) { // null means empty queue
} else { this.queue.push.apply(this.queue, json.queue.map(sectionRunFromJSON));
this.queue.replace(json.queue.map(sectionRunFromJSON));
} }
this.current = json.current == null ? null : sectionRunFromJSON(json.current); this.current = json.current == null ? null : sectionRunFromJSON(json.current);
this.paused = json.paused;
} }
} }

18
common/sprinklers/schedule.ts

@ -1,8 +1,10 @@
import { observable } from "mobx";
export class TimeOfDay { export class TimeOfDay {
hour: number; readonly hour: number;
minute: number; readonly minute: number;
second: number; readonly second: number;
millisecond: number; readonly millisecond: number;
constructor(hour: number, minute: number = 0, second: number = 0, millisecond: number = 0) { constructor(hour: number, minute: number = 0, second: number = 0, millisecond: number = 0) {
this.hour = hour; this.hour = hour;
@ -21,8 +23,8 @@ export enum Weekday {
} }
export class Schedule { export class Schedule {
times: TimeOfDay[] = []; @observable times: TimeOfDay[] = [];
weekdays: Weekday[] = []; @observable weekdays: Weekday[] = [];
from: Date | null = null; @observable from: Date | null = null;
to: Date | null = null; @observable to: Date | null = null;
} }

26
common/tsconfig.json

@ -1,6 +1,26 @@
{ {
"experimentalDecorators": true,
"compilerOptions": { "compilerOptions": {
"target": "es5" "experimentalDecorators": true,
} "target": "es5",
"lib": [
"es6"
],
"types": [
"node"
],
"strict": true,
"allowJs": true,
"baseUrl": "..",
"paths": {
"@common/*": [
"./common/*"
],
"@app/*": [
"./app/*"
]
}
},
"include": [
"./**/*.ts"
]
} }
Loading…
Cancel
Save