forked from aausat/aausat4_beacon_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
78 lines (67 loc) · 2.47 KB
/
config.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
import urllib2
import os
import os.path
import json
import threading
import time
class Config:
DEFAULT_CONFIG_FILE = 'default_config.json'
CONFIG_FILE = 'config.json'
CONFIG_URL = 'https://raw.githubusercontent.com/aausat/aausat4_beacon_parser/master/default_config.json'
UPDATE_FREQUENCY_MINUTES = 30
def __init__(self):
self.config_lock = threading.Lock()
self.config = None
self.load_config()
self.update_config()
def get_config(self):
with self.config_lock:
return self.config.copy()
def load_config(self, only_if_new=False):
# Load config file
config_file = Config.CONFIG_FILE
if not os.path.isfile(config_file):
config_file = Config.DEFAULT_CONFIG_FILE
if not os.path.isfile(config_file):
# No config files
return
with open(config_file, 'r') as f:
new_config = json.load(f)
self.set_config(new_config, only_if_new)
def set_config(self, config_dict, only_if_new):
if not self.verify_config(config_dict):
# Not valid
return
with self.config_lock:
update = True
if only_if_new and self.config != None:
if self.config['version'] >= config_dict['version']:
update = False
else:
print("Config updated (version {}).".format(config_dict['version']))
if update:
self.config = config_dict
else:
print("No need to update.")
def update_config(self):
print("Updating config...")
try:
f = urllib2.urlopen(Config.CONFIG_URL)
content = f.read()
with open(Config.CONFIG_FILE, 'w') as f:
f.write(content)
config = json.loads(content)
self.set_config(config, True)
except Exception as e:
print("Error: {}".format(e))
# Start update config timer
t = threading.Timer(60*Config.UPDATE_FREQUENCY_MINUTES, self.update_config, ())
t.daemon=True
t.start()
def verify_config(self, config):
valid = all(key in config for key in
('version', 'tle', 'radio_settings'))
if valid:
return all(key in config['radio_settings'] for key in
('bitrate', 'power', 'training', 'frequency', 'modindex'))
return False