diff --git a/Dockerfile b/Dockerfile index bddb5d6..218a767 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:8.2-cli +FROM php:8.2-cli as base # Install system dependencies RUN apt-get update && apt-get install -y \ @@ -21,14 +21,15 @@ RUN curl -sS https://get.symfony.com/cli/installer | bash && \ # Set working directory WORKDIR /var/www/html +CMD ["symfony", "serve", "--port=8000", "--no-tls", "--allow-http", "--allow-all-ip"] + +FROM base as production + # Copy application files COPY . . # Install dependencies RUN composer install --no-interaction --optimize-autoloader -# Expose port -EXPOSE 8000 - # Command to run Symfony server CMD ["symfony", "serve", "--port=8000", "--no-tls", "--allow-http", "--allow-all-ip"] diff --git a/docker-compose.server.yml b/docker-compose.server.yml index 378a0fa..a0df609 100644 --- a/docker-compose.server.yml +++ b/docker-compose.server.yml @@ -5,8 +5,9 @@ services: build: context: . dockerfile: Dockerfile + target: production environment: - - APP_ENV=dev + - APP_ENV=prod - APP_DEBUG=1 networks: - proxy diff --git a/docker-compose.yml b/docker-compose.yml index 12ba59c..b214a46 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,11 @@ services: build: context: . dockerfile: Dockerfile + target: base + ports: + - 8000:8000 environment: - APP_ENV=dev - - APP_DEBUG=1 \ No newline at end of file + - APP_DEBUG=1 + volumes: + - ./:/var/www/html diff --git a/src/Controller/CalendarController.php b/src/Controller/CalendarController.php index 1426853..7cec465 100644 --- a/src/Controller/CalendarController.php +++ b/src/Controller/CalendarController.php @@ -2,10 +2,12 @@ namespace App\Controller; +use App\HomeAssistant\HomeAssistantClient; use App\Service\AbsenceManager; use App\Service\CalendarExportService; use App\Service\CalendarService; use DateTime; +use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -17,6 +19,7 @@ final class CalendarController extends AbstractController private readonly AbsenceManager $absenceManager, private readonly CalendarService $calendarService, private readonly CalendarExportService $calendarExportService, + private readonly HomeAssistantClient $homeAssistantClient, private readonly string $requiredApiKey ) { } @@ -57,12 +60,18 @@ final class CalendarController extends AbstractController $days = $this->calendarService->getAllDays($startDate, $endDate); $days = $this->calendarService->processAbsences($days, $absences); + + $entityHistory = $this->homeAssistantClient->getEntityStateHistory( + 'person.tim', + new DateTimeImmutable('-1 year'), + new DateTimeImmutable('+180 days') + ); - $icsContent = $this->calendarExportService->generateIcsContent($days, $absences); + $icsContent = $this->calendarExportService->generateIcsContent($days, $entityHistory, $absences); $response = new Response($icsContent); - $response->headers->set('Content-Type', 'text/calendar; charset=utf-8'); - $response->headers->set('Content-Disposition', 'attachment; filename="work_calendar.ics"'); + //$response->headers->set('Content-Type', 'text/calendar; charset=utf-8'); + //$response->headers->set('Content-Disposition', 'attachment; filename="work_calendar.ics"'); return $response; } diff --git a/src/HomeAssistant/HomeAssistantClient.php b/src/HomeAssistant/HomeAssistantClient.php index 145f23d..a79d4e5 100644 --- a/src/HomeAssistant/HomeAssistantClient.php +++ b/src/HomeAssistant/HomeAssistantClient.php @@ -46,7 +46,7 @@ final readonly class HomeAssistantClient 'filter_entity_id' => $entityId, ]); - return $this->request('GET', "/api/history/period/{$startDate->format('Y-m-d\TH:i:s\Z')}?{$queryParams}"); + return $this->request('GET', "/api/history/period/{$startDate->format('Y-m-d\TH:i:s\Z')}?{$queryParams}")[0]; } public function callService(string $domain, string $service, array $data = []): array diff --git a/src/Service/CalendarExportService.php b/src/Service/CalendarExportService.php index edb513a..1172772 100644 --- a/src/Service/CalendarExportService.php +++ b/src/Service/CalendarExportService.php @@ -14,7 +14,6 @@ final class CalendarExportService private const KAMEN_ADDRESS = 'Privat \nHeidkamp 21\n59174 Kamen\nDeutschland'; private const COMPANY_NAME = 'CHECK24'; private const TIMEZONE = 'Europe/Berlin'; - private const PERSON_ENTITY = 'person.tim'; private const OFFICE_STATE = 'CHECK24'; public function __construct( @@ -22,7 +21,7 @@ final class CalendarExportService ) { } - public function generateIcsContent(array $days, array $absences): string + public function generateIcsContent(array $days, array $entityHistory, array $absences): string { $icsContent = [ 'BEGIN:VCALENDAR', @@ -39,7 +38,14 @@ final class CalendarExportService } $dayDate = clone $day['date']; - $startAndEndTimes = $this->getWorkTimesFromHomeAssistant($dayDate); + $startOfDay = DateTimeImmutable::createFromInterface($dayDate)->setTime(0, 0, 0); + $typicalEndOfDay = DateTimeImmutable::createFromInterface($dayDate)->setTime(17, 30, 0); + + $filteredEntityHistory = array_filter($entityHistory, function (array $state) use ($startOfDay, $typicalEndOfDay) { + return $state['last_changed'] >= $startOfDay && $state['last_changed'] <= $typicalEndOfDay; + }); + + $startAndEndTimes = $this->getWorkTimesFromHomeAssistant($filteredEntityHistory, $dayDate); if ($startAndEndTimes === null) { $eventStart = clone $day['date']; @@ -74,17 +80,9 @@ final class CalendarExportService return implode("\r\n", $icsContent); } - private function getWorkTimesFromHomeAssistant(DateTimeInterface $date): ?array + private function getWorkTimesFromHomeAssistant(array $stateHistory, DateTimeInterface $date): ?array { - $startOfDay = DateTimeImmutable::createFromInterface($date)->setTime(0, 0, 0); - $typicalEndOfDay = DateTimeImmutable::createFromInterface($date)->setTime(17, 30, 0); - try { - $stateHistory = $this->homeAssistantClient->getEntityStateHistory( - self::PERSON_ENTITY, - $startOfDay, - $typicalEndOfDay - ); if (empty($stateHistory) || empty($stateHistory[0])) { return null; diff --git a/src/Service/CalendarStorageService.php b/src/Service/CalendarStorageService.php new file mode 100644 index 0000000..d859351 --- /dev/null +++ b/src/Service/CalendarStorageService.php @@ -0,0 +1,73 @@ +setTimestamp($fileModTime); + + return $fileModDate < $cutoffDate; + } +} \ No newline at end of file