import { useEffect, useState } from "react";
import { Button, Collapse, Drawer, Icon, Steps, Typography } from "tiny-ui";
import WLED from "./WLED.js";
import { useLocalStorage } from "lib/state";
import { isBlank } from "lib/utils";
const { Step } = Steps;
const { Panel } = Collapse;
const { Heading, Paragraph, Text } = Typography;
const Client = ({ name, client, connection, ...rest }) => {
const normalize = (v) => JSON.stringify(v, null, 2);
return (
IP address: {client}
{Object.keys(connection?.state)?.map((o, i) => (
{o}: {normalize(connection?.state?.[o])}
))}
);
};
const WLEDClient = WLED(Client);
const Automation = ({
automation,
presets,
clients,
names,
onPreset,
onSync,
onPower,
connections,
...props
}) => {
const [current, setCurrent] = useLocalStorage("preset", -1, 3600);
const [total, setTotal] = useState();
const [help, setHelp] = useState(false);
const [info, setInfo] = useState(false);
const getTitle = (indx) => presets?.[indx]?.name || indx;
const getName = (v) => {
let indx = clients?.findIndex((o) => o === v);
return indx > -1 ? names?.[indx] : v;
};
const getDescription = (indx) => {
return (
(automation?.[indx] &&
Object.keys(automation?.[indx])
?.map((o) => getName(o))
?.join(", ")) ||
null
);
};
const handleChange = (indx) => {
let keys = Object.keys(automation?.[indx]);
let preset = { clients: keys || [], value: indx };
let power = { on: [], off: [] };
let sync = { enable: [], disable: [] };
// loop the keys and determine what to do
for (let key of keys) {
if (automation?.[indx]?.[key]?.on === true) power.on.push(key);
else power.off.push(key);
if (automation?.[indx]?.[key]?.sync === true) sync.enable.push(key);
else sync.disable.push(key);
}
if (!isBlank(sync?.enable))
onSync({ clients: sync?.enable, value: "enable" }); // enable sync
if (!isBlank(sync?.disable))
onSync({ clients: sync?.disable, value: "disable" }); // disable sync
setTimeout(() => {
onPreset(preset); // apply the preset
}, 100);
setTimeout(() => {
if (!isBlank(power?.on)) onPower({ clients: power?.on, value: "on" }); // power on
if (!isBlank(power?.off)) onPower({ clients: power?.off, value: "off" }); // power off
}, 200);
};
const handlePower = (value) => {
let keys = Object.keys(automation?.[1]);
onPower({ clients: keys, value: value });
};
useEffect(() => {
setTotal((automation && Object.keys(automation)?.length) || 0);
}, []);
useEffect(() => {
if (current > -1 && current < total) {
handleChange(current + 1);
}
}, [current]);
let steps = Object.keys(automation).map((o, i) => {
return (
{getTitle(o)}
{current === o - 1 ? (
}
onClick={() => {
handleChange(o);
}}
/>
) : null}
}
description={getDescription(o)}
>
);
});
return (
<>
}
onClick={() => {
let next = current - 1;
if (next < 0) next = 0;
setCurrent(next);
}}
/>
}
onClick={() => {
handleChange(current + 1);
}}
/>
}
onClick={() => {
let next = current + 1;
if (next > total) next = 0;
setCurrent(next);
}}
/>
{
handlePower("on");
}}
>
On
{
handlePower("off");
}}
>
Off
}
onClick={() => {
setHelp(true);
}}
/>
}
onClick={() => {
setInfo(true);
}}
/>
setCurrent(v)}
>
{steps}
setInfo(false)}
visible={info}
>
{connections?.map((o, i) => (
))}
setHelp(false)}
visible={help}
>
<>
Select one of the presets from the left to start applying preset
automation.
You can use the navigation buttons above to advance forward{" "}
and backward{" "}
through the preset list.
If a preset fails to apply to all of the intended WLED instances
then use the Reapply button {" "}
within the navigation menu bar or within the target step.
>
>
);
};
export default Automation;