-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiHub.py
115 lines (92 loc) · 3.9 KB
/
piHub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import time
import logging
import sys
import os
from libs.config.ConfigManager import ConfigManager
from libs.servers.BedServer import BedServer
from libs.servers.WatchServerSFTP import WatchServerSFTP
from libs.servers.WatchServerOpenTera import WatchServerOpenTera
from libs.servers.folderWatcher import FolderWatcher
from libs.hardware.PiHubHardware import PiHubHardware
from Globals import version_string
def handle_exception(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
logging.critical("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = handle_exception
if __name__ == '__main__':
# Logging module
################
from libs.logging.Logger import init_global_logger
init_global_logger()
# Init globals
##############
config_man = ConfigManager()
# Load config file
logging.info("Starting up PiHub v" + version_string + "...")
config_file = 'config/PiHub_Defaults.json'
if os.path.isfile('config/PiHub.json'):
config_file = 'config/PiHub.json'
else:
logging.warning('No specific config file - using default config!')
logging.info('Using config file: ' + config_file)
if not config_man.load_config(config_file):
logging.critical("Invalid config - system halted.")
exit(1)
# Set logger parameters
if config_man.general_config["enable_logging"]:
from libs.logging.Logger import init_file_logger
init_file_logger(config_man.general_config["logs_path"])
# Initializing...
servers = []
# Wait for Internet connection
# PiHubHardware.wait_for_internet()
# Bed Server
if config_man.general_config["enable_bed_server"]:
# Start bed server
bed_server = BedServer(server_config=config_man.bed_server_config,
sftp_config=config_man.sftp_config)
# Start server
bed_server.start()
servers.append(bed_server)
# Apple Watch server
if config_man.general_config["enable_watch_server"]:
# Start Apple Watch server
watch_server = None
if config_man.watch_server_config['transfer_type'] == 'sftp':
watch_server = WatchServerSFTP(server_config=config_man.watch_server_config,
sftp_config=config_man.sftp_config)
if config_man.watch_server_config['transfer_type'] == 'opentera':
watch_server = WatchServerOpenTera(server_config=config_man.watch_server_config,
opentera_config=config_man.opentera_config)
if not watch_server:
logging.critical('Unknown Watch Server Transfer type "' + config_man.watch_server_config['transfer_type']
+ '" - will not start server')
else:
# Start server
watch_server.start()
servers.append(watch_server)
# Folder Watcher server
if config_man.general_config["enable_folderWatcher_server"]:
# Start Apple Watch server
folderWatcher_server = FolderWatcher(server_config=config_man.folderWatcher_server_config,
sftp_config=config_man.sftp_config)
# Start server
folderWatcher_server.start()
servers.append(folderWatcher_server)
logging.info("PiHub " + version_string + " started.")
try:
# Main loop on main thread
while True:
# Watchdog
# Wait to check again in a few minutes
time.sleep(600)
# Check if we have Internet access or not
PiHubHardware.ensure_internet_is_available()
except (KeyboardInterrupt, SystemExit):
for server in servers:
server.stop()
logging.info("PiHub stopped by user.")
exit(0)
logging.info("PiHub stopped.")