forked from monash-human-power/raspicam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
146 lines (115 loc) · 4.29 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Helper functions for editing configuration file"""
import fnmatch
import json
import os
import random
from warnings import warn
from dotenv import load_dotenv
load_dotenv()
CONFIG_FILE = "configs.json"
ACTIVE_OVERLAY_KEY = "activeOverlay"
OVERLAY_FILE_PATTERN = "overlay_*.py"
CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
ROTATION_KEY = "rotation"
DEFAULT_BROKER_IP = "192.168.100.100"
DEFAULT_BIKE = "V3"
DEFAULT_VIEWPORT_SIZE = [1024, 600]
BATTERY_PUBLISH_INTERVAL = 5 * 60 # seconds
def get_overlays(directory=CURRENT_DIRECTORY):
"""Get overlays stored in directory"""
overlays = [
file
for file in os.listdir(directory)
if fnmatch.fnmatch(file, OVERLAY_FILE_PATTERN)
]
return overlays
def set_overlay(new_overlays, directory=CURRENT_DIRECTORY):
"""Set current camera overlay"""
current_device = os.getenv("MHP_CAMERA")
new_overlay = new_overlays[current_device]
configs = read_configs(directory)
if "device" in configs:
configs.pop("device")
configs[ACTIVE_OVERLAY_KEY] = new_overlay
with open(os.path.join(directory, CONFIG_FILE), "w") as file:
json.dump(configs, file, indent=2, sort_keys=True)
def set_rotation(rotation, directory=CURRENT_DIRECTORY):
"""Set rotation for device"""
configs = read_configs(directory)
if "device" in configs:
configs.pop("device")
configs[ROTATION_KEY] = rotation
with open(os.path.join(directory, CONFIG_FILE), "w") as file:
json.dump(configs, file, indent=2, sort_keys=True)
def read_configs(directory=CURRENT_DIRECTORY):
"""Read config.json file"""
configs_file = os.path.join(directory, CONFIG_FILE)
if not os.path.isfile(configs_file):
create_default_configs(directory)
with open(configs_file) as file:
configs = json.load(file)
if ACTIVE_OVERLAY_KEY not in configs:
create_default_configs(directory)
with open(configs_file) as file:
configs = json.load(file)
configs["device"] = os.getenv("MHP_CAMERA")
if not configs["device"]:
warn("MHP_CAMERA has not been set in .env")
configs["bike"] = os.getenv("MHP_BIKE")
if not configs["bike"]:
warn(f"MHP_BIKE is not set in .env, defaulting to {DEFAULT_BIKE}")
configs["bike"] = DEFAULT_BIKE
configs["broker_ip"] = os.getenv("BROKER_IP")
if not configs["broker_ip"]:
warn(
f"BROKER_IP is not set in .env, defaulting to {DEFAULT_BROKER_IP}"
)
configs["broker_ip"] = DEFAULT_BROKER_IP
try:
# Transform env var string "1280,740" into int tuple (1280, 740)
configs["viewport_size"] = tuple(
map(int, os.getenv("VIEWPORT_SIZE").split(","))
)
except AttributeError:
warn(
"VIEWPORT_SIZE is not set in .env, "
+ f"defaulting to {DEFAULT_VIEWPORT_SIZE}"
)
configs["viewport_size"] = DEFAULT_VIEWPORT_SIZE
except ValueError:
warn(
f"VIEWPORT_SIZE '{os.getenv('VIEWPORT_SIZE')}' in .env is invalid,"
+ f" defaulting to {DEFAULT_VIEWPORT_SIZE}"
)
configs["viewport_size"] = DEFAULT_VIEWPORT_SIZE
if len(configs["viewport_size"]) != 2:
warn(
"VIEWPORT_SIZE must contain exactly two integers, "
+ f"defaulting to {DEFAULT_VIEWPORT_SIZE}"
)
configs["viewport_size"] = DEFAULT_VIEWPORT_SIZE
configs["overlays"] = get_overlays()
return configs
def create_default_configs(directory):
"""Create a default config file"""
random_overlay = random.choice(get_overlays(directory))
with open(os.path.join(directory, CONFIG_FILE), "w") as file:
json.dump(
{ACTIVE_OVERLAY_KEY: random_overlay},
file,
indent=2,
sort_keys=True,
)
return random_overlay
def get_active_overlay(directory=CURRENT_DIRECTORY):
"""Get the current active overlay"""
configs = read_configs(directory)
try:
active_overlay = configs[ACTIVE_OVERLAY_KEY]
except KeyError:
active_overlay = create_default_configs(directory)
return os.path.join(directory, active_overlay)
if __name__ == "__main__":
# set_overlay({'primary': 'overlay_all_stats.py'})
# print(get_active_overlay())
print(json.dumps(read_configs()))