mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 23:18:10 +00:00
feat(#34): adds support for header 'battery-percent'
This commit is contained in:
parent
42c25fc403
commit
af934ffdc2
2 changed files with 33 additions and 0 deletions
|
|
@ -45,6 +45,31 @@ class Device extends Model
|
||||||
return round($percent);
|
return round($percent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate battery voltage from percentage
|
||||||
|
*
|
||||||
|
* @param int $percent Battery percentage (0-100)
|
||||||
|
* @return float Calculated voltage
|
||||||
|
*/
|
||||||
|
public function calculateVoltageFromPercent(int $percent): float
|
||||||
|
{
|
||||||
|
// Define min and max voltage for Li-ion battery (3.0V empty, 4.2V full)
|
||||||
|
$min_volt = 3.0;
|
||||||
|
$max_volt = 4.2;
|
||||||
|
|
||||||
|
// Ensure the percentage is within range
|
||||||
|
if ($percent <= 0) {
|
||||||
|
return $min_volt;
|
||||||
|
} elseif ($percent >= 100) {
|
||||||
|
return $max_volt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate voltage
|
||||||
|
$voltage = $min_volt + (($percent / 100) * ($max_volt - $min_volt));
|
||||||
|
|
||||||
|
return round($voltage, 2);
|
||||||
|
}
|
||||||
|
|
||||||
public function getWifiStrengthAttribute()
|
public function getWifiStrengthAttribute()
|
||||||
{
|
{
|
||||||
$rssi = $this->last_rssi_level;
|
$rssi = $this->last_rssi_level;
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,14 @@ Route::get('/display', function (Request $request) {
|
||||||
'last_refreshed_at' => now(),
|
'last_refreshed_at' => now(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if ($request->hasHeader('battery-percent')) {
|
||||||
|
$batteryPercent = (int) $request->header('battery-percent');
|
||||||
|
$batteryVoltage = $device->calculateVoltageFromPercent($batteryPercent);
|
||||||
|
$device->update([
|
||||||
|
'last_battery_voltage' => $batteryVoltage,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
// Get current screen image from a mirror device or continue if not available
|
// Get current screen image from a mirror device or continue if not available
|
||||||
if (! $image_uuid = $device->mirrorDevice?->current_screen_image) {
|
if (! $image_uuid = $device->mirrorDevice?->current_screen_image) {
|
||||||
$refreshTimeOverride = null;
|
$refreshTimeOverride = null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue