forked from Honey-Pi/rpi-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_settings.py
289 lines (262 loc) · 10.4 KB
/
read_settings.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python
# This file is part of HoneyPi [honey-pi.de] which is released under Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0).
# See file LICENSE or go to http://creativecommons.org/licenses/by-nc-sa/3.0/ for full license details.
import io
import json
import os
from pathlib import Path
from utilities import settingsFile, wittypi_scheduleFile, error_log
from pwd import getpwuid
from grp import getgrgid
def get_defaults():
settings = {}
settings["button_pin"] = 16
settings["led_pin"] = 21
settings["w1gpio"] = 11
settings["debuglevel"] = 10
settings["offline"] = 0
router = {}
router['enabled'] = False
router['ssid'] = None
router['password'] = None
router['wpa_type'] = 0
modem = {}
modem['enabled'] = False
modem['ttyUSB'] = "ttyUSB0"
modem['apn'] = "pinternet.interkom.de"
honeypi = {}
honeypi['ssid'] = "HoneyPi"
honeypi['password'] = "HoneyPi!"
internet = {}
internet['router'] = router
internet['honeypi'] = honeypi
internet['modem'] = modem
settings["internet"] = internet
settings['ts_server_url'] = "https://api.thingspeak.com"
settings["ts_channels"] = []
ts_channel = {}
ts_channel['ts_channel_id'] = None
ts_channel['ts_write_key'] = None
settings["ts_channels"].append(ts_channel)
settings["sensors"] = []
wittyPi = {}
wittyPi["enabled"] = False
wittyPi["version"] = 2
wittyPi["dummyload"] = 15
wittyPi["voltagecheck_enabled"] = False
lowVoltage = {}
lowVoltage["enabled"] = False
lowVoltage["schedule"] = "BEGIN 2015-08-01 06:00:00 \nEND 2025-07-31 23:59:59 \nON M5 WAIT\nOFF H23 M55"
lowVoltage["voltage"] = 11.9
lowVoltage["shutdownAfterTransfer"] = True
lowVoltage["interval"] = 1
wittyPi["low"] = lowVoltage
normalVoltage = {}
normalVoltage["enabled"] = False
normalVoltage["schedule"] = "BEGIN 2015-08-01 00:00:00 \nEND 2025-07-31 23:59:59 \nON M5 WAIT\nOFF M10"
normalVoltage["voltage"] = 12.8
normalVoltage["shutdownAfterTransfer"] = False
normalVoltage["interval"] = 600
wittyPi["normal"] = normalVoltage
settings['wittyPi'] = wittyPi
return settings
# read settings.json which is created by rpi-webinterface
def get_settings():
settings = {}
try:
my_file = Path(settingsFile)
my_abs_path = my_file.resolve()
if str(getpwuid(os.stat(settingsFile).st_uid).pw_name) != "www-data":
os.system("sudo chown www-data " + str(settingsFile))
if str(getgrgid(os.stat(settingsFile).st_gid).gr_name) != "www-data":
os.system("sudo chgrp www-data " + str(settingsFile))
# Check wittypi_scheduleFile
if os.path.exists(wittypi_scheduleFile):
if str(getpwuid(os.stat(wittypi_scheduleFile).st_uid).pw_name) != "www-data":
os.system("sudo chown www-data " + str(wittypi_scheduleFile))
if str(getgrgid(os.stat(wittypi_scheduleFile).st_gid).gr_name) != "www-data":
os.system("sudo chgrp www-data " + str(wittypi_scheduleFile))
with io.open(settingsFile, encoding="utf-8") as data_file:
settings = json.loads(data_file.read())
except Exception as ex:
error_log("Warning: Loading default settings because File does not exist.")
# FileNotFoundError: doesn't exist => default values
# FileReadError / json.loads Error => default values
settings = get_defaults()
write_settings(settings)
settings = validate_settings(settings)
return settings
# function to migrate settings and check the setting file
def validate_settings(settings):
updateSettingsFile = False
try:
settings["button_pin"] = int(settings["button_pin"])
if not settings["button_pin"]:
raise Exception("button_pin is not defined.")
except:
settings["button_pin"] = get_defaults()["button_pin"]
updateSettingsFile = True
try:
settings["led_pin"] = int(settings["led_pin"])
if not settings["led_pin"]:
raise Exception("led_pin is not defined.")
except:
settings["led_pin"] = get_defaults()["led_pin"]
updateSettingsFile = True
try:
settings["debuglevel"]
except:
# Migrate debug to debuglevel
try:
settings["debug"]
if settings["debug"]:
settings["debuglevel"] = 10
else:
settings["debuglevel"] = get_defaults()["debuglevel"]
updateSettingsFile = True
#settings.remove("debug")
except:
settings["debuglevel"] = get_defaults()["debuglevel"]
updateSettingsFile = True
try:
settings["offline"]
except:
settings["offline"] = get_defaults()["offline"]
updateSettingsFile = True
# Add since v1.0.8
try:
settings["ts_server_url"]
except:
settings["ts_server_url"] = get_defaults()["ts_server_url"]
updateSettingsFile = True
# Migrate from v1.0.8-beta
try:
settings["internet"]["modem"]
except:
settings["internet"]["modem"] = get_defaults()["internet"]["modem"]
updateSettingsFile = True
# Migrate v0.1.1 to v1.0 (Multi-Channel)
try:
settings['ts_channels'][0]["ts_channel_id"]
settings['ts_channels'][0]["ts_write_key"]
except:
updateSettingsFile = True
try:
settings["ts_channel_id"]
print("Old Channel ID to be imported " + str(settings["ts_channel_id"]))
settings["ts_write_key"]
print("Old write key to be imported " + settings["ts_write_key"])
ts_channel = {}
ts_channel['ts_channel_id']= settings["ts_channel_id"]
ts_channel['ts_write_key'] = settings["ts_write_key"]
ts_channels = []
ts_channels.append(ts_channel)
settings['ts_channels'] = ts_channels
except:
settings['ts_channels'] = get_defaults()["ts_channels"]
# Migrate v0.1.1 to v1.0 (WittyPi)
try:
settings['wittyPi']["enabled"]
settings['wittyPi']["normal"]
except:
updateSettingsFile = True
try:
settings["wittyPi_enabled"]
print("Old wittyPi_enabled to be imported: '" + str(settings["wittyPi_enabled"]) + "'")
settings["wittyPi_script"]
print("Old wittyPi_script to be imported: '" + settings["wittyPi_script"] + "'")
wittyPi = {}
normalVoltage = {}
wittyPi['enabled']= settings["wittyPi_enabled"]
wittyPi["voltagecheck_enabled"] = False
normalVoltage["enabled"] = settings["wittyPi_enabled"]
normalVoltage["schedule"] = settings["wittyPi_script"]
try:
normalVoltage["shutdownAfterTransfer"] = settings["shutdownAfterTransfer"]
except:
normalVoltage["shutdownAfterTransfer"] = get_defaults()["wittyPi"]["normal"]["voltage"]["shutdownAfterTransfer"]
try:
normalVoltage["interval"] = settings["interval"]
except:
normalVoltage["interval"] = get_defaults()["wittyPi"]["normal"]["voltage"]["interval"]
normalVoltage["voltage"] = get_defaults()["wittyPi"]["normal"]["voltage"]
wittyPi["normal"] = normalVoltage
wittyPi["low"] = get_defaults()["wittyPi"]["low"]
settings['wittyPi'] = wittyPi
except:
settings['wittyPi'] = get_defaults()["wittyPi"]
try:
settings['wittyPi']["normal"]["shutdownAfterTransfer"]
except:
settings['wittyPi']["normal"]["shutdownAfterTransfer"]["shutdownAfterTransfer"] = get_defaults()["wittyPi"]["normal"]["voltage"]["shutdownAfterTransfer"]
try:
settings['wittyPi']["low"]["shutdownAfterTransfer"]
except:
settings['wittyPi']["low"]["shutdownAfterTransfer"]["shutdownAfterTransfer"] = get_defaults()["wittyPi"]["low"]["voltage"]["shutdownAfterTransfer"]
try:
settings['wittyPi']["normal"]["interval"]
except:
settings['wittyPi']["normal"]["interval"] = get_defaults()["wittyPi"]["normal"]["voltage"]["interval"]
try:
settings['wittyPi']["low"]["interval"]
except:
settings['wittyPi']["low"]["interval"] = get_defaults()["wittyPi"]["low"]["voltage"]["interval"]
try:
settings['wittyPi']['enabled']
except:
settings['wittyPi']['enabled'] = get_defaults()["wittyPi"]["enabled"]
try:
settings['wittyPi']["voltagecheck_enabled"]
except:
settings['wittyPi']["voltagecheck_enabled"] = get_defaults()['wittyPi']["voltagecheck_enabled"]
try:
settings['wittyPi']["normal"]["voltage"]
except:
settings['wittyPi']["normal"]["voltage"] = get_defaults()["wittyPi"]["normal"]["voltage"]
try:
settings['wittyPi']["low"]["voltage"]
except:
settings['wittyPi']["low"]["voltage"] = get_defaults()["wittyPi"]["low"]["voltage"]
try:
settings['wittyPi']["normal"]["enabled"]
except:
settings['wittyPi']["normal"]["enabled"] = get_defaults()["wittyPi"]["normal"]["enabled"]
try:
settings['wittyPi']["low"]["enabled"]
except:
settings['wittyPi']["low"]["enabled"] = get_defaults()["wittyPi"]["low"]["enabled"]
try:
settings['wittyPi']["normal"]["schedule"]
except:
settings['wittyPi']["normal"]["schedule"] = get_defaults()["wittyPi"]["normal"]["schedule"]
try:
settings['wittyPi']["low"]["schedule"]
except:
settings['wittyPi']["low"]["schedule"] = get_defaults()["wittyPi"]["low"]["schedule"]
if updateSettingsFile:
print("Info: Settings have been changed because of version migration.")
write_settings(settings)
return settings
# get sensors by type
def get_sensors(settings, type):
try:
all_sensors = settings["sensors"]
except:
# Key doesn't exist => return empty array
return []
sensors = [x for x in all_sensors if "type" in x and x["type"] == type]
# not found => return empty array
if len(sensors) < 1:
return []
else:
return sensors
def write_settings(settings):
try:
# write values to file
outfile = open(settingsFile, "w")
outfile.write(json.dumps(settings, indent=4, sort_keys=True))
outfile.close()
return True
except Exception as ex:
print("write_settings " + str(ex))
return False