-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathozPhotobooth.py
207 lines (153 loc) · 6.51 KB
/
ozPhotobooth.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
#!/usr/bin/python3
#
# pip install zipstream
# pip install pygame
# pip install opencv-python
# pip install Pillow
import sys, os, threading, logging, traceback
import ozPhotoboothConfig, ozPhotoboothTimer, ozPhotoboothSound, ozPhotoboothPhotoDisk, ozPhotoboothPhotoPreview, ozPhotoboothSequence, ozPhotoboothEvent
import time
import subprocess
import re
from network import ozDNSManager
from ozPhotoboothHTTPServer import *
if os.name != 'nt' and os.uname()[1] == 'raspberrypi':
from HAL.raspberry import ozPhotoboothAction, ozPhotoboothInput, ozPhotoboothCamera
else:
from HAL.generic import ozPhotoboothAction, ozPhotoboothInput, ozPhotoboothCamera
logger = logging.getLogger(__name__)
CONFIGURATION_FILENAME = "./ozPhotobooth.cfg"
# =================================================
class ozPhotobooth():
def __init__(self):
self.mfRunning = True
self.mCamera = None
self.mInput = None
self.mAction = None
self.mSequencer = None
self.mHTTPServer = None
self.mSound = None
self.mPhotoDisk = None
self.mDNSManager = None
def Setup(self, aConfigFilename):
self.mConfig = ozPhotoboothConfig.ozPhotoboothConfig(aConfigFilename)
if not self.mConfig.isLoaded():
logger.error("Unable to load configuration !")
return False
logger.debug("Loading sound ...")
self.mSound = ozPhotoboothSound.ozPhotoboothSound(self.mConfig)
logger.debug("Loading camera ...")
self.mCamera = ozPhotoboothCamera.ozPhotoboothCamera(self.mConfig)
if not self.mCamera.Setup():
logger.error("Unable to setup camera !")
return False
self.mCamera.Start()
self.mCamera.StartPreview()
logger.debug("Loading disk ...")
self.mPhotoDisk = ozPhotoboothPhotoDisk.ozPhotoboothPhotoDisk(self.mConfig)
logger.debug("Loading HTTPServer ...")
self.mHTTPServer = ozPhotoboothHTTPServer(self.mConfig, self.mPhotoDisk)
logger.debug("Loading input ...")
self.mInput = ozPhotoboothInput.ozPhotoboothInput(self.mConfig)
if not self.mInput.Setup():
logger.error("Unable to setup input !")
return False
self.mInput.Start()
logger.debug("Loading Action ...")
self.mAction = ozPhotoboothAction.ozPhotoboothAction(self.mConfig)
if not self.mAction.Setup():
logger.error("Unable to setup Action !")
return False
self.mAction.Start()
logger.debug("Loading DNSServer ...")
theDNSServerIP = self.mConfig.GetDNSServerIP()
if len(theDNSServerIP) > 0:
self.mDNSManager = ozDNSManager.ozDNSManager(theDNSServerIP)
self.mDNSManager.Start()
return True
def Quit(self):
self.mfRunning = False
logger.info("Photobooth exiting...")
if self.mSequencer != None and self.mSequencer.IsRunning():
self.mSequencer.Stop()
if self.mInput != None:
self.mInput.Stop()
if self.mAction != None:
self.mAction.Stop()
if self.mHTTPServer != None:
self.mHTTPServer.Stop()
if self.mCamera != None:
self.mCamera.Stop()
if self.mDNSManager != None:
self.mDNSManager.Stop()
def Run(self):
logger.info("Photobooth running...")
while self.mfRunning:
try:
#Handle events
for event in self.mInput.GetEvents():
if self.mSequencer == None or self.mSequencer.IsDone():
self.mSequencer = ozPhotoboothSequence.ozPhotoboothSequence(self.mConfig, self.mCamera, self.mAction, self.mSound, self.mPhotoDisk)
if event.type == ozPhotoboothEvent.ozPhotoboothEvent.ExitButton:
self.mfRunning = False
elif event.type == ozPhotoboothEvent.ozPhotoboothEvent.PictureButton:
if self.mSequencer != None and not self.mSequencer.IsRunning() and not self.mSequencer.IsDone():
self.mSequencer.Start()
#Avoid infinite loop
time.sleep(0.1)
except Exception as e:
logger.error("Photobooth exeception: " + str(traceback.format_exc()))
self.mfRunning = False
# =================================================
def CheckIPTables():
#List all: "sudo iptable -L"
#List prerouting: "sudo iptables -L -n -t nat"
#Delete a route: "sudo iptables -t nat -D PREROUTING 1"
#if os.name != 'nt' and os.uname()[1] == 'raspberrypi':
# os.system("sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 192.168.4.1:8080")
# os.system("sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.4.1:8080")
# os.system("sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080")
#REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 8080
if os.name != 'nt' and os.uname()[1] == 'raspberrypi':
process = subprocess.Popen(['iptables -L -n -t nat'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
stdout = process.communicate()[0]
thefHasRedirect80To8080 = False
theLines = str(stdout).split("\\n")
for line in theLines:
#logger.debug("Parsing iptable line: " + str(line))
if bool(re.search("REDIRECT.*tcp dpt:80 redir ports 8080", str(line))):
logger.info("Route from port 80 to 8080 found, nothing to do.")
thefHasRedirect80To8080 = True
if not thefHasRedirect80To8080:
logger.info("No route found for forwarding port 80 to port 8080, adding it ...")
os.system("sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080")
# =================================================
def AutoMountUSB():
kUSBKeyPath = ["/dev/sda1", "/dev/sdb1", "/dev/sdc1"]
if os.name != 'nt' and os.uname()[1] == 'raspberrypi':
for theDevPath in kUSBKeyPath:
if os.path.exists(theDevPath):
logger.info("Trying to mount " + theDevPath + " to /media/pi/usb")
os.system("sudo umount /media/pi/usb")
os.system("sudo mkdir -p /media/pi/usb")
os.system("sudo mount " + theDevPath + " /media/pi/usb")
return
logger.error("Unable to mount USB key: " + kUSBKeyPath + " not found !")
# =================================================
if __name__ == '__main__':
logging.basicConfig(filename='ozPhotobooth.log', level=logging.DEBUG, format='%(asctime)s %(module)32s: %(levelname)-8s %(message)s')
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(asctime)s %(module)32s: %(levelname)-8s %(message)s\r')
#Working directory is the script directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.environ["DISPLAY"] = ":0.0"
if os.name != 'nt' and os.uname()[1] == 'raspberrypi':
if os.geteuid() != 0:
logger.error("You need to execute this script as root !")
sys.exit(1)
CheckIPTables()
AutoMountUSB()
thePhotobooth = ozPhotobooth()
if thePhotobooth.Setup(CONFIGURATION_FILENAME):
thePhotobooth.Run()
thePhotobooth.Quit()
logger.debug("Program exit !")