-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Settings.py
280 lines (255 loc) · 6.83 KB
/
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
#!/usr/bin/env python3
#############################################################################
####################### Global Settings for RPIEasy #########################
#############################################################################
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import sys
import rpieGlobals
try:
import jsonpickle # pip3 install jsonpickle
except:
print("JSONPickle not found!\nsudo pip3 install jsonpickle")
sys.exit(0)
settingsfile = 'data/settings.json'
tasksfile = 'data/tasks.json'
controllersfile = 'data/controllers.json'
notifiersfile = 'data/notifiers.json'
pinoutfile = 'data/pinout.json'
netdevfile = 'data/netdev.json'
netmanfile = 'data/netman.json'
advsettingsfile = 'data/advsettings.json'
Settings = {
"Name":"RPIEasy",
"Unit":0,
"Password":"",
"Delay":60
}
AdvSettings = {
"serialloglevel":0,
"webloglevel":2,
"consoleloglevel":2,
"fileloglevel":0,
"sysloglevel":0,
"syslogip":"",
"battery": { "enabled":0,"tasknum":0,"taskvaluenum":0}
}
Pinout = []
PinStatesMax = 13
PinStates = ["Default","Input","Input-Pulldown","Input-Pullup","Output","Output-Lo","Output-Hi","H-PWM","1WIRE","Special","IR-RX","IR-TX","IR-PWM","Reserved","Reserved"]
Tasks = [False]
Controllers = [False]
Notifiers = [False]
NetworkDevices = []
NetMan = None
UpdateString = ""
SoundSystem = { # do not save, filled on startup!
"usable":False,
"inuse":False,
"usingplugintaskindex":-1,
"askplugintorelease":None
}
nodelist = [] # for ESPEasy P2P, fill at runtime!
p2plist = [] # for LORA/P2P peer list
WebUIPort = 0 # filled at startup!
# msg arrived from a controller->reroute data to the destination device
# this will do the magic of the two way communication
def callback_from_controllers(controllerindex,idx,values,taskname="",valuename=""):
global Tasks
for x in range(len(Tasks)):
if (Tasks[x] and type(Tasks[x]) is not bool): # device exists
if (Tasks[x].enabled) and (Tasks[x].recdataoption): # device enabled and able to receive data, enable recdataoption at plugin!
if taskname == "": # search task by idx
if (str(Tasks[x].controlleridx[controllerindex])==str(idx)):
Tasks[x].plugin_receivedata(values) # implement plugin_receivedata() at plugin side!
break
else: # search task by name
if (Tasks[x].gettaskname().strip() == taskname.strip()): # match with taskname, case sensitive????
tvalues = []
for u in range(rpieGlobals.VARS_PER_TASK): # fill unused values with -9999, handle at plugin side!!!
tvalues.append(-9999)
for v in range(Tasks[x].valuecount): # match with valuename
if Tasks[x].valuenames[v]==valuename:
tvalues[v] = values
Tasks[x].plugin_receivedata(tvalues)
break
break
def get_i2c_pins(): # get list of enabled i2c pin numbers
global Pinout
gplist = []
try:
for p in range(len(Pinout)):
if int(Pinout[p]["altfunc"])==1:
if "I2C" in Pinout[p]["name"][1]:
gplist.append(Pinout[p]["name"][0]+"/"+Pinout[p]["name"][1])
except:
pass
return gplist
def savesettings():
global Settings, settingsfile
success = 1
try:
f = open(settingsfile,'w')
settingjson = jsonpickle.encode(Settings)
f.write(settingjson)
except:
success = 0
return success
def savetasks():
global Tasks, tasksfile
success = 1
try:
f = open(tasksfile,'w')
settingjson = jsonpickle.encode(Tasks)
f.write(settingjson)
except:
success = 0
return success
def loadsettings():
global Settings, settingsfile, AdvSettings, advsettingsfile
success = 1
try:
f = open(settingsfile)
settingjson = f.read()
Settings = jsonpickle.decode(settingjson)
except:
success = 0
try:
f = open(advsettingsfile)
settingjson = f.read()
AdvSettings = jsonpickle.decode(settingjson)
except:
pass
return success
def loadtasks():
global Tasks, tasksfile
success = 1
try:
f = open(tasksfile)
settingjson = f.read()
Tasks = jsonpickle.decode(settingjson)
except:
success = 0
return success
def savecontrollers():
global Controllers, controllersfile
success = 1
try:
f = open(controllersfile,'w')
settingjson = jsonpickle.encode(Controllers,max_depth=2) # Restrict Jsonpickle to encode vars at first object
f.write(settingjson)
except:
success = 0
return success
def loadcontrollers():
global Controllers, controllersfile
success = 1
try:
f = open(controllersfile)
settingjson = f.read()
Controllers = jsonpickle.decode(settingjson)
except Exception as e:
# print("Critical Jsonpickle error:",str(e))
success = 0
return success
def savepinout():
global Pinout, pinoutfile
success = 1
try:
f = open(pinoutfile,'w')
settingjson = jsonpickle.encode(Pinout)
f.write(settingjson)
except:
success = 0
return success
def loadpinout():
global Pinout, pinoutfile
success = 1
try:
f = open(pinoutfile)
settingjson = f.read()
Pinout = jsonpickle.decode(settingjson)
except:
success = 0
return success
def loadnetsettings():
global NetworkDevices, NetMan, netdevfile, netmanfile
success = 1
try:
f = open(netdevfile)
settingjson = f.read()
NetworkDevices = jsonpickle.decode(settingjson)
except:
success = 0
try:
f = open(netmanfile)
settingjson = f.read()
NetMan = jsonpickle.decode(settingjson)
except:
success = 0
return success
def savenetsettings():
global NetworkDevices, NetMan, netdevfile, netmanfile
success = 1
try:
f = open(netdevfile,"w")
settingjson = jsonpickle.encode(NetworkDevices)
f.write(settingjson)
except:
success = 0
try:
f = open(netmanfile,"w")
settingjson = jsonpickle.encode(NetMan)
f.write(settingjson)
except:
success = 0
return success
def saveadvsettings():
global AdvSettings, advsettingsfile
success = 1
try:
f = open(advsettingsfile,'w')
settingjson = jsonpickle.encode(AdvSettings)
f.write(settingjson)
except:
success = 0
return success
def savenotifiers():
global Notifiers, notifiersfile
success = 1
try:
f = open(notifiersfile,'w')
settingjson = jsonpickle.encode(Notifiers,max_depth=2) # Restrict Jsonpickle to encode vars at first object
f.write(settingjson)
except:
success = 0
return success
def loadnotifiers():
global Notifiers, notifiersfile
success = 1
try:
f = open(notifiersfile)
settingjson = f.read()
Notifiers = jsonpickle.decode(settingjson)
except Exception as e:
# print("Critical Jsonpickle error:",str(e))
success = 0
return success
def getTaskValueIndex(taskname, valuename):
tid = -1
vid = -1
global Tasks
for x in range(0,len(Tasks)):
if (Tasks[x]) and type(Tasks[x]) is not bool:
try:
if Tasks[x].enabled:
if Tasks[x].gettaskname()==taskname:
tid = x+1
for u in range(0,Tasks[x].valuecount):
if Tasks[x].valuenames[u]==valuename:
vid = u+1
return tid, vid
except:
pass
return tid, vid