Added sunset reset feature

This commit is contained in:
ashish committed 2023-07-14 21:18:07 -04:00
1 parent 98a66a1e99
commit 995e81c549
7 files changed
+44 -2

No files matched your search

+1
View File
@@ -1,2 +1,3 @@
flask==2.3.2
paho-mqtt==1.6.1
requests==2.31.0
+35
View File
@@ -1,7 +1,11 @@
import os
import sys
import json
from time import sleep
from threading import Thread
from datetime import datetime, timedelta
import requests
from flask import Blueprint, request, jsonify
import paho.mqtt.publish as mqtt_publish
@@ -9,6 +13,33 @@ import paho.mqtt.publish as mqtt_publish
blueprint = Blueprint('detection', __name__)
def get_sunset() -> datetime:
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_date = datetime.now().date()
if sunset_time < datetime.now().time():
# Sunset has already passed today
sunset_date += timedelta(days=1)
return datetime.combine(sunset_date, sunset_time)
def reset_all_camera_detection_at_sunset():
while True:
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()
sleep(seconds_until_sunset)
set_camera_detection('front_door', True)
set_camera_detection('back_door', True)
set_camera_detection('garage', True)
set_camera_detection('family_room', True)
def set_camera_detection(camera: str, value: bool, delay: int = 0) -> None:
sleep(delay)
@@ -35,3 +66,7 @@ def camera_detect_POST(camera):
return jsonify({
'status': 'success'
}), 200
sunset_reset_thread = Thread(target=reset_all_camera_detection_at_sunset, args=())
sunset_reset_thread.start()