frontend/smart-hut/src/components/dashboard/devices/Switch.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-03-17 16:38:03 +00:00
/**
* Users can add on-off switches. A on-off switch can turn on (or off) lights.
* If a light has an intensity level, when it gets switched back on, it gets the last available
* intensity level that was set by the user (or 100% if no such level exists).
* The user can change the state of a switch through the SmartHut interface.
*/
2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import { Image } from 'semantic-ui-react';
import { connect } from 'react-redux';
import { BottomPanel, StyledDiv } from './styleComponents';
import { imageStyle, nameStyle, turnedOnStyle } from './SwitchStyle';
import { RemoteService } from '../../../remote';
import mapStateToProps from '../../../deviceProps';
2020-03-17 16:38:03 +00:00
2020-04-10 15:25:52 +00:00
class Switch extends Component {
2020-03-23 20:24:17 +00:00
constructor(props) {
2020-03-17 16:38:03 +00:00
super(props);
2020-05-12 13:18:33 +00:00
this.iconOn = '/img/switchOn.svg';
this.iconOff = '/img/switchOff.svg';
2020-03-17 16:38:03 +00:00
}
get turnedOn() {
return this.props.device.on;
}
2020-05-12 13:18:33 +00:00
getIcon = () => (this.turnedOn ? this.iconOn : this.iconOff);
2020-03-17 16:38:03 +00:00
onClickDevice = () => {
const newOn = !this.turnedOn;
2020-05-12 13:18:33 +00:00
const type = newOn ? 'ON' : 'OFF';
2020-04-12 15:49:29 +00:00
this.props
.switchOperate(this.props.id, type)
2020-05-12 13:18:33 +00:00
.catch((err) => console.error('switch operate failed', err));
2020-03-17 16:38:03 +00:00
};
2020-03-23 20:24:17 +00:00
render() {
2020-03-17 16:38:03 +00:00
return (
2020-05-04 09:13:19 +00:00
<StyledDiv onClick={this.props.disabled ? () => {} : this.onClickDevice}>
2020-03-23 20:24:17 +00:00
<Image src={this.getIcon()} style={imageStyle} />
2020-04-12 15:49:29 +00:00
<span style={nameStyle}>Switch</span>
2020-03-25 16:20:53 +00:00
<BottomPanel
style={
this.turnedOn
2020-05-12 13:18:33 +00:00
? { backgroundColor: '#505bda' }
: { backgroundColor: '#1a2849' }
2020-03-25 16:20:53 +00:00
}
>
2020-05-12 13:18:33 +00:00
<span style={turnedOnStyle}>{this.turnedOn ? 'ON' : 'OFF'}</span>
</BottomPanel>
2020-03-17 16:38:03 +00:00
</StyledDiv>
2020-03-25 16:20:53 +00:00
);
2020-03-17 16:38:03 +00:00
}
}
2020-04-10 15:25:52 +00:00
const SwitchContainer = connect(mapStateToProps, RemoteService)(Switch);
export default SwitchContainer;