grocy/services/BatteriesService.php
fipwmaqzufheoxq92ebc 32a4f81f62
Filtering of API-Results (#985)
* Add FilteredApiResponse

* Use FilteredApiResponse for Generic-Entity-Search

* Use FilteredApiResponse for Recipe-Fullfillment

* Use FilteredApiResponse for GetUsers

* Use FilteredApiResponse for current Tasks

* Use FilteredApiResponse for ProductStockEntries & ProductStockLocations

* Use FilteredApiResponse for current chores

* Use FilteredApiResponse for batteries-current

* Fix missing highlighting of "< X days"

* Keep to use existing views

Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-09-01 19:59:40 +02:00

71 lines
1.9 KiB
PHP

<?php
namespace Grocy\Services;
class BatteriesService extends BaseService
{
public function GetBatteryDetails(int $batteryId)
{
if (!$this->BatteryExists($batteryId))
{
throw new \Exception('Battery does not exist');
}
$battery = $this->getDatabase()->batteries($batteryId);
$batteryChargeCyclesCount = $this->getDatabase()->battery_charge_cycles()->where('battery_id = :1 AND undone = 0', $batteryId)->count();
$batteryLastChargedTime = $this->getDatabase()->battery_charge_cycles()->where('battery_id = :1 AND undone = 0', $batteryId)->max('tracked_time');
$nextChargeTime = $this->getDatabase()->batteries_current()->where('battery_id', $batteryId)->min('next_estimated_charge_time');
return [
'battery' => $battery,
'last_charged' => $batteryLastChargedTime,
'charge_cycles_count' => $batteryChargeCyclesCount,
'next_estimated_charge_time' => $nextChargeTime
];
}
public function GetCurrent()
{
return $this->getDatabase()->batteries_current();
}
public function TrackChargeCycle(int $batteryId, string $trackedTime)
{
if (!$this->BatteryExists($batteryId))
{
throw new \Exception('Battery does not exist');
}
$logRow = $this->getDatabase()->battery_charge_cycles()->createRow([
'battery_id' => $batteryId,
'tracked_time' => $trackedTime
]);
$logRow->save();
return $this->getDatabase()->lastInsertId();
}
public function UndoChargeCycle($chargeCycleId)
{
$logRow = $this->getDatabase()->battery_charge_cycles()->where('id = :1 AND undone = 0', $chargeCycleId)->fetch();
if ($logRow == null)
{
throw new \Exception('Charge cycle does not exist or was already undone');
}
// Update log entry
$logRow->update([
'undone' => 1,
'undone_timestamp' => date('Y-m-d H:i:s')
]);
}
private function BatteryExists($batteryId)
{
$batteryRow = $this->getDatabase()->batteries()->where('id = :1', $batteryId)->fetch();
return $batteryRow !== null;
}
}