page.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import { Col, Loader, Row } from "tiny-ui";
  4. import { WLEDClient } from "wled-client";
  5. import { Automation, Layout } from "components";
  6. import automation from "data/automation.json";
  7. import presets from "data/presets.json";
  8. const CLIENTS = process.env.CLIENTS || [];
  9. const NAMES = process.env.NAMES || [];
  10. export default function Home() {
  11. const [names, setNames] = useState(null);
  12. const [clients, setClients] = useState(null);
  13. const [connections, setConnections] = useState(null);
  14. useEffect(() => {
  15. let t = [];
  16. let c = [];
  17. let n = [];
  18. for (let client of CLIENTS) {
  19. c.push(client);
  20. t.push(new WLEDClient(client));
  21. }
  22. for (let name of NAMES) {
  23. n.push(name);
  24. }
  25. setClients(c);
  26. setConnections(t);
  27. setNames(n);
  28. }, []);
  29. const handleSync = async (payload) => {
  30. if (payload?.clients && payload?.value) {
  31. for (let client of payload?.clients) {
  32. let indx = clients.findIndex((o) => o === client);
  33. if (connections[indx]) {
  34. let wled = connections[indx];
  35. switch (payload?.value) {
  36. case "enable":
  37. await wled.enableUDPSync({ send: true, receive: false });
  38. break;
  39. case "disable":
  40. await wled.disableUDPSync();
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. };
  47. const handlePower = async (payload) => {
  48. if (payload?.clients && payload?.value) {
  49. for (let client of payload?.clients) {
  50. let indx = clients.findIndex((o) => o === client);
  51. if (connections[indx]) {
  52. let wled = connections[indx];
  53. switch (payload?.value) {
  54. case "on":
  55. await wled.turnOn();
  56. break;
  57. case "off":
  58. await wled.turnOff();
  59. break;
  60. }
  61. }
  62. }
  63. }
  64. };
  65. const handlePreset = async (payload) => {
  66. if (payload?.clients && payload?.value) {
  67. for (let client of payload?.clients) {
  68. let indx = clients.findIndex((o) => o === client);
  69. if (connections[indx]) {
  70. let wled = connections[indx];
  71. await wled.setPreset(payload?.value);
  72. }
  73. }
  74. }
  75. };
  76. return (
  77. <Layout page="home">
  78. <Row style={{ backgroundColor: "#fff" }}>
  79. <Col style={{ margin: "2rem" }}>
  80. {(connections && clients && (
  81. <Automation
  82. clients={clients}
  83. connections={connections}
  84. names={names}
  85. presets={presets}
  86. automation={automation}
  87. onPreset={handlePreset}
  88. onSync={handleSync}
  89. onPower={handlePower}
  90. />
  91. )) || <Loader size="lg" />}
  92. </Col>
  93. </Row>
  94. </Layout>
  95. );
  96. return;
  97. }