From 995e81c54907153e1a80bcf73aef8a1ff0c2437c Mon Sep 17 00:00:00 2001 From: Ashish D'Souza Date: Tue, 11 Jul 2023 07:32:12 -0400 Subject: [PATCH] Added sunset reset feature --- Jenkinsfile | 2 +- conf/config.yaml | 2 +- conf/docker-compose.yaml | 1 + install.yaml | 4 ++++ start.yaml | 1 + webcontrol/requirements.txt | 1 + webcontrol/src/detection.py | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index aa31e7e..859af97 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,7 +14,7 @@ pipeline { frigate_config = readJSON file: 'frigate_config.json' } - ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, extras: "--extra-vars 'mqtt_password=\"${frigate_config.mqtt.password}\" garage_rtsp_password=\"${frigate_config.rtsp.garage.password}\" front_door_rtsp_password=\"${frigate_config.rtsp.front_door.password}\" back_door_rtsp_password=\"${frigate_config.rtsp.back_door.password}\" family_room_rtsp_password=\"${frigate_config.rtsp.family_room.password}\"'", playbook: 'install.yaml' + ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, extras: "--extra-vars 'mqtt_password=\"${frigate_config.mqtt.password}\" ipgeolocation_api_key=\"${frigate_config.ipgeolocation.api_key}\" garage_rtsp_password=\"${frigate_config.rtsp.garage.password}\" front_door_rtsp_password=\"${frigate_config.rtsp.front_door.password}\" back_door_rtsp_password=\"${frigate_config.rtsp.back_door.password}\" family_room_rtsp_password=\"${frigate_config.rtsp.family_room.password}\"'", playbook: 'install.yaml' ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, playbook: 'stop.yaml' ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, playbook: 'start.yaml' } diff --git a/conf/config.yaml b/conf/config.yaml index d24ee1e..d731a2e 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -72,7 +72,7 @@ cameras: - 242,534,609,527,534,848,89,836 # Plants on patio - 681,528,832,522,876,834,635,829 # Logs on patio family_room: - enabled: true + enabled: false ffmpeg: inputs: - path: 'rtsp://{FRIGATE_FAMILY_ROOM_RTSP_USERNAME}:{FRIGATE_FAMILY_ROOM_RTSP_PASSWORD}@192.168.0.104:554/live/ch0' diff --git a/conf/docker-compose.yaml b/conf/docker-compose.yaml index 6c690d1..2fee7f7 100644 --- a/conf/docker-compose.yaml +++ b/conf/docker-compose.yaml @@ -82,6 +82,7 @@ services: environment: MQTT_USERNAME: frigate MQTT_PASSWORD: ${MQTT_PASSWORD} + IPGEOLOCATION_API_KEY: ${IPGEOLOCATION_API_KEY} volumes: - type: bind source: /etc/localtime diff --git a/install.yaml b/install.yaml index 7ba64f7..f9c64b1 100644 --- a/install.yaml +++ b/install.yaml @@ -3,6 +3,8 @@ vars_prompt: - name: mqtt_password prompt: Enter password for MQTT user frigate + - name: ipgeolocation_api_key + prompt: Enter API key for IPGeolocation - name: garage_rtsp_password prompt: Enter password for garage camera RTSP stream user motion - name: front_door_rtsp_password @@ -114,6 +116,8 @@ frigate_config: mqtt: password: '{{mqtt_password}}' + ipgeolocation: + api_key: '{{ipgeolocation_api_key}}' rtsp: garage: password: '{{garage_rtsp_password}}' diff --git a/start.yaml b/start.yaml index e5ffdd8..481d5cf 100644 --- a/start.yaml +++ b/start.yaml @@ -36,6 +36,7 @@ - name: Docker Compose up Frigate environment: MQTT_PASSWORD: '{{frigate_config.mqtt.password}}' + IPGEOLOCATION_API_KEY: '{{frigate_config.ipgeolocation.api_key}}' FRIGATE_GARAGE_RTSP_PASSWORD: '{{frigate_config.rtsp.garage.password}}' FRIGATE_FRONT_DOOR_RTSP_PASSWORD: '{{frigate_config.rtsp.front_door.password}}' FRIGATE_BACK_DOOR_RTSP_PASSWORD: '{{frigate_config.rtsp.back_door.password}}' diff --git a/webcontrol/requirements.txt b/webcontrol/requirements.txt index b1a39cb..946054f 100644 --- a/webcontrol/requirements.txt +++ b/webcontrol/requirements.txt @@ -1,2 +1,3 @@ flask==2.3.2 paho-mqtt==1.6.1 +requests==2.31.0 diff --git a/webcontrol/src/detection.py b/webcontrol/src/detection.py index 2ec1e44..bdae862 100644 --- a/webcontrol/src/detection.py +++ b/webcontrol/src/detection.py @@ -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()