| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658 |
- // ============================================================================
- // Collapsible Section Toggle
- // ============================================================================
- function toggleSection(headerElement) {
- const contentElement = headerElement.nextElementSibling;
- if (headerElement.classList.contains('collapsed')) {
- // Expand
- headerElement.classList.remove('collapsed');
- contentElement.classList.remove('collapsed');
- } else {
- // Collapse
- headerElement.classList.add('collapsed');
- contentElement.classList.add('collapsed');
- }
- }
- // ============================================================================
- // Constants and Utilities
- // ============================================================================
- // Constants for log message types
- const LOG_TYPE = {
- SUCCESS: 'success',
- WARNING: 'warning',
- ERROR: 'error',
- INFO: 'info',
- DEBUG: 'debug'
- };
- // Helper function to convert provider name to camelCase for ID lookup
- // e.g., "dw_leds" -> "DwLeds", "wled" -> "Wled", "none" -> "None"
- function providerToCamelCase(provider) {
- return provider.split('_').map(word =>
- word.charAt(0).toUpperCase() + word.slice(1)
- ).join('');
- }
- // Constants for cache
- const CACHE_KEYS = {
- CONNECTION_STATUS: 'connection_status',
- LAST_UPDATE: 'last_status_update'
- };
- const CACHE_DURATION = 5000; // 5 seconds cache duration
- // Function to log messages
- function logMessage(message, type = LOG_TYPE.DEBUG) {
- console.log(`[${type}] ${message}`);
- }
- // Function to get cached connection status
- function getCachedConnectionStatus() {
- const cachedData = localStorage.getItem(CACHE_KEYS.CONNECTION_STATUS);
- const lastUpdate = localStorage.getItem(CACHE_KEYS.LAST_UPDATE);
-
- if (cachedData && lastUpdate) {
- const now = Date.now();
- const cacheAge = now - parseInt(lastUpdate);
-
- if (cacheAge < CACHE_DURATION) {
- return JSON.parse(cachedData);
- }
- }
- return null;
- }
- // Function to set cached connection status
- function setCachedConnectionStatus(data) {
- localStorage.setItem(CACHE_KEYS.CONNECTION_STATUS, JSON.stringify(data));
- localStorage.setItem(CACHE_KEYS.LAST_UPDATE, Date.now().toString());
- }
- // Function to update serial connection status
- async function updateSerialStatus(forceUpdate = false) {
- try {
- // Check cache first unless force update is requested
- if (!forceUpdate) {
- const cachedData = getCachedConnectionStatus();
- if (cachedData) {
- updateConnectionUI(cachedData);
- return;
- }
- }
- const response = await fetch('/serial_status');
- if (response.ok) {
- const data = await response.json();
- setCachedConnectionStatus(data);
- updateConnectionUI(data);
- }
- } catch (error) {
- logMessage(`Error checking serial status: ${error.message}`, LOG_TYPE.ERROR);
- }
- }
- // Function to update UI based on connection status
- function updateConnectionUI(data) {
- const statusElement = document.getElementById('serialStatus');
- const iconElement = document.querySelector('.material-icons.text-3xl');
- const disconnectButton = document.getElementById('disconnectButton');
- const portSelectionDiv = document.getElementById('portSelectionDiv');
-
- if (statusElement && iconElement) {
- if (data.connected) {
- statusElement.textContent = `Connected to ${data.port || 'unknown port'}`;
- statusElement.className = 'text-green-500 text-sm font-medium leading-normal';
- iconElement.textContent = 'usb';
- if (disconnectButton) {
- disconnectButton.hidden = false;
- }
- if (portSelectionDiv) {
- portSelectionDiv.hidden = true;
- }
- } else {
- statusElement.textContent = 'Disconnected';
- statusElement.className = 'text-red-500 text-sm font-medium leading-normal';
- iconElement.textContent = 'usb_off';
- if (disconnectButton) {
- disconnectButton.hidden = true;
- }
- if (portSelectionDiv) {
- portSelectionDiv.hidden = false;
- }
- }
- }
- }
- // Function to update available serial ports
- async function updateSerialPorts() {
- try {
- const response = await fetch('/list_serial_ports');
- if (response.ok) {
- const ports = await response.json();
- const portsElement = document.getElementById('availablePorts');
- const portSelect = document.getElementById('portSelect');
- const preferredPortSelect = document.getElementById('preferredPortSelect');
- if (portsElement) {
- portsElement.textContent = ports.length > 0 ? ports.join(', ') : 'No ports available';
- }
- if (portSelect) {
- // Clear existing options except the first one
- while (portSelect.options.length > 1) {
- portSelect.remove(1);
- }
- // Add new options
- ports.forEach(port => {
- const option = document.createElement('option');
- option.value = port;
- option.textContent = port;
- portSelect.appendChild(option);
- });
- // If there's exactly one port available, select and connect to it
- if (ports.length === 1) {
- portSelect.value = ports[0];
- // Trigger connect button click
- const connectButton = document.getElementById('connectButton');
- if (connectButton) {
- connectButton.click();
- }
- }
- }
- // Also update the preferred port select dropdown
- if (preferredPortSelect) {
- // Store current selection
- const currentPreferred = preferredPortSelect.value;
- // Clear existing options except the first one (no preference)
- while (preferredPortSelect.options.length > 1) {
- preferredPortSelect.remove(1);
- }
- // Add all available ports
- ports.forEach(port => {
- const option = document.createElement('option');
- option.value = port;
- option.textContent = port;
- preferredPortSelect.appendChild(option);
- });
- // Restore selection if it's still available
- if (currentPreferred && ports.includes(currentPreferred)) {
- preferredPortSelect.value = currentPreferred;
- }
- }
- }
- } catch (error) {
- logMessage(`Error fetching serial ports: ${error.message}`, LOG_TYPE.ERROR);
- }
- }
- // Function to load and display preferred port setting
- async function loadPreferredPort() {
- try {
- const response = await fetch('/api/preferred-port');
- if (response.ok) {
- const data = await response.json();
- const preferredPortSelect = document.getElementById('preferredPortSelect');
- const currentPreferredPort = document.getElementById('currentPreferredPort');
- const preferredPortDisplay = document.getElementById('preferredPortDisplay');
- if (preferredPortSelect && data.preferred_port) {
- // Check if the preferred port is in the options
- const optionExists = Array.from(preferredPortSelect.options).some(
- opt => opt.value === data.preferred_port
- );
- if (optionExists) {
- preferredPortSelect.value = data.preferred_port;
- } else {
- // Add the preferred port as an option (it might not be currently available)
- const option = document.createElement('option');
- option.value = data.preferred_port;
- option.textContent = `${data.preferred_port} (not currently available)`;
- preferredPortSelect.appendChild(option);
- preferredPortSelect.value = data.preferred_port;
- }
- }
- // Show current preferred port indicator
- if (currentPreferredPort && preferredPortDisplay && data.preferred_port) {
- preferredPortDisplay.textContent = `Currently set to: ${data.preferred_port}`;
- currentPreferredPort.classList.remove('hidden');
- } else if (currentPreferredPort) {
- currentPreferredPort.classList.add('hidden');
- }
- }
- } catch (error) {
- logMessage(`Error loading preferred port: ${error.message}`, LOG_TYPE.ERROR);
- }
- }
- // Function to save preferred port setting
- async function savePreferredPort() {
- const preferredPortSelect = document.getElementById('preferredPortSelect');
- if (!preferredPortSelect) return;
- const preferredPort = preferredPortSelect.value || null;
- try {
- const response = await fetch('/api/settings', {
- method: 'PATCH',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ connection: { preferred_port: preferredPort } })
- });
- if (response.ok) {
- await response.json();
- const currentPreferredPort = document.getElementById('currentPreferredPort');
- const preferredPortDisplay = document.getElementById('preferredPortDisplay');
- if (preferredPort) {
- showStatusMessage(`Preferred port set to: ${preferredPort}`, 'success');
- if (currentPreferredPort && preferredPortDisplay) {
- preferredPortDisplay.textContent = `Currently set to: ${preferredPort}`;
- currentPreferredPort.classList.remove('hidden');
- }
- } else {
- showStatusMessage('Preferred port cleared - will auto-detect on startup', 'success');
- if (currentPreferredPort) {
- currentPreferredPort.classList.add('hidden');
- }
- }
- } else {
- throw new Error('Failed to save preferred port');
- }
- } catch (error) {
- showStatusMessage(`Failed to save preferred port: ${error.message}`, 'error');
- }
- }
- function setWledButtonState(isSet) {
- const saveWledConfig = document.getElementById('saveWledConfig');
- if (!saveWledConfig) return;
- if (isSet) {
- saveWledConfig.className = 'flex items-center justify-center gap-2 min-w-[100px] max-w-[480px] cursor-pointer rounded-lg h-10 px-4 bg-red-600 hover:bg-red-700 text-white text-sm font-medium leading-normal tracking-[0.015em] transition-colors';
- saveWledConfig.innerHTML = '<span class="material-icons text-lg">close</span><span class="truncate">Clear WLED IP</span>';
- } else {
- saveWledConfig.className = 'flex items-center justify-center gap-2 min-w-[100px] max-w-[480px] cursor-pointer rounded-lg h-10 px-4 bg-sky-600 hover:bg-sky-700 text-white text-sm font-medium leading-normal tracking-[0.015em] transition-colors';
- saveWledConfig.innerHTML = '<span class="material-icons text-lg">save</span><span class="truncate">Save Configuration</span>';
- }
- }
- // Handle LED provider selection and show/hide appropriate config sections
- function updateLedProviderUI() {
- const provider = document.querySelector('input[name="ledProvider"]:checked')?.value || 'none';
- const wledConfig = document.getElementById('wledConfig');
- const dwLedsConfig = document.getElementById('dwLedsConfig');
- if (wledConfig && dwLedsConfig) {
- if (provider === 'wled') {
- wledConfig.classList.remove('hidden');
- dwLedsConfig.classList.add('hidden');
- } else if (provider === 'dw_leds') {
- wledConfig.classList.add('hidden');
- dwLedsConfig.classList.remove('hidden');
- } else {
- wledConfig.classList.add('hidden');
- dwLedsConfig.classList.add('hidden');
- }
- }
- }
- // Load LED configuration from server
- async function loadLedConfig() {
- try {
- const response = await fetch('/get_led_config');
- if (response.ok) {
- const data = await response.json();
- // Set provider radio button
- const providerRadio = document.getElementById(`ledProvider${providerToCamelCase(data.provider)}`);
- if (providerRadio) {
- providerRadio.checked = true;
- } else {
- document.getElementById('ledProviderNone').checked = true;
- }
- // Set WLED IP if configured
- if (data.wled_ip) {
- const wledIpInput = document.getElementById('wledIpInput');
- if (wledIpInput) {
- wledIpInput.value = data.wled_ip;
- }
- }
- // Set DW LED configuration if configured
- if (data.dw_led_num_leds) {
- const numLedsInput = document.getElementById('dwLedNumLeds');
- if (numLedsInput) {
- numLedsInput.value = data.dw_led_num_leds;
- }
- }
- if (data.dw_led_gpio_pin) {
- const gpioPinInput = document.getElementById('dwLedGpioPin');
- if (gpioPinInput) {
- gpioPinInput.value = data.dw_led_gpio_pin;
- }
- }
- if (data.dw_led_pixel_order) {
- const pixelOrderInput = document.getElementById('dwLedPixelOrder');
- if (pixelOrderInput) {
- pixelOrderInput.value = data.dw_led_pixel_order;
- }
- }
- // Update UI to show correct config section
- updateLedProviderUI();
- }
- } catch (error) {
- logMessage(`Error loading LED config: ${error.message}`, LOG_TYPE.ERROR);
- }
- }
- // Initialize settings page
- document.addEventListener('DOMContentLoaded', async () => {
- // Initialize UI with default disconnected state
- updateConnectionUI({ connected: false });
- // Handle scroll to section if hash is present in URL
- if (window.location.hash) {
- setTimeout(() => {
- const targetSection = document.querySelector(window.location.hash);
- if (targetSection) {
- targetSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
- // Add a subtle highlight animation
- targetSection.style.transition = 'background-color 0.5s ease';
- const originalBg = targetSection.style.backgroundColor;
- targetSection.style.backgroundColor = 'rgba(14, 165, 233, 0.1)';
- setTimeout(() => {
- targetSection.style.backgroundColor = originalBg;
- }, 2000);
- }
- }, 300); // Delay to ensure page is fully loaded
- }
-
- // Load all data asynchronously using unified settings endpoint
- Promise.all([
- // Unified settings endpoint (replaces multiple individual fetches)
- fetch('/api/settings').then(response => response.json()).catch(() => ({})),
- // Non-settings operational endpoints (kept separate)
- fetch('/serial_status').then(response => response.json()).catch(() => ({ connected: false })),
- fetch('/api/version').then(response => response.json()).catch(() => ({ current: '1.0.0', latest: '1.0.0', update_available: false })),
- fetch('/list_serial_ports').then(response => response.json()).catch(() => []),
- getCachedPatternFiles().catch(() => [])
- ]).then(([settings, statusData, updateData, ports, patterns]) => {
- // Map unified settings to legacy variable names for backward compatibility with existing UI code
- const ledConfigData = {
- provider: settings.led?.provider || 'none',
- wled_ip: settings.led?.wled_ip || null,
- dw_led_num_leds: settings.led?.dw_led?.num_leds,
- dw_led_gpio_pin: settings.led?.dw_led?.gpio_pin,
- dw_led_pixel_order: settings.led?.dw_led?.pixel_order
- };
- const clearPatterns = {
- custom_clear_from_in: settings.patterns?.custom_clear_from_in,
- custom_clear_from_out: settings.patterns?.custom_clear_from_out
- };
- const clearSpeedData = {
- clear_pattern_speed: settings.patterns?.clear_pattern_speed,
- effective_speed: settings.patterns?.clear_pattern_speed // Will be handled by UI
- };
- const appNameData = { app_name: settings.app?.name || 'Dune Weaver' };
- const scheduledPauseData = settings.scheduled_pause || { enabled: false, time_slots: [] };
- const preferredPortData = { preferred_port: settings.connection?.preferred_port };
- // Store full settings for other initialization functions
- window.unifiedSettings = settings;
- // Update connection status
- setCachedConnectionStatus(statusData);
- updateConnectionUI(statusData);
- // Update LED configuration
- const providerRadio = document.getElementById(`ledProvider${providerToCamelCase(ledConfigData.provider)}`);
- if (providerRadio) {
- providerRadio.checked = true;
- } else {
- document.getElementById('ledProviderNone').checked = true;
- }
- if (ledConfigData.wled_ip) {
- const wledIpInput = document.getElementById('wledIpInput');
- if (wledIpInput) wledIpInput.value = ledConfigData.wled_ip;
- }
- // Load DW LED settings
- if (ledConfigData.dw_led_num_leds) {
- const numLedsInput = document.getElementById('dwLedNumLeds');
- if (numLedsInput) numLedsInput.value = ledConfigData.dw_led_num_leds;
- }
- if (ledConfigData.dw_led_gpio_pin) {
- const gpioPinInput = document.getElementById('dwLedGpioPin');
- if (gpioPinInput) gpioPinInput.value = ledConfigData.dw_led_gpio_pin;
- }
- if (ledConfigData.dw_led_pixel_order) {
- const pixelOrderInput = document.getElementById('dwLedPixelOrder');
- if (pixelOrderInput) pixelOrderInput.value = ledConfigData.dw_led_pixel_order;
- }
- updateLedProviderUI()
-
- // Update version display
- const currentVersionText = document.getElementById('currentVersionText');
- const latestVersionText = document.getElementById('latestVersionText');
- const updateButton = document.getElementById('updateSoftware');
- const updateIcon = document.getElementById('updateIcon');
- const updateText = document.getElementById('updateText');
-
- if (currentVersionText) {
- currentVersionText.textContent = updateData.current;
- }
-
- if (latestVersionText) {
- if (updateData.error) {
- latestVersionText.textContent = 'Error checking updates';
- latestVersionText.className = 'text-red-500 text-sm font-normal leading-normal';
- } else {
- latestVersionText.textContent = updateData.latest;
- latestVersionText.className = 'text-slate-500 text-sm font-normal leading-normal';
- }
- }
-
- // Update button state
- if (updateButton && updateIcon && updateText) {
- if (updateData.update_available) {
- updateButton.disabled = false;
- updateButton.className = 'flex items-center justify-center gap-1.5 min-w-[84px] cursor-pointer rounded-lg h-9 px-3 bg-emerald-500 hover:bg-emerald-600 text-white text-xs font-medium leading-normal tracking-[0.015em] transition-colors';
- updateIcon.textContent = 'download';
- updateText.textContent = 'Update';
- } else {
- updateButton.disabled = true;
- updateButton.className = 'flex items-center justify-center gap-1.5 min-w-[84px] cursor-pointer rounded-lg h-9 px-3 bg-gray-400 text-white text-xs font-medium leading-normal tracking-[0.015em] transition-colors disabled:opacity-50 disabled:cursor-not-allowed';
- updateIcon.textContent = 'check';
- updateText.textContent = 'Up to date';
- }
- }
-
- // Update port selection
- const portSelect = document.getElementById('portSelect');
- if (portSelect) {
- // Clear existing options except the first one
- while (portSelect.options.length > 1) {
- portSelect.remove(1);
- }
- // Add new options
- ports.forEach(port => {
- const option = document.createElement('option');
- option.value = port;
- option.textContent = port;
- portSelect.appendChild(option);
- });
- // If there's exactly one port available, select it
- if (ports.length === 1) {
- portSelect.value = ports[0];
- }
- }
- // Update preferred port selection
- const preferredPortSelect = document.getElementById('preferredPortSelect');
- const currentPreferredPort = document.getElementById('currentPreferredPort');
- const preferredPortDisplay = document.getElementById('preferredPortDisplay');
- if (preferredPortSelect) {
- // Clear existing options except the first one (no preference)
- while (preferredPortSelect.options.length > 1) {
- preferredPortSelect.remove(1);
- }
- // Add all available ports
- ports.forEach(port => {
- const option = document.createElement('option');
- option.value = port;
- option.textContent = port;
- preferredPortSelect.appendChild(option);
- });
- // Set the current preferred port value
- if (preferredPortData && preferredPortData.preferred_port) {
- // Check if the preferred port is in the available ports
- const isAvailable = ports.includes(preferredPortData.preferred_port);
- if (isAvailable) {
- preferredPortSelect.value = preferredPortData.preferred_port;
- } else {
- // Add the preferred port as an option (it might not be currently available)
- const option = document.createElement('option');
- option.value = preferredPortData.preferred_port;
- option.textContent = `${preferredPortData.preferred_port} (not currently available)`;
- preferredPortSelect.appendChild(option);
- preferredPortSelect.value = preferredPortData.preferred_port;
- }
- // Show the current preferred port indicator
- if (currentPreferredPort && preferredPortDisplay) {
- preferredPortDisplay.textContent = `Currently set to: ${preferredPortData.preferred_port}`;
- currentPreferredPort.classList.remove('hidden');
- }
- }
- }
- // Initialize autocomplete for clear patterns
- const clearFromInInput = document.getElementById('customClearFromInInput');
- const clearFromOutInput = document.getElementById('customClearFromOutInput');
-
- if (clearFromInInput && clearFromOutInput && patterns && Array.isArray(patterns)) {
- // Store patterns globally for autocomplete
- window.availablePatterns = patterns;
-
- // Set current values if they exist
- if (clearPatterns && clearPatterns.custom_clear_from_in) {
- clearFromInInput.value = clearPatterns.custom_clear_from_in;
- }
- if (clearPatterns && clearPatterns.custom_clear_from_out) {
- clearFromOutInput.value = clearPatterns.custom_clear_from_out;
- }
-
- // Initialize autocomplete for both inputs
- initializeAutocomplete('customClearFromInInput', 'clearFromInSuggestions', 'clearFromInClear', patterns);
- initializeAutocomplete('customClearFromOutInput', 'clearFromOutSuggestions', 'clearFromOutClear', patterns);
-
- console.log('Autocomplete initialized with', patterns.length, 'patterns');
- }
-
- // Set clear pattern speed
- const clearPatternSpeedInput = document.getElementById('clearPatternSpeedInput');
- const effectiveClearSpeed = document.getElementById('effectiveClearSpeed');
- if (clearPatternSpeedInput && clearSpeedData) {
- // Only set value if clear_pattern_speed is not null
- if (clearSpeedData.clear_pattern_speed !== null && clearSpeedData.clear_pattern_speed !== undefined) {
- clearPatternSpeedInput.value = clearSpeedData.clear_pattern_speed;
- if (effectiveClearSpeed) {
- effectiveClearSpeed.textContent = `Current: ${clearSpeedData.clear_pattern_speed} steps/min`;
- }
- } else {
- // Leave empty to show placeholder for default
- clearPatternSpeedInput.value = '';
- if (effectiveClearSpeed && clearSpeedData.effective_speed) {
- effectiveClearSpeed.textContent = `Using default pattern speed: ${clearSpeedData.effective_speed} steps/min`;
- }
- }
- }
-
- // Update app name
- const appNameInput = document.getElementById('appNameInput');
- if (appNameInput && appNameData.app_name) {
- appNameInput.value = appNameData.app_name;
- }
- // Store Still Sands data for later initialization
- window.initialStillSandsData = scheduledPauseData;
- }).catch(error => {
- logMessage(`Error initializing settings page: ${error.message}`, LOG_TYPE.ERROR);
- });
- // Set up event listeners
- setupEventListeners();
- });
- // Setup event listeners
- function setupEventListeners() {
- // Save App Name
- const saveAppNameButton = document.getElementById('saveAppName');
- const appNameInput = document.getElementById('appNameInput');
- if (saveAppNameButton && appNameInput) {
- saveAppNameButton.addEventListener('click', async () => {
- const appName = appNameInput.value.trim() || 'Dune Weaver';
- try {
- const response = await fetch('/api/settings', {
- method: 'PATCH',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ app: { name: appName } })
- });
- if (response.ok) {
- await response.json();
- showStatusMessage('Application name updated successfully. Refresh the page to see changes.', 'success');
- // Update the page title and header immediately
- document.title = `Settings - ${appName}`;
- const headerTitle = document.querySelector('h1.text-gray-800');
- if (headerTitle) {
- // Update just the text content, preserving the connection status dot
- const textNode = headerTitle.childNodes[0];
- if (textNode && textNode.nodeType === Node.TEXT_NODE) {
- textNode.textContent = data.app_name;
- }
- }
- } else {
- throw new Error('Failed to save application name');
- }
- } catch (error) {
- showStatusMessage(`Failed to save application name: ${error.message}`, 'error');
- }
- });
-
- // Handle Enter key in app name input
- appNameInput.addEventListener('keypress', (e) => {
- if (e.key === 'Enter') {
- saveAppNameButton.click();
- }
- });
- }
-
- // LED provider selection change handlers
- const ledProviderRadios = document.querySelectorAll('input[name="ledProvider"]');
- ledProviderRadios.forEach(radio => {
- radio.addEventListener('change', updateLedProviderUI);
- });
- // Save LED configuration
- const saveLedConfig = document.getElementById('saveLedConfig');
- if (saveLedConfig) {
- saveLedConfig.addEventListener('click', async () => {
- const provider = document.querySelector('input[name="ledProvider"]:checked')?.value || 'none';
- let requestBody = { provider };
- if (provider === 'wled') {
- const wledIp = document.getElementById('wledIpInput')?.value;
- if (!wledIp) {
- showStatusMessage('Please enter a WLED IP address', 'error');
- return;
- }
- requestBody.ip_address = wledIp;
- } else if (provider === 'dw_leds') {
- const numLeds = parseInt(document.getElementById('dwLedNumLeds')?.value) || 60;
- const gpioPin = parseInt(document.getElementById('dwLedGpioPin')?.value) || 12;
- const pixelOrder = document.getElementById('dwLedPixelOrder')?.value || 'GRB';
- requestBody.num_leds = numLeds;
- requestBody.gpio_pin = gpioPin;
- requestBody.pixel_order = pixelOrder;
- }
- try {
- const response = await fetch('/set_led_config', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(requestBody)
- });
- if (response.ok) {
- const data = await response.json();
- if (provider === 'wled' && data.wled_ip) {
- localStorage.setItem('wled_ip', data.wled_ip);
- showStatusMessage('WLED configured successfully', 'success');
- } else if (provider === 'dw_leds') {
- // Check if there's a warning (hardware not available but settings saved)
- if (data.warning) {
- showStatusMessage(
- `Settings saved for testing. Hardware issue: ${data.warning}`,
- 'warning'
- );
- } else {
- showStatusMessage(
- `DW LEDs configured: ${data.dw_led_num_leds} LEDs on GPIO${data.dw_led_gpio_pin}`,
- 'success'
- );
- }
- } else if (provider === 'none') {
- localStorage.removeItem('wled_ip');
- showStatusMessage('LED controller disabled', 'success');
- }
- } else {
- // Extract error detail from response
- const errorData = await response.json().catch(() => ({}));
- const errorMessage = errorData.detail || 'Failed to save LED configuration';
- showStatusMessage(errorMessage, 'error');
- }
- } catch (error) {
- showStatusMessage(`Failed to save LED configuration: ${error.message}`, 'error');
- }
- });
- }
- // Update software
- const updateSoftware = document.getElementById('updateSoftware');
- if (updateSoftware) {
- updateSoftware.addEventListener('click', async () => {
- if (updateSoftware.disabled) {
- return;
- }
-
- try {
- const response = await fetch('/api/update', {
- method: 'POST'
- });
- const data = await response.json();
-
- if (data.success) {
- showStatusMessage('Software update started successfully', 'success');
- } else if (data.manual_update_url) {
- // Show modal with manual update instructions, but use wiki link
- const wikiData = {
- ...data,
- manual_update_url: 'https://github.com/tuanchris/dune-weaver/wiki/Updating-software'
- };
- showUpdateInstructionsModal(wikiData);
- } else {
- showStatusMessage(data.message || 'No updates available', 'info');
- }
- } catch (error) {
- logMessage(`Error updating software: ${error.message}`, LOG_TYPE.ERROR);
- showStatusMessage('Failed to check for updates', 'error');
- }
- });
- }
- // Connect button
- const connectButton = document.getElementById('connectButton');
- if (connectButton) {
- connectButton.addEventListener('click', async () => {
- const portSelect = document.getElementById('portSelect');
- if (!portSelect || !portSelect.value) {
- logMessage('Please select a port first', LOG_TYPE.WARNING);
- return;
- }
- try {
- const response = await fetch('/connect', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ port: portSelect.value })
- });
-
- if (response.ok) {
- logMessage('Connected successfully', LOG_TYPE.SUCCESS);
- await updateSerialStatus(true); // Force update after connecting
- } else {
- throw new Error('Failed to connect');
- }
- } catch (error) {
- logMessage(`Error connecting to device: ${error.message}`, LOG_TYPE.ERROR);
- }
- });
- }
- // Disconnect button
- const disconnectButton = document.getElementById('disconnectButton');
- if (disconnectButton) {
- disconnectButton.addEventListener('click', async () => {
- try {
- const response = await fetch('/disconnect', {
- method: 'POST'
- });
- if (response.ok) {
- logMessage('Device disconnected successfully', LOG_TYPE.SUCCESS);
- await updateSerialStatus(true); // Force update after disconnecting
- } else {
- throw new Error('Failed to disconnect device');
- }
- } catch (error) {
- logMessage(`Error disconnecting device: ${error.message}`, LOG_TYPE.ERROR);
- }
- });
- }
- // Save preferred port button
- const savePreferredPortButton = document.getElementById('savePreferredPort');
- if (savePreferredPortButton) {
- savePreferredPortButton.addEventListener('click', savePreferredPort);
- }
- // Save custom clear patterns button
- const saveClearPatterns = document.getElementById('saveClearPatterns');
- if (saveClearPatterns) {
- saveClearPatterns.addEventListener('click', async () => {
- const clearFromInInput = document.getElementById('customClearFromInInput');
- const clearFromOutInput = document.getElementById('customClearFromOutInput');
-
- if (!clearFromInInput || !clearFromOutInput) {
- return;
- }
-
- // Validate that the entered patterns exist (if not empty)
- const inValue = clearFromInInput.value.trim();
- const outValue = clearFromOutInput.value.trim();
-
- if (inValue && window.availablePatterns && !window.availablePatterns.includes(inValue)) {
- showStatusMessage(`Pattern not found: ${inValue}`, 'error');
- return;
- }
-
- if (outValue && window.availablePatterns && !window.availablePatterns.includes(outValue)) {
- showStatusMessage(`Pattern not found: ${outValue}`, 'error');
- return;
- }
-
- try {
- const response = await fetch('/api/settings', {
- method: 'PATCH',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- patterns: {
- custom_clear_from_in: inValue || null,
- custom_clear_from_out: outValue || null
- }
- })
- });
- if (response.ok) {
- showStatusMessage('Clear patterns saved successfully', 'success');
- } else {
- const error = await response.json();
- throw new Error(error.detail || 'Failed to save clear patterns');
- }
- } catch (error) {
- showStatusMessage(`Failed to save clear patterns: ${error.message}`, 'error');
- }
- });
- }
-
- // Logo upload functionality
- const logoFileInput = document.getElementById('logoFileInput');
- const resetLogoBtn = document.getElementById('resetLogoBtn');
- const logoPreview = document.getElementById('logoPreview');
- const logoUploadStatus = document.getElementById('logoUploadStatus');
- if (logoFileInput) {
- logoFileInput.addEventListener('change', async (event) => {
- const file = event.target.files[0];
- if (!file) return;
- // Show uploading status
- if (logoUploadStatus) {
- logoUploadStatus.textContent = 'Uploading...';
- logoUploadStatus.className = 'text-xs text-slate-500';
- }
- const formData = new FormData();
- formData.append('file', file);
- try {
- const response = await fetch('/api/upload-logo', {
- method: 'POST',
- body: formData
- });
- if (response.ok) {
- const data = await response.json();
- // Update preview image with cache-busting query param
- if (logoPreview) {
- logoPreview.src = data.url + '?t=' + Date.now();
- }
- // Show reset button
- if (resetLogoBtn) {
- resetLogoBtn.classList.remove('hidden');
- }
- // Show success status
- if (logoUploadStatus) {
- logoUploadStatus.textContent = 'Logo uploaded successfully!';
- logoUploadStatus.className = 'text-xs text-green-600';
- setTimeout(() => {
- logoUploadStatus.textContent = '';
- }, 3000);
- }
- showStatusMessage('Logo uploaded successfully. Refresh the page to see all changes.', 'success');
- } else {
- const error = await response.json();
- throw new Error(error.detail || 'Failed to upload logo');
- }
- } catch (error) {
- if (logoUploadStatus) {
- logoUploadStatus.textContent = `Error: ${error.message}`;
- logoUploadStatus.className = 'text-xs text-red-600';
- }
- showStatusMessage(`Failed to upload logo: ${error.message}`, 'error');
- }
- // Reset file input so the same file can be re-selected
- logoFileInput.value = '';
- });
- }
- if (resetLogoBtn) {
- resetLogoBtn.addEventListener('click', async () => {
- try {
- const response = await fetch('/api/custom-logo', {
- method: 'DELETE'
- });
- if (response.ok) {
- // Reset preview to default logo
- if (logoPreview) {
- logoPreview.src = '/static/apple-touch-icon.png?t=' + Date.now();
- }
- // Hide reset button
- resetLogoBtn.classList.add('hidden');
- showStatusMessage('Logo reset to default. Refresh the page to see all changes.', 'success');
- } else {
- const error = await response.json();
- throw new Error(error.detail || 'Failed to reset logo');
- }
- } catch (error) {
- showStatusMessage(`Failed to reset logo: ${error.message}`, 'error');
- }
- });
- }
- // Save clear pattern speed button
- const saveClearSpeed = document.getElementById('saveClearSpeed');
- if (saveClearSpeed) {
- saveClearSpeed.addEventListener('click', async () => {
- const clearPatternSpeedInput = document.getElementById('clearPatternSpeedInput');
-
- if (!clearPatternSpeedInput) {
- return;
- }
-
- let speed;
- if (clearPatternSpeedInput.value === '' || clearPatternSpeedInput.value === null) {
- // Empty value means use default (None)
- speed = null;
- } else {
- speed = parseInt(clearPatternSpeedInput.value);
-
- // Validate speed only if it's not null
- if (isNaN(speed) || speed < 50 || speed > 2000) {
- showStatusMessage('Clear pattern speed must be between 50 and 2000, or leave empty for default', 'error');
- return;
- }
- }
-
- try {
- const response = await fetch('/api/settings', {
- method: 'PATCH',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ patterns: { clear_pattern_speed: speed } })
- });
- if (response.ok) {
- await response.json();
- if (speed === null) {
- showStatusMessage('Clear pattern speed set to default', 'success');
- } else {
- showStatusMessage(`Clear pattern speed set to ${speed} steps/min`, 'success');
- }
-
- // Update the effective speed display
- const effectiveClearSpeed = document.getElementById('effectiveClearSpeed');
- if (effectiveClearSpeed) {
- if (speed === null) {
- effectiveClearSpeed.textContent = `Using default pattern speed: ${data.effective_speed} steps/min`;
- } else {
- effectiveClearSpeed.textContent = `Current: ${speed} steps/min`;
- }
- }
- } else {
- const error = await response.json();
- throw new Error(error.detail || 'Failed to save clear pattern speed');
- }
- } catch (error) {
- showStatusMessage(`Failed to save clear pattern speed: ${error.message}`, 'error');
- }
- });
- }
- }
- // Button click handlers
- document.addEventListener('DOMContentLoaded', function() {
- // Home button
- const homeButton = document.getElementById('homeButton');
- if (homeButton) {
- homeButton.addEventListener('click', async () => {
- try {
- const response = await fetch('/send_home', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- const data = await response.json();
- if (data.success) {
- updateStatus('Moving to home position...');
- }
- } catch (error) {
- console.error('Error sending home command:', error);
- updateStatus('Error: Failed to move to home position');
- }
- });
- }
- // Stop button
- const stopButton = document.getElementById('stopButton');
- if (stopButton) {
- stopButton.addEventListener('click', async () => {
- try {
- const response = await fetch('/stop_execution', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- const data = await response.json();
- if (data.success) {
- updateStatus('Execution stopped');
- }
- } catch (error) {
- console.error('Error stopping execution:', error);
- updateStatus('Error: Failed to stop execution');
- }
- });
- }
- // Move to Center button
- const centerButton = document.getElementById('centerButton');
- if (centerButton) {
- centerButton.addEventListener('click', async () => {
- try {
- const response = await fetch('/move_to_center', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- const data = await response.json();
- if (data.success) {
- updateStatus('Moving to center position...');
- }
- } catch (error) {
- console.error('Error moving to center:', error);
- updateStatus('Error: Failed to move to center');
- }
- });
- }
- // Move to Perimeter button
- const perimeterButton = document.getElementById('perimeterButton');
- if (perimeterButton) {
- perimeterButton.addEventListener('click', async () => {
- try {
- const response = await fetch('/move_to_perimeter', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- const data = await response.json();
- if (data.success) {
- updateStatus('Moving to perimeter position...');
- }
- } catch (error) {
- console.error('Error moving to perimeter:', error);
- updateStatus('Error: Failed to move to perimeter');
- }
- });
- }
- });
- // Function to update status
- function updateStatus(message) {
- const statusElement = document.querySelector('.text-slate-800.text-base.font-medium.leading-normal');
- if (statusElement) {
- statusElement.textContent = message;
- // Reset status after 3 seconds if it's a temporary message
- if (message.includes('Moving') || message.includes('Execution')) {
- setTimeout(() => {
- statusElement.textContent = 'Status';
- }, 3000);
- }
- }
- }
- // Function to show status messages (using existing base.js showStatusMessage if available)
- function showStatusMessage(message, type) {
- if (typeof window.showStatusMessage === 'function') {
- window.showStatusMessage(message, type);
- } else {
- // Fallback to console logging
- console.log(`[${type}] ${message}`);
- }
- }
- // Function to show update instructions modal
- function showUpdateInstructionsModal(data) {
- // Create modal HTML
- const modal = document.createElement('div');
- modal.id = 'updateInstructionsModal';
- modal.className = 'fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4';
- modal.innerHTML = `
- <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-md">
- <div class="p-6">
- <div class="text-center mb-4">
- <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-2">Manual Update Required</h2>
- <p class="text-gray-600 dark:text-gray-400 text-sm">
- ${data.message}
- </p>
- </div>
-
- <div class="text-gray-700 dark:text-gray-300 text-sm mb-6">
- <p class="mb-3">${data.instructions}</p>
- </div>
-
- <div class="flex gap-3 justify-center">
- <button id="openGitHubRelease" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors">
- View Update Instructions
- </button>
- <button id="closeUpdateModal" class="px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white rounded-lg transition-colors">
- Close
- </button>
- </div>
- </div>
- </div>
- `;
-
- document.body.appendChild(modal);
-
- // Add event listeners
- const openGitHubButton = modal.querySelector('#openGitHubRelease');
- const closeButton = modal.querySelector('#closeUpdateModal');
-
- openGitHubButton.addEventListener('click', () => {
- window.open(data.manual_update_url, '_blank');
- document.body.removeChild(modal);
- });
-
- closeButton.addEventListener('click', () => {
- document.body.removeChild(modal);
- });
-
- // Close on outside click
- modal.addEventListener('click', (e) => {
- if (e.target === modal) {
- document.body.removeChild(modal);
- }
- });
- }
- // Autocomplete functionality
- function initializeAutocomplete(inputId, suggestionsId, clearButtonId, patterns) {
- const input = document.getElementById(inputId);
- const suggestionsDiv = document.getElementById(suggestionsId);
- const clearButton = document.getElementById(clearButtonId);
- let selectedIndex = -1;
-
- if (!input || !suggestionsDiv) return;
-
- // Function to update clear button visibility
- function updateClearButton() {
- if (clearButton) {
- if (input.value.trim()) {
- clearButton.classList.remove('hidden');
- } else {
- clearButton.classList.add('hidden');
- }
- }
- }
-
- // Format pattern name for display
- function formatPatternName(pattern) {
- return pattern.replace('.thr', '').replace(/_/g, ' ');
- }
-
- // Filter patterns based on input
- function filterPatterns(searchTerm) {
- if (!searchTerm) return patterns.slice(0, 20); // Show first 20 when empty
-
- const term = searchTerm.toLowerCase();
- return patterns.filter(pattern => {
- const name = pattern.toLowerCase();
- return name.includes(term);
- }).sort((a, b) => {
- // Prioritize patterns that start with the search term
- const aStarts = a.toLowerCase().startsWith(term);
- const bStarts = b.toLowerCase().startsWith(term);
- if (aStarts && !bStarts) return -1;
- if (!aStarts && bStarts) return 1;
- return a.localeCompare(b);
- }).slice(0, 20); // Limit to 20 results
- }
-
- // Highlight matching text
- function highlightMatch(text, searchTerm) {
- if (!searchTerm) return text;
-
- const regex = new RegExp(`(${searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
- return text.replace(regex, '<mark>$1</mark>');
- }
-
- // Show suggestions
- function showSuggestions(searchTerm) {
- const filtered = filterPatterns(searchTerm);
-
- if (filtered.length === 0 && searchTerm) {
- suggestionsDiv.innerHTML = '<div class="suggestion-item" style="cursor: default; color: #9ca3af;">No patterns found</div>';
- suggestionsDiv.classList.remove('hidden');
- return;
- }
-
- suggestionsDiv.innerHTML = filtered.map((pattern, index) => {
- const displayName = formatPatternName(pattern);
- const highlighted = highlightMatch(displayName, searchTerm);
- return `<div class="suggestion-item" data-value="${pattern}" data-index="${index}">${highlighted}</div>`;
- }).join('');
-
- suggestionsDiv.classList.remove('hidden');
- selectedIndex = -1;
- }
-
- // Hide suggestions
- function hideSuggestions() {
- setTimeout(() => {
- suggestionsDiv.classList.add('hidden');
- selectedIndex = -1;
- }, 200);
- }
-
- // Select suggestion
- function selectSuggestion(value) {
- input.value = value;
- hideSuggestions();
- updateClearButton();
- }
-
- // Handle keyboard navigation
- function handleKeyboard(e) {
- const items = suggestionsDiv.querySelectorAll('.suggestion-item[data-value]');
-
- if (e.key === 'ArrowDown') {
- e.preventDefault();
- selectedIndex = Math.min(selectedIndex + 1, items.length - 1);
- updateSelection(items);
- } else if (e.key === 'ArrowUp') {
- e.preventDefault();
- selectedIndex = Math.max(selectedIndex - 1, -1);
- updateSelection(items);
- } else if (e.key === 'Enter') {
- e.preventDefault();
- if (selectedIndex >= 0 && items[selectedIndex]) {
- selectSuggestion(items[selectedIndex].dataset.value);
- } else if (items.length === 1) {
- selectSuggestion(items[0].dataset.value);
- }
- } else if (e.key === 'Escape') {
- hideSuggestions();
- }
- }
-
- // Update visual selection
- function updateSelection(items) {
- items.forEach((item, index) => {
- if (index === selectedIndex) {
- item.classList.add('selected');
- item.scrollIntoView({ block: 'nearest' });
- } else {
- item.classList.remove('selected');
- }
- });
- }
-
- // Event listeners
- input.addEventListener('input', (e) => {
- const value = e.target.value.trim();
- updateClearButton();
- if (value.length > 0 || e.target === document.activeElement) {
- showSuggestions(value);
- } else {
- hideSuggestions();
- }
- });
-
- input.addEventListener('focus', () => {
- const value = input.value.trim();
- showSuggestions(value);
- });
-
- input.addEventListener('blur', hideSuggestions);
-
- input.addEventListener('keydown', handleKeyboard);
-
- // Click handler for suggestions
- suggestionsDiv.addEventListener('click', (e) => {
- const item = e.target.closest('.suggestion-item[data-value]');
- if (item) {
- selectSuggestion(item.dataset.value);
- }
- });
-
- // Mouse hover handler
- suggestionsDiv.addEventListener('mouseover', (e) => {
- const item = e.target.closest('.suggestion-item[data-value]');
- if (item) {
- selectedIndex = parseInt(item.dataset.index);
- const items = suggestionsDiv.querySelectorAll('.suggestion-item[data-value]');
- updateSelection(items);
- }
- });
-
- // Clear button handler
- if (clearButton) {
- clearButton.addEventListener('click', () => {
- input.value = '';
- updateClearButton();
- hideSuggestions();
- input.focus();
- });
- }
-
- // Initialize clear button visibility
- updateClearButton();
- }
- // auto_play Mode Functions
- async function initializeauto_playMode() {
- const auto_playToggle = document.getElementById('auto_playModeToggle');
- const auto_playSettings = document.getElementById('auto_playSettings');
- const auto_playPlaylistSelect = document.getElementById('auto_playPlaylistSelect');
- const auto_playRunModeSelect = document.getElementById('auto_playRunModeSelect');
- const auto_playPauseTimeInput = document.getElementById('auto_playPauseTimeInput');
- const auto_playClearPatternSelect = document.getElementById('auto_playClearPatternSelect');
- const auto_playShuffleToggle = document.getElementById('auto_playShuffleToggle');
-
- // Load current auto_play settings
- try {
- const response = await fetch('/api/auto_play-mode');
- const data = await response.json();
-
- auto_playToggle.checked = data.enabled;
- if (data.enabled) {
- auto_playSettings.style.display = 'block';
- }
-
- // Set current values
- auto_playRunModeSelect.value = data.run_mode || 'loop';
- auto_playPauseTimeInput.value = data.pause_time || 5.0;
- auto_playClearPatternSelect.value = data.clear_pattern || 'adaptive';
- auto_playShuffleToggle.checked = data.shuffle || false;
-
- // Load playlists for selection
- const playlistsResponse = await fetch('/list_all_playlists');
- const playlists = await playlistsResponse.json();
-
- // Clear and populate playlist select
- auto_playPlaylistSelect.innerHTML = '<option value="">Select a playlist...</option>';
- playlists.forEach(playlist => {
- const option = document.createElement('option');
- option.value = playlist;
- option.textContent = playlist;
- if (playlist === data.playlist) {
- option.selected = true;
- }
- auto_playPlaylistSelect.appendChild(option);
- });
- } catch (error) {
- logMessage(`Error loading auto_play settings: ${error.message}`, LOG_TYPE.ERROR);
- }
-
- // Get save button
- const saveAutoPlayButton = document.getElementById('saveAutoPlaySettings');
- // Function to save settings
- async function saveSettings(showFeedback = false) {
- const originalButtonHTML = saveAutoPlayButton ? saveAutoPlayButton.innerHTML : '';
- if (showFeedback && saveAutoPlayButton) {
- saveAutoPlayButton.disabled = true;
- saveAutoPlayButton.innerHTML = '<span class="material-icons text-lg animate-spin">refresh</span><span class="truncate">Saving...</span>';
- }
- try {
- const response = await fetch('/api/auto_play-mode', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- enabled: auto_playToggle.checked,
- playlist: auto_playPlaylistSelect.value || null,
- run_mode: auto_playRunModeSelect.value,
- pause_time: parseFloat(auto_playPauseTimeInput.value) || 0,
- clear_pattern: auto_playClearPatternSelect.value,
- shuffle: auto_playShuffleToggle.checked
- })
- });
- if (!response.ok) {
- throw new Error('Failed to save settings');
- }
- if (showFeedback && saveAutoPlayButton) {
- saveAutoPlayButton.innerHTML = '<span class="material-icons text-lg">check</span><span class="truncate">Saved!</span>';
- showStatusMessage('Auto-play settings saved successfully', 'success');
- setTimeout(() => {
- saveAutoPlayButton.innerHTML = originalButtonHTML;
- saveAutoPlayButton.disabled = false;
- }, 2000);
- }
- } catch (error) {
- logMessage(`Error saving auto_play settings: ${error.message}`, LOG_TYPE.ERROR);
- if (showFeedback && saveAutoPlayButton) {
- showStatusMessage(`Failed to save settings: ${error.message}`, 'error');
- saveAutoPlayButton.innerHTML = originalButtonHTML;
- saveAutoPlayButton.disabled = false;
- }
- }
- }
- // Toggle auto_play settings visibility and save
- auto_playToggle.addEventListener('change', async () => {
- auto_playSettings.style.display = auto_playToggle.checked ? 'block' : 'none';
- await saveSettings(false); // Auto-save toggle state without full feedback
- const statusText = auto_playToggle.checked ? 'enabled' : 'disabled';
- showStatusMessage(`Auto-play ${statusText}`, 'success');
- });
- // Save button click handler
- if (saveAutoPlayButton) {
- saveAutoPlayButton.addEventListener('click', () => saveSettings(true));
- }
- }
- // Initialize auto_play mode when DOM is ready
- document.addEventListener('DOMContentLoaded', function() {
- initializeauto_playMode();
- initializeStillSandsMode();
- initializeHomingConfig();
- });
- // Still Sands Mode Functions
- async function initializeStillSandsMode() {
- logMessage('Initializing Still Sands mode', LOG_TYPE.INFO);
- const stillSandsToggle = document.getElementById('scheduledPauseToggle');
- const stillSandsSettings = document.getElementById('scheduledPauseSettings');
- const addTimeSlotButton = document.getElementById('addTimeSlotButton');
- const saveStillSandsButton = document.getElementById('savePauseSettings');
- const timeSlotsContainer = document.getElementById('timeSlotsContainer');
- const wledControlToggle = document.getElementById('stillSandsWledControl');
- const finishPatternToggle = document.getElementById('stillSandsFinishPattern');
- const timezoneSelect = document.getElementById('stillSandsTimezone');
- // Check if elements exist
- if (!stillSandsToggle || !stillSandsSettings || !addTimeSlotButton || !saveStillSandsButton || !timeSlotsContainer) {
- logMessage('Still Sands elements not found, skipping initialization', LOG_TYPE.WARNING);
- logMessage(`Found elements: toggle=${!!stillSandsToggle}, settings=${!!stillSandsSettings}, addBtn=${!!addTimeSlotButton}, saveBtn=${!!saveStillSandsButton}, container=${!!timeSlotsContainer}`, LOG_TYPE.WARNING);
- return;
- }
- logMessage('All Still Sands elements found successfully', LOG_TYPE.INFO);
- // Track time slots
- let timeSlots = [];
- let slotIdCounter = 0;
- // Load current Still Sands settings from initial data
- try {
- // Use the data loaded during page initialization, fallback to API if not available
- let data;
- if (window.initialStillSandsData) {
- data = window.initialStillSandsData;
- // Clear the global variable after use
- delete window.initialStillSandsData;
- } else {
- // Fallback to API call if initial data not available
- const response = await fetch('/api/scheduled-pause');
- data = await response.json();
- }
- stillSandsToggle.checked = data.enabled || false;
- if (data.enabled) {
- stillSandsSettings.style.display = 'block';
- }
- // Load WLED control setting
- if (wledControlToggle) {
- wledControlToggle.checked = data.control_wled || false;
- }
- // Load finish pattern setting
- if (finishPatternToggle) {
- finishPatternToggle.checked = data.finish_pattern || false;
- }
- // Load timezone setting
- if (timezoneSelect) {
- timezoneSelect.value = data.timezone || '';
- }
- // Load existing time slots
- timeSlots = data.time_slots || [];
- // Assign IDs to loaded slots BEFORE rendering
- if (timeSlots.length > 0) {
- slotIdCounter = 0;
- timeSlots.forEach(slot => {
- slot.id = ++slotIdCounter;
- });
- }
- renderTimeSlots();
- } catch (error) {
- logMessage(`Error loading Still Sands settings: ${error.message}`, LOG_TYPE.ERROR);
- // Initialize with empty settings if load fails
- timeSlots = [];
- renderTimeSlots();
- }
- // Function to validate time format (HH:MM)
- function isValidTime(timeString) {
- const timeRegex = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/;
- return timeRegex.test(timeString);
- }
- // Function to create a new time slot element
- function createTimeSlotElement(slot) {
- const slotDiv = document.createElement('div');
- slotDiv.className = 'time-slot-item';
- slotDiv.dataset.slotId = slot.id;
- slotDiv.innerHTML = `
- <div class="flex items-center gap-3">
- <div class="flex-1 grid grid-cols-1 md:grid-cols-2 gap-3">
- <div class="flex flex-col gap-1">
- <label class="text-slate-700 dark:text-slate-300 text-xs font-medium">Start Time</label>
- <input
- type="time"
- class="start-time form-input resize-none overflow-hidden rounded-lg text-slate-900 focus:outline-0 focus:ring-2 focus:ring-sky-500 border border-slate-300 bg-white focus:border-sky-500 h-9 px-3 text-sm font-normal leading-normal transition-colors"
- value="${slot.start_time || ''}"
- required
- />
- </div>
- <div class="flex flex-col gap-1">
- <label class="text-slate-700 dark:text-slate-300 text-xs font-medium">End Time</label>
- <input
- type="time"
- class="end-time form-input resize-none overflow-hidden rounded-lg text-slate-900 focus:outline-0 focus:ring-2 focus:ring-sky-500 border border-slate-300 bg-white focus:border-sky-500 h-9 px-3 text-sm font-normal leading-normal transition-colors"
- value="${slot.end_time || ''}"
- required
- />
- </div>
- </div>
- <div class="flex flex-col gap-1">
- <label class="text-slate-700 dark:text-slate-300 text-xs font-medium">Days</label>
- <select class="days-select form-select resize-none overflow-hidden rounded-lg text-slate-900 focus:outline-0 focus:ring-2 focus:ring-sky-500 border border-slate-300 bg-white focus:border-sky-500 h-9 px-3 text-sm font-normal transition-colors">
- <option value="daily" ${slot.days === 'daily' ? 'selected' : ''}>Daily</option>
- <option value="weekdays" ${slot.days === 'weekdays' ? 'selected' : ''}>Weekdays</option>
- <option value="weekends" ${slot.days === 'weekends' ? 'selected' : ''}>Weekends</option>
- <option value="custom" ${slot.days === 'custom' ? 'selected' : ''}>Custom</option>
- </select>
- </div>
- <button
- type="button"
- class="remove-slot-btn flex items-center justify-center w-9 h-9 text-red-600 hover:text-red-700 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-lg transition-colors"
- title="Remove time slot"
- >
- <span class="material-icons text-base">delete</span>
- </button>
- </div>
- <div class="custom-days-container mt-2" style="display: ${slot.days === 'custom' ? 'block' : 'none'};">
- <label class="text-slate-700 dark:text-slate-300 text-xs font-medium mb-1 block">Select Days</label>
- <div class="flex flex-wrap gap-2">
- ${['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'].map(day => `
- <label class="flex items-center gap-1 text-xs">
- <input
- type="checkbox"
- name="custom-days-${slot.id}"
- value="${day}"
- ${slot.custom_days && slot.custom_days.includes(day) ? 'checked' : ''}
- class="rounded border-slate-300 text-sky-600 focus:ring-sky-500"
- />
- <span class="text-slate-700 dark:text-slate-300 capitalize">${day.substring(0, 3)}</span>
- </label>
- `).join('')}
- </div>
- </div>
- `;
- // Add event listeners for this slot
- const startTimeInput = slotDiv.querySelector('.start-time');
- const endTimeInput = slotDiv.querySelector('.end-time');
- const daysSelect = slotDiv.querySelector('.days-select');
- const customDaysContainer = slotDiv.querySelector('.custom-days-container');
- const removeButton = slotDiv.querySelector('.remove-slot-btn');
- // Show/hide custom days based on selection
- daysSelect.addEventListener('change', () => {
- customDaysContainer.style.display = daysSelect.value === 'custom' ? 'block' : 'none';
- updateTimeSlot(slot.id);
- });
- // Update slot data when inputs change
- startTimeInput.addEventListener('change', () => updateTimeSlot(slot.id));
- endTimeInput.addEventListener('change', () => updateTimeSlot(slot.id));
- // Handle custom day checkboxes
- customDaysContainer.addEventListener('change', () => updateTimeSlot(slot.id));
- // Remove slot button
- removeButton.addEventListener('click', () => {
- removeTimeSlot(slot.id);
- });
- return slotDiv;
- }
- // Function to render all time slots
- function renderTimeSlots() {
- timeSlotsContainer.innerHTML = '';
- if (timeSlots.length === 0) {
- timeSlotsContainer.innerHTML = `
- <div class="text-center py-8 text-slate-500 dark:text-slate-400">
- <span class="material-icons text-4xl mb-2 block">schedule</span>
- <p>No time slots configured</p>
- <p class="text-xs mt-1">Click "Add Time Slot" to create a pause schedule</p>
- </div>
- `;
- return;
- }
- timeSlots.forEach(slot => {
- const slotElement = createTimeSlotElement(slot);
- timeSlotsContainer.appendChild(slotElement);
- });
- }
- // Function to add a new time slot
- function addTimeSlot() {
- const newSlot = {
- id: ++slotIdCounter,
- start_time: '22:00',
- end_time: '08:00',
- days: 'daily',
- custom_days: []
- };
- timeSlots.push(newSlot);
- renderTimeSlots();
- }
- // Function to remove a time slot
- function removeTimeSlot(slotId) {
- timeSlots = timeSlots.filter(slot => slot.id !== slotId);
- renderTimeSlots();
- }
- // Function to update a time slot's data
- function updateTimeSlot(slotId) {
- const slotElement = timeSlotsContainer.querySelector(`[data-slot-id="${slotId}"]`);
- if (!slotElement) return;
- const slot = timeSlots.find(s => s.id === slotId);
- if (!slot) return;
- // Update slot data from inputs
- slot.start_time = slotElement.querySelector('.start-time').value;
- slot.end_time = slotElement.querySelector('.end-time').value;
- slot.days = slotElement.querySelector('.days-select').value;
- // Update custom days if applicable
- if (slot.days === 'custom') {
- const checkedDays = Array.from(slotElement.querySelectorAll(`input[name="custom-days-${slotId}"]:checked`))
- .map(cb => cb.value);
- slot.custom_days = checkedDays;
- } else {
- slot.custom_days = [];
- }
- }
- // Function to validate all time slots
- function validateTimeSlots() {
- const errors = [];
- timeSlots.forEach((slot, index) => {
- if (!slot.start_time || !isValidTime(slot.start_time)) {
- errors.push(`Time slot ${index + 1}: Invalid start time`);
- }
- if (!slot.end_time || !isValidTime(slot.end_time)) {
- errors.push(`Time slot ${index + 1}: Invalid end time`);
- }
- if (slot.days === 'custom' && (!slot.custom_days || slot.custom_days.length === 0)) {
- errors.push(`Time slot ${index + 1}: Please select at least one day for custom schedule`);
- }
- });
- return errors;
- }
- // Function to save settings
- async function saveStillSandsSettings() {
- // Update all slots from current form values
- timeSlots.forEach(slot => updateTimeSlot(slot.id));
- // Validate time slots
- const validationErrors = validateTimeSlots();
- if (validationErrors.length > 0) {
- showStatusMessage(`Validation errors: ${validationErrors.join(', ')}`, 'error');
- return;
- }
- // Update button UI to show loading state
- const originalButtonHTML = saveStillSandsButton.innerHTML;
- saveStillSandsButton.disabled = true;
- saveStillSandsButton.innerHTML = '<span class="material-icons text-lg animate-spin">refresh</span><span class="truncate">Saving...</span>';
- try {
- const response = await fetch('/api/scheduled-pause', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- enabled: stillSandsToggle.checked,
- control_wled: wledControlToggle ? wledControlToggle.checked : false,
- finish_pattern: finishPatternToggle ? finishPatternToggle.checked : false,
- timezone: timezoneSelect ? (timezoneSelect.value || null) : null,
- time_slots: timeSlots.map(slot => ({
- start_time: slot.start_time,
- end_time: slot.end_time,
- days: slot.days,
- custom_days: slot.custom_days
- }))
- })
- });
- if (!response.ok) {
- const errorData = await response.json();
- throw new Error(errorData.detail || 'Failed to save Still Sands settings');
- }
- // Show success state temporarily
- saveStillSandsButton.innerHTML = '<span class="material-icons text-lg">check</span><span class="truncate">Saved!</span>';
- showStatusMessage('Still Sands settings saved successfully', 'success');
- // Restore button after 2 seconds
- setTimeout(() => {
- saveStillSandsButton.innerHTML = originalButtonHTML;
- saveStillSandsButton.disabled = false;
- }, 2000);
- } catch (error) {
- logMessage(`Error saving Still Sands settings: ${error.message}`, LOG_TYPE.ERROR);
- showStatusMessage(`Failed to save settings: ${error.message}`, 'error');
- // Restore button immediately on error
- saveStillSandsButton.innerHTML = originalButtonHTML;
- saveStillSandsButton.disabled = false;
- }
- }
- // Note: Slot IDs are now assigned during initialization above, before first render
- // Event listeners
- stillSandsToggle.addEventListener('change', async () => {
- logMessage(`Still Sands toggle changed: ${stillSandsToggle.checked}`, LOG_TYPE.INFO);
- stillSandsSettings.style.display = stillSandsToggle.checked ? 'block' : 'none';
- logMessage(`Settings display set to: ${stillSandsSettings.style.display}`, LOG_TYPE.INFO);
- // Auto-save when toggle changes
- try {
- await saveStillSandsSettings();
- const statusText = stillSandsToggle.checked ? 'enabled' : 'disabled';
- showStatusMessage(`Still Sands ${statusText} successfully`, 'success');
- } catch (error) {
- logMessage(`Error saving Still Sands toggle: ${error.message}`, LOG_TYPE.ERROR);
- showStatusMessage(`Failed to save Still Sands setting: ${error.message}`, 'error');
- }
- });
- addTimeSlotButton.addEventListener('click', addTimeSlot);
- saveStillSandsButton.addEventListener('click', saveStillSandsSettings);
- // Add listener for WLED control toggle
- if (wledControlToggle) {
- wledControlToggle.addEventListener('change', async () => {
- logMessage(`WLED control toggle changed: ${wledControlToggle.checked}`, LOG_TYPE.INFO);
- // Auto-save when WLED control changes
- await saveStillSandsSettings();
- });
- }
- // Add listener for finish pattern toggle
- if (finishPatternToggle) {
- finishPatternToggle.addEventListener('change', async () => {
- logMessage(`Finish pattern toggle changed: ${finishPatternToggle.checked}`, LOG_TYPE.INFO);
- // Auto-save when finish pattern setting changes
- await saveStillSandsSettings();
- });
- }
- // Add listener for timezone select
- if (timezoneSelect) {
- timezoneSelect.addEventListener('change', async () => {
- logMessage(`Timezone changed: ${timezoneSelect.value || 'System Default'}`, LOG_TYPE.INFO);
- // Auto-save when timezone changes
- await saveStillSandsSettings();
- });
- }
- }
- // Homing Configuration
- async function initializeHomingConfig() {
- logMessage('Initializing homing configuration', LOG_TYPE.INFO);
- const homingModeCrash = document.getElementById('homingModeCrash');
- const homingModeSensor = document.getElementById('homingModeSensor');
- const angularOffsetInput = document.getElementById('angularOffsetInput');
- const compassOffsetContainer = document.getElementById('compassOffsetContainer');
- const saveHomingConfigButton = document.getElementById('saveHomingConfig');
- const homingInfoContent = document.getElementById('homingInfoContent');
- const autoHomeEnabledToggle = document.getElementById('autoHomeEnabledToggle');
- const autoHomeSettings = document.getElementById('autoHomeSettings');
- const autoHomeAfterPatternsInput = document.getElementById('autoHomeAfterPatternsInput');
- // Check if elements exist
- if (!homingModeCrash || !homingModeSensor || !angularOffsetInput || !saveHomingConfigButton || !homingInfoContent || !compassOffsetContainer) {
- logMessage('Homing configuration elements not found, skipping initialization', LOG_TYPE.WARNING);
- return;
- }
- logMessage('Homing configuration elements found successfully', LOG_TYPE.INFO);
- // Function to get selected homing mode
- function getSelectedMode() {
- return homingModeCrash.checked ? 0 : 1;
- }
- // Function to update info box and visibility based on selected mode
- function updateHomingInfo() {
- const mode = getSelectedMode();
- // Show/hide compass offset based on mode
- if (mode === 0) {
- compassOffsetContainer.style.display = 'none';
- homingInfoContent.innerHTML = `
- <p class="font-medium text-blue-800">Crash Homing Mode:</p>
- <ul class="mt-1 space-y-1 text-blue-700">
- <li>• Y axis moves -22mm (or -30mm for mini) until physical stop</li>
- <li>• Theta set to 0, rho set to 0</li>
- <li>• No x0 y0 command sent</li>
- <li>• No hardware sensors required</li>
- </ul>
- `;
- } else {
- compassOffsetContainer.style.display = 'block';
- homingInfoContent.innerHTML = `
- <p class="font-medium text-blue-800">Sensor Homing Mode:</p>
- <ul class="mt-1 space-y-1 text-blue-700">
- <li>• Requires hardware limit switches</li>
- <li>• Requires additional configuration</li>
- </ul>
- `;
- }
- }
- // Load current homing configuration
- try {
- const response = await fetch('/api/homing-config');
- const data = await response.json();
- // Set radio button based on mode
- if (data.homing_mode === 1) {
- homingModeSensor.checked = true;
- } else {
- homingModeCrash.checked = true;
- }
- angularOffsetInput.value = data.angular_homing_offset_degrees || 0;
- // Load auto-home settings
- if (autoHomeEnabledToggle) {
- autoHomeEnabledToggle.checked = data.auto_home_enabled || false;
- if (autoHomeSettings) {
- autoHomeSettings.style.display = data.auto_home_enabled ? 'block' : 'none';
- }
- }
- if (autoHomeAfterPatternsInput) {
- autoHomeAfterPatternsInput.value = data.auto_home_after_patterns || 5;
- }
- updateHomingInfo();
- logMessage(`Loaded homing config: mode=${data.homing_mode}, offset=${data.angular_homing_offset_degrees}°, auto_home=${data.auto_home_enabled}, after=${data.auto_home_after_patterns}`, LOG_TYPE.INFO);
- } catch (error) {
- logMessage(`Error loading homing configuration: ${error.message}`, LOG_TYPE.ERROR);
- // Initialize with defaults if load fails
- homingModeCrash.checked = true;
- angularOffsetInput.value = 0;
- if (autoHomeEnabledToggle) autoHomeEnabledToggle.checked = false;
- if (autoHomeAfterPatternsInput) autoHomeAfterPatternsInput.value = 5;
- updateHomingInfo();
- }
- // Function to save homing configuration
- async function saveHomingConfig() {
- // Update button UI to show loading state
- const originalButtonHTML = saveHomingConfigButton.innerHTML;
- saveHomingConfigButton.disabled = true;
- saveHomingConfigButton.innerHTML = '<span class="material-icons text-lg animate-spin">refresh</span><span class="truncate">Saving...</span>';
- try {
- const requestBody = {
- homing_mode: getSelectedMode(),
- angular_homing_offset_degrees: parseFloat(angularOffsetInput.value) || 0
- };
- // Include auto-home settings if elements exist
- if (autoHomeEnabledToggle) {
- requestBody.auto_home_enabled = autoHomeEnabledToggle.checked;
- }
- if (autoHomeAfterPatternsInput) {
- const afterPatterns = parseInt(autoHomeAfterPatternsInput.value);
- if (!isNaN(afterPatterns) && afterPatterns >= 1) {
- requestBody.auto_home_after_patterns = afterPatterns;
- }
- }
- const response = await fetch('/api/homing-config', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(requestBody)
- });
- if (!response.ok) {
- const errorData = await response.json();
- throw new Error(errorData.detail || 'Failed to save homing configuration');
- }
- // Show success state temporarily
- saveHomingConfigButton.innerHTML = '<span class="material-icons text-lg">check</span><span class="truncate">Saved!</span>';
- showStatusMessage('Homing configuration saved successfully', 'success');
- // Restore button after 2 seconds
- setTimeout(() => {
- saveHomingConfigButton.innerHTML = originalButtonHTML;
- saveHomingConfigButton.disabled = false;
- }, 2000);
- } catch (error) {
- logMessage(`Error saving homing configuration: ${error.message}`, LOG_TYPE.ERROR);
- showStatusMessage(`Failed to save homing configuration: ${error.message}`, 'error');
- // Restore button immediately on error
- saveHomingConfigButton.innerHTML = originalButtonHTML;
- saveHomingConfigButton.disabled = false;
- }
- }
- // Event listeners
- homingModeCrash.addEventListener('change', updateHomingInfo);
- homingModeSensor.addEventListener('change', updateHomingInfo);
- saveHomingConfigButton.addEventListener('click', saveHomingConfig);
- // Auto-home toggle event listener
- if (autoHomeEnabledToggle && autoHomeSettings) {
- autoHomeEnabledToggle.addEventListener('change', () => {
- autoHomeSettings.style.display = autoHomeEnabledToggle.checked ? 'block' : 'none';
- });
- }
- }
- // Toggle password visibility helper
- function togglePasswordVisibility(inputId, button) {
- const input = document.getElementById(inputId);
- if (!input || !button) return;
- const icon = button.querySelector('.material-icons');
- if (input.type === 'password') {
- input.type = 'text';
- if (icon) icon.textContent = 'visibility';
- } else {
- input.type = 'password';
- if (icon) icon.textContent = 'visibility_off';
- }
- }
- // MQTT Configuration
- async function initializeMqttConfig() {
- logMessage('Initializing MQTT configuration', LOG_TYPE.INFO);
- const mqttEnableToggle = document.getElementById('mqttEnableToggle');
- const mqttSettings = document.getElementById('mqttSettings');
- const mqttStatusBanner = document.getElementById('mqttStatusBanner');
- const mqttConnectedBanner = document.getElementById('mqttConnectedBanner');
- const mqttDisconnectedBanner = document.getElementById('mqttDisconnectedBanner');
- const mqttBrokerInput = document.getElementById('mqttBrokerInput');
- const mqttPortInput = document.getElementById('mqttPortInput');
- const mqttUsernameInput = document.getElementById('mqttUsernameInput');
- const mqttPasswordInput = document.getElementById('mqttPasswordInput');
- const mqttDeviceNameInput = document.getElementById('mqttDeviceNameInput');
- const mqttDeviceIdInput = document.getElementById('mqttDeviceIdInput');
- const mqttClientIdInput = document.getElementById('mqttClientIdInput');
- const mqttDiscoveryPrefixInput = document.getElementById('mqttDiscoveryPrefixInput');
- const testMqttButton = document.getElementById('testMqttConnection');
- const mqttTestResult = document.getElementById('mqttTestResult');
- const saveMqttButton = document.getElementById('saveMqttConfig');
- const mqttRestartNotice = document.getElementById('mqttRestartNotice');
- // Check if elements exist
- if (!mqttEnableToggle || !mqttSettings || !saveMqttButton) {
- logMessage('MQTT configuration elements not found, skipping initialization', LOG_TYPE.WARNING);
- return;
- }
- logMessage('MQTT configuration elements found successfully', LOG_TYPE.INFO);
- // Track if settings have changed (to show restart notice)
- let originalConfig = null;
- let configChanged = false;
- // Function to update UI based on enabled state
- function updateMqttSettingsVisibility() {
- mqttSettings.style.display = mqttEnableToggle.checked ? 'block' : 'none';
- if (mqttStatusBanner) {
- mqttStatusBanner.classList.toggle('hidden', !mqttEnableToggle.checked);
- }
- }
- // Function to update connection status banners
- function updateConnectionStatus(connected) {
- if (mqttConnectedBanner && mqttDisconnectedBanner) {
- if (connected) {
- mqttConnectedBanner.classList.remove('hidden');
- mqttDisconnectedBanner.classList.add('hidden');
- } else {
- mqttConnectedBanner.classList.add('hidden');
- mqttDisconnectedBanner.classList.remove('hidden');
- }
- }
- }
- // Function to check if config has changed
- function checkConfigChanged() {
- if (!originalConfig) return false;
- const currentConfig = {
- enabled: mqttEnableToggle.checked,
- broker: mqttBrokerInput.value,
- port: parseInt(mqttPortInput.value) || 1883,
- username: mqttUsernameInput.value,
- password: mqttPasswordInput.value,
- device_name: mqttDeviceNameInput.value,
- device_id: mqttDeviceIdInput.value,
- client_id: mqttClientIdInput.value,
- discovery_prefix: mqttDiscoveryPrefixInput.value
- };
- return JSON.stringify(currentConfig) !== JSON.stringify(originalConfig);
- }
- // Function to show/hide restart notice
- function updateRestartNotice() {
- configChanged = checkConfigChanged();
- if (mqttRestartNotice) {
- mqttRestartNotice.classList.toggle('hidden', !configChanged);
- }
- }
- // Load current MQTT configuration
- try {
- const response = await fetch('/api/mqtt-config');
- const data = await response.json();
- mqttEnableToggle.checked = data.enabled || false;
- mqttBrokerInput.value = data.broker || '';
- mqttPortInput.value = data.port || 1883;
- mqttUsernameInput.value = data.username || '';
- // Note: Password is not returned from API for security
- mqttDeviceNameInput.value = data.device_name || 'Dune Weaver';
- mqttDeviceIdInput.value = data.device_id || 'dune_weaver';
- mqttClientIdInput.value = data.client_id || 'dune_weaver';
- mqttDiscoveryPrefixInput.value = data.discovery_prefix || 'homeassistant';
- // Store original config for change detection
- originalConfig = {
- enabled: data.enabled || false,
- broker: data.broker || '',
- port: data.port || 1883,
- username: data.username || '',
- password: '', // We don't have the original password
- device_name: data.device_name || 'Dune Weaver',
- device_id: data.device_id || 'dune_weaver',
- client_id: data.client_id || 'dune_weaver',
- discovery_prefix: data.discovery_prefix || 'homeassistant'
- };
- updateMqttSettingsVisibility();
- // Update connection status if MQTT is enabled
- if (data.enabled) {
- updateConnectionStatus(data.connected || false);
- }
- logMessage(`Loaded MQTT config: enabled=${data.enabled}, broker=${data.broker}`, LOG_TYPE.INFO);
- } catch (error) {
- logMessage(`Error loading MQTT configuration: ${error.message}`, LOG_TYPE.ERROR);
- // Initialize with defaults if load fails
- mqttEnableToggle.checked = false;
- updateMqttSettingsVisibility();
- }
- // Function to save MQTT configuration
- async function saveMqttConfig() {
- // Validate required fields if MQTT is enabled
- if (mqttEnableToggle.checked && !mqttBrokerInput.value.trim()) {
- showStatusMessage('MQTT broker address is required when MQTT is enabled', 'error');
- mqttBrokerInput.focus();
- return;
- }
- // Update button UI to show loading state
- const originalButtonHTML = saveMqttButton.innerHTML;
- saveMqttButton.disabled = true;
- saveMqttButton.innerHTML = '<span class="material-icons text-lg animate-spin">refresh</span><span class="truncate">Saving...</span>';
- try {
- const requestBody = {
- enabled: mqttEnableToggle.checked,
- broker: mqttBrokerInput.value.trim(),
- port: parseInt(mqttPortInput.value) || 1883,
- username: mqttUsernameInput.value.trim() || null,
- device_name: mqttDeviceNameInput.value.trim() || 'Dune Weaver',
- device_id: mqttDeviceIdInput.value.trim() || 'dune_weaver',
- client_id: mqttClientIdInput.value.trim() || 'dune_weaver',
- discovery_prefix: mqttDiscoveryPrefixInput.value.trim() || 'homeassistant'
- };
- // Only include password if it was changed (not empty)
- if (mqttPasswordInput.value) {
- requestBody.password = mqttPasswordInput.value;
- }
- const response = await fetch('/api/mqtt-config', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(requestBody)
- });
- if (!response.ok) {
- const errorData = await response.json();
- throw new Error(errorData.detail || 'Failed to save MQTT configuration');
- }
- const data = await response.json();
- // Update original config for change detection
- originalConfig = {
- enabled: requestBody.enabled,
- broker: requestBody.broker,
- port: requestBody.port,
- username: requestBody.username || '',
- password: '', // Reset password tracking
- device_name: requestBody.device_name,
- device_id: requestBody.device_id,
- client_id: requestBody.client_id,
- discovery_prefix: requestBody.discovery_prefix
- };
- // Clear password field after save
- mqttPasswordInput.value = '';
- // Show success state temporarily
- saveMqttButton.innerHTML = '<span class="material-icons text-lg">check</span><span class="truncate">Saved!</span>';
- showStatusMessage('MQTT configuration saved successfully. Restart the application to apply changes.', 'success');
- // Show restart notice
- if (mqttRestartNotice) {
- mqttRestartNotice.classList.remove('hidden');
- }
- // Restore button after 2 seconds
- setTimeout(() => {
- saveMqttButton.innerHTML = originalButtonHTML;
- saveMqttButton.disabled = false;
- }, 2000);
- } catch (error) {
- logMessage(`Error saving MQTT configuration: ${error.message}`, LOG_TYPE.ERROR);
- showStatusMessage(`Failed to save MQTT configuration: ${error.message}`, 'error');
- // Restore button immediately on error
- saveMqttButton.innerHTML = originalButtonHTML;
- saveMqttButton.disabled = false;
- }
- }
- // Function to test MQTT connection
- async function testMqttConnection() {
- // Validate broker address
- if (!mqttBrokerInput.value.trim()) {
- showStatusMessage('Please enter a broker address to test', 'error');
- mqttBrokerInput.focus();
- return;
- }
- // Update button UI to show loading state
- const originalButtonHTML = testMqttButton.innerHTML;
- testMqttButton.disabled = true;
- testMqttButton.innerHTML = '<span class="material-icons text-lg animate-spin">refresh</span><span class="truncate">Testing...</span>';
- // Clear previous result
- if (mqttTestResult) {
- mqttTestResult.innerHTML = '';
- }
- try {
- const requestBody = {
- broker: mqttBrokerInput.value.trim(),
- port: parseInt(mqttPortInput.value) || 1883,
- username: mqttUsernameInput.value.trim() || null,
- password: mqttPasswordInput.value || null
- };
- const response = await fetch('/api/mqtt-test', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(requestBody)
- });
- const data = await response.json();
- if (data.success) {
- if (mqttTestResult) {
- mqttTestResult.innerHTML = '<span class="material-icons text-green-600 mr-1">check_circle</span><span class="text-green-600">Connection successful!</span>';
- }
- showStatusMessage('MQTT connection test successful', 'success');
- } else {
- if (mqttTestResult) {
- mqttTestResult.innerHTML = `<span class="material-icons text-red-600 mr-1">error</span><span class="text-red-600">${data.error || 'Connection failed'}</span>`;
- }
- showStatusMessage(`MQTT test failed: ${data.error || 'Connection failed'}`, 'error');
- }
- } catch (error) {
- logMessage(`Error testing MQTT connection: ${error.message}`, LOG_TYPE.ERROR);
- if (mqttTestResult) {
- mqttTestResult.innerHTML = `<span class="material-icons text-red-600 mr-1">error</span><span class="text-red-600">Test failed: ${error.message}</span>`;
- }
- showStatusMessage(`MQTT test failed: ${error.message}`, 'error');
- } finally {
- // Restore button
- testMqttButton.innerHTML = originalButtonHTML;
- testMqttButton.disabled = false;
- }
- }
- // Event listeners
- mqttEnableToggle.addEventListener('change', () => {
- updateMqttSettingsVisibility();
- updateRestartNotice();
- });
- // Track changes to show restart notice
- [mqttBrokerInput, mqttPortInput, mqttUsernameInput, mqttPasswordInput,
- mqttDeviceNameInput, mqttDeviceIdInput, mqttClientIdInput, mqttDiscoveryPrefixInput].forEach(input => {
- if (input) {
- input.addEventListener('input', updateRestartNotice);
- }
- });
- testMqttButton.addEventListener('click', testMqttConnection);
- saveMqttButton.addEventListener('click', saveMqttConfig);
- }
- // Initialize MQTT config when DOM is ready
- document.addEventListener('DOMContentLoaded', function() {
- initializeMqttConfig();
- initializeTableTypeConfig();
- });
- // ============================================================================
- // Table Type Configuration
- // ============================================================================
- function initializeTableTypeConfig() {
- const tableTypeSelect = document.getElementById('tableTypeSelect');
- const saveTableTypeButton = document.getElementById('saveTableType');
- const detectedTableType = document.getElementById('detectedTableType');
- if (!tableTypeSelect || !saveTableTypeButton) {
- logMessage('Table type elements not found', LOG_TYPE.WARNING);
- return;
- }
- // Load current settings
- loadTableTypeSettings();
- // Save button click handler
- saveTableTypeButton.addEventListener('click', saveTableTypeConfig);
- async function loadTableTypeSettings() {
- try {
- const response = await fetch('/api/settings');
- if (!response.ok) throw new Error('Failed to fetch settings');
- const settings = await response.json();
- const machine = settings.machine || {};
- // Populate dropdown with available table types
- tableTypeSelect.innerHTML = '<option value="">Auto-detect (use detected type)</option>';
- if (machine.available_table_types) {
- machine.available_table_types.forEach(type => {
- const option = document.createElement('option');
- option.value = type.value;
- option.textContent = type.label;
- tableTypeSelect.appendChild(option);
- });
- }
- // Set current override value
- if (machine.table_type_override) {
- tableTypeSelect.value = machine.table_type_override;
- } else {
- tableTypeSelect.value = '';
- }
- // Update detected type display
- if (detectedTableType) {
- const detected = machine.detected_table_type;
- if (detected) {
- // Find the label for the detected type
- const typeInfo = machine.available_table_types?.find(t => t.value === detected);
- detectedTableType.textContent = typeInfo ? typeInfo.label : detected;
- } else {
- detectedTableType.textContent = 'Not connected';
- }
- }
- logMessage('Table type settings loaded', LOG_TYPE.DEBUG);
- } catch (error) {
- logMessage(`Error loading table type settings: ${error.message}`, LOG_TYPE.ERROR);
- }
- }
- async function saveTableTypeConfig() {
- const originalButtonHTML = saveTableTypeButton.innerHTML;
- saveTableTypeButton.disabled = true;
- saveTableTypeButton.innerHTML = '<span class="material-icons text-lg animate-spin">refresh</span><span class="truncate">Saving...</span>';
- try {
- const response = await fetch('/api/settings', {
- method: 'PATCH',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- machine: {
- table_type_override: tableTypeSelect.value || ''
- }
- })
- });
- if (!response.ok) throw new Error('Failed to save settings');
- const result = await response.json();
- if (result.success) {
- showStatusMessage('Table type settings saved. Changes will take effect on next connection.', 'success');
- // Reload to show updated effective type
- await loadTableTypeSettings();
- } else {
- throw new Error('Save failed');
- }
- } catch (error) {
- logMessage(`Error saving table type: ${error.message}`, LOG_TYPE.ERROR);
- showStatusMessage(`Failed to save table type: ${error.message}`, 'error');
- } finally {
- saveTableTypeButton.innerHTML = originalButtonHTML;
- saveTableTypeButton.disabled = false;
- }
- }
- }
- // ============================================================================
- // Application Logs Modal
- // ============================================================================
- let logsWebSocket = null;
- let logsEntries = [];
- const MAX_LOG_ENTRIES = 500;
- // Color classes for different log levels
- const LOG_LEVEL_COLORS = {
- DEBUG: 'text-gray-500 dark:text-gray-400',
- INFO: 'text-blue-600 dark:text-blue-400',
- WARNING: 'text-amber-600 dark:text-amber-400',
- ERROR: 'text-red-600 dark:text-red-400',
- CRITICAL: 'text-red-700 dark:text-red-300 font-bold'
- };
- const LOG_LEVEL_BG = {
- DEBUG: 'bg-gray-100 dark:bg-gray-700',
- INFO: 'bg-blue-50 dark:bg-blue-900/30',
- WARNING: 'bg-amber-50 dark:bg-amber-900/30',
- ERROR: 'bg-red-50 dark:bg-red-900/30',
- CRITICAL: 'bg-red-100 dark:bg-red-900/50'
- };
- function openLogsModal() {
- const modal = document.getElementById('logsModal');
- modal.classList.remove('hidden');
- // Load initial logs
- loadInitialLogs();
- // Connect to WebSocket for real-time updates
- connectLogsWebSocket();
- // Close on overlay click
- modal.addEventListener('click', (e) => {
- if (e.target === modal) {
- closeLogsModal();
- }
- });
- // Close on Escape key
- document.addEventListener('keydown', handleLogsEscapeKey);
- }
- function closeLogsModal() {
- const modal = document.getElementById('logsModal');
- modal.classList.add('hidden');
- // Disconnect WebSocket
- if (logsWebSocket) {
- logsWebSocket.close();
- logsWebSocket = null;
- }
- // Remove event listener
- document.removeEventListener('keydown', handleLogsEscapeKey);
- }
- function handleLogsEscapeKey(e) {
- if (e.key === 'Escape') {
- closeLogsModal();
- }
- }
- async function loadInitialLogs() {
- const logsContent = document.getElementById('logsContent');
- logsContent.innerHTML = '<div class="text-gray-500 dark:text-gray-400">Loading logs...</div>';
- try {
- const response = await fetch('/api/logs?limit=500');
- const data = await response.json();
- logsEntries = data.logs || [];
- renderLogs();
- updateLogsCount();
- // Scroll to bottom
- const container = document.getElementById('logsContainer');
- container.scrollTop = container.scrollHeight;
- } catch (error) {
- console.error('Failed to load logs:', error);
- logsContent.innerHTML = '<div class="text-red-500">Failed to load logs</div>';
- }
- }
- function connectLogsWebSocket() {
- const statusEl = document.getElementById('logsConnectionStatus');
- const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
- const wsUrl = `${protocol}//${window.location.host}/ws/logs`;
- try {
- logsWebSocket = new WebSocket(wsUrl);
- logsWebSocket.onopen = () => {
- statusEl.textContent = 'Live';
- statusEl.className = 'text-xs px-2 py-1 rounded-full bg-green-100 dark:bg-green-900 text-green-700 dark:text-green-300';
- };
- logsWebSocket.onmessage = (event) => {
- const message = JSON.parse(event.data);
- if (message.type === 'log_entry') {
- addLogEntry(message.data);
- }
- // Ignore heartbeat messages
- };
- logsWebSocket.onclose = () => {
- statusEl.textContent = 'Disconnected';
- statusEl.className = 'text-xs px-2 py-1 rounded-full bg-gray-200 dark:bg-gray-600 text-gray-600 dark:text-gray-300';
- };
- logsWebSocket.onerror = (error) => {
- console.error('Logs WebSocket error:', error);
- statusEl.textContent = 'Error';
- statusEl.className = 'text-xs px-2 py-1 rounded-full bg-red-100 dark:bg-red-900 text-red-700 dark:text-red-300';
- };
- } catch (error) {
- console.error('Failed to connect to logs WebSocket:', error);
- statusEl.textContent = 'Failed';
- statusEl.className = 'text-xs px-2 py-1 rounded-full bg-red-100 dark:bg-red-900 text-red-700 dark:text-red-300';
- }
- }
- function addLogEntry(entry) {
- // Add to beginning (newest first in memory, but we render oldest first in UI)
- logsEntries.unshift(entry);
- // Trim if exceeds max
- if (logsEntries.length > MAX_LOG_ENTRIES) {
- logsEntries.pop();
- }
- // Add to UI
- const logsContent = document.getElementById('logsContent');
- const entryEl = createLogEntryElement(entry);
- // Insert at the end (bottom) for newest
- logsContent.appendChild(entryEl);
- // Auto-scroll if enabled
- const autoScroll = document.getElementById('logsAutoScroll').checked;
- if (autoScroll) {
- const container = document.getElementById('logsContainer');
- container.scrollTop = container.scrollHeight;
- }
- // Apply filter if active
- const levelFilter = document.getElementById('logLevelFilter').value;
- if (levelFilter && entry.level !== levelFilter) {
- entryEl.classList.add('hidden');
- }
- updateLogsCount();
- }
- function createLogEntryElement(entry) {
- const div = document.createElement('div');
- div.className = `log-entry flex gap-2 py-1 px-2 rounded ${LOG_LEVEL_BG[entry.level] || 'bg-gray-50 dark:bg-gray-800'}`;
- div.dataset.level = entry.level;
- // Format timestamp (show only time part for brevity)
- const timestamp = new Date(entry.timestamp);
- const timeStr = timestamp.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' });
- const msStr = timestamp.getMilliseconds().toString().padStart(3, '0');
- div.innerHTML = `
- <span class="text-gray-400 dark:text-gray-500 shrink-0">${timeStr}.${msStr}</span>
- <span class="${LOG_LEVEL_COLORS[entry.level] || 'text-gray-600'} shrink-0 w-16">${entry.level}</span>
- <span class="text-purple-600 dark:text-purple-400 shrink-0 truncate max-w-32" title="${entry.logger}:${entry.line}">${entry.module}:${entry.line}</span>
- <span class="text-slate-700 dark:text-gray-200 break-all">${escapeHtml(entry.message)}</span>
- `;
- return div;
- }
- function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
- }
- function renderLogs() {
- const logsContent = document.getElementById('logsContent');
- logsContent.innerHTML = '';
- // Render in reverse order (oldest first, newest at bottom)
- const reversedLogs = [...logsEntries].reverse();
- const levelFilter = document.getElementById('logLevelFilter').value;
- for (const entry of reversedLogs) {
- const entryEl = createLogEntryElement(entry);
- if (levelFilter && entry.level !== levelFilter) {
- entryEl.classList.add('hidden');
- }
- logsContent.appendChild(entryEl);
- }
- }
- function filterLogs() {
- const levelFilter = document.getElementById('logLevelFilter').value;
- const entries = document.querySelectorAll('#logsContent .log-entry');
- let visibleCount = 0;
- entries.forEach(entry => {
- if (!levelFilter || entry.dataset.level === levelFilter) {
- entry.classList.remove('hidden');
- visibleCount++;
- } else {
- entry.classList.add('hidden');
- }
- });
- // Update count to show filtered amount
- const countEl = document.getElementById('logsCount');
- if (levelFilter) {
- countEl.textContent = `${visibleCount} of ${logsEntries.length} entries (filtered)`;
- } else {
- countEl.textContent = `${logsEntries.length} entries`;
- }
- }
- function updateLogsCount() {
- const countEl = document.getElementById('logsCount');
- const levelFilter = document.getElementById('logLevelFilter').value;
- if (levelFilter) {
- const filteredCount = logsEntries.filter(e => e.level === levelFilter).length;
- countEl.textContent = `${filteredCount} of ${logsEntries.length} entries (filtered)`;
- } else {
- countEl.textContent = `${logsEntries.length} entries`;
- }
- }
- async function clearLogs() {
- try {
- await fetch('/api/logs', { method: 'DELETE' });
- logsEntries = [];
- document.getElementById('logsContent').innerHTML = '<div class="text-gray-500 dark:text-gray-400 text-center py-4">Logs cleared</div>';
- updateLogsCount();
- } catch (error) {
- console.error('Failed to clear logs:', error);
- }
- }
|