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

434 lines
11 KiB
JavaScript
Raw Normal View History

2020-05-12 13:18:33 +00:00
import React, { Component } from 'react';
import styled from 'styled-components';
2020-03-23 20:24:17 +00:00
import {
Button,
Dropdown,
Form,
Icon,
Image,
Input,
Modal,
2020-05-12 13:18:33 +00:00
} from 'semantic-ui-react';
import { connect } from 'react-redux';
import { RemoteService } from '../../../remote';
const StyledDiv = styled.div`
2020-03-25 16:20:53 +00:00
background-color: #505bda;
padding: 3rem;
width: 10rem;
height: 10rem;
2020-03-23 20:24:17 +00:00
border-radius: 100%;
border: none;
position: relative;
box-shadow: 3px 2px 10px 5px #ccc;
2020-03-25 16:20:53 +00:00
transition: all 0.3s ease-out;
:hover {
background-color: #4345d9;
}
2020-03-25 16:20:53 +00:00
:active {
transform: translate(0.3px, 0.8px);
box-shadow: 0.5px 0.5px 7px 3.5px #ccc;
}
`;
2020-04-10 21:13:23 +00:00
class NewDevice extends Component {
2020-03-18 13:18:37 +00:00
constructor(props) {
super(props);
this.state = {
step: 1,
2020-03-23 20:24:17 +00:00
openModal: false,
2020-03-24 19:35:23 +00:00
motion: false,
2020-05-12 13:18:33 +00:00
deviceName: '',
};
2020-03-23 20:24:17 +00:00
this.baseState = this.state;
2020-03-19 10:52:13 +00:00
this.createDevice = this.createDevice.bind(this);
2020-03-18 13:18:37 +00:00
}
2020-03-23 20:24:17 +00:00
handleOpen = () => {
this.setState({ openModal: true });
};
2020-05-12 13:18:33 +00:00
2020-03-23 20:24:17 +00:00
handleClose = () => {
this.setState({ openModal: false });
};
2020-03-18 13:18:37 +00:00
resetState = () => {
this.setState(this.baseState);
this.handleClose();
};
2020-03-18 13:18:37 +00:00
nextStep = () => {
2020-03-23 20:24:17 +00:00
this.setState((prevState) => ({ step: prevState.step + 1 }));
2020-03-18 13:18:37 +00:00
};
2020-05-12 13:18:33 +00:00
2020-03-18 13:18:37 +00:00
previousStep = () => {
2020-03-23 20:24:17 +00:00
this.setState((prevState) => ({ step: prevState.step - 1 }));
2020-03-18 13:18:37 +00:00
};
2020-03-18 13:18:37 +00:00
setTypeOfDevice = (e, d) => {
2020-05-12 13:18:33 +00:00
if (d.value === 'dimmableLight') {
2020-03-23 20:24:17 +00:00
this.setState({ typeOfDevice: d.value, intensity: 0 });
} else {
this.setState({ typeOfDevice: d.value });
}
2020-03-18 13:18:37 +00:00
};
2020-03-18 13:18:37 +00:00
setDeviceName = (e, d) => {
2020-03-23 20:24:17 +00:00
this.setState({ deviceName: d.value });
2020-03-18 13:18:37 +00:00
};
2020-03-18 13:18:37 +00:00
setTypeOfSensor = (e, d) => {
2020-03-24 19:35:23 +00:00
console.log(d.value);
2020-05-12 13:18:33 +00:00
if (d.value === 'motionSensor') {
2020-03-24 19:35:23 +00:00
this.setState({ typeOfSensor: d.value, motion: true });
} else {
this.setState({ typeOfSensor: d.value });
}
2020-03-18 13:18:37 +00:00
};
setLightsDimmerSwitch = (e, d) => {
2020-03-23 20:24:17 +00:00
this.setState({ lightsAttached: d.value });
2020-03-18 13:18:37 +00:00
};
2020-04-11 16:29:32 +00:00
async createDevice() {
2020-03-18 13:18:37 +00:00
// Connect to the backend and create device here.
2020-03-19 10:52:13 +00:00
const data = {
2020-04-11 16:29:32 +00:00
id: null,
roomId: this.props.activeRoom,
name: this.state.deviceName,
2020-05-12 13:18:33 +00:00
kind: this.state.motion ? 'motionSensor' : this.state.typeOfDevice,
2020-04-11 16:29:32 +00:00
};
let outputs = null;
const defaultNames = {
2020-05-12 13:18:33 +00:00
regularLight: 'New regular light',
dimmableLight: 'New intensity light',
smartPlug: 'New smart Plug',
sensor: 'New sensor',
switch: 'New switch',
buttonDimmer: 'New button dimmer',
knobDimmer: 'New knob dimmer',
securityCamera: 'New security camera',
thermostat: 'New thermostat',
curtains: 'New curtains',
2020-03-23 20:24:17 +00:00
};
2020-03-20 17:42:38 +00:00
2020-05-12 13:18:33 +00:00
if (this.state.deviceName === '') {
2020-04-11 16:29:32 +00:00
data.name = defaultNames[this.state.typeOfDevice];
}
2020-05-12 13:18:33 +00:00
console.log('-------------------------');
2020-04-23 13:34:23 +00:00
console.log(this.state.typeOfDevice);
2020-04-11 16:29:32 +00:00
2020-03-23 20:24:17 +00:00
switch (this.state.typeOfDevice) {
2020-05-12 13:18:33 +00:00
// trying to make securityCamera work
// case "securityCamera":
2020-04-28 08:48:07 +00:00
// data.path="/security_camera_videos/security_camera_1.mp4";
// data.on=false;
2020-05-12 13:18:33 +00:00
// break;
// trying to make thermostat work
case 'thermostat':
2020-04-28 08:48:07 +00:00
data.targetTemperature = 0;
data.measuredTemperature = 0;
2020-04-23 13:34:23 +00:00
break;
2020-05-12 13:18:33 +00:00
case 'dimmableLight':
2020-04-11 16:29:32 +00:00
data.intensity = 0;
2020-03-20 17:42:38 +00:00
break;
2020-05-12 13:18:33 +00:00
case 'sensor':
2020-03-24 19:35:23 +00:00
if (!this.state.motion) {
2020-04-11 16:29:32 +00:00
data.sensor = this.state.typeOfSensor;
data.value = 0;
2020-03-24 19:35:23 +00:00
}
2020-03-23 18:08:31 +00:00
break;
2020-05-12 13:18:33 +00:00
case 'switch':
case 'buttonDimmer':
case 'knobDimmer':
2020-04-11 16:29:32 +00:00
outputs = this.state.lightsAttached;
if (
2020-05-12 13:18:33 +00:00
this.state.lightsAttached === undefined
|| this.state.lightsAttached.length === 0
) {
alert(
2020-05-12 13:18:33 +00:00
'No lights attached to this switch! Please, add a light a first.',
);
return;
2020-05-06 13:51:26 +00:00
}
2020-03-25 18:58:19 +00:00
break;
2020-03-23 20:24:17 +00:00
default:
2020-03-20 17:42:38 +00:00
break;
}
2020-03-26 10:11:44 +00:00
2020-04-11 16:29:32 +00:00
try {
2020-05-12 13:18:33 +00:00
const newDevice = await this.props.saveDevice(data);
2020-04-11 16:29:32 +00:00
if (outputs) {
await this.props.connectOutputs(newDevice, outputs);
}
this.resetState();
} catch (e) {
2020-05-12 13:18:33 +00:00
console.error('device creation error: ', e);
2020-04-11 16:29:32 +00:00
}
2020-03-23 20:24:17 +00:00
}
2020-03-18 13:18:37 +00:00
render() {
const deviceOptions = [
2020-05-12 13:18:33 +00:00
// stuff
{
2020-05-12 13:18:33 +00:00
key: 'thermostat',
text: 'Thermostat',
value: 'thermostat',
image: { avatar: true, src: '/img/thermostat-icon.png' },
},
{
2020-05-12 13:18:33 +00:00
key: 'curtains',
text: 'Curtain',
value: 'curtains',
image: { avatar: true, src: '/img/curtains-icon.png' },
},
2020-05-12 13:18:33 +00:00
// stuff ends
2020-03-18 13:18:37 +00:00
{
2020-05-12 13:18:33 +00:00
key: 'light',
text: 'Normal Light',
value: 'regularLight',
image: { avatar: true, src: '/img/lightOn.svg' },
2020-03-18 13:18:37 +00:00
},
{
2020-05-12 13:18:33 +00:00
key: 'intensity-light',
text: 'Intensity Light',
value: 'dimmableLight',
image: { avatar: true, src: '/img/intensity-light.svg' },
2020-03-18 13:18:37 +00:00
},
{
2020-05-12 13:18:33 +00:00
key: 'smart-plug',
text: 'Smart Plug',
value: 'smartPlug',
image: { avatar: true, src: '/img/smart-plug.svg' },
2020-03-18 13:18:37 +00:00
},
{
2020-05-12 13:18:33 +00:00
key: 'sensor',
text: 'Sensor',
value: 'sensor',
image: { avatar: true, src: '/img/sensorOn.svg' },
2020-03-18 13:18:37 +00:00
},
{
2020-05-12 13:18:33 +00:00
key: 'switch',
text: 'Switch',
value: 'switch',
image: { avatar: true, src: '/img/switchOn.svg' },
2020-03-18 13:18:37 +00:00
},
2020-03-25 15:04:02 +00:00
{
2020-05-12 13:18:33 +00:00
key: 'knobDimmer',
text: 'Knob Dimmer',
value: 'knobDimmer',
image: { avatar: true, src: '/img/knob.svg' },
2020-03-25 15:04:02 +00:00
},
2020-03-18 13:18:37 +00:00
{
2020-05-12 13:18:33 +00:00
key: 'buttonDimmer',
text: 'Button Dimmer',
value: 'buttonDimmer',
image: { avatar: true, src: '/img/plusMinus.svg' },
2020-03-18 13:18:37 +00:00
},
2020-04-22 12:25:24 +00:00
{
2020-05-12 13:18:33 +00:00
key: 'securityCamera',
text: 'Security Camera',
value: 'securityCamera',
image: { avatar: true, src: '/img/security-icon.png' },
2020-04-28 08:48:07 +00:00
},
2020-03-18 13:18:37 +00:00
];
const sensorOptions = [
{
2020-05-12 13:18:33 +00:00
key: 'temperature',
text: 'Temperature Sensor',
value: 'TEMPERATURE',
image: { avatar: true, src: '/img/temperature-sensor.svg' },
2020-03-18 13:18:37 +00:00
},
{
2020-05-12 13:18:33 +00:00
key: 'humidity',
text: 'Humidity Sensor',
value: 'HUMIDITY',
image: { avatar: true, src: '/img/humidity-sensor.svg' },
2020-03-18 13:18:37 +00:00
},
{
2020-05-12 13:18:33 +00:00
key: 'light',
text: 'Light Sensor',
value: 'LIGHT',
image: { avatar: true, src: '/img/light-sensor.svg' },
2020-03-18 13:18:37 +00:00
},
{
2020-05-12 13:18:33 +00:00
key: 'motion',
text: 'Motion Sensor',
value: 'motionSensor',
image: { avatar: true, src: '/img/sensorOn.svg' },
2020-03-23 20:24:17 +00:00
},
2020-03-18 13:18:37 +00:00
];
2020-03-25 12:55:52 +00:00
const availableSwitchDevices = [];
const availableDimmerDevices = [];
2020-03-23 20:24:17 +00:00
this.props.devices.forEach((d) => {
2020-03-25 12:55:52 +00:00
if (
2020-05-12 13:18:33 +00:00
d.kind === 'regularLight'
|| d.kind === 'dimmableLight'
|| d.kind === 'smartPlug'
2020-03-25 12:55:52 +00:00
) {
availableSwitchDevices.push({
key: d.id,
text: d.name,
value: d.id,
});
}
2020-05-12 13:18:33 +00:00
if (d.kind === 'dimmableLight') {
2020-03-25 12:55:52 +00:00
availableDimmerDevices.push({
key: d.id,
text: d.name,
value: d.id,
});
}
2020-03-18 13:18:37 +00:00
});
const step1 = (
<Dropdown
name="typeOfDevice"
2020-03-23 20:24:17 +00:00
placeholder="Select a Type of Device"
2020-03-18 13:18:37 +00:00
fluid
selection
onChange={this.setTypeOfDevice}
options={deviceOptions}
/>
);
const step2 = (typeOfDevice) => {
2020-03-23 20:24:17 +00:00
const deviceName = (
<div>
<Form.Field>
<label>Device Name: </label>
<Input
fluid
2020-05-12 13:18:33 +00:00
size="large"
2020-03-23 20:24:17 +00:00
onChange={this.setDeviceName}
focus
placeholder="Device Name"
/>
</Form.Field>
</div>
);
const sensorForm = (
2020-05-12 13:18:33 +00:00
<Form.Field style={{ marginTop: '1rem' }}>
2020-03-18 13:18:37 +00:00
<label>Type of Sensor: </label>
<Dropdown
name="typeOfDevice"
2020-03-23 20:24:17 +00:00
placeholder="Select a Type of Sensor"
2020-03-18 13:18:37 +00:00
fluid
selection
onChange={this.setTypeOfSensor}
options={sensorOptions}
/>
2020-03-23 20:24:17 +00:00
</Form.Field>
);
2020-03-25 12:55:52 +00:00
const switchOptions = (
2020-05-12 13:18:33 +00:00
<Form.Field style={{ marginTop: '1rem' }}>
2020-03-25 21:15:52 +00:00
<label>Select the lights or smart plugs You Want to Attach: </label>
2020-03-25 12:55:52 +00:00
<Dropdown
name="typeOfDevice"
placeholder="Select Lights"
fluid
multiple
onChange={this.setLightsDimmerSwitch}
options={availableSwitchDevices}
/>
</Form.Field>
);
const dimmerOptions = (
2020-05-12 13:18:33 +00:00
<Form.Field style={{ marginTop: '1rem' }}>
2020-03-25 21:15:52 +00:00
<label>Select the dimmable lights You Want to Attach: </label>
2020-03-23 20:24:17 +00:00
<Dropdown
name="typeOfDevice"
placeholder="Select Lights"
fluid
multiple
onChange={this.setLightsDimmerSwitch}
2020-03-25 12:55:52 +00:00
options={availableDimmerDevices}
2020-03-23 20:24:17 +00:00
/>
</Form.Field>
);
2020-03-18 13:18:37 +00:00
return (
<Form>
{deviceName}
2020-05-12 13:18:33 +00:00
{this.state.typeOfDevice === 'sensor' ? sensorForm : ''}
{this.state.typeOfDevice === 'switch' ? switchOptions : ''}
{this.state.typeOfDevice === 'buttonDimmer'
|| this.state.typeOfDevice === 'knobDimmer'
2020-03-25 12:55:52 +00:00
? dimmerOptions
2020-05-12 13:18:33 +00:00
: ''}
2020-03-18 13:18:37 +00:00
</Form>
2020-03-23 20:24:17 +00:00
);
2020-03-18 13:18:37 +00:00
};
const steps = [step1, step2()];
return (
2020-03-23 20:24:17 +00:00
<Modal
closeIcon
open={this.state.openModal}
onClose={this.resetState}
2020-05-12 13:18:33 +00:00
trigger={(
2020-05-01 18:05:39 +00:00
<StyledDiv
onClick={this.handleOpen}
style={{
2020-05-12 13:18:33 +00:00
position: 'relative',
top: 'calc(50% - 5rem)',
left: 'calc(50% - 5rem)',
2020-05-01 18:05:39 +00:00
}}
>
2020-05-12 13:18:33 +00:00
<Image src="/img/add.svg" style={{ filter: 'invert()' }} />
2020-03-23 20:24:17 +00:00
</StyledDiv>
2020-05-12 13:18:33 +00:00
)}
centered
2020-03-23 20:24:17 +00:00
>
2020-03-18 13:18:37 +00:00
<Modal.Header>Add a New Device</Modal.Header>
2020-03-23 20:24:17 +00:00
<Modal.Content>{steps[this.state.step - 1]}</Modal.Content>
2020-03-18 13:18:37 +00:00
<Modal.Actions>
{this.state.step > 1 ? (
2020-03-23 20:24:17 +00:00
<Button
onClick={this.previousStep}
color="blue"
icon
labelPosition="left"
>
<Icon name="left arrow" />
2020-03-18 13:18:37 +00:00
Back
</Button>
2020-03-23 20:24:17 +00:00
) : (
2020-05-12 13:18:33 +00:00
''
2020-03-25 16:20:53 +00:00
)}
2020-03-18 13:18:37 +00:00
{this.state.step < steps.length ? (
2020-03-23 20:24:17 +00:00
<Button
color="blue"
onClick={this.nextStep}
icon
labelPosition="right"
>
2020-03-18 13:18:37 +00:00
Next
2020-03-23 20:24:17 +00:00
<Icon name="right arrow" />
2020-03-18 13:18:37 +00:00
</Button>
2020-03-23 20:24:17 +00:00
) : (
2020-05-12 13:18:33 +00:00
''
2020-03-25 16:20:53 +00:00
)}
2020-03-18 13:18:37 +00:00
{this.state.step === steps.length ? (
2020-03-23 20:24:17 +00:00
<Button
onClick={this.createDevice}
color="blue"
icon
labelPosition="right"
>
<Icon name="up arrow" />
2020-03-18 13:18:37 +00:00
Finish
</Button>
2020-03-23 20:24:17 +00:00
) : (
2020-05-12 13:18:33 +00:00
''
2020-03-25 16:20:53 +00:00
)}
2020-03-18 13:18:37 +00:00
</Modal.Actions>
</Modal>
2020-03-23 20:24:17 +00:00
);
2020-03-18 13:18:37 +00:00
}
}
2020-04-10 21:13:23 +00:00
const mapStateToProps = (state, _) => ({
2020-04-11 16:29:32 +00:00
devices: Object.values(state.devices),
activeRoom: state.active.activeRoom,
2020-04-10 21:13:23 +00:00
});
2020-04-11 16:29:32 +00:00
const NewDeviceContainer = connect(mapStateToProps, RemoteService)(NewDevice);
export default NewDeviceContainer;