|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +- Initiates the LED visuals |
| 4 | +
|
| 5 | +- Installs new requirements as part of an update |
| 6 | +(if needed, might want to have a seprate process for this) |
| 7 | +
|
| 8 | +- Checks for connection to server, |
| 9 | +- If no connection is found then LED indication is set. |
| 10 | +- Pulls configuration files from server. |
| 11 | +- Launches main program |
| 12 | +""" |
| 13 | +import sys |
| 14 | +import json |
| 15 | +import threading |
| 16 | +import subprocess |
| 17 | + |
| 18 | +# ---------------------------------------------------------------------------- # |
| 19 | +# Check Requirements # |
| 20 | +# ---------------------------------------------------------------------------- # |
| 21 | +try: |
| 22 | + import config |
| 23 | +except ImportError : |
| 24 | + subprocess.call(['sudo', 'pip3', 'install', 'config']) |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +from modules import rec_lan, rec_log |
| 29 | +from modules.rec_log import exception_log |
| 30 | + |
| 31 | +from settings import LED_STAT |
| 32 | +import settings |
| 33 | + |
| 34 | +if settings.Pi: |
| 35 | + try: |
| 36 | + from modules import rec_xbee |
| 37 | + except ModuleNotFoundError as err: |
| 38 | + exception_log.error("%s", err) |
| 39 | + |
| 40 | + try: |
| 41 | + from modules import rec_gpio |
| 42 | + except ModuleNotFoundError as err: |
| 43 | + exception_log.error("%s", err) |
| 44 | + |
| 45 | + |
| 46 | +# ---------------------------------------------------------------------------- # |
| 47 | +# Program Start Visual # |
| 48 | +# ---------------------------------------------------------------------------- # |
| 49 | +if settings.Pi: |
| 50 | + led_stat_thread = threading.Thread(target = rec_gpio.led_stat_thread) |
| 51 | + led_stat_thread.start() |
| 52 | + |
| 53 | + rec_gpio.state(LED_STAT, 1, 0) |
| 54 | + |
| 55 | + |
| 56 | +# ---------------------------------------------------------------------------- # |
| 57 | +# Check Network Connection # |
| 58 | +# ---------------------------------------------------------------------------- # |
| 59 | +try: |
| 60 | + rec_lan.monitor_network() #Monitors the network connection while the program is running. |
| 61 | + |
| 62 | +except RuntimeError as err: |
| 63 | + exception_log.error("FATAL - Start Network Monitoring - Error: %s", err) |
| 64 | + |
| 65 | +finally: |
| 66 | + exception_log.debug("Launcher - Exiting Check Network Connection") |
| 67 | + |
| 68 | +# ---------------------------------------------------------------------------- # |
| 69 | +# Update version in system.json # |
| 70 | +# ---------------------------------------------------------------------------- # |
| 71 | +#Only updates if called by bash script and environmental variable was passed in. |
| 72 | +if len(sys.argv) > 1: |
| 73 | + with open("system.json", "r+", encoding="utf-8") as file: |
| 74 | + data = json.load(file) |
| 75 | + data.update( {"CurrentVersion":sys.argv[1]} ) |
| 76 | + file.seek(0) |
| 77 | + json.dump(data, file) |
| 78 | + file.truncate() |
| 79 | + |
| 80 | + |
| 81 | +# ---------------------------------------------------------------------------- # |
| 82 | +# Logs Settings For Debugging # |
| 83 | +# ---------------------------------------------------------------------------- # |
| 84 | +try: |
| 85 | + public, local = rec_lan.get_ip() |
| 86 | + rec_log.snapshot(public, local) |
| 87 | + |
| 88 | +except RuntimeError as err: |
| 89 | + exception_log.error("FAITIAL - Generate Snapshot - Error: %s", err) |
| 90 | + |
| 91 | +finally: |
| 92 | + exception_log.debug("Launcher - Exiting Settings for Debugging") |
| 93 | + |
| 94 | +# ---------------------------------------------------------------------------- # |
| 95 | +# XBee Configuration # |
| 96 | +# ---------------------------------------------------------------------------- # |
| 97 | +if settings.Pi: |
| 98 | + with open('system.json', 'r', encoding="utf-8") as file: |
| 99 | + system_data = json.load(file) |
| 100 | + |
| 101 | + if system_data.get('XBEE_OP', False) is False: |
| 102 | + try: |
| 103 | + rec_xbee.xbee_info() |
| 104 | + except UnboundLocalError as err: |
| 105 | + exception_log.error("Unable to capture XBee info - Error: %s", err) |
| 106 | + |
| 107 | +# ---------------------------------------------------------------------------- # |
| 108 | +# Main HUB Program # |
| 109 | +# ---------------------------------------------------------------------------- # |
| 110 | +try: |
| 111 | + import hub |
| 112 | + |
| 113 | +except RuntimeError as err: |
| 114 | + exception_log.error("Could not start hub.py with error: %s", err) |
| 115 | + |
| 116 | + # Performs Seppuku to triger bash script and perform update pull (student). |
| 117 | + subprocess.call(['pkill', '-f', 'hub.py']) |
| 118 | + |
| 119 | + # Performs Seppuku to triger bash script and perform update pull |
| 120 | + # (can't kill the master before the student). |
| 121 | + subprocess.call(['pkill', '-f', 'HUB_Launcher.py']) |
0 commit comments