Skip to content

Commit 6d7b038

Browse files
authored
Battery/AC power, plus more (#355)
* feat: create enum for thermal states * feat: create enum for SystemIdleStates * feat: create PowerMonitor class * feat: create facade wrapper * feat: create enum for power states * feat: create events for PowerMonitor * Fix styling
1 parent 1c4d2f8 commit 6d7b038

File tree

8 files changed

+175
-0
lines changed

8 files changed

+175
-0
lines changed

src/Enums/PowerStatesEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Native\Laravel\Enums;
4+
5+
enum PowerStatesEnum: string
6+
{
7+
case AC = 'on-ac';
8+
case BATTERY = 'on-battery';
9+
}

src/Enums/SystemIdleStatesEnum.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Native\Laravel\Enums;
4+
5+
enum SystemIdleStatesEnum: string
6+
{
7+
case ACTIVE = 'active';
8+
case IDLE = 'idle';
9+
case LOCKED = 'locked';
10+
case UNKNOWN = 'unknown';
11+
}

src/Enums/ThermalStatesEnum.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Native\Laravel\Enums;
4+
5+
enum ThermalStatesEnum: string
6+
{
7+
case UNKNOWN = 'unknown';
8+
case NOMINAL = 'nominal';
9+
case FAIR = 'fair';
10+
case SERIOUS = 'serious';
11+
case CRITICAL = 'critical';
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
use Native\Laravel\Enums\PowerStatesEnum;
11+
12+
class PowerStateChanged implements ShouldBroadcastNow
13+
{
14+
use Dispatchable, InteractsWithSockets, SerializesModels;
15+
16+
public PowerStatesEnum $state;
17+
18+
public function __construct(string $state)
19+
{
20+
$this->state = PowerStatesEnum::from($state);
21+
}
22+
23+
public function broadcastOn()
24+
{
25+
return [
26+
new Channel('nativephp'),
27+
];
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class SpeedLimitChanged implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public int $limit;
16+
17+
public function __construct(string $limit)
18+
{
19+
$this->limit = (int) $limit;
20+
}
21+
22+
public function broadcastOn()
23+
{
24+
return [
25+
new Channel('nativephp'),
26+
];
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\PowerMonitor;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
use Native\Laravel\Enums\ThermalStatesEnum;
11+
12+
class ThermalStateChanged implements ShouldBroadcastNow
13+
{
14+
use Dispatchable, InteractsWithSockets, SerializesModels;
15+
16+
public ThermalStatesEnum $state;
17+
18+
public function __construct(string $state)
19+
{
20+
$this->state = ThermalStatesEnum::from($state);
21+
}
22+
23+
public function broadcastOn()
24+
{
25+
return [
26+
new Channel('nativephp'),
27+
];
28+
}
29+
}

src/Facades/PowerMonitor.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Native\Laravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static \Native\Laravel\Enums\SystemIdelStatesEnum getSystemIdleState(int $threshold)
9+
* @method static int getSystemIdleTime()
10+
* @method static \Native\Laravel\Enums\ThermalStatesEnum getCurrentThermalState()
11+
* @method static bool isOnBatteryPower()
12+
*/
13+
class PowerMonitor extends Facade
14+
{
15+
protected static function getFacadeAccessor()
16+
{
17+
return \Native\Laravel\PowerMonitor::class;
18+
}
19+
}

src/PowerMonitor.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Native\Laravel;
4+
5+
use Native\Laravel\Client\Client;
6+
use Native\Laravel\Enums\SystemIdleStatesEnum;
7+
use Native\Laravel\Enums\ThermalStatesEnum;
8+
9+
class PowerMonitor
10+
{
11+
public function __construct(protected Client $client) {}
12+
13+
public function getSystemIdleState(int $threshold): SystemIdleStatesEnum
14+
{
15+
$result = $this->client->get('power-monitor/get-system-idle-state', [
16+
'threshold' => $threshold,
17+
])->json('result');
18+
19+
return SystemIdleStatesEnum::tryFrom($result) ?? SystemIdleStatesEnum::UNKNOWN;
20+
}
21+
22+
public function getSystemIdleTime(): int
23+
{
24+
return $this->client->get('power-monitor/get-system-idle-time')->json('result');
25+
}
26+
27+
public function getCurrentThermalState(): ThermalStatesEnum
28+
{
29+
$result = $this->client->get('power-monitor/get-current-thermal-state')->json('result');
30+
31+
return ThermalStatesEnum::tryFrom($result) ?? ThermalStatesEnum::UNKNOWN;
32+
}
33+
34+
public function isOnBatteryPower(): bool
35+
{
36+
return $this->client->get('power-monitor/is-on-battery-power')->json('result');
37+
}
38+
}

0 commit comments

Comments
 (0)