-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAFA.py
114 lines (94 loc) · 3.63 KB
/
AFA.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
from time import sleep, asctime
from sys import argv
from http.server import HTTPServer
from managerHardware import Lights, Mister, Fan, Peltier
from backendServer import Server
from backendGPIO import GPIOManager
from backendI2C import Sensors
from backendCamera import Camera
from backendJSONUtil import populateimagelist
def main():
try:
# populate list of images in case they have been deleted
populateimagelist()
# init GPIO
gpiomanager = GPIOManager()
# init camera to take picture every 5 seconds
camera = Camera(5)
camera.start_timed_capture()
# init lights
lights = Lights("RELAY1", gpiomanager, camera)
lights.startscheduledlighting(ton = (8, 0), toff = (22, 0))
# init mister
mister = Mister("RELAY2", gpiomanager)
mister.startscheduledmisting(everyNsec = 10, forNsec = 5)
# init fan
fan = Fan("RELAY3", gpiomanager)
fan.startscheduledfanning(everyNsec = 10, forNsec = 5)
# init peltier
peltier = Peltier("RELAY4", gpiomanager, ontemp = 20, bufferlen = 10)
# init sensors
sensors = Sensors(peltier)
sensors.startsensorpoll()
sensors.startfilewriterthread(secondsPerWrite = 5)
# start server
HOST_NAME = 'localhost'
PORT_NUMBER = 8000
httpd = HTTPServer((HOST_NAME, PORT_NUMBER), Server)
print(asctime(), 'Server UP - %s:%s' % (HOST_NAME, PORT_NUMBER))
httpd.serve_forever()
# try and switch things switch off if there are any errors or user shutdown
except KeyboardInterrupt:
try:
camera.stop_timed_capture()
except BaseException as e:
print("\n\Camera off may have failed due to error:\n{}\n".format(e))
try:
lights.lightschedule.sentinel = False
lights.lightschedule.stop()
lights.lightschedulethread.join()
lights.off()
except BaseException as e:
print("\n\nLights off may have failed due to error:\n{}\n".format(e))
try:
mister.misterschedule.sentinel = False
mister.misterschedule.stop()
mister.misterschedulethread.join()
mister.off()
except BaseException as e:
print("\n\nMister off may have failed due to error:\n{}\n".format(e))
try:
fan.fanschedule.sentinel = False
fan.fanschedule.stop()
fan.fanschedulethread.join()
fan.off()
except BaseException as e:
print("\n\Fan off may have failed due to error:\n{}\n".format(e))
try:
peltier.off()
except BaseException as e:
print("\n\Peltier off may have failed due to error:\n{}\n".format(e))
try:
httpd.server_close()
print(asctime(), 'Server DOWN - %s:%s' % (HOST_NAME, PORT_NUMBER))
except:
pass
try:
sensors.sentinel = False
sensors.filewriterthreadtimer.stop()
#~ sensors.lightschedulethread.join()
except BaseException as e:
print("\n\Sensors off may have failed due to error:\n{}\n".format(e))
try:
gpiomanager.powerdown()
except BaseException as e:
print("\n\GPIO powerdown may have failed due to error:\n{}\n".format(e))
def cleanup():
# if user passes "cleardata" as argument argv to running then
# deletes all picture and sensor data
pass
if __name__ == '__main__':
if len(argv)==2 and argv[1]=="cleardata":
cleanup()
else:
main()