askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
context-detection.esm.js · 16.2 KB · 451 lines
| 1 | /** |
| 2 | * Semantic Components v1.0.0 |
| 3 | * context-detection.js |
| 4 | * |
| 5 | * Intent-based, context-aware, modality-agnostic web components |
| 6 | * https://semantic-components.dev |
| 7 | * |
| 8 | * Copyright (c) 2025 Semantic Components Team |
| 9 | * Released under the MIT License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Context Detection System |
| 14 | * Automatically detects environmental context using device sensors and APIs |
| 15 | * - Ambient noise level |
| 16 | * - Number of viewers (via camera/proximity) |
| 17 | * - Lighting conditions |
| 18 | * - Device orientation |
| 19 | * - User activity/idle state |
| 20 | * - Network conditions |
| 21 | */ |
| 22 | |
| 23 | class ContextDetectionSystem { |
| 24 | constructor(contextManager) { |
| 25 | this.context = contextManager; |
| 26 | this.sensors = {}; |
| 27 | this.detectionInterval = null; |
| 28 | this.isRunning = false; |
| 29 | |
| 30 | this.init(); |
| 31 | } |
| 32 | |
| 33 | async init() { |
| 34 | await this.setupAudioDetection(); |
| 35 | await this.setupVideoDetection(); |
| 36 | await this.setupLightDetection(); |
| 37 | await this.setupMotionDetection(); |
| 38 | await this.setupIdleDetection(); |
| 39 | await this.setupNetworkDetection(); |
| 40 | |
| 41 | console.log('Context detection initialized'); |
| 42 | } |
| 43 | |
| 44 | // ======================================================================== |
| 45 | // AUDIO DETECTION - Ambient noise level |
| 46 | // ======================================================================== |
| 47 | |
| 48 | async setupAudioDetection() { |
| 49 | try { |
| 50 | const stream = await navigator.mediaDevices.getUserMedia({ |
| 51 | audio: { |
| 52 | echoCancellation: true, |
| 53 | noiseSuppression: false, |
| 54 | autoGainControl: false |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | const audioContext = new (window.AudioContext || window.webkitAudioContext)(); |
| 59 | const analyser = audioContext.createAnalyser(); |
| 60 | const microphone = audioContext.createMediaStreamSource(stream); |
| 61 | const dataArray = new Uint8Array(analyser.frequencyBinCount); |
| 62 | |
| 63 | analyser.smoothingTimeConstant = 0.8; |
| 64 | analyser.fftSize = 1024; |
| 65 | |
| 66 | microphone.connect(analyser); |
| 67 | |
| 68 | this.sensors.audio = { |
| 69 | analyser, |
| 70 | dataArray, |
| 71 | stream, |
| 72 | detectNoise: () => { |
| 73 | analyser.getByteFrequencyData(dataArray); |
| 74 | |
| 75 | // Calculate average volume |
| 76 | let sum = 0; |
| 77 | for (let i = 0; i < dataArray.length; i++) { |
| 78 | sum += dataArray[i]; |
| 79 | } |
| 80 | const average = sum / dataArray.length; |
| 81 | |
| 82 | // Convert to 0-100 scale |
| 83 | const noiseLevel = Math.min(100, (average / 255) * 100); |
| 84 | |
| 85 | return Math.round(noiseLevel); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | console.log('Audio detection enabled'); |
| 90 | } catch (error) { |
| 91 | console.warn('Audio detection not available:', error.message); |
| 92 | this.sensors.audio = { detectNoise: () => 0 }; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // ======================================================================== |
| 97 | // VIDEO DETECTION - Number of faces/viewers |
| 98 | // ======================================================================== |
| 99 | |
| 100 | async setupVideoDetection() { |
| 101 | try { |
| 102 | const stream = await navigator.mediaDevices.getUserMedia({ |
| 103 | video: { |
| 104 | width: { ideal: 640 }, |
| 105 | height: { ideal: 480 }, |
| 106 | facingMode: 'user' |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | // Create hidden video element for face detection |
| 111 | const video = document.createElement('video'); |
| 112 | video.srcObject = stream; |
| 113 | video.autoplay = true; |
| 114 | video.style.display = 'none'; |
| 115 | document.body.appendChild(video); |
| 116 | |
| 117 | // Simple face detection using canvas analysis |
| 118 | // In production, use Face-API.js or TensorFlow.js |
| 119 | const canvas = document.createElement('canvas'); |
| 120 | const ctx = canvas.getContext('2d'); |
| 121 | |
| 122 | this.sensors.video = { |
| 123 | video, |
| 124 | stream, |
| 125 | canvas, |
| 126 | ctx, |
| 127 | detectViewers: () => { |
| 128 | // This is a simplified version |
| 129 | // In production, use proper face detection library |
| 130 | canvas.width = video.videoWidth; |
| 131 | canvas.height = video.videoHeight; |
| 132 | |
| 133 | if (canvas.width === 0) return 1; // Default to 1 viewer |
| 134 | |
| 135 | ctx.drawImage(video, 0, 0); |
| 136 | |
| 137 | const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 138 | const data = imageData.data; |
| 139 | |
| 140 | // Simple motion/presence detection |
| 141 | let movement = 0; |
| 142 | for (let i = 0; i < data.length; i += 4) { |
| 143 | const brightness = (data[i] + data[i + 1] + data[i + 2]) / 3; |
| 144 | if (brightness > 50) movement++; |
| 145 | } |
| 146 | |
| 147 | const presenceRatio = movement / (data.length / 4); |
| 148 | |
| 149 | // Rough estimate: more presence = more viewers |
| 150 | if (presenceRatio > 0.3) return Math.min(5, Math.ceil(presenceRatio * 3)); |
| 151 | return 1; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | console.log('Video detection enabled'); |
| 156 | } catch (error) { |
| 157 | console.warn('Video detection not available:', error.message); |
| 158 | this.sensors.video = { detectViewers: () => 1 }; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // ======================================================================== |
| 163 | // LIGHT DETECTION - Ambient lighting |
| 164 | // ======================================================================== |
| 165 | |
| 166 | async setupLightDetection() { |
| 167 | try { |
| 168 | // Try AmbientLightSensor API (limited browser support) |
| 169 | if ('AmbientLightSensor' in window) { |
| 170 | const sensor = new AmbientLightSensor(); |
| 171 | sensor.start(); |
| 172 | |
| 173 | this.sensors.light = { |
| 174 | sensor, |
| 175 | detectLighting: () => { |
| 176 | const lux = sensor.illuminance || 500; |
| 177 | |
| 178 | if (lux < 50) return 'dark'; |
| 179 | if (lux < 200) return 'dim'; |
| 180 | if (lux < 1000) return 'normal'; |
| 181 | return 'bright'; |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | console.log('Light sensor enabled'); |
| 186 | } else { |
| 187 | // Fallback: Use time of day |
| 188 | this.sensors.light = { |
| 189 | detectLighting: () => { |
| 190 | const hour = new Date().getHours(); |
| 191 | |
| 192 | if (hour < 6 || hour > 20) return 'dark'; |
| 193 | if (hour < 8 || hour > 18) return 'dim'; |
| 194 | return 'normal'; |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | console.log('Light detection using time-based fallback'); |
| 199 | } |
| 200 | } catch (error) { |
| 201 | console.warn('Light detection not available:', error.message); |
| 202 | this.sensors.light = { detectLighting: () => 'normal' }; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // ======================================================================== |
| 207 | // MOTION DETECTION - Device orientation and movement |
| 208 | // ======================================================================== |
| 209 | |
| 210 | async setupMotionDetection() { |
| 211 | try { |
| 212 | if ('DeviceOrientationEvent' in window) { |
| 213 | let orientation = { alpha: 0, beta: 0, gamma: 0 }; |
| 214 | |
| 215 | window.addEventListener('deviceorientation', (event) => { |
| 216 | orientation = { |
| 217 | alpha: event.alpha || 0, |
| 218 | beta: event.beta || 0, |
| 219 | gamma: event.gamma || 0 |
| 220 | }; |
| 221 | }); |
| 222 | |
| 223 | this.sensors.motion = { |
| 224 | orientation, |
| 225 | detectOrientation: () => { |
| 226 | // Detect if device is portrait or landscape |
| 227 | // Detect if device is flat (on table) vs held |
| 228 | const beta = Math.abs(orientation.beta); |
| 229 | |
| 230 | if (beta < 30) return 'flat'; // On table/desk |
| 231 | if (beta < 60) return 'tilted'; // Partially held |
| 232 | return 'held'; // Actively held |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | console.log('Motion detection enabled'); |
| 237 | } else { |
| 238 | this.sensors.motion = { detectOrientation: () => 'unknown' }; |
| 239 | } |
| 240 | } catch (error) { |
| 241 | console.warn('Motion detection not available:', error.message); |
| 242 | this.sensors.motion = { detectOrientation: () => 'unknown' }; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // ======================================================================== |
| 247 | // IDLE DETECTION - User activity state |
| 248 | // ======================================================================== |
| 249 | |
| 250 | async setupIdleDetection() { |
| 251 | let lastActivity = Date.now(); |
| 252 | let idleTimeout = null; |
| 253 | |
| 254 | const resetIdleTimer = () => { |
| 255 | lastActivity = Date.now(); |
| 256 | if (idleTimeout) { |
| 257 | clearTimeout(idleTimeout); |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | // Track user activity |
| 262 | ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart'].forEach(event => { |
| 263 | document.addEventListener(event, resetIdleTimer, true); |
| 264 | }); |
| 265 | |
| 266 | this.sensors.idle = { |
| 267 | lastActivity, |
| 268 | detectIdleState: () => { |
| 269 | const idleTime = Date.now() - lastActivity; |
| 270 | |
| 271 | if (idleTime < 5000) return 'active'; // Active in last 5 seconds |
| 272 | if (idleTime < 30000) return 'idle'; // Idle for 5-30 seconds |
| 273 | return 'away'; // Away for more than 30 seconds |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | // Try to use official Idle Detection API if available |
| 278 | if ('IdleDetector' in window) { |
| 279 | try { |
| 280 | const idleDetector = new IdleDetector(); |
| 281 | idleDetector.addEventListener('change', () => { |
| 282 | const userState = idleDetector.userState; // 'active' or 'idle' |
| 283 | const screenState = idleDetector.screenState; // 'locked' or 'unlocked' |
| 284 | |
| 285 | console.log(`Idle state: ${userState}, Screen: ${screenState}`); |
| 286 | }); |
| 287 | |
| 288 | await idleDetector.start({ threshold: 60000 }); |
| 289 | console.log('Idle detection API enabled'); |
| 290 | } catch (error) { |
| 291 | console.warn('Idle detection API error:', error.message); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | console.log('Idle detection enabled'); |
| 296 | } |
| 297 | |
| 298 | // ======================================================================== |
| 299 | // NETWORK DETECTION - Connection quality |
| 300 | // ======================================================================== |
| 301 | |
| 302 | async setupNetworkDetection() { |
| 303 | try { |
| 304 | const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection; |
| 305 | |
| 306 | if (connection) { |
| 307 | this.sensors.network = { |
| 308 | connection, |
| 309 | detectQuality: () => { |
| 310 | const effectiveType = connection.effectiveType; // '4g', '3g', '2g', 'slow-2g' |
| 311 | const downlink = connection.downlink; // Mbps |
| 312 | const rtt = connection.rtt; // Round-trip time in ms |
| 313 | |
| 314 | if (effectiveType === '4g' && downlink > 10) return 'excellent'; |
| 315 | if (effectiveType === '4g' || downlink > 5) return 'good'; |
| 316 | if (effectiveType === '3g') return 'fair'; |
| 317 | return 'poor'; |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | connection.addEventListener('change', () => { |
| 322 | console.log('Network conditions changed:', this.sensors.network.detectQuality()); |
| 323 | this.updateContext(); |
| 324 | }); |
| 325 | |
| 326 | console.log('Network detection enabled'); |
| 327 | } else { |
| 328 | this.sensors.network = { detectQuality: () => 'unknown' }; |
| 329 | } |
| 330 | } catch (error) { |
| 331 | console.warn('Network detection not available:', error.message); |
| 332 | this.sensors.network = { detectQuality: () => 'unknown' }; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // ======================================================================== |
| 337 | // CONTINUOUS DETECTION |
| 338 | // ======================================================================== |
| 339 | |
| 340 | start() { |
| 341 | if (this.isRunning) return; |
| 342 | |
| 343 | this.isRunning = true; |
| 344 | |
| 345 | // Update context every second |
| 346 | this.detectionInterval = setInterval(() => { |
| 347 | this.updateContext(); |
| 348 | }, 1000); |
| 349 | |
| 350 | // Initial update |
| 351 | this.updateContext(); |
| 352 | |
| 353 | console.log('Context detection started'); |
| 354 | } |
| 355 | |
| 356 | stop() { |
| 357 | if (!this.isRunning) return; |
| 358 | |
| 359 | this.isRunning = false; |
| 360 | |
| 361 | if (this.detectionInterval) { |
| 362 | clearInterval(this.detectionInterval); |
| 363 | this.detectionInterval = null; |
| 364 | } |
| 365 | |
| 366 | // Stop all sensors |
| 367 | if (this.sensors.audio?.stream) { |
| 368 | this.sensors.audio.stream.getTracks().forEach(track => track.stop()); |
| 369 | } |
| 370 | if (this.sensors.video?.stream) { |
| 371 | this.sensors.video.stream.getTracks().forEach(track => track.stop()); |
| 372 | } |
| 373 | if (this.sensors.light?.sensor) { |
| 374 | this.sensors.light.sensor.stop(); |
| 375 | } |
| 376 | |
| 377 | console.log('Context detection stopped'); |
| 378 | } |
| 379 | |
| 380 | updateContext() { |
| 381 | const updates = { |
| 382 | environment: { |
| 383 | noiseLevel: this.sensors.audio?.detectNoise() || 0, |
| 384 | viewerCount: this.sensors.video?.detectViewers() || 1, |
| 385 | lighting: this.sensors.light?.detectLighting() || 'normal', |
| 386 | orientation: this.sensors.motion?.detectOrientation() || 'unknown' |
| 387 | }, |
| 388 | user: { |
| 389 | idleState: this.sensors.idle?.detectIdleState() || 'active', |
| 390 | isBusy: this.sensors.idle?.detectIdleState() === 'active', |
| 391 | networkQuality: this.sensors.network?.detectQuality() || 'unknown' |
| 392 | } |
| 393 | }; |
| 394 | |
| 395 | // Update context manager |
| 396 | this.context.updateContext(updates); |
| 397 | } |
| 398 | |
| 399 | // ======================================================================== |
| 400 | // MANUAL SIMULATION (for testing without permissions) |
| 401 | // ======================================================================== |
| 402 | |
| 403 | simulate(config) { |
| 404 | // Allow manual simulation for testing |
| 405 | console.log('Simulating context:', config); |
| 406 | |
| 407 | this.context.updateContext({ |
| 408 | environment: { |
| 409 | noiseLevel: config.noiseLevel !== undefined ? config.noiseLevel : this.context.context.environment.noiseLevel, |
| 410 | viewerCount: config.viewerCount !== undefined ? config.viewerCount : this.context.context.environment.viewerCount, |
| 411 | lighting: config.lighting || this.context.context.environment.lighting, |
| 412 | isPublic: config.isPublic !== undefined ? config.isPublic : this.context.context.environment.isPublic |
| 413 | }, |
| 414 | user: { |
| 415 | isBusy: config.isBusy !== undefined ? config.isBusy : this.context.context.user.isBusy, |
| 416 | inFocusMode: config.inFocusMode !== undefined ? config.inFocusMode : this.context.context.user.inFocusMode |
| 417 | } |
| 418 | }); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Initialize context detection when ready |
| 423 | if (typeof window !== 'undefined') { |
| 424 | window.addEventListener('DOMContentLoaded', async () => { |
| 425 | if (window.semanticContext) { |
| 426 | window.contextDetection = new ContextDetectionSystem(window.semanticContext); |
| 427 | |
| 428 | // Don't auto-start (requires permissions) |
| 429 | // User can manually start via window.contextDetection.start() |
| 430 | |
| 431 | console.log('Context detection system ready. Call window.contextDetection.start() to begin.'); |
| 432 | |
| 433 | // Add keyboard shortcut to toggle (Ctrl+Shift+C) |
| 434 | document.addEventListener('keydown', (e) => { |
| 435 | if (e.ctrlKey && e.shiftKey && e.key === 'C') { |
| 436 | e.preventDefault(); |
| 437 | if (window.contextDetection.isRunning) { |
| 438 | window.contextDetection.stop(); |
| 439 | } else { |
| 440 | window.contextDetection.start(); |
| 441 | } |
| 442 | } |
| 443 | }); |
| 444 | } |
| 445 | }); |
| 446 | } |
| 447 | |
| 448 | // Export |
| 449 | |
| 450 | |
| 451 | export default window.semanticContext || {}; |