Do not use internal and actual values anymore (Fixes #42)
This commit is contained in:
parent
b40f4a774e
commit
c0117fa7d6
13 changed files with 421 additions and 393 deletions
|
|
@ -18,9 +18,6 @@ import ControlMap from "components/ControlMap";
|
|||
import TopBar from "components/TopBar";
|
||||
import UiItemList from "components/UiItemList";
|
||||
|
||||
import keyOf from "utils/keyOf";
|
||||
import { toRawIcon } from "config/icon";
|
||||
|
||||
import connectMqtt from "../connectMqtt";
|
||||
|
||||
export type AppProps = {
|
||||
|
|
@ -31,7 +28,7 @@ export type AppState = {
|
|||
selectedControl: ?Control,
|
||||
drawerOpened: boolean,
|
||||
mqttState: State,
|
||||
mqttSend: (topic: string, value: Actual) => void,
|
||||
mqttSend: (topic: string, value: Buffer) => void,
|
||||
mqttConnected: boolean,
|
||||
};
|
||||
|
||||
|
|
@ -41,16 +38,15 @@ class App extends React.PureComponent<AppProps & Classes, AppState> {
|
|||
this.state = {
|
||||
selectedControl: null,
|
||||
drawerOpened: false,
|
||||
mqttState: mapValues(this.topics, (topic) => ({
|
||||
actual: topic.defaultValue,
|
||||
internal: keyOf(topic.values, topic.defaultValue)
|
||||
})),
|
||||
mqttState: mapValues(this.topics, (topic) => topic.defaultValue),
|
||||
mqttSend: connectMqtt(props.config.space.mqtt, {
|
||||
onMessage: this.receiveMessage.bind(this),
|
||||
onConnect: () => this.setState({ mqttConnected: true }),
|
||||
onReconnect: () => this.setState({ mqttConnected: false }),
|
||||
onDisconnect: () => this.setState({ mqttConnected: false }),
|
||||
subscribe: map(this.topics, (x) => x.state)
|
||||
subscribe: map(
|
||||
filter(keys(this.topics), (x) => this.topics[x].state != null),
|
||||
(x) => this.topics[x].state.name)
|
||||
}),
|
||||
mqttConnected: false
|
||||
};
|
||||
|
|
@ -77,23 +73,23 @@ class App extends React.PureComponent<AppProps & Classes, AppState> {
|
|||
});
|
||||
}
|
||||
|
||||
receiveMessage(rawTopic: string, message: Object) {
|
||||
receiveMessage(rawTopic: string, message: Buffer) {
|
||||
const topics = filter(
|
||||
keys(this.topics),
|
||||
(k) => this.topics[k].state === rawTopic
|
||||
(k) => this.topics[k].state != null &&
|
||||
this.topics[k].state.name === rawTopic
|
||||
);
|
||||
if (topics.length === 0) {
|
||||
return;
|
||||
}
|
||||
for (let i in topics) {
|
||||
// TODO: Remove FlowFixMe
|
||||
const topic = topics[i];
|
||||
const parseValue = this.topics[topic].type;
|
||||
// $FlowFixMe
|
||||
const parseValue = this.topics[topic].state.type;
|
||||
const val = parseValue == null ? message.toString() : parseValue(message);
|
||||
this.setState({mqttState: Object.assign({}, merge(this.state.mqttState,
|
||||
{ [topic]: {
|
||||
actual: val,
|
||||
internal: keyOf(this.topics[topic].values, val) || val
|
||||
}}))});
|
||||
{ [topic]: val}))});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -105,15 +101,15 @@ class App extends React.PureComponent<AppProps & Classes, AppState> {
|
|||
this.setState({drawerOpened: false});
|
||||
}
|
||||
|
||||
changeState(topic: string, value: Actual) {
|
||||
const rawTopic = this.topics[topic].command;
|
||||
if (rawTopic == null) {
|
||||
changeState(topic: string, value: string) {
|
||||
if (this.topics[topic].command == null) {
|
||||
return;
|
||||
}
|
||||
this.state.mqttSend(
|
||||
rawTopic,
|
||||
String(this.topics[topic].values[value] || value)
|
||||
);
|
||||
const rawTopic = this.topics[topic].command.name;
|
||||
const transformValue = this.topics[topic].command.type;
|
||||
const val =
|
||||
transformValue == null ? value : transformValue(Buffer.from(value));
|
||||
this.state.mqttSend(rawTopic, Buffer.from(val));
|
||||
}
|
||||
|
||||
render() {
|
||||
|
|
@ -127,8 +123,7 @@ class App extends React.PureComponent<AppProps & Classes, AppState> {
|
|||
control={this.state.selectedControl}
|
||||
onCloseRequest={this.closeDrawer.bind(this)}
|
||||
icon={this.state.selectedControl == null ? null :
|
||||
toRawIcon(this.state.selectedControl.icon,
|
||||
this.state.mqttState)}
|
||||
this.state.selectedControl.icon(this.state.mqttState)}
|
||||
>
|
||||
{this.state.selectedControl == null
|
||||
|| <UiItemList state={this.state.mqttState}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue