2020-04-27 15:00:46 +00:00
|
|
|
import React, {Component, useState, useEffect} from "react";
|
|
|
|
import {connect} from "react-redux";
|
|
|
|
import {RemoteService} from "../../remote";
|
2020-04-24 10:26:55 +00:00
|
|
|
import "./Automations.css";
|
2020-04-27 15:00:46 +00:00
|
|
|
|
2020-04-24 10:26:55 +00:00
|
|
|
import {
|
|
|
|
Segment,
|
|
|
|
Grid,
|
|
|
|
Icon,
|
|
|
|
Header,
|
|
|
|
Input,
|
|
|
|
Button,
|
|
|
|
List,
|
|
|
|
Dropdown,
|
|
|
|
Form,
|
|
|
|
Divider,
|
|
|
|
Checkbox,
|
|
|
|
Menu,
|
2020-04-28 09:39:38 +00:00
|
|
|
Label,
|
2020-04-24 10:26:55 +00:00
|
|
|
} from "semantic-ui-react";
|
|
|
|
|
2020-04-27 15:00:46 +00:00
|
|
|
const operands = [
|
|
|
|
{key: "EQUAL", text: "=", value: "EQUAL"},
|
2020-04-24 10:26:55 +00:00
|
|
|
{
|
2020-04-27 15:00:46 +00:00
|
|
|
key: "GREATER_EQUAL",
|
2020-04-24 10:26:55 +00:00
|
|
|
text: "\u2265",
|
2020-04-27 15:00:46 +00:00
|
|
|
value: "GREATER_EQUAL",
|
2020-04-24 10:26:55 +00:00
|
|
|
},
|
|
|
|
{
|
2020-04-27 15:00:46 +00:00
|
|
|
key: "GREATER",
|
2020-04-24 10:26:55 +00:00
|
|
|
text: ">",
|
2020-04-27 15:00:46 +00:00
|
|
|
value: "GREATER",
|
2020-04-24 10:26:55 +00:00
|
|
|
},
|
|
|
|
{
|
2020-04-27 15:00:46 +00:00
|
|
|
key: "LESS_EQUAL",
|
2020-04-24 10:26:55 +00:00
|
|
|
text: "\u2264",
|
2020-04-27 15:00:46 +00:00
|
|
|
value: "LESS_EQUAL",
|
2020-04-24 10:26:55 +00:00
|
|
|
},
|
|
|
|
{
|
2020-04-27 15:00:46 +00:00
|
|
|
key: "LESS",
|
2020-04-24 10:26:55 +00:00
|
|
|
text: "<",
|
2020-04-27 15:00:46 +00:00
|
|
|
value: "LESS",
|
2020-04-24 10:26:55 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-04-27 15:00:46 +00:00
|
|
|
const deviceStateOptions = [
|
|
|
|
{key: "off", text: "off", value: false},
|
|
|
|
{key: "on", text: "on", value: true},
|
|
|
|
];
|
|
|
|
|
2020-04-24 10:26:55 +00:00
|
|
|
const CreateTrigger = (props) => {
|
2020-04-27 15:00:46 +00:00
|
|
|
const [activeOperand, setActiveOperand] = useState(true);
|
|
|
|
const admitedDevices = ["sensor", "regularLight", "dimmableLight"]; // TODO Complete this list
|
|
|
|
const deviceList = props.devices
|
|
|
|
.map((device) => {
|
|
|
|
return {
|
|
|
|
key: device.id,
|
|
|
|
text: device.name,
|
|
|
|
value: device.id,
|
|
|
|
kind: device.kind,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter((e) => admitedDevices.includes(e.kind));
|
|
|
|
|
|
|
|
const onChange = (e, val) => {
|
|
|
|
props.inputChange(val);
|
|
|
|
if (
|
|
|
|
props.devices
|
|
|
|
.filter((d) => d.id === val.value)[0]
|
|
|
|
.hasOwnProperty("on")
|
|
|
|
) {
|
|
|
|
setActiveOperand(false);
|
|
|
|
} else {
|
|
|
|
setActiveOperand(true);
|
|
|
|
}
|
|
|
|
};
|
2020-04-24 10:26:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<List.Item>
|
|
|
|
<List.Content>
|
|
|
|
<Form>
|
|
|
|
<Form.Group>
|
|
|
|
<Form.Field inline width={7}>
|
|
|
|
<Dropdown
|
2020-04-27 15:00:46 +00:00
|
|
|
onChange={onChange}
|
2020-04-24 10:26:55 +00:00
|
|
|
name="device"
|
|
|
|
search
|
|
|
|
selection
|
2020-04-27 15:00:46 +00:00
|
|
|
options={deviceList}
|
2020-04-24 10:26:55 +00:00
|
|
|
placeholder="Device"
|
|
|
|
/>
|
|
|
|
</Form.Field>
|
2020-04-27 15:00:46 +00:00
|
|
|
{activeOperand ? (
|
|
|
|
<React.Fragment>
|
|
|
|
<Form.Field inline width={2}>
|
|
|
|
<Dropdown
|
|
|
|
onChange={(e, val) =>
|
|
|
|
props.inputChange(val)
|
|
|
|
}
|
|
|
|
name="operand"
|
|
|
|
compact
|
|
|
|
selection
|
|
|
|
options={operands}
|
|
|
|
/>
|
|
|
|
</Form.Field>
|
|
|
|
<Form.Field inline width={7}>
|
|
|
|
<Input
|
|
|
|
onChange={(e, val) =>
|
|
|
|
props.inputChange(val)
|
|
|
|
}
|
|
|
|
name="value"
|
|
|
|
type="number"
|
|
|
|
placeholder="Value"
|
|
|
|
/>
|
|
|
|
</Form.Field>
|
|
|
|
</React.Fragment>
|
|
|
|
) : (
|
|
|
|
<Form.Field inline width={7}>
|
|
|
|
<Dropdown
|
|
|
|
onChange={(e, val) =>
|
|
|
|
props.inputChange(val)
|
|
|
|
}
|
|
|
|
placeholder="State"
|
|
|
|
name="value"
|
|
|
|
compact
|
|
|
|
selection
|
|
|
|
options={deviceStateOptions}
|
|
|
|
/>
|
|
|
|
</Form.Field>
|
|
|
|
)}
|
2020-04-24 10:26:55 +00:00
|
|
|
</Form.Group>
|
|
|
|
</Form>
|
|
|
|
</List.Content>
|
|
|
|
</List.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const SceneItem = (props) => {
|
2020-04-27 15:00:46 +00:00
|
|
|
let position = props.order.indexOf(props.scene.id);
|
2020-04-24 10:26:55 +00:00
|
|
|
return (
|
|
|
|
<List.Item>
|
|
|
|
<List.Header>
|
|
|
|
<Grid textAlign="center">
|
|
|
|
<Grid.Row>
|
|
|
|
<Grid.Column width={4}>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Checkbox
|
|
|
|
toggle
|
|
|
|
onChange={(e, val) =>
|
|
|
|
props.orderScenes(
|
|
|
|
props.scene.id,
|
|
|
|
val.checked
|
|
|
|
)
|
|
|
|
}
|
|
|
|
checked={position + 1 > 0}
|
|
|
|
/>
|
|
|
|
</Grid.Column>
|
2020-04-28 09:39:38 +00:00
|
|
|
<Grid.Column width={8}>
|
2020-04-27 15:00:46 +00:00
|
|
|
<h3>{props.scene.name}</h3>
|
2020-04-24 10:26:55 +00:00
|
|
|
</Grid.Column>
|
|
|
|
<Grid.Column width={4}>
|
2020-04-27 15:00:46 +00:00
|
|
|
<h3>
|
|
|
|
{position !== -1 ? "# " + (position + 1) : ""}
|
|
|
|
</h3>
|
2020-04-24 10:26:55 +00:00
|
|
|
</Grid.Column>
|
|
|
|
</Grid.Row>
|
|
|
|
</Grid>
|
|
|
|
</List.Header>
|
|
|
|
</List.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-27 15:00:46 +00:00
|
|
|
const Trigger = ({deviceName, trigger, onRemove, index}) => {
|
|
|
|
const {device, operand, value} = trigger;
|
|
|
|
let symbol;
|
|
|
|
if (operand) {
|
|
|
|
symbol = operands.filter((opt) => opt.key === operand)[0].text;
|
|
|
|
}
|
2020-04-24 10:26:55 +00:00
|
|
|
return (
|
2020-04-27 15:00:46 +00:00
|
|
|
<List.Item className="trigger-item">
|
2020-04-24 10:26:55 +00:00
|
|
|
<Menu compact>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Menu.Item as="span">{deviceName}</Menu.Item>
|
|
|
|
{operand ? <Menu.Item as="span">{symbol}</Menu.Item> : ""}
|
2020-04-28 09:39:38 +00:00
|
|
|
<Menu.Item as="span">
|
|
|
|
{operand ? value : value ? "on" : "off"}
|
|
|
|
</Menu.Item>
|
2020-04-24 10:26:55 +00:00
|
|
|
</Menu>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Icon
|
|
|
|
as={"i"}
|
2020-04-28 09:39:38 +00:00
|
|
|
onClick={() => onRemove(index)}
|
2020-04-27 15:00:46 +00:00
|
|
|
className="remove-icon"
|
|
|
|
name="remove"
|
|
|
|
/>
|
2020-04-24 10:26:55 +00:00
|
|
|
</List.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-28 09:39:38 +00:00
|
|
|
export const CreateAutomation = (props) => {
|
2020-04-24 10:26:55 +00:00
|
|
|
const [triggerList, setTrigger] = useState([]);
|
2020-04-27 15:00:46 +00:00
|
|
|
const [order, setOrder] = useState([]);
|
|
|
|
const [stateScenes, setScenes] = useState(props.scenes);
|
|
|
|
const [automationName, setautomationName] = useState("New Automation");
|
|
|
|
const [editName, setEditName] = useState(false);
|
|
|
|
const [newTrigger, setNewTrigger] = useState({});
|
2020-04-24 10:26:55 +00:00
|
|
|
|
2020-04-27 15:00:46 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setScenes(props.scenes);
|
|
|
|
}, [props]);
|
|
|
|
|
|
|
|
const _checkNewTrigger = (trigger) => {
|
|
|
|
const auxDevice = props.devices.filter(
|
|
|
|
(d) => d.id === trigger.device
|
|
|
|
)[0];
|
|
|
|
if (auxDevice && auxDevice.hasOwnProperty("on")) {
|
|
|
|
if (!trigger.device || !trigger.value == null) {
|
|
|
|
return {
|
|
|
|
result: false,
|
|
|
|
message: "There are missing fields!",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!trigger.device || !trigger.operand || !trigger.value) {
|
|
|
|
return {
|
|
|
|
result: false,
|
|
|
|
message: "There are missing fields",
|
|
|
|
};
|
|
|
|
}
|
2020-04-24 10:26:55 +00:00
|
|
|
}
|
|
|
|
const result = !triggerList.some(
|
|
|
|
(t) => t.device === trigger.device && t.operand === trigger.operand
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
result: result,
|
|
|
|
message: result
|
|
|
|
? ""
|
|
|
|
: "You have already created a trigger for this device with the same conditions",
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const addTrigger = () => {
|
2020-04-27 15:00:46 +00:00
|
|
|
const {result, message} = _checkNewTrigger(newTrigger);
|
|
|
|
const auxTrigger = newTrigger;
|
2020-04-24 10:26:55 +00:00
|
|
|
if (result) {
|
2020-04-27 15:00:46 +00:00
|
|
|
if (
|
|
|
|
props.devices
|
|
|
|
.filter((d) => d.id === newTrigger.device)[0]
|
|
|
|
.hasOwnProperty("on")
|
|
|
|
) {
|
|
|
|
delete auxTrigger.operand;
|
|
|
|
}
|
|
|
|
setTrigger((prevList) => [...prevList, auxTrigger]);
|
2020-04-24 10:26:55 +00:00
|
|
|
} else {
|
|
|
|
alert(message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-28 09:39:38 +00:00
|
|
|
const removeTrigger = (index) => {
|
|
|
|
setTrigger((prevList) => prevList.filter((t, i) => i !== index));
|
2020-04-27 15:00:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This gets triggered when the devices dropdown changes the value.
|
2020-04-24 10:26:55 +00:00
|
|
|
const onInputChange = (val) => {
|
2020-04-27 15:00:46 +00:00
|
|
|
setNewTrigger({...newTrigger, [val.name]: val.value});
|
|
|
|
};
|
|
|
|
const onChangeName = (e, val) => setautomationName(val.value);
|
|
|
|
|
|
|
|
const orderScenes = (id, checked) => {
|
|
|
|
if (checked) {
|
|
|
|
setOrder((prevList) => [...prevList, id]);
|
|
|
|
} else {
|
|
|
|
setOrder((prevList) => prevList.filter((e) => e !== id));
|
|
|
|
}
|
2020-04-24 10:26:55 +00:00
|
|
|
};
|
2020-04-27 15:00:46 +00:00
|
|
|
const searchScenes = (e, {value}) => {
|
|
|
|
if (value.length > 0) {
|
|
|
|
setScenes((prevScenes) => {
|
|
|
|
return stateScenes.filter((e) => {
|
|
|
|
return e.name.includes(value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setScenes(props.scenes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const _generateKey = (trigger) => {
|
|
|
|
if (trigger.hasOwnProperty("operand")) {
|
|
|
|
return trigger.device + trigger.operand + trigger.value;
|
|
|
|
}
|
|
|
|
return trigger.device + trigger.value;
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkBeforeSave = () => {
|
|
|
|
if (automationName.length <= 0) {
|
|
|
|
alert("Give a name to the automation");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (triggerList.length <= 0) {
|
|
|
|
alert("You have to create a trigger");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (order.length <= 0) {
|
|
|
|
alert("You need at least one active scene");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-04-28 09:39:38 +00:00
|
|
|
};
|
2020-04-27 15:00:46 +00:00
|
|
|
|
|
|
|
const saveAutomation = () => {
|
|
|
|
//if(checkBeforeSave()){
|
|
|
|
const automation = {
|
2020-04-28 09:39:38 +00:00
|
|
|
name: automationName,
|
|
|
|
};
|
2020-04-27 15:00:46 +00:00
|
|
|
props.save({automation, triggerList, order});
|
|
|
|
//}
|
2020-04-28 09:39:38 +00:00
|
|
|
};
|
2020-04-24 10:26:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Header style={{display: "inline", marginRight: "1rem"}}>
|
|
|
|
{editName ? (
|
|
|
|
<Input
|
|
|
|
focus
|
|
|
|
transparent
|
|
|
|
placeholder="New automation name..."
|
|
|
|
onChange={onChangeName}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
automationName
|
|
|
|
)}
|
2020-04-24 10:26:55 +00:00
|
|
|
</Header>
|
|
|
|
|
|
|
|
<Button
|
2020-04-27 15:00:46 +00:00
|
|
|
onClick={() => setEditName((prev) => !prev)}
|
|
|
|
style={{display: "inline"}}
|
2020-04-24 10:26:55 +00:00
|
|
|
circular
|
|
|
|
size="small"
|
2020-04-27 15:00:46 +00:00
|
|
|
icon={editName ? "save" : "edit"}
|
2020-04-24 10:26:55 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<Segment placeholder className="segment-automations">
|
|
|
|
<Grid columns={2} stackable textAlign="center">
|
2020-04-27 15:00:46 +00:00
|
|
|
<Divider vertical/>
|
2020-04-24 10:26:55 +00:00
|
|
|
<Grid.Row verticalAlign="middle">
|
|
|
|
<Grid.Column>
|
|
|
|
<Header>Create Triggers</Header>
|
|
|
|
<List divided relaxed>
|
|
|
|
{triggerList.length > 0 &&
|
2020-04-27 15:00:46 +00:00
|
|
|
triggerList.map((trigger, i) => {
|
|
|
|
const deviceName = props.devices.filter(
|
|
|
|
(d) => d.id === trigger.device
|
|
|
|
)[0].name;
|
|
|
|
const key = _generateKey(trigger);
|
|
|
|
return (
|
|
|
|
<Trigger
|
|
|
|
key={key}
|
|
|
|
index={i}
|
|
|
|
deviceName={deviceName}
|
|
|
|
trigger={trigger}
|
|
|
|
onRemove={removeTrigger}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
<CreateTrigger
|
|
|
|
devices={props.devices}
|
|
|
|
inputChange={onInputChange}
|
|
|
|
/>
|
2020-04-24 10:26:55 +00:00
|
|
|
</List>
|
|
|
|
<Button
|
|
|
|
onClick={addTrigger}
|
|
|
|
circular
|
|
|
|
icon="add"
|
|
|
|
color="blue"
|
|
|
|
size="huge"
|
|
|
|
/>
|
|
|
|
</Grid.Column>
|
|
|
|
<Grid.Column>
|
2020-04-27 15:00:46 +00:00
|
|
|
{props.scenes.length > 0 ? (
|
2020-04-24 10:26:55 +00:00
|
|
|
<React.Fragment>
|
|
|
|
<Header>Activate Scenes</Header>
|
|
|
|
<Input
|
|
|
|
icon="search"
|
|
|
|
placeholder="Search..."
|
|
|
|
fluid
|
2020-04-27 15:00:46 +00:00
|
|
|
onChange={searchScenes}
|
2020-04-24 10:26:55 +00:00
|
|
|
/>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Divider horizontal/>
|
2020-04-24 10:26:55 +00:00
|
|
|
<List divided relaxed>
|
2020-04-27 15:00:46 +00:00
|
|
|
{stateScenes.map((scene) => (
|
|
|
|
<SceneItem
|
|
|
|
key={scene.id}
|
|
|
|
scene={scene}
|
|
|
|
order={order}
|
|
|
|
orderScenes={orderScenes}
|
|
|
|
/>
|
2020-04-24 10:26:55 +00:00
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
</React.Fragment>
|
|
|
|
) : (
|
|
|
|
<React.Fragment>
|
|
|
|
<Header icon>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Icon name="world"/>
|
2020-04-24 10:26:55 +00:00
|
|
|
</Header>
|
|
|
|
<Button primary>Create Scene</Button>
|
|
|
|
</React.Fragment>
|
|
|
|
)}
|
|
|
|
</Grid.Column>
|
|
|
|
</Grid.Row>
|
|
|
|
</Grid>
|
|
|
|
</Segment>
|
|
|
|
<Grid>
|
|
|
|
<Grid.Row>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Grid.Column style={{marginRight: "1rem"}}>
|
2020-04-28 09:39:38 +00:00
|
|
|
<Button onClick={() => saveAutomation()} color="green">
|
|
|
|
SAVE
|
|
|
|
</Button>
|
2020-04-24 10:26:55 +00:00
|
|
|
</Grid.Column>
|
|
|
|
</Grid.Row>
|
|
|
|
</Grid>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
};
|
2020-04-14 12:58:03 +00:00
|
|
|
|
2020-04-28 09:39:38 +00:00
|
|
|
const Automation = ({automation, devices, scenes, removeAutomation}) => {
|
|
|
|
const {triggers} = automation;
|
|
|
|
const scenePriorities = automation.scenes;
|
|
|
|
const getOperator = (operand) =>
|
|
|
|
operands.filter((o) => o.key == operand)[0].text;
|
|
|
|
|
2020-04-27 15:00:46 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<Header style={{display: "inline", marginRight: "1rem"}}>
|
|
|
|
{automation.name}
|
|
|
|
</Header>
|
|
|
|
<Button
|
|
|
|
style={{display: "inline"}}
|
|
|
|
circular
|
|
|
|
size="small"
|
|
|
|
icon={"edit"}
|
|
|
|
/>
|
2020-04-28 09:39:38 +00:00
|
|
|
<Button
|
|
|
|
style={{display: "inline"}}
|
|
|
|
circular
|
|
|
|
onClick={() => removeAutomation(automation.id)}
|
|
|
|
color="red"
|
|
|
|
size="small"
|
|
|
|
icon={"trash alternate outline"}
|
|
|
|
/>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Segment placeholder>
|
|
|
|
<Grid columns={2} stackable textAlign="center">
|
|
|
|
<Divider vertical></Divider>
|
|
|
|
<Grid.Row verticalAlign="middle">
|
|
|
|
<Grid.Column>
|
|
|
|
<Header>Triggers</Header>
|
|
|
|
<List divided relaxed>
|
2020-04-28 09:39:38 +00:00
|
|
|
{triggers !== undefined &&
|
|
|
|
triggers.map((trigger) => {
|
|
|
|
const device = devices.filter(
|
|
|
|
(d) => d.id === trigger.deviceId
|
|
|
|
)[0];
|
2020-04-27 15:00:46 +00:00
|
|
|
return (
|
|
|
|
<Menu key={trigger.id} compact>
|
2020-04-28 09:39:38 +00:00
|
|
|
<Menu.Item as="span">
|
|
|
|
{device.name}{" "}
|
|
|
|
{trigger.operator
|
|
|
|
? getOperator(
|
|
|
|
trigger.operator
|
|
|
|
) +
|
|
|
|
" " +
|
|
|
|
trigger.range
|
|
|
|
: trigger.value
|
|
|
|
? " - on"
|
|
|
|
: " - off"}
|
2020-04-27 15:00:46 +00:00
|
|
|
</Menu.Item>
|
|
|
|
</Menu>
|
2020-04-28 09:39:38 +00:00
|
|
|
);
|
2020-04-27 15:00:46 +00:00
|
|
|
})}
|
|
|
|
</List>
|
|
|
|
</Grid.Column>
|
|
|
|
<Grid.Column>
|
|
|
|
<Header>Scenes</Header>
|
2020-04-28 09:39:38 +00:00
|
|
|
<List divided relaxed>
|
|
|
|
{scenePriorities !== undefined &&
|
|
|
|
scenePriorities.map((sp) => {
|
|
|
|
const sceneData = scenes.filter(
|
|
|
|
(s) => s.id === sp.sceneId
|
|
|
|
)[0];
|
|
|
|
return (
|
|
|
|
<Menu key={sceneData.id} compact>
|
|
|
|
<Menu.Item as="span">
|
|
|
|
{sceneData.name}
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</List>
|
2020-04-27 15:00:46 +00:00
|
|
|
</Grid.Column>
|
|
|
|
</Grid.Row>
|
|
|
|
</Grid>
|
|
|
|
</Segment>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-14 12:58:03 +00:00
|
|
|
class AutomationsPanel extends Component {
|
2020-04-24 10:26:55 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-04-28 09:39:38 +00:00
|
|
|
this.state = {openModal: false}
|
2020-04-27 15:00:46 +00:00
|
|
|
this.getDevices();
|
|
|
|
this.getScenes();
|
|
|
|
this.getAutomations();
|
|
|
|
}
|
|
|
|
|
|
|
|
getScenes() {
|
|
|
|
this.props.fetchAllScenes().catch(console.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDevices() {
|
|
|
|
this.props
|
|
|
|
.fetchDevices()
|
|
|
|
.catch((err) => console.error(`error fetching devices:`, err));
|
|
|
|
}
|
|
|
|
|
|
|
|
getAutomations() {
|
|
|
|
this.props
|
|
|
|
.fetchAutomations()
|
|
|
|
.catch((err) => console.error(`error fetching automations:`, err));
|
2020-04-24 10:26:55 +00:00
|
|
|
}
|
2020-04-14 12:58:03 +00:00
|
|
|
|
2020-04-28 09:39:38 +00:00
|
|
|
|
|
|
|
removeAutomation = (id) => {
|
|
|
|
this.props
|
|
|
|
.deleteAutomation(id)
|
|
|
|
.catch((err) => console.error(`error removing automation ${id}:`, err));
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:26:55 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2020-04-28 09:39:38 +00:00
|
|
|
<Grid style={{marginTop: "3rem"}}>
|
2020-04-27 15:00:46 +00:00
|
|
|
<Grid.Row>
|
2020-04-28 09:39:38 +00:00
|
|
|
<Grid.Column textAlign="center" width={16}>
|
|
|
|
{!this.state.openModal ?
|
|
|
|
(
|
|
|
|
<List>
|
|
|
|
<CreateAutomation
|
|
|
|
save={this.props.saveAutomation}
|
|
|
|
scenes={this.props.scenes}
|
|
|
|
devices={this.props.devices}
|
|
|
|
/>
|
|
|
|
</List>
|
|
|
|
) :
|
|
|
|
(
|
|
|
|
<Button color="green">CREATE AUTOMATION</Button>
|
|
|
|
)}
|
|
|
|
|
2020-04-27 15:00:46 +00:00
|
|
|
</Grid.Column>
|
|
|
|
</Grid.Row>
|
|
|
|
<Grid.Row>
|
2020-04-28 09:39:38 +00:00
|
|
|
{this.props.automations.map((automation, i) => {
|
2020-04-27 15:00:46 +00:00
|
|
|
return (
|
2020-04-28 09:39:38 +00:00
|
|
|
<Grid.Column
|
|
|
|
key={i}
|
|
|
|
width={8}
|
|
|
|
style={{margin: "2rem 0"}}
|
|
|
|
>
|
|
|
|
<Automation
|
|
|
|
removeAutomation={this.removeAutomation}
|
|
|
|
scenes={this.props.scenes}
|
|
|
|
devices={this.props.devices}
|
|
|
|
automation={automation}
|
|
|
|
/>
|
2020-04-27 15:00:46 +00:00
|
|
|
</Grid.Column>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Grid.Row>
|
|
|
|
</Grid>
|
2020-04-24 10:26:55 +00:00
|
|
|
);
|
|
|
|
}
|
2020-04-14 12:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state, _) => ({
|
2020-04-24 10:26:55 +00:00
|
|
|
activeRoom: state.active.activeRoom,
|
|
|
|
activeTab: state.active.activeTab,
|
2020-04-27 15:00:46 +00:00
|
|
|
get scenes() {
|
|
|
|
return Object.values(state.scenes);
|
|
|
|
},
|
|
|
|
get devices() {
|
|
|
|
return Object.values(state.devices);
|
|
|
|
},
|
|
|
|
get automations() {
|
|
|
|
return Object.values(state.automations);
|
2020-04-28 09:39:38 +00:00
|
|
|
},
|
2020-04-14 12:58:03 +00:00
|
|
|
});
|
|
|
|
const AutomationsPanelContainer = connect(
|
2020-04-24 10:26:55 +00:00
|
|
|
mapStateToProps,
|
|
|
|
RemoteService
|
2020-04-14 12:58:03 +00:00
|
|
|
)(AutomationsPanel);
|
|
|
|
export default AutomationsPanelContainer;
|