Add --debug flag to index.php script, restrict foodsi bot query to get only today packages.

This commit is contained in:
Krzysztof Płaczek
2024-12-15 11:10:30 +01:00
parent 13028af1db
commit a98934dfad
3 changed files with 30 additions and 3 deletions

View File

@@ -9,6 +9,10 @@ require_once 'vendor/autoload.php';
date_default_timezone_set('Europe/Warsaw'); date_default_timezone_set('Europe/Warsaw');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load(); $dotenv->load();
$_ENV['DEBUG'] = false;
if ($argc === 2 and isset($argv[1]) && $argv[1] === '--debug') {
$_ENV['DEBUG'] = true;
}
$client = new Client(); $client = new Client();
$homeassistant = new HomeassistantService($client); $homeassistant = new HomeassistantService($client);
$foodsi = new FoodsiService($client); $foodsi = new FoodsiService($client);

View File

@@ -14,6 +14,12 @@ class BotService
public function run(): void public function run(): void
{ {
if ($this->isDebugEnabled()) {
$packageInfo = $this->foodsiService->getPackageInfo();
print_r($packageInfo);
return;
}
if (!$this->isEnabled()) { if (!$this->isEnabled()) {
return; return;
} }
@@ -29,13 +35,19 @@ class BotService
$this->notificationSent = true; $this->notificationSent = true;
} }
} }
if ($this->notificationSent) { if ($this->notificationSent && !$this->isDebugEnabled()) {
$this->updateNotificationLog(); $this->updateNotificationLog();
$this->homeassistantService->updateEntity('input_boolean.foodsi_bot_enabled', 'off'); $this->homeassistantService->updateEntity('input_boolean.foodsi_bot_enabled', 'off');
} }
$this->homeassistantService->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]); $this->homeassistantService->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]);
} }
public function updateHomeAssistant(): void
{
$packageInfo = $this->foodsiService->getPackageInfo();
$this->homeassistantService->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]);
}
private function isEnabled(): bool private function isEnabled(): bool
{ {
return $this->homeassistantService->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'on'; return $this->homeassistantService->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'on';
@@ -65,6 +77,11 @@ class BotService
private function shouldSendNotification(array $packageInfo): bool private function shouldSendNotification(array $packageInfo): bool
{ {
return $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d'); return !$this->isDebugEnabled() && $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d');
}
private function isDebugEnabled(): bool
{
return $_ENV['DEBUG'];
} }
} }

View File

@@ -18,7 +18,12 @@ readonly class FoodsiService
{ {
$this->login(); $this->login();
$response = $this->httpClient->get( $response = $this->httpClient->get(
'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[current_quantity][gt]=-1&filter[original_price]=35', 'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[current_quantity][gt]=-1'.
'&filter[pickup_from][gt]='.date('Y-m-dT00:00:00+01:00').
'&filter[pickup_from][lt]='.date('Y-m-dT22:00:00+01:00').
'&filter[pickup_to][gt]='.date('Y-m-dT00:00:00+01:00').
'&filter[pickup_to][lt]='.date('Y-m-dT22:00:00+01:00').
'&filter[original_price]=35',
[ [
'headers' => [ 'headers' => [
'Access-Token' => $this->accessToken, 'Access-Token' => $this->accessToken,
@@ -27,6 +32,7 @@ readonly class FoodsiService
], ],
], ],
); );
$data = json_decode($response->getBody(), true); $data = json_decode($response->getBody(), true);
if (isset($data['data'][0])) { if (isset($data['data'][0])) {
return $data['data'][0]['attributes']; return $data['data'][0]['attributes'];