-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurveillance_frame.py
More file actions
294 lines (249 loc) · 11.6 KB
/
surveillance_frame.py
File metadata and controls
294 lines (249 loc) · 11.6 KB
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
#!/usr/bin/env python3
"""
Surveillance Frame main module.
"""
import argparse
import datetime
import logging
import os
import re
import signal
import sys
from logging.handlers import TimedRotatingFileHandler
from queue import Queue
from typing import List, Optional, Tuple
import RPi.GPIO as GPIO # pylint: disable=import-error
from events.event import Event
from events.signals import Signal
from objects.button import Button
from objects.camera_stream import CameraStream
from objects.camera_motion import CameraMotion
from objects.display_power import DisplayPower
from objects.event_dispatcher import EventDispatcher
from objects.motion_sensor import MotionSensor
from objects.notifier import Notifier
from objects.power_manager import PowerManager, PowerSchedule
from objects.slideshow import Slideshow
from objects.threaded_object_supervisor import ThreadedObjectSupervisor
# Define the logger
LOG = logging.getLogger(os.path.basename(__file__).split('.')[0])
def parse_arguments() -> argparse.Namespace:
"""
Parse the command line arguments.
:return: Parsed command line arguments.
"""
parser = argparse.ArgumentParser(
description="Surveillance frame application controlling a picture slideshow, display power\n"
"and a surveillance camera stream. The behavior of the application is configured\n"
"by (multiple) power mode schedules.\n\n"
"The following power modes are available:\n"
"- ALWAYS_ON: Display is always on but can be powered off with a long button\n"
" press (power back on with any button press).\n"
"- CAMERA_MOTION: Display is off unless the camera detected motion. A short\n"
" button press enables the display and shows the camera stream\n"
" for 30 seconds.\n"
"- MOTION_SENSOR: Display is off but will be switched on for 'motion-timeout'\n"
" seconds if the motion sensor detected motion (timer is\n"
" restarted upon further motion). It will also be switched on while\n"
" the camera detects motion. A short button press enables the\n"
" display and shows the camera stream for 60 seconds.",
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-b", "--button-gpio", metavar="GPIO", action="store",
help="GPIO BOARD channel number a push button is connected to (active high)")
parser.add_argument("-i", "--slideshow-interval", metavar="SECONDS", action="store", default=15,
help="time in seconds each picture will be shown (default: %(default)s)")
parser.add_argument("-l", "--listen", metavar="IP:PORT", action="store", default="0.0.0.0:10042",
help="address to bind the HTTP motion trigger server to (default: %(default)s)")
parser.add_argument("-L", "--log-file", action="store", help="log to the given file (rotated at midnight)")
parser.add_argument("-m", "--motion-gpio", metavar="GPIO", action="store",
help="GPIO BOARD channel number a motion sensor is connected to (active high on motion)")
parser.add_argument("-p", "--picture-dir", metavar="PATH", action="store",
help="path to the directory containing pictures to be shown")
parser.add_argument("-s", "--stream-url", metavar="URL", action="store", required=True,
help="camera stream URL to be shown")
parser.add_argument("-S", "--schedule", action="store", nargs="+",
help="power mode schedule in the format: <weekday>,<start>,<end>,<mode>\n"
" <weekday>: day of the week (Monday, Tuesday, ...), 'weekday' (Monday until\n"
" Friday), 'weekend' (Saturday and Sunday) or 'anyday' (Monday until\n"
" Sunday)\n"
" <start>: start time in the format HH:MM\n"
" <end>: end time (not included) in the format HH:MM\n"
" <mode>: power mode, see above\n"
"Example: Tuesday,22:00,0:00,CAMERA_MOTION\n"
"Default mode if no schedule matches is ALWAYS_ON. First matching schedule will\n"
"be used.")
parser.add_argument("-t", "--motion-timeout", metavar="SECONDS", action="store", default=3600,
help="timeout for which the display will be switched on when motion has been detected (default:"
" %(default)s)")
parser.add_argument("-v", "--verbose", action="store_true", help="enable verbose logging")
return parser.parse_args()
def configure_logging(arguments: argparse.Namespace) -> None:
"""
Configure logging.
:param arguments: Parsed command line arguments.
:return: None
"""
if arguments.verbose:
formatter = logging.Formatter("%(asctime)s [%(levelname)s] <%(name)s> %(message)s")
level = logging.DEBUG
else:
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
level = logging.INFO
root_logger = logging.getLogger()
root_logger.setLevel(level)
# Log to console
console_log = logging.StreamHandler()
console_log.setLevel(level)
console_log.setFormatter(formatter)
root_logger.addHandler(console_log)
# Log to file
if arguments.log_file:
file_log = TimedRotatingFileHandler(arguments.log_file, when="midnight", backupCount=13)
file_log.setLevel(level)
file_log.setFormatter(formatter)
root_logger.addHandler(file_log)
def get_listen(listen: str) -> Tuple[str, int]:
"""
Get the parsed --listen command line argument.
:param listen: Value of the --listen command line argument.
:return: Tuple consisting of IP and port to bind.
"""
bind_ip = "0.0.0.0"
result = re.match(r"^([0-9]+(?:\.[0-9]+){3}):([0-9]+)$", listen)
if result:
if result.group(1):
bind_ip = result.group(1)
bind_port = int(result.group(2))
else:
LOG.critical("Invalid IP/port specified.")
sys.exit(-1)
return bind_ip, bind_port
def get_schedules(schedules: List[str]) -> Optional[List[PowerSchedule]]:
"""
Get a list of power schedules from the given command line arguments.
:param schedules:
:return:
"""
if schedules is None:
return None
power_schedules = []
for schedule in schedules:
elements = schedule.split(",")
if len(elements) != 4:
LOG.critical("Invalid power schedule '%s' given.", schedule)
sys.exit(-1)
# Weekday
weekdays = {"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3, "friday": 4, "saturday": 5, "sunday": 6,
"weekday": PowerSchedule.WEEKDAY, "weekend": PowerSchedule.WEEKEND, "anyday": PowerSchedule.ANYDAY}
try:
weekday = weekdays[elements[0].lower()]
except KeyError:
LOG.critical("Invalid weekday '%s' given.", elements[0])
sys.exit(-1)
# Start time
time_pattern = r"^(\d{1,2}):(\d{2})$"
result = re.match(time_pattern, elements[1])
if result:
start = datetime.time(int(result.group(1)), int(result.group(2)), 0)
else:
LOG.critical("Invalid start time '%s' given.", elements[1])
sys.exit(-1)
# End time
result = re.match(time_pattern, elements[2])
if result:
end = datetime.time(int(result.group(1)), int(result.group(2)), 0)
if end != datetime.time(0) and start >= end:
LOG.critical("End time '%s' must be after start time.", elements[2])
sys.exit(-1)
else:
LOG.critical("Invalid end time '%s' given.", elements[2])
sys.exit(-1)
# Power mode
modes = {"ALWAYS_ON": PowerManager.Mode.ALWAYS_ON, "CAMERA_MOTION": PowerManager.Mode.CAMERA_MOTION,
"MOTION_SENSOR": PowerManager.Mode.MOTION_SENSOR}
try:
mode = modes[elements[3].upper()]
except KeyError:
LOG.critical("Invalid power mode '%s' given.", elements[3])
sys.exit(-1)
# Argument is valid, add the power schedule
power_schedules.append(PowerSchedule(weekday, start, end, mode))
return power_schedules
def signal_handler(signal_number: int, _) -> None:
"""
Signal handler.
:param signal_number: Signal number.
:param _: Stack frame.
:return: None
:raise: OSError with signal string.
"""
# pylint: disable=no-member
raise OSError(signal.Signals(signal_number).name)
# pylint: disable=too-many-locals
def main() -> None:
"""
Main entry point.
:return: None
"""
# Application setup
arguments = parse_arguments()
configure_logging(arguments)
bind_ip, bind_port = get_listen(arguments.listen)
schedules = get_schedules(arguments.schedule)
signal.signal(signal.SIGTERM, signal_handler)
# GPIO mode
GPIO.setmode(GPIO.BOARD)
# Start the application
threaded_object_supervisor = None
threaded_objects = []
try:
communication_objects = []
communication_queue = Queue()
# Display power
display_power = DisplayPower(communication_queue)
communication_objects.append(display_power)
# Slideshow
if arguments.picture_dir:
slideshow = Slideshow(communication_queue, arguments.picture_dir, int(arguments.slideshow_interval))
communication_objects.append(slideshow)
# Camera stream
camera_stream = CameraStream(communication_queue, arguments.stream_url)
communication_objects.append(camera_stream)
# Camera motion
camera_motion = CameraMotion(communication_queue, bind_ip, bind_port).start()
communication_objects.append(camera_motion)
threaded_objects.append(camera_motion)
# Motion sensor
if arguments.motion_gpio:
motion_sensor = MotionSensor(communication_queue, int(arguments.motion_gpio))
communication_objects.append(motion_sensor)
# Button
if arguments.button_gpio:
button = Button(communication_queue, int(arguments.button_gpio))
communication_objects.append(button)
# Power manager
power_manager = PowerManager(communication_queue, int(arguments.motion_timeout), schedules).start()
communication_objects.append(power_manager)
threaded_objects.append(power_manager)
# Notifier
notifier = Notifier(communication_queue).start()
communication_objects.append(notifier)
threaded_objects.append(notifier)
# Event dispatcher
event_dispatcher = EventDispatcher(communication_queue, communication_objects).start()
threaded_objects.append(event_dispatcher)
# Threaded object supervisor
threaded_object_supervisor = ThreadedObjectSupervisor(threaded_objects).start()
threaded_object_supervisor.join()
except KeyboardInterrupt:
LOG.info("Received keyboard interrupt, shutting down...")
except OSError as exception:
LOG.info("Received %s, shutting down...", exception)
finally:
# Stop all objects
if threaded_object_supervisor:
threaded_object_supervisor.dispatch(Event(Signal.TERMINATE))
# Configure the GPIOs to their previous state
GPIO.cleanup()
if __name__ == "__main__":
main()