Automation.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { useEffect, useState } from "react";
  2. import useSWR from "swr";
  3. import { Button, Icon, Steps } from "tiny-ui";
  4. import { fetcher, isArray, isEmpty } from "lib/utils";
  5. import { useScreenSize } from "components";
  6. const { Step } = Steps;
  7. const Automation = ({
  8. clients,
  9. names,
  10. presets,
  11. automation,
  12. onAutomation,
  13. onPower,
  14. onPreset,
  15. busy
  16. }) => {
  17. const [current, setCurrent] = useState(-1);
  18. const [preset, setPreset] = useState();
  19. const [total, setTotal] = useState();
  20. const { data, mutate } = useSWR("/api/state", fetcher);
  21. const screenSize = useScreenSize();
  22. const getTitle = (indx) => presets?.[indx]?.name || indx;
  23. const getName = (v) => {
  24. let indx = clients?.findIndex((o) => o === v);
  25. return indx > -1 ? names?.[indx] : v;
  26. };
  27. const getDescription = (indx) => {
  28. return (
  29. (automation?.[indx] &&
  30. Object.keys(automation?.[indx])
  31. ?.map((o) => getName(o))
  32. ?.join(", ")) ||
  33. null
  34. );
  35. };
  36. useEffect(() => {
  37. setTotal((automation && Object.keys(automation)?.length) || 0);
  38. }, [automation]);
  39. useEffect(() => {
  40. if (current > -1 && current < total && (!preset || preset !== current)) {
  41. onAutomation(current + 1, () => {
  42. mutate();
  43. });
  44. }
  45. }, [current, total]);
  46. useEffect(() => {
  47. if (preset && preset !== current) setCurrent(preset);
  48. }, [preset]);
  49. useEffect(() => {
  50. if (isArray(data) && !isEmpty(data)) {
  51. let ps = [...new Set(data?.map((o) => o?.ps))];
  52. if (ps.length === 1 && ps?.[0] !== 200) setPreset(ps?.[0] - 1);
  53. }
  54. }, [data]);
  55. let steps = Object.keys(automation).map((o, i) => {
  56. return (
  57. <Step
  58. key={i}
  59. title={
  60. <div style={{ display: "flex", flexDirection: "row" }}>
  61. <div>{getTitle(o)}</div>
  62. {current === o - 1 ? (
  63. <div style={{ marginLeft: "3rem" }}>
  64. <Button
  65. title="Reapply"
  66. round
  67. icon={<Icon name="loader-circle" />}
  68. onClick={() => {
  69. onAutomation(o, () => {
  70. mutate();
  71. });
  72. }}
  73. />
  74. </div>
  75. ) : null}
  76. </div>
  77. }
  78. description={getDescription(o)}
  79. ></Step>
  80. );
  81. });
  82. return (
  83. <>
  84. <div className="commandbar">
  85. <Button.Group size={screenSize?.width > 550 ? "lg" : "md"} round>
  86. <Button
  87. title="Previous"
  88. icon={<Icon name="arrow-left" />}
  89. onClick={() => {
  90. let next = current - 1;
  91. if (next < 0) next = 0;
  92. setCurrent(next);
  93. }}
  94. />
  95. <Button
  96. title="Reapply"
  97. icon={<Icon name="loader-circle" />}
  98. onClick={() => {
  99. onAutomation(current + 1, () => {
  100. mutate();
  101. });
  102. }}
  103. />
  104. <Button
  105. title="Next"
  106. icon={<Icon name="arrow-right" />}
  107. onClick={() => {
  108. let next = current + 1;
  109. if (next > total) next = 0;
  110. setCurrent(next);
  111. }}
  112. />
  113. <Button
  114. title="Initial state"
  115. icon={<Icon name="no-idea" />}
  116. onClick={() => {
  117. setCurrent(-1);
  118. setPreset(null);
  119. onPreset(200, () => {
  120. mutate();
  121. });
  122. }}
  123. />
  124. <Button
  125. title="Power on"
  126. onClick={() => {
  127. onPower("on", () => {
  128. mutate();
  129. });
  130. }}
  131. >
  132. On
  133. </Button>
  134. <Button
  135. title="Power off"
  136. onClick={() => {
  137. onPower("off", () => {
  138. mutate();
  139. });
  140. }}
  141. >
  142. Off
  143. </Button>
  144. </Button.Group>
  145. </div>
  146. <div>
  147. <Steps
  148. current={current}
  149. direction="vertical"
  150. onChange={(v) => setCurrent(v)}
  151. >
  152. {steps}
  153. </Steps>
  154. </div>
  155. </>
  156. );
  157. };
  158. export default Automation;