From fc63499bf05773993186c27b6ddc2ee0d59d89ba Mon Sep 17 00:00:00 2001 From: Ashish D'Souza Date: Sun, 7 Jan 2024 20:17:06 -0600 Subject: [PATCH] Fixed datetime bug --- notify/src/frigate_event_notifier.py | 7 +++---- webcontrol/src/detection.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/notify/src/frigate_event_notifier.py b/notify/src/frigate_event_notifier.py index 7430913..f635492 100644 --- a/notify/src/frigate_event_notifier.py +++ b/notify/src/frigate_event_notifier.py @@ -1,7 +1,6 @@ import os import json -from time import sleep -from datetime import datetime, timedelta +import datetime as dt import requests import paho.mqtt.client as mqtt @@ -24,8 +23,8 @@ class FrigateEventNotifier: self.last_notification_time = {} def send_notification(self, event_id, camera, object_label, score, priority=3): - now = datetime.now() - if now - self.last_notification_time.get(camera, datetime.min) >= timedelta(seconds=self.quiet_period): + now = dt.datetime.now() + if now - self.last_notification_time.get(camera, dt.datetime.min) >= dt.timedelta(seconds=self.quiet_period): # Quiet period has passed since the last notification for this camera self.last_notification_time[camera] = now camera_location = ' '.join(word.capitalize() for word in camera.split('_')) diff --git a/webcontrol/src/detection.py b/webcontrol/src/detection.py index 39a335e..0e5a2ae 100644 --- a/webcontrol/src/detection.py +++ b/webcontrol/src/detection.py @@ -4,7 +4,7 @@ import json import time import traceback from threading import Thread -from datetime import datetime, time, timedelta +import datetime as dt import requests from flask import Blueprint, request, jsonify @@ -14,23 +14,23 @@ import paho.mqtt.publish as mqtt_publish blueprint = Blueprint('detection', __name__) -def get_sunset() -> datetime: - sunset_date = datetime.now().date() +def get_sunset() -> dt.datetime: + sunset_date = dt.datetime.now().date() try: IPGEOLOCATION_API_KEY = os.environ['IPGEOLOCATION_API_KEY'] ipgeolocation_api_response = requests.get(f'https://api.ipgeolocation.io/astronomy?apiKey={IPGEOLOCATION_API_KEY}&location=Winter+Haven,+FL') ipgeolocation_api_response.raise_for_status() astronomical_json = json.loads(ipgeolocation_api_response.content) - sunset_time = datetime.strptime(astronomical_json['sunset'], '%H:%M').time() + sunset_time = dt.datetime.strptime(astronomical_json['sunset'], '%H:%M').time() except Exception: traceback.print_exc() - sunset_time = time(20, 00, 00) + sunset_time = dt.time(20, 00, 00) finally: - if sunset_time < datetime.now().time(): + if sunset_time < dt.datetime.now().time(): # Sunset has already passed today - sunset_date += timedelta(days=1) - return datetime.combine(sunset_date, sunset_time) + sunset_date += dt.timedelta(days=1) + return dt.datetime.combine(sunset_date, sunset_time) def reset_all_camera_detection_at_sunset() -> None: @@ -43,7 +43,7 @@ def reset_all_camera_detection_at_sunset() -> None: sunset = get_sunset() print(f'Waiting until {sunset} to reset detection for all cameras...', file=sys.stderr) - seconds_until_sunset = (sunset - datetime.now()).total_seconds() + seconds_until_sunset = (sunset - dt.datetime.now()).total_seconds() time.sleep(seconds_until_sunset) for camera_name in frigate_camera_config: