Changed to be run all the time, on/off switch in homeassistant controls notification

This commit is contained in:
Krzysztof Płaczek
2025-01-26 09:28:30 +01:00
parent 61b1cbadbb
commit 8d23c68812
5 changed files with 60 additions and 17 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
.env
.idea
last_notification_date.txt
login_data.json

9
README.md Normal file
View File

@@ -0,0 +1,9 @@
## FOODSI HOMEASSISTANT BOT
### Run in debug mode
`--debug` flag does not update Homeassistant, does not send notifications it just prints out current number of packages on the screen.
```bash
php index.php --debug
```

View File

@@ -1,6 +1,5 @@
<?php
use GuzzleHttp\Client;
use Kplaczek\FoodsiBot\BotService;
use Kplaczek\FoodsiBot\FoodsiService;
use Kplaczek\FoodsiBot\HomeassistantService;
@@ -13,7 +12,7 @@ $_ENV['DEBUG'] = false;
if ($argc === 2 and isset($argv[1]) && $argv[1] === '--debug') {
$_ENV['DEBUG'] = true;
}
$client = new Client();
$client = new GuzzleHttp\Client();
$homeassistant = new HomeassistantService($client);
$foodsi = new FoodsiService($client);
$bot = new BotService($homeassistant, $foodsi);

View File

@@ -6,6 +6,8 @@ class BotService
{
private bool $notificationSent = false;
private const string LAST_NOTIFICATION_DATE_FILE = 'last_notification_date.txt';
public function __construct(
private HomeassistantService $homeassistantService,
private FoodsiService $foodsiService,
@@ -20,9 +22,6 @@ class BotService
return;
}
if (!$this->isEnabled()) {
return;
}
$packageInfo = $this->foodsiService->getPackageInfo();
foreach ($this->getReceivers() as $receiver) {
$this->initializeNotificationLog();
@@ -60,24 +59,34 @@ class BotService
private function initializeNotificationLog(): void
{
if (!file_exists('last_notification_date.txt')) {
touch('last_notification_date.txt');
if (!file_exists(self::LAST_NOTIFICATION_DATE_FILE)) {
touch(self::LAST_NOTIFICATION_DATE_FILE);
}
}
private function updateNotificationLog(): void
{
file_put_contents('last_notification_date.txt', date('Y-m-d'));
file_put_contents(self::LAST_NOTIFICATION_DATE_FILE, date('Y-m-d'));
}
private function getNotificationLog(): string
{
return file_get_contents('last_notification_date.txt');
return file_get_contents(self::LAST_NOTIFICATION_DATE_FILE);
}
private function shouldSendNotification(array $packageInfo): bool
{
return !$this->isDebugEnabled() && $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d');
return !$this->isDebugEnabled() && $this->isEnabled() && $this->isPackageAvailable($packageInfo) && !$this->isPackageNotificationSentToday();
}
private function isPackageAvailable(array $packageInfo): bool
{
return $packageInfo['current_quantity'] > 0;
}
private function isPackageNotificationSentToday(): bool
{
return $this->getNotificationLog() !== date('Y-m-d');
}
private function isDebugEnabled(): bool

View File

@@ -9,6 +9,7 @@ readonly class FoodsiService
private string $accessToken;
private string $client;
private string $uid;
private const string LOGIN_DATA_FILE = 'login_data.json';
public function __construct(private Client $httpClient)
{
@@ -18,11 +19,11 @@ readonly class FoodsiService
{
$this->login();
$response = $this->httpClient->get(
'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').
'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' => [
@@ -32,7 +33,6 @@ readonly class FoodsiService
],
],
);
$data = json_decode($response->getBody(), true);
if (isset($data['data'][0])) {
return $data['data'][0]['attributes'];
@@ -43,6 +43,7 @@ readonly class FoodsiService
private function login(): void
{
$this->restoreLoginData();
if (empty($this->accessToken)) {
$response = $this->httpClient->post('https://api.foodsi.pl/api/v2/auth/sign_in', [
'json' => [
@@ -53,6 +54,30 @@ readonly class FoodsiService
$this->accessToken = $response->getHeaderLine('Access-Token');
$this->client = $response->getHeaderLine('Client');
$this->uid = $response->getHeaderLine('Uid');
$this->cacheLoginData();
}
}
private function restoreLoginData(): void
{
if (file_exists(self::LOGIN_DATA_FILE)) {
$loginData = json_decode(file_get_contents(self::LOGIN_DATA_FILE), 1);
if ($loginData['time'] + 3600 > time()) {
$this->accessToken = $loginData['Access-Token'];
$this->client = $loginData['Client'];
$this->uid = $loginData['Uid'];
}
}
}
private function cacheLoginData(): void
{
$loginData = [
'Access-Token' => $this->accessToken,
'Client' => $this->client,
'Uid' => $this->uid,
'time' => time(),
];
file_put_contents(self::LOGIN_DATA_FILE, json_encode($loginData));
}
}