-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
RPIEasy.py
executable file
·353 lines (324 loc) · 10.5 KB
/
RPIEasy.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python3
#############################################################################
########################## RPI Easy main program ############################
#############################################################################
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import rpieGlobals
import time
import threading
import Settings
from rpieTime import *
import webserver
import misc
import glob
import sys, signal, os
import commands
import socket
from datetime import datetime
try:
import gpios
except Exception as e:
pass
# sudo apt install python3-pip screen alsa-utils wireless-tools wpasupplicant net-tools
# sudo pip3 install jsonpickle
def signal_handler(signal, frame):
global init_ok
init_ok = False
Settings.savetasks()
webserver.WebServer.stop()
gpios.HWPorts.cleanup()
procarr = []
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
if (Settings.Tasks[x].enabled): # device enabled
t = threading.Thread(target=Settings.Tasks[x].plugin_exit)
t.daemon = True
procarr.append(t)
t.start()
if len(procarr)>0:
for process in procarr:
process.join(1)
for y in range(0,len(Settings.Controllers)):
if (Settings.Controllers[y]):
if (Settings.Controllers[y].enabled):
t = threading.Thread(target=Settings.Controllers[y].controller_exit)
t.daemon = True
procarr.append(t)
t.start()
if len(procarr)>0:
for process in procarr:
process.join()
time.sleep(1)
print("\nProgram exiting gracefully")
sys.exit(0)
timer100ms = 0
timer20ms = 0
timer1s = 0
timer2s = 0
timer30s = 0
init_ok = False
prevminute = -1
def hardwareInit():
#print("Init hardware...")
rpieGlobals.osinuse = misc.getosname(0)
rpieGlobals.ossubtype = misc.getsupportlevel(1)
pinout = "0"
if rpieGlobals.osinuse=="linux":
import linux_os as OS
import linux_network as Network
Settings.NetMan = Network.NetworkManager()
if len(OS.getsounddevs())>0:
Settings.SoundSystem["usable"]=True
if rpieGlobals.ossubtype == 10:
rpv = OS.getRPIVer()
if rpv:
pinout = rpv["pins"]
Settings.loadnetsettings()
Settings.NetMan.networkinit()
if pinout != "0":
Settings.loadpinout()
if pinout == "40" and len(Settings.Pinout)!=41:
Settings.Pinout=gpios.PINOUT40
elif pinout == "26R1" and len(Settings.Pinout)!=27:
for p in range(27):
Settings.Pinout.append(gpios.PINOUT40[p])
for p in range(len(gpios.PINOUT26R1_DELTA)):
pi = int(gpios.PINOUT26R1_DELTA[p]["ID"])
Settings.Pinout[pi] = gpios.PINOUT26R1_DELTA[p]
elif pinout == "26R2" and len(Settings.Pinout)!=27:
for p in range(27):
Settings.Pinout.append(gpios.PINOUT40[p])
for p in range(len(gpios.PINOUT26R2_DELTA)):
pi = int(gpios.PINOUT26R2_DELTA[p]["ID"])
Settings.Pinout[pi] = gpios.PINOUT26R2_DELTA[p]
gpios.HWPorts.readconfig()
gpios.HWPorts.initpinstates()
else:
Settings.Pinout = []
return 0
def PluginInit():
tarr = []
filenames = glob.glob('_P*.py')
filenames.sort()
for fname in filenames:
tarr = [0,0,0]
tarr[0] = fname
with open(fname,"r") as fcont:
for line in fcont:
if "PLUGIN_ID" in line:
tarr[1] = line[line.find("=")+1:].strip().replace('"',"")
if "PLUGIN_NAME" in line:
tarr[2] = line[line.find("=")+1:].strip().replace('"',"")
break
tarr[0] = tarr[0].replace(".py","")
rpieGlobals.deviceselector.append(tarr) # create list for form select
#print("Load devices from file")
Settings.loadtasks()
return 0
def CPluginInit():
tarr = []
filenames = glob.glob('_C*.py')
filenames.sort()
for fname in filenames:
tarr = [0,0,0]
tarr[0] = fname
with open(fname,"r") as fcont:
for line in fcont:
if "CONTROLLER_ID" in line:
tarr[1] = line[line.find("=")+1:].strip().replace('"',"")
if "CONTROLLER_NAME" in line:
tarr[2] = line[line.find("=")+1:].strip().replace('"',"")
break
tarr[0] = tarr[0].replace(".py","")
rpieGlobals.controllerselector.append(tarr) # create list for form select
#print("Load controllers from file")
Settings.loadcontrollers()
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
try:
if (Settings.Tasks[x].enabled): # device enabled
Settings.Tasks[x].plugin_init(None) # init plugin at startup
for y in range(len(Settings.Tasks[x].senddataenabled)):
if (Settings.Tasks[x].senddataenabled[y]):
if (Settings.Controllers[y]):
if (Settings.Controllers[y].enabled):
Settings.Tasks[x].controllercb[y] = Settings.Controllers[y].senddata # assign controller callback to plugins that sends data
except Exception as e:
Settings.Tasks[x] = False
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Task" +str(x+1)+ " is malformed, deleted!")
for y in range(0,len(Settings.Controllers)):
if (Settings.Controllers[y]):
if (Settings.Controllers[y].enabled):
Settings.Controllers[y].controller_init(None) # init controller at startup
Settings.Controllers[y].setonmsgcallback(Settings.callback_from_controllers) # set global msg callback for 2way comm
return 0
def NPluginInit():
return 0
def RulesInit():
rules = ""
try:
with open(rpieGlobals.FILE_RULES,'r') as f:
rules = f.read()
except:
pass
if rules!="":
#print("Loading rules...")
commands.splitruletoevents(rules)
commands.rulesProcessing("System#Boot",rpieGlobals.RULE_SYSTEM)
def timeoutReached(timerval):
if (millis()>=timerval):
return True
return False
def run50timespersecond():
procarr = []
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
if (Settings.Tasks[x].enabled): # device enabled
if (Settings.Tasks[x].timer20ms): # scheduling needed
t = threading.Thread(target=Settings.Tasks[x].timer_fifty_per_second)
t.daemon = True
procarr.append(t)
t.start()
if len(procarr)>0:
for process in procarr:
process.join(1)
return 0
def run10timespersecond():
procarr = []
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
if (Settings.Tasks[x].enabled): # device enabled
if (Settings.Tasks[x].timer100ms): # scheduling needed
t = threading.Thread(target=Settings.Tasks[x].timer_ten_per_second)
t.daemon = True
procarr.append(t)
t.start()
if len(procarr)>0:
for process in procarr:
process.join(1)
return 0
def runoncepersecond():
procarr = []
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
if (Settings.Tasks[x].enabled): # device enabled
if (Settings.Tasks[x].timer1s): # scheduling needed
t = threading.Thread(target=Settings.Tasks[x].timer_once_per_second)
t.daemon = True
procarr.append(t)
t.start()
checkSensors()
if len(procarr)>0:
for process in procarr:
process.join()
return 0
def runon2seconds():
procarr = []
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
if (Settings.Tasks[x].enabled): # device enabled
if (Settings.Tasks[x].timer2s): # scheduling needed
t = threading.Thread(target=Settings.Tasks[x].timer_two_second)
t.daemon = True
procarr.append(t)
t.start()
if len(procarr)>0:
for process in procarr:
process.join()
return 0
def runon30seconds(): # Only controllers has this function # DEBUG!!!
procarr = []
for y in range(0,len(Settings.Controllers)):
if (Settings.Controllers[y]):
if (Settings.Controllers[y].enabled):
if (Settings.Controllers[y].timer30s): # scheduling needed
t = threading.Thread(target=Settings.Controllers[y].timer_thirty_second)
t.daemon = True
procarr.append(t)
t.start()
if len(procarr)>0:
for process in procarr:
process.join()
return 0
def checkSensors():
procarr2 = []
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
if (Settings.Tasks[x].enabled): # device enabled
if (Settings.Tasks[x].is_read_timely()): # check if device update needed
# print("pluginread task"+str(x)+" "+str(Settings.Tasks[x]._lastdataservetime)) # DEBUG ONLY!
t2 = threading.Thread(target=Settings.Tasks[x].plugin_read)
t2.daemon = True
procarr2.append(t2)
t2.start()
if len(procarr2)>0:
for process2 in procarr2:
process2.join()
return True
def mainloop():
global timer100ms, timer20ms, timer1s, timer2s, timer30s, init_ok, prevminute
while init_ok:
time.sleep(0.05)
if (timeoutReached(timer20ms)):
run50timespersecond()
timer20ms = millis()+20
if (timeoutReached(timer100ms)):
run10timespersecond()
timer100ms = millis()+100
if (timeoutReached(timer1s)):
runoncepersecond()
timer1s = millis()+1000
if (str(prevminute) != str(datetime.now().strftime('%M'))):
commands.rulesProcessing("Clock#Time="+str(datetime.now().strftime('%a,%H:%M')),rpieGlobals.RULE_CLOCK)
prevminute = datetime.now().strftime('%M')
if (timeoutReached(timer30s)):
runon30seconds()
timer30s = millis()+30000
if (timeoutReached(timer2s)):
runon2seconds()
timer2s = millis()+2000
def initprogram():
global timer100ms, timer20ms, timer1s, timer2s, timer30s, init_ok
try:
os.chdir(os.path.dirname(os.path.realpath(__file__)))
except:
pass
#print("Loading settings")
Settings.loadsettings()
hardwareInit()
PluginInit()
CPluginInit()
NPluginInit()
RulesInit()
timer100ms = millis()
timer20ms = timer100ms
timer1s = timer100ms
timer2s = timer100ms
timer30s = timer100ms
init_ok = True
t = threading.Thread(target=mainloop) # starting sensors and background functions
t.daemon = True
t.start()
ports = [80,8080,8008] # check for usable ports
up = 0
for p in ports:
up = p
try:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), up))
serversocket.close()
except:
up = 0
if up>0:
break
if up == 0:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Webserver can not be started, no available port found!")
else:
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Webserver starting at port "+str(up))
Settings.WebUIPort = up
webserver.WebServer.start('', up) # starts webserver GUI
# MAIN
signal.signal(signal.SIGINT, signal_handler)
initprogram()