Create new class to handle app logic.
This commit is contained in:
28
index.php
28
index.php
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Kplaczek\FoodsiBot\BotService;
|
||||
use Kplaczek\FoodsiBot\FoodsiService;
|
||||
use Kplaczek\FoodsiBot\HomeassistantService;
|
||||
|
||||
@@ -10,29 +11,6 @@ $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$dotenv->load();
|
||||
$client = new Client();
|
||||
$homeassistant = new HomeassistantService($client);
|
||||
if ($homeassistant->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'off') {
|
||||
return;
|
||||
}
|
||||
$foodsi = new FoodsiService($client);
|
||||
$packageInfo = $foodsi->getPackageInfo();
|
||||
$receivers = explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']);
|
||||
$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')]);
|
||||
$bot = new BotService($homeassistant, $foodsi);
|
||||
$bot->run();
|
||||
|
||||
Reference in New Issue
Block a user