Browse Source

feat: Add default config management and device improvements

Implement centralized default device configuration management with interactive and JSON editing modes. Add real-time device status tracking, inline BLE/WiFi toggles, and device deletion functionality to streamline device lifecycle management.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
root 4 weeks ago
parent
commit
945cefcb33

+ 44 - 0
backend/alembic/versions/20251229_0046_c1148f55dcd9_add_settings_table.py

@@ -0,0 +1,44 @@
+"""add_settings_table
+
+Revision ID: c1148f55dcd9
+Revises: 277d91f6540f
+Create Date: 2025-12-29 00:46:10.442689+00:00
+
+"""
+from typing import Sequence, Union
+
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.dialects import postgresql
+
+
+# revision identifiers, used by Alembic.
+revision: str = 'c1148f55dcd9'
+down_revision: Union[str, None] = '277d91f6540f'
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+    # Create settings table
+    op.create_table(
+        'settings',
+        sa.Column('id', sa.Integer(), nullable=False),
+        sa.Column('key', sa.String(length=255), nullable=False),
+        sa.Column('value', postgresql.JSON(astext_type=sa.Text()), nullable=False),
+        sa.Column('updated_at', sa.DateTime(), nullable=True),
+        sa.PrimaryKeyConstraint('id'),
+        sa.UniqueConstraint('key')
+    )
+
+    # Insert default auto_registration setting (enabled by default)
+    op.execute(
+        """
+        INSERT INTO settings (key, value, updated_at)
+        VALUES ('auto_registration', '{"enabled": true}', NOW())
+        """
+    )
+
+
+def downgrade() -> None:
+    op.drop_table('settings')

+ 8 - 47
backend/app/api/v1/config.py

