~rexgarland/rpi-security-cam

9233c2c13d1d0dc791aa8cf23db606b4bef0be8f — Rex Garland 1 year, 16 days ago 0f3e6e8 main
Retry for 10 minutes on reboot
1 files changed, 16 insertions(+), 2 deletions(-)

M rpi/bin/send_email.py
M rpi/bin/send_email.py => rpi/bin/send_email.py +16 -2
@@ 1,7 1,8 @@
import smtplib
import os
from email.message import EmailMessage

from time import sleep
import socket

def send_email(subject, body):
    # Create a text/plain message


@@ 17,9 18,22 @@ def send_email(subject, body):
    s.quit()


def try_multiple_times(fn):
    max_tries = 10
    num_tries = 0
    while num_tries < max_tries - 1:
        try:
            fn()
            return
        except socket.gaierror:
            num_tries += 1
            sleep(60)
    fn() # on the last try, do not catch the exception


def send_reboot_email():
    subject = f'[rpi-security-cam] Reboot detected'
    body = """The security camera was just turned on.
    
If you did not expect this, please review the camera footage."""
    send_email(subject, body)
\ No newline at end of file
    try_multiple_times(lambda: send_email(subject, body))
\ No newline at end of file