sprinklers3/app/components/DeviceView.tsx

89 lines
3.3 KiB
TypeScript
Raw Normal View History

import * as classNames from "classnames";
2017-08-29 22:42:56 -06:00
import { observer } from "mobx-react";
import * as React from "react";
2018-06-29 18:16:06 -06:00
import { Grid, Header, Icon, Item, SemanticICONS } from "semantic-ui-react";
2017-10-14 01:51:22 -06:00
import { injectState, StateBase } from "@app/state";
2018-06-30 14:49:13 -06:00
import { ConnectionState as ConState } from "@common/sprinklers";
2017-09-10 17:06:11 -06:00
import { ProgramTable, RunSectionForm, SectionRunnerView, SectionTable } from ".";
2017-10-31 17:23:27 -06:00
import "./DeviceView.scss";
2018-06-17 01:04:30 -06:00
const ConnectionState = observer(({ connectionState, className }:
{ connectionState: ConState, className?: string }) => {
2018-06-16 23:54:03 -06:00
const connected = connectionState.isConnected;
2017-08-29 23:21:36 -06:00
const classes = classNames({
2017-10-31 17:23:27 -06:00
connectionState: true,
2018-06-29 18:16:06 -06:00
connected: connected === true,
disconnected: connected === false,
unknown: connected === null,
2017-10-13 16:11:37 -06:00
}, className);
2018-06-16 23:54:03 -06:00
let connectionText: string;
2018-06-29 18:16:06 -06:00
let iconName: SemanticICONS = "unlinkify";
2018-06-16 23:54:03 -06:00
if (connected) {
connectionText = "Connected";
2018-06-29 18:16:06 -06:00
iconName = "linkify";
} else if (connected === null) {
connectionText = "Unknown";
iconName = "question";
} else if (connectionState.noPermission) {
connectionText = "No permission for this device";
iconName = "ban";
2018-06-16 23:54:03 -06:00
} else if (connectionState.serverToBroker) {
connectionText = "Device Disconnected";
} else if (connectionState.clientToServer) {
connectionText = "Broker Disconnected";
} else {
connectionText = "Server Disconnected";
}
2017-08-29 23:21:36 -06:00
return (
2017-10-13 16:11:37 -06:00
<div className={classes}>
2018-06-29 18:16:06 -06:00
<Icon name={iconName}/>&nbsp;
2018-06-16 23:54:03 -06:00
{connectionText}
2017-10-13 16:11:37 -06:00
</div>
2017-08-29 23:21:36 -06:00
);
2018-06-17 01:04:30 -06:00
});
2017-09-10 17:06:11 -06:00
interface DeviceViewProps {
deviceId: string;
2017-10-14 01:51:22 -06:00
state: StateBase;
2017-09-10 17:06:11 -06:00
}
class DeviceView extends React.Component<DeviceViewProps> {
render() {
2018-06-29 21:30:55 -06:00
const { uiStore, sprinklersApi } = this.props.state;
const device = sprinklersApi.getDevice(this.props.deviceId);
const { id, connectionState, sections, programs, sectionRunner } = device;
const deviceBody = connectionState.isAvailable && (
<React.Fragment>
<SectionRunnerView sectionRunner={sectionRunner} sections={sections}/>
<Grid>
<Grid.Column mobile="16" tablet="16" computer="8">
<SectionTable sections={sections}/>
</Grid.Column>
<Grid.Column mobile="16" tablet="16" computer="8">
<RunSectionForm device={device} uiStore={uiStore}/>
</Grid.Column>
</Grid>
<ProgramTable programs={programs} sections={sections}/>
</React.Fragment>
);
return (
<Item>
2018-06-16 23:54:03 -06:00
<Item.Image src={require("@app/images/raspberry_pi.png")}/>
2017-10-31 17:23:27 -06:00
<Item.Content className="device">
<Header as="h1">
2017-10-13 16:11:37 -06:00
<div>Device <kbd>{id}</kbd></div>
2018-06-16 23:54:03 -06:00
<ConnectionState connectionState={connectionState}/>
</Header>
<Item.Meta>
2017-10-31 17:23:27 -06:00
Raspberry Pi Grinklers Device
</Item.Meta>
2018-06-29 21:30:55 -06:00
{deviceBody}
</Item.Content>
</Item>
);
}
}
2017-09-10 12:30:23 -06:00
2017-09-10 17:06:11 -06:00
export default injectState(observer(DeviceView));