Exemple #1
0
 def _handle_event_start(self, **kwargs):
     logger.info('Writing event_start to DB')
     _date = datetime.strptime(kwargs['call_date'], '%Y-%m-%d %H:%M:%S')
     _call_date = datetime.strptime(kwargs['handler_call_time'],
                                    '%Y-%m-%d %H:%M:%S')
     db_session.add(
         MotionEvent(text_event=kwargs['text_event'],
                     date=_date,
                     event_id=kwargs['event_id'],
                     frame_num=kwargs['frame_num'],
                     cam_num=kwargs['cam'],
                     cam_name=kwargs['cam_name'],
                     host=kwargs['host'],
                     changed_pixels=kwargs['changed_px'],
                     threshold=kwargs['threshold'],
                     despeckle_labels=kwargs['despeckle_labels'],
                     fps=kwargs['fps'],
                     noise=kwargs['noise'],
                     motion_width=kwargs['motion_width'],
                     motion_height=kwargs['motion_height'],
                     motion_center_x=kwargs['motion_center_x'],
                     motion_center_y=kwargs['motion_center_y'],
                     handler_call_start_datetime=_call_date))
     db_session.commit()
     from motion_pipeline.celerytasks.tasks import newevent_ready
     newevent_ready.delay(kwargs['text_event'])
Exemple #2
0
 def _handle_event_end(self, **kwargs):
     e = db_session.query(MotionEvent).get(kwargs['text_event'])
     _call_date = datetime.strptime(kwargs['handler_call_time'],
                                    '%Y-%m-%d %H:%M:%S')
     if e is None:
         logger.info(
             'Writing event_end to DB without matching started event '
             '(text_event=%s)', kwargs['text_event'])
         _date = datetime.strptime(kwargs['call_date'], '%Y-%m-%d %H:%M:%S')
         e = MotionEvent(text_event=kwargs['text_event'],
                         date=_date,
                         event_id=kwargs['event_id'],
                         frame_num=kwargs['frame_num'],
                         cam_num=kwargs['cam'],
                         cam_name=kwargs['cam_name'],
                         host=kwargs['host'],
                         changed_pixels=kwargs['changed_px'],
                         threshold=kwargs['threshold'],
                         despeckle_labels=kwargs['despeckle_labels'],
                         fps=kwargs['fps'],
                         noise=kwargs['noise'],
                         motion_width=kwargs['motion_width'],
                         motion_height=kwargs['motion_height'],
                         motion_center_x=kwargs['motion_center_x'],
                         motion_center_y=kwargs['motion_center_y'])
         db_session.add(e)
     else:
         logger.info('Writing event_end to DB for existing event %s',
                     kwargs['text_event'])
     e.is_finished = True
     e.handler_call_end_datetime = _call_date
     if kwargs.get('debug_frame_info', None) is not None:
         self._handle_debug_frame_info(kwargs['text_event'],
                                       kwargs['debug_frame_info'])
     db_session.commit()
Exemple #3
0
def purge_archived_from_db():
    bucket = get_s3_bucket(settings)
    archived = db_session.query(Video).filter(
        Video.is_archived.__eq__(True)).all()
    logger.info('Found %d archived videos in database', len(archived))
    for video in archived:
        for n in db_session.query(Notification).filter(
                Notification.video_filename.__eq__(video.filename)):
            db_session.delete(n)
        try:
            remove_bucket_object(bucket,
                                 settings.BUCKET_PREFIX + video.filename)
            if video.thumbnail_name is not None:
                remove_bucket_object(
                    bucket, settings.BUCKET_PREFIX + video.thumbnail_name)
        except Exception as ex:
            logger.error('Excepting deleting archived video filename=%s: %s',
                         video.filename,
                         ex,
                         exc_info=True)
            continue
        db_session.delete(video)
        db_session.delete(video.event)
        for fdi in video.event.frame_debug_infos:
            db_session.delete(fdi)
        db_session.commit()
Exemple #4
0
 def get(self, path):
     return_to = request.args.get('return_to', '/simple/videos')
     upload = db_session.query(Video).get(path)
     assert upload is not None
     logger.info('Archiving: %s', path)
     upload.is_archived = True
     db_session.commit()
     return proxy_aware_redirect(return_to, code=302)
Exemple #5
0
def purge_orphaned_events():
    orphaned = db_session.query(MotionEvent).filter(
        MotionEvent.video.__eq__(None),
        MotionEvent.date.__lt__(dtnow() - timedelta(days=1))).all()
    logger.info('Found %d events older than one day with no video',
                len(orphaned))
    for e in orphaned:
        db_session.delete(e)
        db_session.commit()
Exemple #6
0
 def post(self, text_event):
     event = db_session.query(MotionEvent).get(text_event)
     assert event is not None
     m = request.get_json()
     if m is None:
         m = request.get_data()
     logger.info('Setting disposition of event %s to: %s', text_event,
                 m['disposition'])
     if m['disposition'] == 'none':
         event.disposition = None
     else:
         event.disposition = EventDispositionEnum[m['disposition']]
     db_session.commit()
     return 'OK'
