NativePHP
Windows support is here! 🔥 Read the full announcement →
Power Monitor

NativePHP is currently in alpha development

Let's get to beta!

The power monitor allows you to gather information about the power state of the device.

#
System Idle State

You can check if the system is idle with the getSystemIdleState method.

It expects a int $threshold argument, which is the number of seconds the system must be idle before it is considered idle. And it'll return an enum value of SystemIdleStatesEnum.

Copied!
1use Native\Laravel\Enums\SystemIdleStatesEnum;
2use Native\Laravel\Facades\PowerMonitor;
3 
4$state = PowerMonitor::getSystemIdleState(60);
5 
6if ($state === SystemIdleStatesEnum::IDLE) {
7 // The system is idle!
8}

The possible values for the SystemIdleStatesEnum enum are:

  • SystemIdleStatesEnum::ACTIVE
  • SystemIdleStatesEnum::IDLE
  • SystemIdleStatesEnum::LOCKED
  • SystemIdleStatesEnum::UNKNOWN

#
System Idle Time

You can get the number of seconds the system has been idle with the getSystemIdleTime method.

Copied!
1use Native\Laravel\Facades\PowerMonitor;
2 
3$seconds = PowerMonitor::getSystemIdleTime();

#
Current Thermal State

You can get the current thermal state of the system with the getCurrentThermalState method. It'll return an enum value of ThermalStatesEnum.

Copied!
1use Native\Laravel\Enums\ThermalStatesEnum;
2use Native\Laravel\Facades\PowerMonitor;
3 
4$thermalState = PowerMonitor::getCurrentThermalState();
5 
6if ($state === ThermalStatesEnum::CRITICAL) {
7 // Wow, the CPU is running hot!
8}

The possible values for the ThermalStatesEnum enum are:

  • ThermalStatesEnum::UNKNOWN
  • ThermalStatesEnum::NOMINAL
  • ThermalStatesEnum::FAIR
  • ThermalStatesEnum::SERIOUS
  • ThermalStatesEnum::CRITICAL

#
Battery Information

You can determine if the device is running on battery power or AC power with the isOnBatteryPower method.

Copied!
1use Native\Laravel\Facades\PowerMonitor;
2 
3if (PowerMonitor::isOnBatteryPower()) {
4 // The device is running on battery power.
5} else {
6 // The device is running on AC power.
7}

#
Events

You can listen to the following events to get handle when the system's power state changes:

#
Native\Laravel\Events\PowerStateChanged

This is fired whenever the power state of the system changes. For example, when the system goes from battery power to AC power, or vice versa.

The event contains a public $state property which is an enum value of Native\Laravel\Enums\PowerStatesEnum.

#
Native\Laravel\Events\SpeedLimitChanged

This is fired whenever the CPU speed limit changes, usually due to thermal throttling or low battery.

The event contains a public $limit property which is the percentage of the maximum CPU speed that is currently allowed.

#
Native\Laravel\Events\ThermalStateChanged

This is fired whenever the thermal state of the system changes.

The event contains a public $state property which is an enum value of Native\Laravel\Enums\ThermalStatesEnum.