Bladeren bron

add wled embeded support, disable websocket connection by default

Tuan Nguyen 11 maanden geleden
bovenliggende
commit
71f17d5512

+ 8 - 5
dune_weaver_flask/modules/connection/connection_manager.py

@@ -161,7 +161,8 @@ def device_init(homing=True):
 def connect_device(homing=True):
     ports = list_serial_ports()
     if not ports:
-        state.conn = WebSocketConnection('ws://fluidnc.local:81')
+        # state.conn = WebSocketConnection('ws://fluidnc.local:81')
+        return
     else:
         state.conn = SerialConnection(ports[0])
     if (state.conn.is_connected() if state.conn else False):
@@ -332,10 +333,12 @@ def get_machine_position(timeout=5):
     logger.warning("Timeout reached waiting for machine position")
     return None, None
 
-def update_machine_position():
-    state.machine_x, state.machine_y = get_machine_position()
-    state.save()
-
+def update_machine_position():     
+    if (state.conn.is_connected() if state.conn else False):
+        logger.info('Saving machine position')
+        state.machine_x, state.machine_y = get_machine_position()
+        state.save()
+    
 def restart_connection(homing=False):
     """
     Restart the connection. If a connection exists, close it and attempt to establish a new one.

+ 13 - 0
dune_weaver_flask/static/css/style.css

@@ -1108,6 +1108,19 @@ input[type="number"]:focus {
     min-width: 200px;
 }
 
+
+#wled-container {
+    flex: 1 1 auto;
+    display: flex;
+    flex-direction: column;
+  }
+  
+  #wled-frame {
+    flex: 1 1 auto;
+    width: 100%;
+    border: none;
+  }
+
 /* Notification Styles */
 .notification {
     display: flex;

+ 70 - 0
dune_weaver_flask/static/js/main.js

@@ -1693,6 +1693,74 @@ function switchTab(tabName) {
     }
 }
 
+// Update the small UI segment to show the IP or hide it if none
+function updateWledUI() {
+    const wledIp = localStorage.getItem('wled_ip');
+    const wledContainer = document.getElementById('wled-container');
+    const wledFrame = document.getElementById('wled-frame');
+    const wledStatus = document.getElementById('wled-status');
+
+    if (!wledIp) {
+        wledContainer.classList.add('hidden');
+        return;
+    }
+
+    // Show the container and load WLED UI
+    wledContainer.classList.remove('hidden');
+    wledFrame.src = `http://${wledIp}`;
+
+}
+
+function saveWledIp() {
+    const ipInput = document.getElementById('wled_ip');
+    const saveButton = document.querySelector('.wled-settings button.cta');
+    const currentIp = localStorage.getItem('wled_ip');
+  
+    if (currentIp) {
+      // Clear the saved IP if one is already set
+      localStorage.removeItem('wled_ip');
+      ipInput.disabled = false;
+      ipInput.value = '';
+      saveButton.innerHTML = '<i class="fa-solid fa-save"></i><span>Save</span>';
+      logMessage('WLED IP cleared.', LOG_TYPE.INFO);
+    } else {
+      // Validate and save the new IP
+      const ip = ipInput.value.trim();
+      if (!validateIp(ip)) {
+        logMessage('Invalid IP address format.', LOG_TYPE.ERROR);
+        return;
+      }
+      localStorage.setItem('wled_ip', ip);
+      ipInput.disabled = true;
+      saveButton.innerHTML = '<i class="fa-solid fa-xmark"></i><span>Clear</span>';
+      logMessage(`WLED IP saved: ${ip}`, LOG_TYPE.SUCCESS);
+    }
+    
+    updateWledUI(); // Optionally update other parts of your UI
+  }
+  
+  function loadWledIp() {
+    const ipInput = document.getElementById('wled_ip');
+    const saveButton = document.querySelector('.wled-settings button.cta');
+    const savedIp = localStorage.getItem('wled_ip');
+  
+    if (savedIp) {
+      ipInput.value = savedIp;
+      ipInput.disabled = true;
+      saveButton.innerHTML = '<i class="fa-solid fa-xmark"></i><span>Clear</span>';
+    } else {
+      ipInput.disabled = false;
+      saveButton.innerHTML = '<i class="fa-solid fa-save"></i><span>Save</span>';
+    }
+    
+    updateWledUI(); // Update any UI segment that depends on the IP
+  }
+
+function validateIp(ip) {
+    const ipRegex = /^(25[0-5]|2[0-4]\d|1\d\d|\d?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|\d?\d)){3}$/;
+    return ipRegex.test(ip);
+  }
+
 document.addEventListener("visibilitychange", handleVisibilityChange);
 
 // Initialization
@@ -1704,6 +1772,8 @@ document.addEventListener('DOMContentLoaded', () => {
     loadAllPlaylists(); // Load all playlists on page load
     attachSettingsSaveListeners(); // Attach event listeners to save changes
     attachFullScreenListeners();
+    loadWledIp();
+    updateWledUI();
 
     // Periodically check for currently playing status
     if (document.hasFocus()) {

+ 21 - 0
dune_weaver_flask/templates/index.html

@@ -301,6 +301,12 @@
                     </button>
                 </div>
             </div>
+            <!-- LED Control Panel -->
+            <div id="wled-container" class="hidden">
+                <h3>WLED Controller</h3>
+                <iframe id="wled-frame" src="" width="100%" height="400px" frameborder="0"></iframe>
+                <p id="wled-status"></p>
+            </div>
         </section>
 
         <section id="settings-container">
@@ -331,6 +337,20 @@
                 </div>
             </section>
 
+            <section class="wled-settings">
+                <div class="header">
+                    <h2>WLED Configuration</h2>
+                </div>
+                <div class="control-group">
+                    <label for="wled_ip">WLED IP Address:</label>
+                    <input type="text" id="wled_ip" placeholder="Enter WLED IP">
+                    <button class="cta" onclick="saveWledIp()">
+                        <i class="fa-solid fa-save"></i>
+                        <span>Save</span>
+                    </button>
+                </div>
+            </section>
+
             <section class="software version">
                 <div class="header">
                     <h2>Software Version</h2>
@@ -402,5 +422,6 @@
 </div>
 
 <script src="../static/js/main.js"></script>
+<script src="../static/js/led_controller.js"></script>
 </body>
 </html>