@@ -55,7 +55,7 @@ double mcu_temp = 0;
5555
5656TerransPowerBoxGoV2::TerransPowerBoxGoV2 () : INDI::PowerInterface(this )
5757{
58- setVersion (1 , 0 );
58+ setVersion (1 , 1 );
5959}
6060
6161bool TerransPowerBoxGoV2::initProperties ()
@@ -421,6 +421,11 @@ void TerransPowerBoxGoV2::TimerHit()
421421
422422 // Update PowerInterface properties
423423 char res[CMD_LEN] = {0 };
424+ bool powerChanged = false ;
425+ bool usbChanged = false ;
426+ bool stateSaveChanged = false ;
427+ bool sensorsChanged = false ;
428+ bool mcuTempChanged = false ;
424429
425430 // Get Power Channel States
426431 for (size_t i = 0 ; i < PI::PowerChannelsSP.size (); ++i)
@@ -430,22 +435,26 @@ void TerransPowerBoxGoV2::TimerHit()
430435 {
431436 if (sendCommand (portCmds[i], res))
432437 {
433- if (!strcmp (res, " *D1NNN" )) // Assuming D1NNN for ON, D0NNN for OFF
438+ // Response format: *DA1NNN (channel A, state 1), *DB0NNN (channel B, state 0), etc.
439+ // res[2] is the channel letter (A, B, C, D, E)
440+ // res[3] is the state ('1' for ON, '0' for OFF)
441+ char expectedChannel = ' A' + i;
442+ if (strlen (res) >= 4 && res[2 ] == expectedChannel)
434443 {
435- PI::PowerChannelsSP[i].setState (ISS_ON);
436- }
437- else if (!strcmp (res, " *D0NNN" ))
438- {
439- PI::PowerChannelsSP[i].setState (ISS_OFF);
440- }
441- else
442- {
443- PI::PowerChannelsSP[i].setState (ISS_OFF); // Default to OFF on unknown response
444+ ISState newState = (res[3 ] == ' 1' ) ? ISS_ON : ISS_OFF;
445+ if (PI::PowerChannelsSP[i].getState () != newState)
446+ {
447+ PI::PowerChannelsSP[i].setState (newState);
448+ powerChanged = true ;
449+ }
444450 }
445451 }
446452 }
447453 }
448- PI::PowerChannelsSP.apply ();
454+ if (powerChanged)
455+ {
456+ PI::PowerChannelsSP.apply ();
457+ }
449458
450459 // Get USB Port States
451460 for (size_t i = 0 ; i < PI::USBPortSP.size (); ++i)
@@ -455,95 +464,125 @@ void TerransPowerBoxGoV2::TimerHit()
455464 {
456465 if (sendCommand (usbCmds[i], res))
457466 {
458- // Assuming *UA111N for ON, *UA000N for OFF for USB3.0 A/B
459- // Assuming *UE11NN for ON, *UE00NN for OFF for USB2.0 E/F
460- if ((i == 0 || i == 1 ) && ! strcmp (res, " *U111N " ) )
461- {
462- PI::USBPortSP[i]. setState (ISS_ON) ;
463- }
464- else if ((i == 0 || i == 1 ) && ! strcmp ( res, " *U000N " ) )
467+ // Response format: *UA000N (USB A, OFF), *UB111N (USB B, ON) for USB3.0 A/B
468+ // Response format: *UE00NN (USB E, OFF), *UF11NN (USB F, ON) for USB2.0 E/F
469+ // res[2] is the USB port letter (A, B, E, F )
470+ // res[3] is the first state bit ('1' for ON, '0' for OFF)
471+ const char *portLetters = " ABEF " ;
472+ char expectedPort = portLetters[i];
473+ if (strlen (res) >= 4 && res[ 2 ] == expectedPort )
465474 {
466- PI::USBPortSP[i].setState (ISS_OFF);
467- }
468- else if ((i == 2 || i == 3 ) && !strcmp (res, " *U11NN" ))
469- {
470- PI::USBPortSP[i].setState (ISS_ON);
471- }
472- else if ((i == 2 || i == 3 ) && !strcmp (res, " *U00NN" ))
473- {
474- PI::USBPortSP[i].setState (ISS_OFF);
475- }
476- else
477- {
478- PI::USBPortSP[i].setState (ISS_OFF); // Default to OFF on unknown response
475+ ISState newState = (res[3 ] == ' 1' ) ? ISS_ON : ISS_OFF;
476+ if (PI::USBPortSP[i].getState () != newState)
477+ {
478+ PI::USBPortSP[i].setState (newState);
479+ usbChanged = true ;
480+ }
479481 }
480482 }
481483 }
482484 }
483- PI::USBPortSP.apply ();
485+ if (usbChanged)
486+ {
487+ PI::USBPortSP.apply ();
488+ }
484489
485490 // Get State Save Switch
486491 if (sendCommand (" >GS#" , res))
487492 {
493+ // Response format: *SS1NNN (save enabled), *SS0NNN (save disabled)
488494 if (!strcmp (res, " *SS1NNN" ))
489495 {
490- StateSaveSP[0 ].setState (ISS_ON);
491- StateSaveSP[1 ].setState (ISS_OFF);
496+ if (StateSaveSP[0 ].getState () != ISS_ON || StateSaveSP[1 ].getState () != ISS_OFF)
497+ {
498+ StateSaveSP[0 ].setState (ISS_ON);
499+ StateSaveSP[1 ].setState (ISS_OFF);
500+ stateSaveChanged = true ;
501+ }
492502 }
493503 else if (!strcmp (res, " *SS0NNN" ))
494504 {
495- StateSaveSP[0 ].setState (ISS_OFF);
496- StateSaveSP[1 ].setState (ISS_ON);
497- }
498- else
499- {
500- StateSaveSP[0 ].setState (ISS_OFF);
501- StateSaveSP[1 ].setState (ISS_OFF);
505+ if (StateSaveSP[0 ].getState () != ISS_OFF || StateSaveSP[1 ].getState () != ISS_ON)
506+ {
507+ StateSaveSP[0 ].setState (ISS_OFF);
508+ StateSaveSP[1 ].setState (ISS_ON);
509+ stateSaveChanged = true ;
510+ }
502511 }
503512 }
504- StateSaveSP.apply ();
513+ if (stateSaveChanged)
514+ {
515+ StateSaveSP.apply ();
516+ }
505517
506518 // Get Input Voltage, Current, Power
519+ double newVoltage = PI::PowerSensorsNP[PI::SENSOR_VOLTAGE].getValue ();
520+ double newCurrent = PI::PowerSensorsNP[PI::SENSOR_CURRENT].getValue ();
521+ double newPower = PI::PowerSensorsNP[PI::SENSOR_POWER].getValue ();
522+
507523 if (sendCommand (" >GPA#" , res))
508524 {
509525 ch1_bus = (res[6 ] - 0x30 ) + ((res[5 ] - 0x30 ) * 10 ) + ((res[4 ] - 0x30 ) * 100 ) + ((res[3 ] - 0x30 ) * 1000 );
510526 ch1_bus = ch1_bus * 4 / 1000 ;
511- PI::PowerSensorsNP[PI::SENSOR_VOLTAGE]. setValue ( ch1_bus) ;
527+ newVoltage = ch1_bus;
512528 }
513529
514530 if (sendCommand (" >GPB#" , res))
515531 {
516532 ch1_shuntv = (res[6 ] - 0x30 ) + ((res[5 ] - 0x30 ) * 10 ) + ((res[4 ] - 0x30 ) * 100 ) + ((res[3 ] - 0x30 ) * 1000 );
517533 ch1_current = ch1_shuntv * 10 / 1000000 / 0.002 ;
518- PI::PowerSensorsNP[PI::SENSOR_CURRENT].setValue (ch1_current);
519- PI::PowerSensorsNP[PI::SENSOR_POWER].setValue (ch1_current * ch1_bus); // Calculate power
534+ newCurrent = ch1_current;
535+ newPower = ch1_current * ch1_bus; // Calculate power
536+ }
537+
538+ if (PI::PowerSensorsNP[PI::SENSOR_VOLTAGE].getValue () != newVoltage ||
539+ PI::PowerSensorsNP[PI::SENSOR_CURRENT].getValue () != newCurrent ||
540+ PI::PowerSensorsNP[PI::SENSOR_POWER].getValue () != newPower)
541+ {
542+ PI::PowerSensorsNP[PI::SENSOR_VOLTAGE].setValue (newVoltage);
543+ PI::PowerSensorsNP[PI::SENSOR_CURRENT].setValue (newCurrent);
544+ PI::PowerSensorsNP[PI::SENSOR_POWER].setValue (newPower);
545+ PI::PowerSensorsNP.setState (IPS_OK);
546+ sensorsChanged = true ;
547+ }
548+ if (sensorsChanged)
549+ {
550+ PI::PowerSensorsNP.apply ();
520551 }
521- PI::PowerSensorsNP.setState (IPS_OK);
522- PI::PowerSensorsNP.apply ();
523552
524553 // Get MCU Temperature
525554 if (sendCommand (" >GC#" , res))
526555 {
556+ double newMcuTemp = MCUTempNP[0 ].getValue ();
527557 mcu_temp = (res[6 ] - 0x30 ) + ((res[5 ] - 0x30 ) * 10 ) + ((res[4 ] - 0x30 ) * 100 ) + ((res[3 ] - 0x30 ) * 1000 );
528558 if (mcu_temp == 0 )
529559 {
530- MCUTempNP[ 0 ]. setValue ( 0 ) ;
560+ newMcuTemp = 0 ;
531561 }
532562 else
533563 {
534564 if (res[2 ] == ' A' )
535565 {
536566 mcu_temp = mcu_temp / 100 ;
537- MCUTempNP[ 0 ]. setValue ( mcu_temp) ;
567+ newMcuTemp = mcu_temp;
538568 }
539569 else if (res[2 ] == ' B' )
540570 {
541571 mcu_temp = mcu_temp / 100 ;
542572 mcu_temp = mcu_temp * (-1 );
543- MCUTempNP[ 0 ]. setValue ( mcu_temp) ;
573+ newMcuTemp = mcu_temp;
544574 }
545575 }
546- MCUTempNP.setState (IPS_OK);
576+
577+ if (MCUTempNP[0 ].getValue () != newMcuTemp)
578+ {
579+ MCUTempNP[0 ].setValue (newMcuTemp);
580+ MCUTempNP.setState (IPS_OK);
581+ mcuTempChanged = true ;
582+ }
583+ }
584+ if (mcuTempChanged)
585+ {
547586 MCUTempNP.apply ();
548587 }
549588
0 commit comments