Skip to content

Commit

Permalink
v1.1 reboot after beacon
Browse files Browse the repository at this point in the history
created for very specific corner cases when the hotspot is freezing after generating a beacon
  • Loading branch information
cstnn authored Mar 27, 2022
1 parent d6dd83f commit f643880
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions helium_watchdog_beacon_reboot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import bobcat
import helium
import _gmail
from utilities import LogFile

import time
from datetime import datetime

import db_actions
import secrets

secrets = db_actions.secrets()

verbose = secrets['verbose']
send_emails = secrets['send_emails']
dry_run = secrets['dry_run']
cache_name = secrets['cache_name']
cache_address = secrets['cache_address']
log = secrets['log']
email_to = secrets['email_to']
sleep = int(secrets['sleep'])
# print(verbose)

cache_last_beacon = '1000000000'

# Check for stale hotspot
def beacon_check(address = secrets['cache_address'], ip=secrets['cache_ip']):
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")

last_beacon = helium.get_hotspot_beacon(secrets['cache_address'])
last_beacon_date_time = last_beacon['last_beacon_date_time']
if last_beacon['status_code'] == 200:
if int(last_beacon_date_time) > int(cache_last_beacon) and int(cache_last_beacon) != 1000000000: # v1.1 fix to avoid reboot when starting the script for the 1st time as the new Beacont time value will always be higher than 1000000000
if dry_run == 'False':
if verbose == 'True':
print(f" {dt_string} Updated last Beacont to : {int(last_beacon_date_time)}")
bobcat.bobcat_reboot(ip)
time.sleep(30)
else:
if verbose == 'True':
print(f" {dt_string} Updated last Beacont to : {int(last_beacon_date_time)}")

if send_emails == 'True':
_gmail.send_email("Hotspot Restarted after BEACON", f"""{dt_string}\n{cache_name}\nHotspot was restarted after a BEACON) \n>>>>> REBOOTING <<<<<""", email_to)
if verbose == 'True':
print(f" {dt_string} Sending email ...")
else:
if verbose == 'True':
print(f" {dt_string} Helium API not responding")
# stale_check()

#################################################
if log == 'True':
LogFile.start()

# initialize()
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
if send_emails == 'True':
_gmail.send_email(f"ECHO watchdog started at {dt_string}", f"""Hi,\nI'm ECHO the watchdog for your BOBCAT 300 {secrets['cache_name']}.\nEvery {int(secrets['sleep'])/60/60} hours I will check the status.\nIf something important happens I will bark and send you an email.""", email_to)
if verbose == 'True':
print(f" {dt_string} Sending email ...")

global iteration
iteration = 0
print("=====================================================")
print(f":: MONITORING session in a {sleep/60/60} hours loop ::")
print("=====================================================")
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print(f":::::: Starting LOOP at {dt_string}")
try:
while True:
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")


#####
if verbose == 'True':
print(f":::::: Beacon check")

try:
beacon_check()
except Exception as ex:
if verbose == 'True':
print(f" {dt_string} ERROR in BEACON check \n{ex}")

iteration += 1
if verbose == 'True':
print(f":::::: {dt_string} Going to sleep for {sleep / 60} minutes")
time.sleep(sleep)
except Exception as ex:
if verbose == 'True':
print(f" {dt_string} ERROR the loop \n{ex}")
finally:
if log == 'True':
LogFile.close()

0 comments on commit f643880

Please sign in to comment.