diff --git a/motioneye/handlers/action.py b/motioneye/handlers/action.py index b494d0742..1687002be 100644 --- a/motioneye/handlers/action.py +++ b/motioneye/handlers/action.py @@ -66,6 +66,18 @@ async def post(self, camera_id, action): ) return self.record_stop(camera_id) + elif action == 'eventstart': + logging.debug( + f'executing event_start action for camera with id {camera_id}' + ) + await self.event_start(camera_id) + return + + elif action == 'eventend': + logging.debug(f'executing event_end action for camera with id {camera_id}') + await self.event_end(camera_id) + return + action_commands = config.get_action_commands(local_config) command = action_commands.get(action) if not command: @@ -123,3 +135,11 @@ def record_start(self, camera_id): def record_stop(self, camera_id): return self.finish_json({}) + + async def event_start(self, camera_id): + await motionctl.start_event(camera_id) + return self.finish_json({}) + + async def event_end(self, camera_id): + await motionctl.end_event(camera_id) + return self.finish_json({}) diff --git a/motioneye/motionctl.py b/motioneye/motionctl.py index 272e487af..d9b392949 100644 --- a/motioneye/motionctl.py +++ b/motioneye/motionctl.py @@ -301,6 +301,58 @@ async def take_snapshot(camera_id): logging.debug(f'successfully took snapshot for camera with id {camera_id}') +async def start_event(camera_id): + motion_camera_id = camera_id_to_motion_camera_id(camera_id) + if motion_camera_id is None: + return logging.error( + f'could not find motion camera id for camera with id {camera_id}' + ) + + logging.debug(f'starting event for camera with id {camera_id}') + + url = f'http://127.0.0.1:{settings.MOTION_CONTROL_PORT}/{motion_camera_id}/action/eventstart' + + request = HTTPRequest( + url, + connect_timeout=_MOTION_CONTROL_TIMEOUT, + request_timeout=_MOTION_CONTROL_TIMEOUT, + ) + resp = await AsyncHTTPClient().fetch(request) + if resp.error: + logging.error( + f'failed to start event for camera with id {camera_id}: {utils.pretty_http_error(resp)}' + ) + + else: + logging.debug(f'successfully start event for camera with id {camera_id}') + + +async def end_event(camera_id): + motion_camera_id = camera_id_to_motion_camera_id(camera_id) + if motion_camera_id is None: + return logging.error( + f'could not find motion camera id for camera with id {camera_id}' + ) + + logging.debug(f'ending event for camera with id {camera_id}') + + url = f'http://127.0.0.1:{settings.MOTION_CONTROL_PORT}/{motion_camera_id}/action/eventend' + + request = HTTPRequest( + url, + connect_timeout=_MOTION_CONTROL_TIMEOUT, + request_timeout=_MOTION_CONTROL_TIMEOUT, + ) + resp = await AsyncHTTPClient().fetch(request) + if resp.error: + logging.error( + f'failed to end event for camera with id {camera_id}: {utils.pretty_http_error(resp)}' + ) + + else: + logging.debug(f'successfully end event for camera with id {camera_id}') + + def is_motion_detected(camera_id): return _motion_detected.get(camera_id, False)