@@ -286,6 +286,7 @@ export default function DashboardPage() {
286286 const [ deviceStatus , setDeviceStatus ] = useState < boolean > ( false ) ; // Device status (on/off)
287287 const [ ws , setWs ] = useState < WebSocket | null > ( null ) ; // WebSocket instance
288288 const [ timeframe , setTimeframe ] = useState ( "real-time" ) ; // Default to real-time (last 10 readings)
289+ const [ espID , setEspID ] = useState ( "" ) ;
289290
290291 // Initialize the device document in Firestore if it doesn't exist
291292 useEffect ( ( ) => {
@@ -448,6 +449,8 @@ export default function DashboardPage() {
448449 ws . onmessage = ( event ) => {
449450 const newData = JSON . parse ( event . data ) ;
450451
452+ if ( newData . device_id ) setEspID ( newData . device_id ) ;
453+
451454 // Add timestamp to the data if not present
452455 newData . timestamp = newData . timestamp || new Date ( ) . toISOString ( ) ;
453456
@@ -460,22 +463,28 @@ export default function DashboardPage() {
460463
461464 // Update total power consumed (only if power is valid)
462465 setTotalPowerConsumed ( ( prevTotal ) => {
463- // Make sure we're adding a valid number
464466 const powerIncrement = newData . power / 1000 || 0 ;
465467 const newTotal = ( prevTotal || 0 ) + powerIncrement ;
466- return isNaN ( newTotal ) ? 0 : newTotal ; // Ensure we never return NaN
468+ return isNaN ( newTotal ) ? 0 : newTotal ;
467469 } ) ;
468470
469- // Update device status
471+ // Update device status from WebSocket (primary source of truth)
470472 if ( newData . status !== undefined ) {
471473 setDeviceStatus ( newData . status ) ;
474+
475+ // Sync Firestore with the WebSocket status
476+ const deviceDocRef = doc ( db , "devices" , "ESP32_TEST_DEVICE" ) ;
477+ updateDoc ( deviceDocRef , {
478+ status : newData . status ? "on" : "off" ,
479+ last_updated : new Date ( ) . toISOString ( ) ,
480+ } ) ;
472481 }
473482
474- // Update state for UI (keep only the last 10 readings)
483+ // Update sensor data
475484 setSensorData ( ( prevData ) => {
476485 const updatedData = [ ...prevData , newData ] ;
477486 if ( updatedData . length > maxDataPoints ) {
478- return updatedData . slice ( - maxDataPoints ) ; // Keep only the last 10 readings
487+ return updatedData . slice ( - maxDataPoints ) ;
479488 }
480489 return updatedData ;
481490 } ) ;
@@ -498,22 +507,40 @@ export default function DashboardPage() {
498507 } ;
499508 } , [ ] ) ;
500509
501- // Handle device status toggle
502510 const handleToggleDeviceStatus = async ( checked : boolean ) => {
503- const deviceDocRef = doc ( db , "devices" , "ESP32_TEST_DEVICE" ) ; // Replace with your device ID
504- await updateDoc ( deviceDocRef , {
505- status : checked ? "on" : "off" ,
506- last_updated : new Date ( ) . toISOString ( ) ,
507- } ) ;
508-
509- // Send WebSocket message to ESP32
510- if ( ws ) {
511- ws . send (
512- JSON . stringify ( { device_id : "ESP32_TEST_DEVICE" , status : checked } )
513- ) ;
511+ if ( ! ws ) {
512+ console . error ( "WebSocket is not connected." ) ;
513+ return ;
514514 }
515515
516+ // Optimistically update the UI
516517 setDeviceStatus ( checked ) ;
518+
519+ // Send the toggle command via WebSocket
520+ try {
521+ const response = await fetch (
522+ `https://voltsense-server-110999938896.asia-south1.run.app/api/send-command` ,
523+ {
524+ method : "POST" ,
525+ headers : {
526+ "Content-Type" : "application/json" ,
527+ } ,
528+ body : JSON . stringify ( {
529+ device_id : espID ,
530+ command : "toggle_relay" ,
531+ } ) ,
532+ }
533+ ) ;
534+
535+ if ( ! response . ok ) {
536+ throw new Error ( "Failed to send command" ) ;
537+ }
538+ } catch ( error ) {
539+ console . error ( "Error toggling device:" , error ) ;
540+
541+ // Revert the UI state if the command fails
542+ setDeviceStatus ( ! checked ) ;
543+ }
517544 } ;
518545
519546 // For the currentPower calculation
0 commit comments