2020-03-23 20:24:17 +00:00
|
|
|
import React, { Component } from "react";
|
|
|
|
import styled from "styled-components";
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
Form,
|
|
|
|
Icon,
|
|
|
|
Image,
|
|
|
|
Input,
|
|
|
|
Modal,
|
|
|
|
} from "semantic-ui-react";
|
2020-04-10 21:13:23 +00:00
|
|
|
import { RemoteService } from "../../../remote";
|
|
|
|
import { connect } from "react-redux";
|
2020-03-09 12:52:27 +00:00
|
|
|
|
|
|
|
const StyledDiv = styled.div`
|
2020-03-25 16:20:53 +00:00
|
|
|
background-color: #505bda;
|
|
|
|
padding: 3rem;
|
2020-03-09 12:52:27 +00:00
|
|
|
width: 10rem;
|
|
|
|
height: 10rem;
|
2020-03-23 20:24:17 +00:00
|
|
|
border-radius: 100%;
|
|
|
|
border: none;
|
|
|
|
position: relative;
|
2020-03-09 12:52:27 +00:00
|
|
|
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 11:13:15 +00:00
|
|
|
}
|
2020-03-25 16:20:53 +00:00
|
|
|
:active {
|
|
|
|
transform: translate(0.3px, 0.8px);
|
2020-03-25 11:13:15 +00:00
|
|
|
box-shadow: 0.5px 0.5px 7px 3.5px #ccc;
|
2020-03-09 12:52:27 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
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-03-26 10:11:44 +00:00
|
|
|
deviceName: "",
|
2020-03-15 14:50:25 +00:00
|
|
|
};
|
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-15 14:50:25 +00:00
|
|
|
|
2020-03-23 20:24:17 +00:00
|
|
|
handleOpen = () => {
|
|
|
|
this.setState({ openModal: true });
|
|
|
|
};
|
|
|
|
handleClose = () => {
|
|
|
|
this.setState({ openModal: false });
|
|
|
|
};
|
2020-03-15 14:50:25 +00:00
|
|
|
|
2020-03-18 13:18:37 +00:00
|
|
|
resetState = () => {
|
|
|
|
this.setState(this.baseState);
|
|
|
|
this.handleClose();
|
|
|
|
};
|
2020-03-09 12:52:27 +00:00
|
|
|
|
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
|
|
|
};
|
|
|
|
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-15 14:50:25 +00:00
|
|
|
|
2020-03-18 13:18:37 +00:00
|
|
|
setTypeOfDevice = (e, d) => {
|
2020-03-23 20:24:17 +00:00
|
|
|
if (d.value === "dimmableLight") {
|
|
|
|
this.setState({ typeOfDevice: d.value, intensity: 0 });
|
|
|
|
} else {
|
|
|
|
this.setState({ typeOfDevice: d.value });
|
|
|
|
}
|
2020-03-18 13:18:37 +00:00
|
|
|
};
|
2020-03-15 14:50:25 +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-15 14:50:25 +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);
|
|
|
|
if (d.value === "motionSensor") {
|
|
|
|
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,
|
|
|
|
kind: this.state.motion ? "motionSensor" : this.state.typeOfDevice,
|
|
|
|
};
|
|
|
|
let outputs = null;
|
|
|
|
|
|
|
|
const defaultNames = {
|
|
|
|
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",
|
2020-04-28 08:48:07 +00:00
|
|
|
securityCamera: "New security camera",
|
2020-03-23 20:24:17 +00:00
|
|
|
};
|
2020-03-20 17:42:38 +00:00
|
|
|
|
2020-04-11 16:29:32 +00:00
|
|
|
if (this.state.deviceName === "") {
|
|
|
|
data.name = defaultNames[this.state.typeOfDevice];
|
|
|
|
}
|
2020-04-23 13:34:23 +00:00
|
|
|
console.log("-------------------------");
|
|
|
|
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-04-23 13:34:23 +00:00
|
|
|
//trying to make securityCamera work
|
2020-04-23 14:02:02 +00:00
|
|
|
//case "securityCamera":
|
2020-04-28 08:48:07 +00:00
|
|
|
// data.path="/security_camera_videos/security_camera_1.mp4";
|
|
|
|
// data.on=false;
|
|
|
|
//break;
|
2020-04-23 13:34:23 +00:00
|
|
|
//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-03-20 17:42:38 +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-03-23 18:08:31 +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;
|
|
|
|
case "switch":
|
2020-03-25 18:58:19 +00:00
|
|
|
case "buttonDimmer":
|
|
|
|
case "knobDimmer":
|
2020-04-11 16:29:32 +00:00
|
|
|
outputs = this.state.lightsAttached;
|
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 {
|
|
|
|
let newDevice = await this.props.saveDevice(data);
|
|
|
|
if (outputs) {
|
|
|
|
await this.props.connectOutputs(newDevice, outputs);
|
|
|
|
}
|
|
|
|
this.resetState();
|
|
|
|
} catch (e) {
|
|
|
|
console.error("device creation error: ", e);
|
|
|
|
}
|
2020-03-23 20:24:17 +00:00
|
|
|
}
|
2020-03-09 12:52:27 +00:00
|
|
|
|
2020-03-18 13:18:37 +00:00
|
|
|
render() {
|
|
|
|
const deviceOptions = [
|
2020-04-28 08:48:07 +00:00
|
|
|
//stuff
|
|
|
|
{ key: "thermostat", text: "Thermostat", value: "thermostat", image: {} },
|
|
|
|
{ key: "curtains", text: "Curtain", value: "curtains", image: {} },
|
|
|
|
//stuff ends
|
2020-03-18 13:18:37 +00:00
|
|
|
{
|
2020-03-23 20:24:17 +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-03-23 20:24:17 +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-03-23 20:24:17 +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-03-23 20:24:17 +00:00
|
|
|
key: "sensor",
|
|
|
|
text: "Sensor",
|
|
|
|
value: "sensor",
|
|
|
|
image: { avatar: true, src: "/img/sensorOn.svg" },
|
2020-03-18 13:18:37 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-23 20:24:17 +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-03-25 16:20:53 +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-03-25 16:20:53 +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-04-28 08:48:07 +00:00
|
|
|
key: "securityCamera",
|
|
|
|
text: "Security Camera",
|
|
|
|
value: "securityCamera",
|
2020-05-01 09:45:19 +00:00
|
|
|
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 = [
|
|
|
|
{
|
|
|
|
key: "temperature",
|
|
|
|
text: "Temperature Sensor",
|
2020-03-20 17:42:38 +00:00
|
|
|
value: "TEMPERATURE",
|
2020-03-23 20:24:17 +00:00
|
|
|
image: { avatar: true, src: "/img/temperature-sensor.svg" },
|
2020-03-18 13:18:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "humidity",
|
|
|
|
text: "Humidity Sensor",
|
2020-03-20 17:42:38 +00:00
|
|
|
value: "HUMIDITY",
|
2020-03-23 20:24:17 +00:00
|
|
|
image: { avatar: true, src: "/img/humidity-sensor.svg" },
|
2020-03-18 13:18:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "light",
|
|
|
|
text: "Light Sensor",
|
2020-03-20 17:42:38 +00:00
|
|
|
value: "LIGHT",
|
2020-03-23 20:24:17 +00:00
|
|
|
image: { avatar: true, src: "/img/light-sensor.svg" },
|
2020-03-18 13:18:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "motion",
|
|
|
|
text: "Motion Sensor",
|
2020-03-20 17:42:38 +00:00
|
|
|
value: "motionSensor",
|
2020-03-23 20:24:17 +00:00
|
|
|
image: { avatar: true, src: "/img/sensorOn.svg" },
|
|
|
|
},
|
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 (
|
|
|
|
d.kind === "regularLight" ||
|
|
|
|
d.kind === "dimmableLight" ||
|
|
|
|
d.kind === "smartPlug"
|
|
|
|
) {
|
|
|
|
availableSwitchDevices.push({
|
|
|
|
key: d.id,
|
|
|
|
text: d.name,
|
|
|
|
value: d.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (d.kind === "dimmableLight") {
|
|
|
|
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
|
|
|
|
size={"large"}
|
|
|
|
onChange={this.setDeviceName}
|
|
|
|
focus
|
|
|
|
placeholder="Device Name"
|
|
|
|
/>
|
|
|
|
</Form.Field>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
const sensorForm = (
|
|
|
|
<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 = (
|
|
|
|
<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-03-23 20:24:17 +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-03-23 20:24:17 +00:00
|
|
|
{this.state.typeOfDevice === "sensor" ? sensorForm : ""}
|
2020-03-25 12:55:52 +00:00
|
|
|
{this.state.typeOfDevice === "switch" ? switchOptions : ""}
|
|
|
|
{this.state.typeOfDevice === "buttonDimmer" ||
|
2020-03-25 16:20:53 +00:00
|
|
|
this.state.typeOfDevice === "knobDimmer"
|
2020-03-25 12:55:52 +00:00
|
|
|
? dimmerOptions
|
2020-03-23 20:24:17 +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}
|
|
|
|
trigger={
|
|
|
|
<StyledDiv onClick={this.handleOpen}>
|
|
|
|
<Image src="/img/add.svg" style={{ filter: "invert()" }} />
|
|
|
|
</StyledDiv>
|
|
|
|
}
|
|
|
|
centered={true}
|
|
|
|
>
|
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-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-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-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-03-09 12:52:27 +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;
|