-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Settings.py
317 lines (292 loc) · 8.12 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/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":"",
"portlist":[80,8080,8008,591],
"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)
if valuename=="":
Tasks[x].plugin_receivedata(values) # send all
else:
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(channel=-1): # get list of enabled i2c pin numbers
global Pinout
gplist = []
sname = "I2C"
if channel>-1:
sname += str(channel)
try:
for p in range(len(Pinout)):
if int(Pinout[p]["altfunc"])!=0:
n = Pinout[p]["name"]
for i in range(len(n)):
if sname in Pinout[p]["name"][i]:
gplist.append(Pinout[p]["name"][0]+"/"+Pinout[p]["name"][i])
except:
pass
if len(gplist)<1:
gplist.append("I2C")
gplist.append(channel)
return gplist
def savesettings():
global Settings, settingsfile
success = 1
try:
f = open(settingsfile,'w',encoding="utf8")
settingjson = jsonpickle.encode(Settings)
f.write(settingjson)
except Exception as e:
success = 0
return success
def savetasks():
global Tasks, tasksfile
success = 1
try:
import copy
Tasks_Shadow = copy.copy(Tasks) # make a copy of original tasks
except Exception as e:
Tasks_Shadow = Tasks.copy() # this method is not working well
tasktoinit = []
try:
if len(Tasks)>0: # debug
for T in range(len(Tasks_Shadow)):
try:
for i in Tasks_Shadow[T].__dict__:
try:
test = jsonpickle.encode(Tasks_Shadow[T].__dict__[i]) # check if jsonpickle is needed
except:
Tasks_Shadow[T].__dict__[i]=None
if not T in tasktoinit:
tasktoinit.append(T)
except Exception as e:
pass
f = open(tasksfile,'w',encoding="utf8")
settingjson = jsonpickle.encode(Tasks_Shadow)
f.write(settingjson)
for t in tasktoinit:
try:
Tasks[t].plugin_init()
except:
pass
except Exception as e:
success = 0
return success
def loadsettings():
global Settings, settingsfile, AdvSettings, advsettingsfile
success = 1
try:
f = open(settingsfile,encoding="utf8")
settingjson = f.read()
Settings = jsonpickle.decode(settingjson)
except:
success = 0
try:
f = open(advsettingsfile,encoding="utf8")
settingjson = f.read()
AdvSettings = jsonpickle.decode(settingjson)
except:
pass
return success
def loadtasks():
global Tasks, tasksfile
success = 1
tTasks = [False]
try:
f = open(tasksfile,encoding="utf8")
settingjson = f.read()
tTasks = jsonpickle.decode(settingjson)
Tasks = tTasks
except Exception as e:
success = 0
return success
def savecontrollers():
global Controllers, controllersfile
success = 1
try:
f = open(controllersfile,'w',encoding="utf8")
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,encoding="utf8")
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',encoding="utf8")
settingjson = jsonpickle.encode(Pinout)
f.write(settingjson)
except:
success = 0
return success
def loadpinout():
global Pinout, pinoutfile
success = 1
try:
f = open(pinoutfile,encoding="utf8")
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,encoding="utf8")
settingjson = f.read()
NetworkDevices = jsonpickle.decode(settingjson)
except:
success = 0
try:
f = open(netmanfile,encoding="utf8")
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",encoding="utf8")
settingjson = jsonpickle.encode(NetworkDevices)
f.write(settingjson)
except:
success = 0
try:
f = open(netmanfile,"w",encoding="utf8")
settingjson = jsonpickle.encode(NetMan)
f.write(settingjson)
except:
success = 0
return success
def saveadvsettings():
global AdvSettings, advsettingsfile
success = 1
try:
f = open(advsettingsfile,'w',encoding="utf8")
settingjson = jsonpickle.encode(AdvSettings)
f.write(settingjson)
except:
success = 0
return success
def savenotifiers():
global Notifiers, notifiersfile
success = 1
try:
f = open(notifiersfile,'w',encoding="utf8")
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,encoding="utf8")
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