page.js 3.2 KB

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