-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
121 lines (86 loc) · 3.91 KB
/
controller.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
import time
import string
import logging
from threading import Thread
import config as cfg
from camera import Camera
from rcwl_0516 import RCWL_0516
from bot import SurveillanceBot
from role import Role
class Controller():
def __init__(self):
#: Init bot, camera and motion detector
self.bot = SurveillanceBot(self.pause_unpause_callback)
self.camera = Camera()
self.rcwl = RCWL_0516(self.motion_state_change_callback)
#: Start detecting in dedicated thread
Thread(target = self.rcwl.detect).start()
self.motion_active = False
#: Logging setup
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', level=logging.DEBUG)
#: Init logger
self.logger = logging.getLogger(__name__)
self.surveillance_paused = True
self.no_detection_timeframe_start = None #TODO allow to set starttime of a no-detection timeframe
self.no_detection_timeframe_end = None #TODO allow to set endtime of a no-detection timeframe
def pause_unpause_callback(self, pause_surveillance: bool) -> bool:
''' Callback for telegram bot to inform about pausing and unpausing the surveillance
#: returns whether or not the state changed
'''
#: save if the state is about to change
did_change = pause_surveillance != self.surveillance_paused
#: set anyways
self.surveillance_paused = pause_surveillance
#: return wheter the update changed the state
return did_change
def motion_state_change_callback(self, is_motion_start):
'''
'''
#: If motion sensor registered movement ...
if is_motion_start:
self.motion_active = True
#: ... and camera is not yet recording and survaillance is not paused - start recording
if not self.camera.is_recording and not self.surveillance_paused:
Thread(target = self._start_recording).start()
else:
self.motion_active = False
def _start_recording(self):
'''
'''
self.camera.start_recording()
self.logger.debug('Started recording')
self.bot.alert('Motion detected, recording started!', Role.OPEN)
self._timer()
#self.timer_thread = Thread(target = self._timer)
#self.timer_thread.start()
def _stop_recording(self):
'''
'''
video = self.camera.stop_recording()
self.logger.debug('Stopped recording')
self.bot.send_surveillance_video(video)
def _timer(self):
'''
'''
inactive = 0
#: Check every second until max video length
for i in range(cfg.MAX_VIDEO_LENGTH):
time.sleep(1)
#: If motion stopped, increment counter
if not self.motion_active:
inactive += 1
self.logger.debug('recording [{}/{} s] - motion inactive [{}/{}]'.format(i, cfg.MAX_VIDEO_LENGTH, inactive, cfg.BUFFER_TIME_STEPS))
#: else reset counter
else:
self.logger.debug('recording [{}/{} s] - motion active'.format(i, cfg.MAX_VIDEO_LENGTH))
inactive = 0
#: If motion is inactive for too long, stop video
if inactive >= cfg.BUFFER_TIME_STEPS:
Thread(target = self._stop_recording).start()
return
#: If the recording oulasts the max lenght, stop
if self.camera.is_recording:
Thread(target = self._stop_recording).start()
#: Start new recording only if surveillance is not paused
if not self.surveillance_paused:
self._start_recording()