Fixed datetime bug

This commit is contained in:
Ashish D'Souza 2024-01-07 20:17:06 -06:00
parent 4ec4531f86
commit fc63499bf0
2 changed files with 12 additions and 13 deletions

View File

@ -1,7 +1,6 @@
import os import os
import json import json
from time import sleep import datetime as dt
from datetime import datetime, timedelta
import requests import requests
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
@ -24,8 +23,8 @@ class FrigateEventNotifier:
self.last_notification_time = {} self.last_notification_time = {}
def send_notification(self, event_id, camera, object_label, score, priority=3): def send_notification(self, event_id, camera, object_label, score, priority=3):
now = datetime.now() now = dt.datetime.now()
if now - self.last_notification_time.get(camera, datetime.min) >= timedelta(seconds=self.quiet_period): 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 # Quiet period has passed since the last notification for this camera
self.last_notification_time[camera] = now self.last_notification_time[camera] = now
camera_location = ' '.join(word.capitalize() for word in camera.split('_')) camera_location = ' '.join(word.capitalize() for word in camera.split('_'))

View File

@ -4,7 +4,7 @@ import json
import time import time
import traceback import traceback
from threading import Thread from threading import Thread
from datetime import datetime, time, timedelta import datetime as dt
import requests import requests
from flask import Blueprint, request, jsonify from flask import Blueprint, request, jsonify
@ -14,23 +14,23 @@ import paho.mqtt.publish as mqtt_publish
blueprint = Blueprint('detection', __name__) blueprint = Blueprint('detection', __name__)
def get_sunset() -> datetime: def get_sunset() -> dt.datetime:
sunset_date = datetime.now().date() sunset_date = dt.datetime.now().date()
try: try:
IPGEOLOCATION_API_KEY = os.environ['IPGEOLOCATION_API_KEY'] 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 = requests.get(f'https://api.ipgeolocation.io/astronomy?apiKey={IPGEOLOCATION_API_KEY}&location=Winter+Haven,+FL')
ipgeolocation_api_response.raise_for_status() ipgeolocation_api_response.raise_for_status()
astronomical_json = json.loads(ipgeolocation_api_response.content) 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: except Exception:
traceback.print_exc() traceback.print_exc()
sunset_time = time(20, 00, 00) sunset_time = dt.time(20, 00, 00)
finally: finally:
if sunset_time < datetime.now().time(): if sunset_time < dt.datetime.now().time():
# Sunset has already passed today # Sunset has already passed today
sunset_date += timedelta(days=1) sunset_date += dt.timedelta(days=1)
return datetime.combine(sunset_date, sunset_time) return dt.datetime.combine(sunset_date, sunset_time)
def reset_all_camera_detection_at_sunset() -> None: def reset_all_camera_detection_at_sunset() -> None:
@ -43,7 +43,7 @@ def reset_all_camera_detection_at_sunset() -> None:
sunset = get_sunset() sunset = get_sunset()
print(f'Waiting until {sunset} to reset detection for all cameras...', file=sys.stderr) 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) time.sleep(seconds_until_sunset)
for camera_name in frigate_camera_config: for camera_name in frigate_camera_config: