Create new class to handle app logic.

This commit is contained in:
Krzysztof Płaczek
2024-11-12 16:03:22 +01:00
parent a262679916
commit c8ff5cb067
2 changed files with 73 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
<?php <?php
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Kplaczek\FoodsiBot\BotService;
use Kplaczek\FoodsiBot\FoodsiService; use Kplaczek\FoodsiBot\FoodsiService;
use Kplaczek\FoodsiBot\HomeassistantService; use Kplaczek\FoodsiBot\HomeassistantService;
@@ -10,29 +11,6 @@ $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load(); $dotenv->load();
$client = new Client(); $client = new Client();
$homeassistant = new HomeassistantService($client); $homeassistant = new HomeassistantService($client);
if ($homeassistant->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'off') {
return;
}
$foodsi = new FoodsiService($client); $foodsi = new FoodsiService($client);
$packageInfo = $foodsi->getPackageInfo(); $bot = new BotService($homeassistant, $foodsi);
$receivers = explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']); $bot->run();
$notificationSent = false;
foreach ($receivers as $receiver) {
if (!file_exists('last_notification_date.txt')) {
touch('last_notification_date.txt');
}
$lastNotificationDate = file_get_contents('last_notification_date.txt');
if ($packageInfo['current_quantity'] > 0 && $lastNotificationDate !== date('Y-m-d')) {
$homeassistant->sendNotification(
$receiver,
'Dostępna paczka w 3 kromki chleba',
sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']),
);
$notificationSent = true;
}
}
if ($notificationSent) {
file_put_contents('last_notification_date.txt', date('Y-m-d'));
$homeassistant->updateEntity('input_boolean.foodsi_bot_enabled', 'off');
}
$homeassistant->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]);

70
src/BotService.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace Kplaczek\FoodsiBot;
class BotService
{
private bool $notificationSent = false;
public function __construct(
private HomeassistantService $homeassistantService,
private FoodsiService $foodsiService,
) {
}
public function run(): void
{
if (!$this->isEnabled()) {
return;
}
$packageInfo = $this->foodsiService->getPackageInfo();
foreach ($this->getReceivers() as $receiver) {
$this->initializeNotificationLog();
if ($this->shouldSendNotification($packageInfo)) {
$this->homeassistantService->sendNotification(
$receiver,
'Dostępna paczka w 3 kromki chleba',
sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']),
);
$this->notificationSent = true;
}
}
if ($this->notificationSent) {
$this->updateNotificationLog();
$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')]);
}
private function isEnabled(): bool
{
return $this->homeassistantService->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'on';
}
private function getReceivers(): array
{
return explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']);
}
private function initializeNotificationLog(): void
{
if (!file_exists('last_notification_date.txt')) {
touch('last_notification_date.txt');
}
}
private function updateNotificationLog(): void
{
file_put_contents('last_notification_date.txt', date('Y-m-d'));
}
private function getNotificationLog(): string
{
return file_get_contents('last_notification_date.txt');
}
private function shouldSendNotification(array $packageInfo): bool
{
return $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d');
}
}