Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Battery/AC power, plus more #355

Merged
merged 7 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Enums/PowerStatesEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Native\Laravel\Enums;

enum PowerStatesEnum: string
{
case AC = 'on-ac';
case BATTERY = 'on-battery';
}
11 changes: 11 additions & 0 deletions src/Enums/SystemIdleStatesEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Native\Laravel\Enums;

enum SystemIdleStatesEnum: string
{
case ACTIVE = 'active';
case IDLE = 'idle';
case LOCKED = 'locked';
case UNKNOWN = 'unknown';
}
12 changes: 12 additions & 0 deletions src/Enums/ThermalStatesEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Native\Laravel\Enums;

enum ThermalStatesEnum: string
{
case UNKNOWN = 'unknown';
case NOMINAL = 'nominal';
case FAIR = 'fair';
case SERIOUS = 'serious';
case CRITICAL = 'critical';
}
29 changes: 29 additions & 0 deletions src/Events/PowerMonitor/PowerStateChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Native\Laravel\Events\PowerMonitor;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Native\Laravel\Enums\PowerStatesEnum;

class PowerStateChanged implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public PowerStatesEnum $state;

public function __construct(string $state)
{
$this->state = PowerStatesEnum::from($state);
}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
28 changes: 28 additions & 0 deletions src/Events/PowerMonitor/SpeedLimitChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Native\Laravel\Events\PowerMonitor;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class SpeedLimitChanged implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public int $limit;

public function __construct(string $limit)
{
$this->limit = (int) $limit;
}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
29 changes: 29 additions & 0 deletions src/Events/PowerMonitor/ThermalStateChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Native\Laravel\Events\PowerMonitor;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Native\Laravel\Enums\ThermalStatesEnum;

class ThermalStateChanged implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public ThermalStatesEnum $state;

public function __construct(string $state)
{
$this->state = ThermalStatesEnum::from($state);
}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
19 changes: 19 additions & 0 deletions src/Facades/PowerMonitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Native\Laravel\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static \Native\Laravel\Enums\SystemIdelStatesEnum getSystemIdleState(int $threshold)
* @method static int getSystemIdleTime()
* @method static \Native\Laravel\Enums\ThermalStatesEnum getCurrentThermalState()
* @method static bool isOnBatteryPower()
*/
class PowerMonitor extends Facade
{
protected static function getFacadeAccessor()
{
return \Native\Laravel\PowerMonitor::class;
}
}
38 changes: 38 additions & 0 deletions src/PowerMonitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Native\Laravel;

use Native\Laravel\Client\Client;
use Native\Laravel\Enums\SystemIdleStatesEnum;
use Native\Laravel\Enums\ThermalStatesEnum;

class PowerMonitor
{
public function __construct(protected Client $client) {}

public function getSystemIdleState(int $threshold): SystemIdleStatesEnum
{
$result = $this->client->get('power-monitor/get-system-idle-state', [
'threshold' => $threshold,
])->json('result');

return SystemIdleStatesEnum::tryFrom($result) ?? SystemIdleStatesEnum::UNKNOWN;
}

public function getSystemIdleTime(): int
{
return $this->client->get('power-monitor/get-system-idle-time')->json('result');
}

public function getCurrentThermalState(): ThermalStatesEnum
{
$result = $this->client->get('power-monitor/get-current-thermal-state')->json('result');

return ThermalStatesEnum::tryFrom($result) ?? ThermalStatesEnum::UNKNOWN;
}

public function isOnBatteryPower(): bool
{
return $this->client->get('power-monitor/is-on-battery-power')->json('result');
}
}
Loading