led_control.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // LED Strip Control Functions
  2. function hexToRgb(hex) {
  3. // Remove the '#' if present
  4. hex = hex.replace('#', '');
  5. // Parse the hex values
  6. const r = parseInt(hex.substring(0, 2), 16);
  7. const g = parseInt(hex.substring(2, 4), 16);
  8. const b = parseInt(hex.substring(4, 6), 16);
  9. return [r, g, b];
  10. }
  11. function updateLEDStatus() {
  12. fetch('/api/led/status')
  13. .then(response => response.json())
  14. .then(data => {
  15. document.getElementById('led_mode').textContent = data.mode;
  16. document.getElementById('led_current_animation').textContent = data.current_animation || 'None';
  17. document.getElementById('led_power').checked = data.is_on;
  18. // Update controls to match current state
  19. document.getElementById('led_brightness').value = data.brightness;
  20. document.getElementById('led_speed').value = data.animation_speed;
  21. if (data.current_animation) {
  22. document.getElementById('led_animation').value = data.current_animation;
  23. }
  24. })
  25. .catch(error => {
  26. console.error('Error fetching LED status:', error);
  27. });
  28. }
  29. function setLEDColor(colorHex) {
  30. const rgb = hexToRgb(colorHex);
  31. fetch('/api/led/color', {
  32. method: 'POST',
  33. headers: {
  34. 'Content-Type': 'application/json'
  35. },
  36. body: JSON.stringify({
  37. color: rgb
  38. })
  39. })
  40. .then(response => response.json())
  41. .then(data => {
  42. if (data.error) {
  43. console.error('Error setting LED color:', data.error);
  44. }
  45. updateLEDStatus();
  46. })
  47. .catch(error => {
  48. console.error('Error setting LED color:', error);
  49. });
  50. }
  51. function setLEDBrightness(brightness) {
  52. fetch('/api/led/brightness', {
  53. method: 'POST',
  54. headers: {
  55. 'Content-Type': 'application/json'
  56. },
  57. body: JSON.stringify({
  58. brightness: parseInt(brightness)
  59. })
  60. })
  61. .then(response => response.json())
  62. .then(data => {
  63. if (data.error) {
  64. console.error('Error setting LED brightness:', data.error);
  65. }
  66. updateLEDStatus();
  67. })
  68. .catch(error => {
  69. console.error('Error setting LED brightness:', error);
  70. });
  71. }
  72. function setLEDAnimation(animation) {
  73. if (animation === 'none') {
  74. fetch('/api/led/animation/stop', {
  75. method: 'POST'
  76. })
  77. .then(response => response.json())
  78. .then(data => {
  79. if (data.error) {
  80. console.error('Error stopping LED animation:', data.error);
  81. }
  82. updateLEDStatus();
  83. })
  84. .catch(error => {
  85. console.error('Error stopping LED animation:', error);
  86. });
  87. } else {
  88. fetch('/api/led/animation', {
  89. method: 'POST',
  90. headers: {
  91. 'Content-Type': 'application/json'
  92. },
  93. body: JSON.stringify({
  94. animation: animation
  95. })
  96. })
  97. .then(response => response.json())
  98. .then(data => {
  99. if (data.error) {
  100. console.error('Error setting LED animation:', data.error);
  101. }
  102. updateLEDStatus();
  103. })
  104. .catch(error => {
  105. console.error('Error setting LED animation:', error);
  106. });
  107. }
  108. }
  109. function setLEDSpeed(speed) {
  110. fetch('/api/led/speed', {
  111. method: 'POST',
  112. headers: {
  113. 'Content-Type': 'application/json'
  114. },
  115. body: JSON.stringify({
  116. speed: parseInt(speed)
  117. })
  118. })
  119. .then(response => response.json())
  120. .then(data => {
  121. if (data.error) {
  122. console.error('Error setting LED speed:', data.error);
  123. }
  124. updateLEDStatus();
  125. })
  126. .catch(error => {
  127. console.error('Error setting LED speed:', error);
  128. });
  129. }
  130. function setLEDPower(state) {
  131. fetch('/api/led/power', {
  132. method: 'POST',
  133. headers: {
  134. 'Content-Type': 'application/json'
  135. },
  136. body: JSON.stringify({
  137. state: state
  138. })
  139. })
  140. .then(response => response.json())
  141. .then(data => {
  142. if (data.error) {
  143. console.error('Error setting LED power:', data.error);
  144. }
  145. updateLEDStatus();
  146. })
  147. .catch(error => {
  148. console.error('Error setting LED power:', error);
  149. });
  150. }
  151. // Update LED status every 5 seconds
  152. setInterval(updateLEDStatus, 5000);
  153. // Initial status update
  154. document.addEventListener('DOMContentLoaded', function () {
  155. updateLEDStatus();
  156. });