Exemple #7
0
 def _get_notification_from_db(self, notification_id):
     if notification_id is not None:
         n = db_session.query(Notification).get(notification_id)
         if n is not None:
             logger.debug('Found existing Notification with ID %s', n.id)
             return n
     logger.debug('Generating new Notification in DB')
     n = Notification(video_filename=self._filename,
                      text_event=self._event_text,
                      generated_time=datetime.now(),
                      provider_name='pushover')
     db_session.add(n)
     db_session.commit()
     return n
Exemple #8
0
 def send(self, params):
     """send to pushover"""
     logger.debug('Sending Pushover notification; params=%s', params)
     r = requests.post('https://api.pushover.net/1/messages.json', **params)
     logger.debug('Pushover POST response HTTP %s: %s', r.status_code,
                  r.text)
     r.raise_for_status()
     if r.json()['status'] != 1:
         raise RuntimeError('Error response from Pushover: %s', r.text)
     self._notification.sent_time = datetime.now()
     self._notification.num_retries = self._celery_request.retries
     self._notification.provider_response = r.text
     db_session.commit()
     db_session.expunge_all()
     cleanup_db()
Exemple #9
0
 def _handle_file_upload(self, **kwargs):
     logger.info('Writing file upload to DB for filename: %s',
                 kwargs['filename'])
     _date = datetime.strptime(kwargs['call_date'], '%Y-%m-%d %H:%M:%S')
     _call_date = datetime.strptime(kwargs['handler_call_time'],
                                    '%Y-%m-%d %H:%M:%S')
     db_session.add(
         Video(filename=os.path.basename(kwargs['filename']),
               date=_date,
               event_id=kwargs['event_id'],
               cam_num=kwargs['cam'],
               cam_name=kwargs['cam_name'],
               host=kwargs['host'],
               text_event=kwargs['text_event'],
               file_type=kwargs['filetype'],
               threshold=kwargs['threshold'],
               despeckle_labels=kwargs['despeckle_labels'],
               fps=kwargs['fps'],
               handler_call_datetime=_call_date))
     db_session.commit()
     from motion_pipeline.celerytasks.tasks import do_thumbnail
     do_thumbnail.delay(os.path.basename(kwargs['filename']))
Exemple #10
0
 def create_and_upload_thumbnail(self,
                                 filename,
                                 trigger_newvideo_ready=True):
     if settings.MINIO_LOCAL_MOUNTPOINT is None:
         raise NotImplementedError(
             'ERROR: Downloading new videos from S3 not yet implemented!')
     video = db_session.query(Video).get(filename)
     assert video is not None
     s3 = get_s3_bucket(settings, tasklogger=logger)
     vid_path = os.path.join(settings.MINIO_LOCAL_MOUNTPOINT, filename)
     logger.debug('Handling new video at: %s', vid_path)
     thumbnail_name = '%s.jpg' % filename
     logger.info('Generating still thumbnail of second frame of %s to: %s',
                 filename, thumbnail_name)
     video_length = None
     with autoremoving_tempfile(suffix='.jpg') as imgpath:
         with autoremoving_tempfile(suffix='.jpg') as framepath:
             # get the length of the video
             cmd = [
                 'ffprobe', '-show_format', '-of', 'json', '-i', vid_path
             ]
             logger.debug('Running: %s', ' '.join(cmd))
             res = run(cmd, stdout=PIPE, timeout=120)
             ffprobe_json = deserialize(res.stdout.strip().decode())
             logger.debug('ffprobe JSON lists video duration as: %s',
                          ffprobe_json['format']['duration'])
             video_length = Decimal(
                 ffprobe_json['format']['duration']).quantize(
                     Decimal('.001'), rounding=ROUND_UP)
             # Extract the second frame from the video
             cmd = [
                 'ffmpeg', '-y', '-ss', '00:00:00.2', '-i', vid_path,
                 '-frames:v', '1', framepath
             ]
             logger.debug('Running: %s', ' '.join(cmd))
             res = run(cmd, stdout=PIPE, stderr=STDOUT, timeout=120)
             if res.returncode != 0:
                 raise RuntimeError(
                     'ERROR: ffmpeg command "%s" exited %d:\n%s' %
                     (' '.join(cmd), res.returncode, res.stdout))
             fsize = os.stat(framepath).st_size
             if fsize < 100:
                 raise RuntimeError(
                     'ERROR: ffmpeg command "%s" resulted in %d '
                     'byte file:\n%s', ' '.join(cmd), fsize, res.stdout)
             logger.debug('ffmpeg complete; %d-byte file', fsize)
             # create a thumbnail from that
             logger.debug('Creating thumbnail of still...')
             i = Image.open(framepath)
             i.thumbnail(settings.THUMBNAIL_MAX_SIZE, Image.ANTIALIAS)
             i.save(imgpath, 'JPEG')
             key = '%s%s' % (settings.BUCKET_PREFIX, thumbnail_name)
             logger.debug('Uploading thumbnail to S3 at: %s', key)
             s3.upload_file(imgpath, key)
     logger.info('Still thumbnail of %s uploaded to S3 at: %s', filename,
                 key)
     # update the DB with the thumbnail name
     video.thumbnail_name = thumbnail_name
     video.length_sec = video_length
     db_session.commit()
     if trigger_newvideo_ready:
         from motion_pipeline.celerytasks.tasks import newvideo_ready
         newvideo_ready.delay(video.filename, video.text_event)
     db_session.expunge_all()
     cleanup_db()