From 5b16015e3bf36de70da360a54291ecc21ffe8b81 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Wed, 3 May 2017 23:00:10 -0600 Subject: [PATCH] More work on programs and other stuff --- app/script/App.tsx | 29 +++++++++++++++++++++++++++-- app/script/mqtt.ts | 38 ++++++++++++++++++++++++++++++++++++-- app/script/sprinklers.ts | 5 ++++- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/app/script/App.tsx b/app/script/App.tsx index 6d61a88..68653cd 100644 --- a/app/script/App.tsx +++ b/app/script/App.tsx @@ -1,6 +1,6 @@ import * as React from "react"; import { observer } from "mobx-react"; -import { SprinklersDevice, Section } from "./sprinklers"; +import { SprinklersDevice, Section, Program } from "./sprinklers"; import { Item, Table, Header } from "semantic-ui-react"; import FontAwesome = require("react-fontawesome"); import * as classNames from "classnames"; @@ -22,10 +22,23 @@ class SectionRow extends React.PureComponent<{ section: Section }, void> { } } +@observer +class ProgramRow extends React.PureComponent<{ program: Program }, void> { + render() { + const { name, running } = this.props.program; + return ( + + Program {name} + running: {running + ""} + + ); + } +} + @observer class DeviceView extends React.PureComponent<{ device: SprinklersDevice }, void> { render() { - const { id, connected, sections } = this.props.device; //src={require("app/images/raspberry_pi.png")} + const { id, connected, sections, programs } = this.props.device; //src={require("app/images/raspberry_pi.png")} return ( @@ -57,6 +70,18 @@ class DeviceView extends React.PureComponent<{ device: SprinklersDevice }, void> } + + + + Programs + + + + { + programs.map((p, i) => ) + } + +
); diff --git a/app/script/mqtt.ts b/app/script/mqtt.ts index ff7fa75..69044c8 100644 --- a/app/script/mqtt.ts +++ b/app/script/mqtt.ts @@ -3,7 +3,7 @@ import "paho-mqtt/mqttws31"; import MQTT = Paho.MQTT; import { EventEmitter } from "events"; -import { SprinklersDevice, SprinklersApi, Section } from "./sprinklers"; +import { SprinklersDevice, SprinklersApi, Section, Program } from "./sprinklers"; export class MqttApiClient extends EventEmitter implements SprinklersApi { @@ -93,7 +93,9 @@ class MqttSprinklersDevice extends SprinklersDevice { return [ `${this.prefix}/connected`, `${this.prefix}/sections`, - `${this.prefix}/sections/+/#` + `${this.prefix}/sections/+/#`, + `${this.prefix}/programs`, + `${this.prefix}/programs/+/#` ]; } @@ -133,6 +135,19 @@ class MqttSprinklersDevice extends SprinklersDevice { } (section as MqttSection).onMessage(subTopic, payload); } + } else if ((matches = topic.match(/^programs(?:\/(\d+)(?:\/?(.+))?)?$/)) != null) { + const [topic, progStr, subTopic] = matches; + // console.log(`program: ${progStr}, topic: ${subTopic}, payload: ${payload}`); + if (!progStr) { // new number of programs + this.programs = new Array(Number(payload)); + } else { + const progNum = Number(progStr); + var program = this.programs[progNum]; + if (!program) { + this.programs[progNum] = program = new MqttProgram(); + } + (program as MqttProgram).onMessage(subTopic, payload); + } } else { console.warn(`MqttSprinklersDevice recieved invalid topic: ${topic}`) } @@ -157,4 +172,23 @@ class MqttSection extends Section { this.name = json.name; } } +} + +interface ProgramJSON { + name: string; + enabled: boolean; + // sequence: Array; + // sched: Schedule; +} + +class MqttProgram extends Program { + onMessage(topic: string, payload: string) { + if (topic == "running") { + this.running = (payload == "true"); + } else if (topic == null) { + const json = JSON.parse(payload) as Partial; + this.name = json.name; + this.enabled = json.enabled; + } + } } \ No newline at end of file diff --git a/app/script/sprinklers.ts b/app/script/sprinklers.ts index dfe83be..c5c7692 100644 --- a/app/script/sprinklers.ts +++ b/app/script/sprinklers.ts @@ -33,7 +33,7 @@ class ProgramItem { duration: number = 0; } -class Program { +export class Program { @observable name: string = "" @observable @@ -44,6 +44,9 @@ class Program { @observable sequence: Array = []; + + @observable + running: boolean = false; } export abstract class SprinklersDevice {