diff --git a/server.py b/server.py index 98cb13e..70da40e 100644 --- a/server.py +++ b/server.py @@ -38,6 +38,16 @@ OPEN_ALERT_THRESHOLDS_MINUTES = [5, 15, 30, 60, 120] OPEN_ALERTS_SENT_FOR_CURRENT_OPENING = [] +def controller_message(message): + payload = {mysecrets.CONTROLLER_KEY: message} + logging.info('Controller message: %s', message) + r = requests.post(mysecrets.CONTROLLER_URL, data=payload, timeout=10) + if r.status_code == 200: + return True + else: + logging.exception('Unable to communicate with controller! Message: ' + message) + return False + def get_derived_state(): """Derives the state from the prediction history.""" state = "unknown" @@ -188,7 +198,9 @@ async def monitor_state_transitions(app): for threshold in OPEN_ALERT_THRESHOLDS_MINUTES: if open_duration_minutes >= threshold and threshold not in OPEN_ALERTS_SENT_FOR_CURRENT_OPENING: - logging.info(f"ALERT: Garage door has been open for {threshold} minutes.") + msg = f"ALERT: Garage door has been open for {threshold} minutes." + controller_message(msg) + logging.info(msg) OPEN_ALERTS_SENT_FOR_CURRENT_OPENING.append(threshold) elif current_state == 'closed': DOOR_OPEN_START_TIME = None