@@ -51,54 +51,15 @@ async def get_device_config(
     Get device configuration.
 
     Returns config with BLE/WiFi settings, tunnel config, etc.
+    Updates device last_seen_at timestamp.
     """
     device = await _auth_device_token(authorization, db)
 
-    # Default config (like stub)
-    default_config = {
-        "force_cloud": False,
-        "ble": {
-            "enabled": True,
-            "batch_interval_ms": 2500,
-            "uuid_filter_hex": "",
-            "upload_endpoint": "",
-        },
-        "wifi": {
-            "client_enabled": False,
-            "ssid": "PT",
-            "psk": "suhariki",
-            "monitor_enabled": True,
-            "batch_interval_ms": 10000,
-            "upload_endpoint": "",
-        },
-        "ssh_tunnel": {
-            "enabled": False,
-            "server": "192.168.5.4",
-            "port": 22,
-            "user": "tunnel",
-            "remote_port": 0,
-            "keepalive_interval": 30,
-        },
-        "dashboard_tunnel": {
-            "enabled": False,
-            "server": "192.168.5.4",
-            "port": 22,
-            "user": "tunnel",
-            "remote_port": 0,
-            "keepalive_interval": 30,
-        },
-        "dashboard": {
-            "enabled": True,
-        },
-        "net": {
-            "ntp": {
-                "servers": ["pool.ntp.org", "time.google.com"],
-            },
-        },
-        "debug": False,
-    }
+    # Update last_seen_at timestamp
+    from datetime import datetime, timezone
+    device.last_seen_at = datetime.now(timezone.utc)
+    await db.commit()
 
-    # Merge with device-specific config overrides
-    config = {**default_config, **(device.config or {})}
-
-    return config
+    # Return device config from database
+    # Config was copied from default_config.json during registration
+    return device.config or {}

+ 17 - 2
backend/app/api/v1/registration.py

@@ -2,9 +2,12 @@
 Device registration endpoint.
 """
 
+import copy
+import json
 import secrets
 from base64 import b64encode
 from datetime import datetime, timezone
+from pathlib import Path
 from typing import Annotated
 
 from fastapi import APIRouter, Depends, HTTPException, status
@@ -18,6 +21,11 @@ from app.models.settings import Settings
 
 router = APIRouter()
 
+# Load default config from JSON file
+DEFAULT_CONFIG_PATH = Path(__file__).parent.parent.parent / "default_config.json"
+with open(DEFAULT_CONFIG_PATH, "r") as f:
+    DEFAULT_CONFIG = json.load(f)
+
 
 class RegistrationRequest(BaseModel):
     """Device registration request."""
@@ -91,12 +99,19 @@ async def register_device(
             detail="Registration disabled. Contact administrator.",
         )
 
-    # Create new device
+    # Create new device with default config
+    # Deep copy to avoid modifying the global default
+    device_config = copy.deepcopy(DEFAULT_CONFIG)
+
+    # Add SSH public key if provided
+    if data.ssh_public_key:
+        device_config["ssh_public_key"] = data.ssh_public_key
+
     device = Device(
         mac_address=mac_address,
         organization_id=None,  # Unassigned
         status="online",
-        config={"ssh_public_key": data.ssh_public_key} if data.ssh_public_key else {},
+        config=device_config,
         device_token=_generate_token(),
         device_password=_generate_password(),
     )

+ 2 - 1
backend/app/api/v1/superadmin/__init__.py

@@ -4,7 +4,7 @@ Superadmin API endpoints.
 
 from fastapi import APIRouter
 
-from app.api.v1.superadmin import devices, organizations, settings, tunnels, users
+from app.api.v1.superadmin import default_config, devices, organizations, settings, tunnels, users
 
 router = APIRouter()
 
@@ -13,3 +13,4 @@ router.include_router(users.router, prefix="/users", tags=["superadmin-users"])
 router.include_router(devices.router, prefix="/devices", tags=["superadmin-devices"])
 router.include_router(settings.router, prefix="/settings", tags=["superadmin-settings"])
 router.include_router(tunnels.router)
+router.include_router(default_config.router, tags=["superadmin-config"])

+ 66 - 0
backend/app/api/v1/superadmin/default_config.py

@@ -0,0 +1,66 @@
+"""
+Superadmin endpoint for managing default device configuration.
+"""
+
+import json
+from pathlib import Path
+from typing import Annotated
+
+from fastapi import APIRouter, Depends, HTTPException, status
+from pydantic import BaseModel
+
+from app.api.deps import get_current_superadmin
+from app.models.user import User
+
+router = APIRouter()
+
+DEFAULT_CONFIG_PATH = Path(__file__).parent.parent.parent.parent / "default_config.json"
+
+
+@router.get("/default-config")
+async def get_default_config(
+    current_user: Annotated[User, Depends(get_current_superadmin)],
+):
+    """
+    Get default device configuration (superadmin only).
+
+    Returns the default config that will be copied to newly registered devices.
+    """
+    try:
+        with open(DEFAULT_CONFIG_PATH, "r") as f:
+            config = json.load(f)
+        return config
+    except FileNotFoundError:
+        raise HTTPException(
+            status_code=status.HTTP_404_NOT_FOUND,
+            detail="Default config file not found",
+        )
+    except json.JSONDecodeError:
+        raise HTTPException(
+            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
+            detail="Invalid JSON in default config file",
+        )
+
+
+@router.put("/default-config")
+async def update_default_config(
+    config: dict,
+    current_user: Annotated[User, Depends(get_current_superadmin)],
+):
+    """
+    Update default device configuration (superadmin only).
+
+    Updates the default config file. Changes only affect newly registered devices.
+    Existing devices keep their current configuration.
+    """
+    try:
+        # Write config to file with pretty formatting
+        with open(DEFAULT_CONFIG_PATH, "w") as f:
+            json.dump(config, f, indent=2)
+
+        return {"success": True, "message": "Default config updated"}
+    except Exception as e:
+        raise HTTPException(
+            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
+            detail=f"Failed to save config: {str(e)}",
+        )

+ 16 - 0
backend/app/api/v1/superadmin/devices.py

@@ -2,6 +2,7 @@
 Superadmin endpoints for device management.
 """
 
+from datetime import datetime, timezone
 from typing import Annotated
 
 from fastapi import APIRouter, Depends, HTTPException, Query, status
@@ -57,6 +58,21 @@ async def list_devices(
         search=search,
     )
 
+    # Update status dynamically based on last_seen_at
+    # Device is online if it fetched config within last 60 seconds
+    now = datetime.now(timezone.utc)
+    for device in devices:
+        if device.last_seen_at:
+            # Remove timezone info for comparison if needed
+            last_seen = device.last_seen_at
+            if last_seen.tzinfo is None:
+                last_seen = last_seen.replace(tzinfo=timezone.utc)
+
+            delta_seconds = (now - last_seen).total_seconds()
+            device.status = "online" if delta_seconds < 60 else "offline"
+        else:
+            device.status = "offline"
+
     return DeviceListResponse(
         devices=devices,
         total=total,

+ 46 - 0
backend/app/default_config.json

@@ -0,0 +1,46 @@
+{
+  "force_cloud": false,
+  "cfg_polling_timeout": 30,
+  "ble": {
+    "enabled": true,
+    "batch_interval_ms": 2500,
+    "uuid_filter_hex": "",
+    "upload_endpoint": ""
+  },
+  "wifi": {
+    "client_enabled": false,
+    "ssid": "",
+    "psk": "",
+    "monitor_enabled": true,
+    "batch_interval_ms": 10000,
+    "upload_endpoint": ""
+  },
+  "ssh_tunnel": {
+    "enabled": false,
+    "server": "192.168.5.4",
+    "port": 22,
+    "user": "tunnel",
+    "remote_port": 0,
+    "keepalive_interval": 30
+  },
+  "dashboard_tunnel": {
+    "enabled": false,
+    "server": "192.168.5.4",
+    "port": 22,
+    "user": "tunnel",
+    "remote_port": 0,
+    "keepalive_interval": 30
+  },
+  "dashboard": {
+    "enabled": true
+  },
+  "net": {
+    "ntp": {
+      "servers": [
+        "pool.ntp.org",
+        "time.google.com"
+      ]
+    }
+  },
+  "debug": false
+}

+ 10 - 0
frontend/src/api/devices.js

@@ -22,6 +22,16 @@ export default {
     return data
   },
 
+  async getDefaultConfig() {
+    const { data } = await client.get('/superadmin/default-config')
+    return data
+  },
+
+  async updateDefaultConfig(config) {
+    const { data } = await client.put('/superadmin/default-config', config)
+    return data
+  },
+
   // Client endpoints
   async getAllClient(params = {}) {
     const { data } = await client.get('/client/devices', { params })

+ 518 - 1
frontend/src/views/superadmin/DevicesView.vue

@@ -25,6 +25,10 @@
           <input type="checkbox" v-model="onlineOnly" @change="loadDevices" />
           <span>{{ $t('devices.onlineOnly') }}</span>
         </label>
+
+        <button @click="showDefaultConfigModal" class="btn-primary btn-edit-default">
+          Edit Default Config
+        </button>
       </div>
 
       <div v-if="loading" class="loading">{{ $t('common.loading') }}</div>
@@ -42,6 +46,8 @@
               {{ $t('devices.macAddress') }}
               <span v-if="sortColumn === 'mac_address'">{{ sortDirection === 'asc' ? '↑' : '↓' }}</span>
             </th>
+            <th>BLE Enabled</th>
+            <th>WiFi Enabled</th>
             <th @click="sortBy('status')">
               {{ $t('common.status') }}
               <span v-if="sortColumn === 'status'">{{ sortDirection === 'asc' ? '↑' : '↓' }}</span>
@@ -57,8 +63,28 @@
           <tr v-for="device in sortedDevices" :key="device.id">
             <td><strong>#{{ device.simple_id }}</strong></td>
             <td><code>{{ device.mac_address }}</code></td>
+            <td class="text-center">
+              <label class="toggle-switch-inline">
+                <input
+                  type="checkbox"
+                  :checked="device.config?.ble?.enabled ?? false"
+                  @click="toggleBLE(device, $event)"
+                />
+                <span class="toggle-slider"></span>
+              </label>
+            </td>
+            <td class="text-center">
+              <label class="toggle-switch-inline">
+                <input
+                  type="checkbox"
+                  :checked="device.config?.wifi?.monitor_enabled ?? false"
+                  @click="toggleWiFi(device, $event)"
+                />
+                <span class="toggle-slider"></span>
+              </label>
+            </td>
             <td><span class="badge" :class="`status-${device.status}`">{{ $t(`devices.${device.status}`) }}</span></td>
-            <td>{{ formatDate(device.last_seen_at) }}</td>
+            <td>{{ formatRelativeTime(device.last_seen_at) }}</td>
             <td>
               <button @click="showEditModal(device)" class="btn-icon" title="Edit">✏️</button>
               <button
@@ -183,6 +209,11 @@
           <!-- Other Settings -->
           <div class="config-section">
             <h3>Other</h3>
+            <div class="form-group">
+              <label>Config Polling Timeout (seconds)</label>
+              <input v-model.number="config.cfg_polling_timeout" type="number" min="5" max="300" step="5" />
+              <div class="form-hint">How often device fetches config from server (min: 5s, recommended: 30-300s)</div>
+            </div>
             <div class="toggle-row">
               <span>{{ $t('devices.config.forceCloud') }}</span>
               <label class="toggle-switch">
@@ -207,6 +238,10 @@
           </div>
 
           <div class="modal-footer">
+            <button type="button" @click="deleteDevice" class="btn-danger btn-delete" :disabled="saving">
+              Delete Device
+            </button>
+            <div style="flex: 1"></div>
             <button type="button" @click="closeModal" class="btn-secondary">{{ $t('common.cancel') }}</button>
             <button type="submit" :disabled="saving" class="btn-primary">
               {{ saving ? $t('common.loading') : $t('common.save') }}
@@ -216,6 +251,184 @@
       </div>
     </div>
 
+    <!-- Edit Default Config Modal -->
+    <div v-if="defaultConfigModalVisible" class="modal-overlay" @click="closeDefaultConfigModal">
+      <div class="modal modal-wide" @click.stop>
+        <div class="modal-header">
+          <h2>Edit Default Device Configuration</h2>
+          <button @click="closeDefaultConfigModal" class="btn-close">×</button>
+        </div>
+
+        <!-- Tabs -->
+        <div class="modal-tabs">
+          <button
+            @click="defaultConfigTab = 'interactive'"
+            :class="['tab-button', { active: defaultConfigTab === 'interactive' }]"
+          >
+            Interactive
+          </button>
+          <button
+            @click="switchToJsonTab"
+            :class="['tab-button', { active: defaultConfigTab === 'json' }]"
+          >
+            JSON
+          </button>
+        </div>
+
+        <div class="modal-body">
+          <p style="margin-bottom: 16px; color: #718096; font-size: 14px;">
+            This configuration will be copied to all newly registered devices.
+            Changes do not affect existing devices.
+          </p>
+
+          <!-- Interactive Tab -->
+          <div v-if="defaultConfigTab === 'interactive'">
+            <!-- WiFi Scanner Section -->
+            <div class="config-section">
+              <h3>WiFi Scanner</h3>
+              <div class="toggle-row">
+                <span>WiFi Scanner Enabled</span>
+                <label class="toggle-switch">
+                  <input type="checkbox" v-model="defaultConfig.wifi.monitor_enabled" @change="onDefaultWifiMonitorChange" />
+                  <span class="toggle-slider"></span>
+                </label>
+              </div>
+              <div v-if="defaultConfig.wifi.monitor_enabled" class="toggle-content">
+                <div class="form-group">
+                  <label>Batch Interval (ms)</label>
+                  <input v-model.number="defaultConfig.wifi.batch_interval_ms" type="number" min="1000" step="1000" />
+                </div>
+                <div class="form-group">
+                  <label>Upload Endpoint (optional)</label>
+                  <input v-model="defaultConfig.wifi.upload_endpoint" type="text" placeholder="http://custom.example.com:8080/wifi" />
+                  <div class="form-hint">Custom upload endpoint URL (leave empty for default)</div>
+                </div>
+              </div>
+            </div>
+
+            <!-- BLE Section -->
+            <div class="config-section">
+              <h3>BLE Scanner</h3>
+              <div class="toggle-row">
+                <span>BLE Scanner Enabled</span>
+                <label class="toggle-switch">
+                  <input type="checkbox" v-model="defaultConfig.ble.enabled" />
+                  <span class="toggle-slider"></span>
+                </label>
+              </div>
+              <div v-if="defaultConfig.ble.enabled" class="toggle-content">
+                <div class="form-group">
+                  <label>Batch Interval (ms)</label>
+                  <input v-model.number="defaultConfig.ble.batch_interval_ms" type="number" min="100" step="100" />
+                </div>
+                <div class="form-group">
+                  <label>UUID Filter</label>
+                  <input v-model="defaultConfig.ble.uuid_filter_hex" type="text" placeholder="f7826da64fa24e988024bc5b71e0893e" maxlength="32" />
+                  <div class="form-hint">Filter beacons by UUID (32 hex chars, optional)</div>
+                </div>
+                <div class="form-group">
+                  <label>Upload Endpoint (optional)</label>
+                  <input v-model="defaultConfig.ble.upload_endpoint" type="text" placeholder="http://custom.example.com:8080/ble" />
+                  <div class="form-hint">Custom upload endpoint URL (leave empty for default)</div>
+                </div>
+              </div>
+            </div>
+
+            <!-- WiFi Client Section -->
+            <div class="config-section">
+              <h3>WiFi Client</h3>
+              <div class="toggle-row">
+                <span>WiFi Client Enabled</span>
+                <label class="toggle-switch">
+                  <input type="checkbox" v-model="defaultConfig.wifi.client_enabled" @change="onDefaultWifiClientChange" />
+                  <span class="toggle-slider"></span>
+                </label>
+              </div>
+              <div v-if="defaultConfig.wifi.client_enabled" class="toggle-content">
+                <div class="form-row">
+                  <div class="form-group">
+                    <label>WiFi SSID</label>
+                    <input v-model="defaultConfig.wifi.ssid" type="text" />
+                  </div>
+                  <div class="form-group">
+                    <label>WiFi Password</label>
+                    <input v-model="defaultConfig.wifi.psk" type="password" />
+                  </div>
+                </div>
+              </div>
+            </div>
+
+            <!-- NTP Section -->
+            <div class="config-section">
+              <h3>NTP Servers</h3>
+              <div class="form-group">
+                <label>NTP Servers</label>
+                <input v-model="defaultNtpServersText" type="text" placeholder="pool.ntp.org, time.google.com" />
+                <div class="form-hint">Comma-separated list of NTP server addresses</div>
+              </div>
+            </div>
+
+            <!-- Other Settings -->
+            <div class="config-section">
+              <h3>Other</h3>
+              <div class="form-group">
+                <label>Config Polling Timeout (seconds)</label>
+                <input v-model.number="defaultConfig.cfg_polling_timeout" type="number" min="5" max="300" step="5" />
+                <div class="form-hint">How often device fetches config from server (min: 5s, recommended: 30-300s)</div>
+              </div>
+              <div class="toggle-row">
+                <span>Force Cloud Mode</span>
+                <label class="toggle-switch">
+                  <input type="checkbox" v-model="defaultConfig.force_cloud" />
+                  <span class="toggle-slider"></span>
+                </label>
+              </div>
+              <div class="toggle-row">
+                <span>Debug Logging</span>
+                <label class="toggle-switch">
+                  <input type="checkbox" v-model="defaultConfig.debug" />
+                  <span class="toggle-slider"></span>
+                </label>
+              </div>
+              <div class="toggle-row">
+                <span>Dashboard Enabled</span>
+                <label class="toggle-switch">
+                  <input type="checkbox" v-model="defaultConfig.dashboard.enabled" />
+                  <span class="toggle-slider"></span>
+                </label>
+              </div>
+            </div>
+          </div>
+
+          <!-- JSON Tab -->
+          <div v-if="defaultConfigTab === 'json'">
+            <div class="form-group">
+              <label>Configuration (JSON)</label>
+              <textarea
+                v-model="defaultConfigJson"
+                rows="25"
+                class="config-editor"
+                @input="validateJson"
+              ></textarea>
+              <div v-if="jsonError" class="form-error">{{ jsonError }}</div>
+            </div>
+          </div>
+        </div>
+
+        <div class="modal-footer">
+          <button type="button" @click="closeDefaultConfigModal" class="btn-secondary">Cancel</button>
+          <button
+            type="button"
+            @click="saveDefaultConfig"
+            :disabled="savingDefaultConfig || (defaultConfigTab === 'json' && !!jsonError)"
+            class="btn-primary"
+          >
+            {{ savingDefaultConfig ? 'Saving...' : 'Save Default Config' }}
+          </button>
+        </div>
+      </div>
+    </div>
+
   </div>
 </template>
 
@@ -238,11 +451,61 @@ const sortColumn = ref('simple_id')
 const sortDirection = ref('desc')
 const ntpServersText = ref('')
 const tunnelLoading = ref({})
+const defaultConfigModalVisible = ref(false)
+const defaultConfigTab = ref('interactive')
+const defaultConfigJson = ref('')
+const savingDefaultConfig = ref(false)
+const jsonError = ref(null)
+const defaultNtpServersText = ref('')
+const defaultConfig = ref({
+  force_cloud: false,
+  cfg_polling_timeout: 30,
+  ble: {
+    enabled: true,
+    batch_interval_ms: 2500,
+    uuid_filter_hex: '',
+    upload_endpoint: ''
+  },
+  wifi: {
+    client_enabled: false,
+    ssid: '',
+    psk: '',
+    monitor_enabled: true,
+    batch_interval_ms: 10000,
+    upload_endpoint: ''
+  },
+  ssh_tunnel: {
+    enabled: false,
+    server: '192.168.5.4',
+    port: 22,
+    user: 'tunnel',
+    remote_port: 0,
+    keepalive_interval: 30
+  },
+  dashboard_tunnel: {
+    enabled: false,
+    server: '192.168.5.4',
+    port: 22,
+    user: 'tunnel',
+    remote_port: 0,
+    keepalive_interval: 30
+  },
+  dashboard: {
+    enabled: true
+  },
+  net: {
+    ntp: {
+      servers: ['pool.ntp.org', 'time.google.com']
+    }
+  },
+  debug: false
+})
 let searchDebounceTimer = null
 let pollingInterval = null
 
 const config = ref({
   force_cloud: false,
+  cfg_polling_timeout: 30,
   ble: {
     enabled: true,
     batch_interval_ms: 2500,
@@ -349,11 +612,35 @@ function formatDate(dateStr) {
   return date.toLocaleString()
 }
 
+function formatRelativeTime(dateStr) {
+  if (!dateStr) return 'Never'
+
+  const now = new Date()
+  const then = new Date(dateStr)
+  const diffMs = now - then
+  const diffMinutes = Math.floor(diffMs / 60000)
+
+  if (diffMinutes < 1) {
+    return 'Just now'
+  } else if (diffMinutes < 120) {
+    return `${diffMinutes} min ago`
+  } else {
+    const diffHours = Math.floor(diffMinutes / 60)
+    if (diffHours < 24) {
+      return `${diffHours} hour${diffHours > 1 ? 's' : ''} ago`
+    } else {
+      const diffDays = Math.floor(diffHours / 24)
+      return `${diffDays} day${diffDays > 1 ? 's' : ''} ago`
+    }
+  }
+}
+
 function showEditModal(device) {
   editingDevice.value = device
   // Deep copy device config with defaults
   config.value = {
     force_cloud: device.config?.force_cloud ?? false,
+    cfg_polling_timeout: device.config?.cfg_polling_timeout ?? 30,
     ble: {
       enabled: device.config?.ble?.enabled ?? true,
       batch_interval_ms: device.config?.ble?.batch_interval_ms ?? 2500,
@@ -423,6 +710,31 @@ async function saveDevice() {
   }
 }
 
+async function deleteDevice() {
+  const deviceName = `#${editingDevice.value.simple_id} (${editingDevice.value.mac_address})`
+
+  const confirmed = confirm(
+    `Are you sure you want to DELETE device ${deviceName}?\n\n` +
+    `This will permanently remove the device from the system.\n` +
+    `The device will need to re-register to be used again.\n\n` +
+    `This action CANNOT be undone!`
+  )
+
+  if (!confirmed) {
+    return
+  }
+
+  saving.value = true
+  try {
+    await devicesApi.deleteSuperadmin(editingDevice.value.id)
+    await loadDevices()
+    closeModal()
+  } catch (err) {
+    alert(err.response?.data?.detail || 'Failed to delete device')
+    saving.value = false
+  }
+}
+
 async function openTunnel(device, tunnelType) {
   const loadingKey = `${device.id}:${tunnelType}`
   tunnelLoading.value[loadingKey] = true
@@ -480,6 +792,192 @@ function openDashboard(device) {
   openTunnel(device, 'dashboard')
 }
 
+async function toggleBLE(device, event) {
+  event.preventDefault()
+
+  const newState = !(device.config?.ble?.enabled ?? false)
+  const action = newState ? 'enable' : 'disable'
+
+  if (!confirm(`Are you sure you want to ${action} BLE scanner for device #${device.simple_id}?`)) {
+    return
+  }
+
+  try {
+    const updatedConfig = {
+      ...device.config,
+      ble: {
+        ...(device.config?.ble || {}),
+        enabled: newState
+      }
+    }
+
+    await devicesApi.updateSuperadmin(device.id, { config: updatedConfig })
+    await loadDevices()
+  } catch (err) {
+    alert(err.response?.data?.detail || 'Failed to update BLE scanner')
+  }
+}
+
+async function toggleWiFi(device, event) {
+  event.preventDefault()
+
+  const newState = !(device.config?.wifi?.monitor_enabled ?? false)
+  const action = newState ? 'enable' : 'disable'
+
+  if (!confirm(`Are you sure you want to ${action} WiFi scanner for device #${device.simple_id}?`)) {
+    return
+  }
+
+  try {
+    const updatedConfig = {
+      ...device.config,
+      wifi: {
+        ...(device.config?.wifi || {}),
+        monitor_enabled: newState
+      }
+    }
+
+    await devicesApi.updateSuperadmin(device.id, { config: updatedConfig })
+    await loadDevices()
+  } catch (err) {
+    alert(err.response?.data?.detail || 'Failed to update WiFi scanner')
+  }
+}
+
+async function showDefaultConfigModal() {
+  try {
+    const configData = await devicesApi.getDefaultConfig()
+
+    // Load into interactive form
+    defaultConfig.value = {
+      force_cloud: configData.force_cloud ?? false,
+      cfg_polling_timeout: configData.cfg_polling_timeout ?? 30,
+      ble: {
+        enabled: configData.ble?.enabled ?? true,
+        batch_interval_ms: configData.ble?.batch_interval_ms ?? 2500,
+        uuid_filter_hex: configData.ble?.uuid_filter_hex ?? '',
+        upload_endpoint: configData.ble?.upload_endpoint ?? ''
+      },
+      wifi: {
+        client_enabled: configData.wifi?.client_enabled ?? false,
+        ssid: configData.wifi?.ssid ?? '',
+        psk: configData.wifi?.psk ?? '',
+        monitor_enabled: configData.wifi?.monitor_enabled ?? true,
+        batch_interval_ms: configData.wifi?.batch_interval_ms ?? 10000,
+        upload_endpoint: configData.wifi?.upload_endpoint ?? ''
+      },
+      ssh_tunnel: configData.ssh_tunnel || {
+        enabled: false,
+        server: '192.168.5.4',
+        port: 22,
+        user: 'tunnel',
+        remote_port: 0,
+        keepalive_interval: 30
+      },
+      dashboard_tunnel: configData.dashboard_tunnel || {
+        enabled: false,
+        server: '192.168.5.4',
+        port: 22,
+        user: 'tunnel',
+        remote_port: 0,
+        keepalive_interval: 30
+      },
+      dashboard: {
+        enabled: configData.dashboard?.enabled ?? true
+      },
+      net: {
+        ntp: {
+          servers: configData.net?.ntp?.servers ?? ['pool.ntp.org', 'time.google.com']
+        }
+      },
+      debug: configData.debug ?? false
+    }
+
+    // Convert NTP servers to text
+    defaultNtpServersText.value = defaultConfig.value.net.ntp.servers.join(', ')
+
+    // Also load into JSON
+    defaultConfigJson.value = JSON.stringify(configData, null, 2)
+
+    jsonError.value = null
+    defaultConfigTab.value = 'interactive'
+    defaultConfigModalVisible.value = true
+  } catch (err) {
+    alert(err.response?.data?.detail || 'Failed to load default config')
+  }
+}
+
+function closeDefaultConfigModal() {
+  defaultConfigModalVisible.value = false
+  defaultConfigTab.value = 'interactive'
+  defaultConfigJson.value = ''
+  jsonError.value = null
+}
+
+function onDefaultWifiClientChange() {
+  if (defaultConfig.value.wifi.client_enabled) {
+    defaultConfig.value.wifi.monitor_enabled = false
+  }
+}
+
+function onDefaultWifiMonitorChange() {
+  if (defaultConfig.value.wifi.monitor_enabled) {
+    defaultConfig.value.wifi.client_enabled = false
+  }
+}
+
+function switchToJsonTab() {
+  // Convert interactive form to JSON before switching
+  defaultConfig.value.net.ntp.servers = defaultNtpServersText.value
+    .split(',')
+    .map(s => s.trim())
+    .filter(s => s.length > 0)
+
+  defaultConfigJson.value = JSON.stringify(defaultConfig.value, null, 2)
+  defaultConfigTab.value = 'json'
+}
+
+function validateJson() {
+  try {
+    JSON.parse(defaultConfigJson.value)
+    jsonError.value = null
+  } catch (e) {
+    jsonError.value = `Invalid JSON: ${e.message}`
+  }
+}
+
+async function saveDefaultConfig() {
+  try {
+    let configToSave
+
+    if (defaultConfigTab.value === 'interactive') {
+      // Parse NTP servers from text
+      defaultConfig.value.net.ntp.servers = defaultNtpServersText.value
+        .split(',')
+        .map(s => s.trim())
+        .filter(s => s.length > 0)
+
+      configToSave = defaultConfig.value
+    } else {
+      // Validate and parse JSON
+      configToSave = JSON.parse(defaultConfigJson.value)
+    }
+
+    savingDefaultConfig.value = true
+    await devicesApi.updateDefaultConfig(configToSave)
+    alert('Default configuration saved successfully!')
+    closeDefaultConfigModal()
+  } catch (err) {
+    if (err instanceof SyntaxError) {
+      jsonError.value = `Invalid JSON: ${err.message}`
+    } else {
+      alert(err.response?.data?.detail || 'Failed to save default config')
+    }
+  } finally {
+    savingDefaultConfig.value = false
+  }
+}
+
 onMounted(() => {
   loadDevices()
   loadOrganizations()
@@ -511,6 +1009,7 @@ onBeforeUnmount(() => {
 
 /* Filters Row */
 .filters-row { display: flex; gap: 16px; align-items: center; margin-bottom: 20px; }
+.btn-edit-default { margin-left: auto; }
 .search-box { position: relative; flex: 1; max-width: 400px; }
 .search-input { width: 100%; padding: 10px 40px 10px 14px; border: 1px solid #e2e8f0; border-radius: 8px; font-size: 14px; transition: border-color 0.2s; }
 .search-input:focus { outline: none; border-color: #667eea; }
@@ -553,14 +1052,22 @@ code { background: #f7fafc; padding: 4px 8px; border-radius: 4px; font-family: m
 .modal-header h2 { font-size: 24px; font-weight: 700; color: #1a202c; }
 .btn-close { width: 32px; height: 32px; border: none; background: none; font-size: 32px; color: #718096; cursor: pointer; line-height: 1; }
 .btn-close:hover { color: #1a202c; }
+.modal-tabs { display: flex; border-bottom: 2px solid #e2e8f0; padding: 0 24px; }
+.tab-button { padding: 12px 24px; background: none; border: none; border-bottom: 3px solid transparent; color: #718096; font-weight: 500; font-size: 14px; cursor: pointer; transition: all 0.2s; margin-bottom: -2px; }
+.tab-button:hover { color: #4a5568; background: #f7fafc; }
+.tab-button.active { color: #667eea; border-bottom-color: #667eea; }
 .modal-body { padding: 16px; }
 .modal-footer { display: flex; justify-content: flex-end; gap: 12px; padding: 16px; border-top: 1px solid #e2e8f0; }
+.btn-delete { margin-right: auto; }
 .form-group { margin-bottom: 12px; }
 .form-group label { display: block; margin-bottom: 4px; font-weight: 500; color: #4a5568; font-size: 13px; }
 .form-group input, .form-group select { width: 100%; padding: 8px 10px; border: 1px solid #e2e8f0; border-radius: 6px; font-size: 13px; transition: border-color 0.2s; }
 .form-group input:focus, .form-group select:focus { outline: none; border-color: #667eea; }
 .form-group input:disabled { background: #f7fafc; color: #718096; }
 .form-hint { margin-top: 4px; font-size: 11px; color: #718096; }
+.config-editor { width: 100%; padding: 12px; border: 1px solid #e2e8f0; border-radius: 6px; font-family: 'Courier New', monospace; font-size: 13px; line-height: 1.5; resize: vertical; transition: border-color 0.2s; }
+.config-editor:focus { outline: none; border-color: #667eea; }
+.form-error { margin-top: 8px; padding: 8px 12px; background: #fed7d7; color: #742a2a; border-radius: 6px; font-size: 13px; }
 
 /* Config sections */
 .config-section { border: 1px solid #e2e8f0; border-radius: 6px; padding: 12px; margin-bottom: 12px; background: #fafafa; }
@@ -581,6 +1088,16 @@ code { background: #f7fafc; padding: 4px 8px; border-radius: 4px; font-family: m
 .toggle-switch input:checked + .toggle-slider:before { transform: translateX(22px); }
 .toggle-switch input:disabled + .toggle-slider { opacity: 0.5; cursor: not-allowed; }
 
+/* Toggle switch inline (for table cells) */
+.toggle-switch-inline { position: relative; display: inline-block; width: 38px; height: 20px; }
+.toggle-switch-inline input { opacity: 0; width: 0; height: 0; }
+.toggle-switch-inline .toggle-slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #cbd5e0; border-radius: 20px; transition: 0.3s; }
+.toggle-switch-inline .toggle-slider:before { position: absolute; content: ""; height: 14px; width: 14px; left: 3px; bottom: 3px; background-color: white; border-radius: 50%; transition: 0.3s; }
+.toggle-switch-inline input:checked + .toggle-slider { background-color: #667eea; }
+.toggle-switch-inline input:checked + .toggle-slider:before { transform: translateX(18px); }
+.toggle-switch-inline input:disabled + .toggle-slider { opacity: 0.5; cursor: not-allowed; }
+.text-center { text-align: center; }
+
 /* Toggle content (expanded section) */
 .toggle-content { padding-left: 0; margin-top: 8px; border-top: 1px solid #e2e8f0; padding-top: 12px; }
 .toggle-content .form-group:last-child { margin-bottom: 0; }