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"; @@ -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 { @@ -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() {

15
common/sprinklers/Section.ts

@ -3,16 +3,15 @@ import { Duration } from "./Duration"; @@ -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 { @@ -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}}`;
}
}

15
common/sprinklers/SectionRunner.ts

@ -3,7 +3,7 @@ import { Duration } from "./Duration"; @@ -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 { @@ -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;

20
common/sprinklers/SprinklersDevice.ts

@ -5,28 +5,16 @@ import { Section } from "./Section"; @@ -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 {

11
common/sprinklers/json/index.ts

@ -66,9 +66,9 @@ export function programItemToJSON(programItem: s.ProgramItem): IProgramItemJSON @@ -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 { @@ -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);
}

15
common/sprinklers/mqtt/index.ts

@ -161,7 +161,7 @@ class MqttSprinklersDevice extends SprinklersDevice { @@ -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 { @@ -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 { @@ -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 { @@ -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;
}
}

18
common/sprinklers/schedule.ts

@ -1,8 +1,10 @@ @@ -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 { @@ -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;
}

26
common/tsconfig.json

@ -1,6 +1,26 @@ @@ -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…
Cancel
Save