/
main.py
executable file
·215 lines (172 loc) · 5.68 KB
/
main.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
#! venv/bin/python
from gpiozero import MotionSensor, LED, PWMLED, Button
from picamera import PiCamera
from datetime import datetime
from subprocess import run
from threading import Thread
from time import sleep
import os
import warnings
import requests
camera = PiCamera()
pir = MotionSensor(4)
standbyLight = LED(27)
armingLight = PWMLED(22)
armedLight = LED(18)
recordingLight = PWMLED(23)
uploadingLight = PWMLED(5)
startButton = Button(24)
stopButton = Button(25, hold_time=5)
VIDEOS_DIR = os.path.expanduser(os.path.join('~', 'smbshare', 'panopticon', 'public', 'storage'))
os.chdir(VIDEOS_DIR)
idCounter = 0
exitFlag = 0
running = 0
initialized = 0
recording = []
uploading = []
localServer = 'http://localhost'
class DropboxThread(Thread):
def __init__(self, threadID, name, videoFilePath, dropboxSavePath):
Thread.__init__(self)
self.threadID = threadID
self.name = name
self.videoFilePath = videoFilePath
self.dropboxSavePath = dropboxSavePath
def run(self):
global uploading
print("Uploading " + self.videoFilePath)
if exitFlag or not running:
print('Not uploading file')
return
try:
uploadingLight.pulse()
uploading.append(self.videoFilePath)
run(['dropbox_uploader.sh', 'upload', self.videoFilePath, self.dropboxSavePath])
uploading.pop()
print("Uploaded " + self.videoFilePath)
except Exception as exp:
uploading.pop()
print('There was a problem uploading to Dropbox: ', self.videoFilePath, ': ', exp.args)
finally:
if len(uploading) == 0:
uploadingLight.off()
def init():
global exitFlag
global running
global initialized
if initialized:
print("Already initialized")
return
while not running:
initialized = 1
requests.put(localServer + '/api/alarms/1', data={'status': 'initialized'})
with warnings.catch_warnings():
warnings.simplefilter('ignore')
pir.when_motion = None
armedLight.off()
standbyLight.on()
print('Press the start button to arm')
while not(running or exitFlag):
pass
if exitFlag:
return
start()
def motionDetected():
global idCounter, recording
if exitFlag or not running:
return
print('Motion detected')
requests.put(localServer + '/api/alarms/1', data={'status': 'recording'})
# Create year and month folder if not exists
current_year = datetime.now().strftime("%Y")
current_month = datetime.now().strftime("%m")
current_day = datetime.now().strftime("%d")
year_dir = os.path.join(VIDEOS_DIR, current_year)
if not os.path.isdir(year_dir):
os.mkdir(year_dir)
month_dir = os.path.join(year_dir, current_month)
if not os.path.isdir(month_dir):
os.mkdir(month_dir)
save_dir = os.path.join(month_dir, current_day)
if not os.path.isdir(save_dir):
os.mkdir(save_dir)
os.chdir(save_dir)
# Set file path for video
filename = datetime.now().strftime("%H.%M.%S")
videoFileName = os.path.join(save_dir, filename)
tempVideoFile = videoFileName + '.h264'
videoFile = videoFileName + '.mp4'
print('Start recording video')
recording.append(videoFile)
recordingLight.pulse()
camera.start_recording(tempVideoFile)
# pir.wait_for_no_motion()
sleep(10)
# Motion no longer detected
camera.stop_recording()
recording.pop()
if len(recording) == 0:
recordingLight.off()
requests.put(localServer + '/api/alarms/1', data={'status': 'recording'})
# If the video file was saved convert it to MP4
if os.path.isfile(tempVideoFile):
try:
uploadingLight.blink()
run(['MP4Box', '-fps', '30', '-add', tempVideoFile, videoFile])
os.remove(tempVideoFile);
except Exception as exp:
print('There was a problem while converting ', videoFile, ': ', exp.args)
finally:
uploadingLight.off()
print('Waiting for motion...')
requests.put(localServer + '/api/alarms/1', data={'status': 'armed'})
filename = os.path.basename(videoFile)
idCounter += 1
#dropboxUploadThread = DropboxThread(idCounter, filename, videoFile,
# os.path.join(current_year, current_month, current_day, filename))
#dropboxUploadThread.start()
def start():
global exitFlag
global running
global initialized
standbyLight.off()
initialized = 0
print("Arming...")
armingLight.pulse()
requests.put(localServer + '/api/alarms/1', data={'status': 'arming'})
r = requests.get(localServer + '/api/alarms/1')
data = r.json()['data']
armingDuration = data['arming_duration']
sleep(armingDuration)
armingLight.off()
armedLight.on()
print('Armed.')
requests.put(localServer + '/api/alarms/1', data={'status': 'armed'})
print('Waiting for motion...')
pir.when_motion = motionDetected
while not exitFlag and running:
pass
def end():
global exitFlag
print("Security program ending")
exitFlag = 1
if camera.recording:
camera.stop_recording()
requests.put(localServer + '/api/alarms/1', data={'status': 'disabled'})
exit()
def enableRunning():
global running
running = 1
def disableRunning():
global running
running = 0
startButton.when_pressed = enableRunning;
stopButton.when_pressed = disableRunning
stopButton.when_held = end
if __name__ == "__main__":
try:
init()
except Exception as e:
print(e)
end()