Ejemplo n.º 1
0
    def on_media_files(media_files):
        io_loop.stop()
        
        timestamp = time.mktime(moment.timetuple())

        if media_files:
            logging.debug('got media files')
            media_files = [m for m in media_files if abs(m['timestamp'] - timestamp) < timespan] # filter out non-recent media files
            media_files.sort(key=lambda m: m['timestamp'], reverse=True)
            media_files = [os.path.join(camera_config['target_dir'], re.sub('^/', '', m['path'])) for m in media_files]
        
            logging.debug('selected %d pictures' % len(media_files))

        format_dict = {
            'camera': camera_config['@name'],
            'hostname': socket.gethostname(),
            'moment': moment.strftime('%Y-%m-%d %H:%M:%S'),
        }
        
        if settings.LOCAL_TIME_FILE:
            format_dict['timezone'] = tzctl.get_time_zone()

        else:
            format_dict['timezone'] = 'local time'
    
        m = message % format_dict
        s = subject % format_dict
        s = s.replace('\n', ' ')
    
        m += '\n\n'
        m += 'motionEye.'
        
        callback(s, m, media_files)
Ejemplo n.º 2
0
    def mail(obj,confidence):
        import sendmail
        import tzctl
        import smtplib

        logging.debug('sending notification email')

        try:
            subject = sendmail.subjects['object_detected']
            message = sendmail.messages['object_detected']
            format_dict = {
                'camera': camera_config['@name'],
                'hostname': socket.gethostname(),
                'moment': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                'object': obj,
                'confidence': confidence,
            }
            if settings.LOCAL_TIME_FILE:
                format_dict['timezone'] = tzctl.get_time_zone()

            else:
                format_dict['timezone'] = 'local time'

            message = message % format_dict
            subject = subject % format_dict

            old_timeout = settings.SMTP_TIMEOUT
            settings.SMTP_TIMEOUT = 10
            sendmail.send_mail(data['smtp_server'], int(data['smtp_port']), data['smtp_account'],
                               data['smtp_password'], data['smtp_tls'], data['from'], [data['addresses']],
                               subject=subject, message=message, files=[])

            settings.SMTP_TIMEOUT = old_timeout

            self.finish_json()

            logging.debug('notification email succeeded')

        except Exception as e:
            if isinstance(e, smtplib.SMTPResponseException):
                msg = e.smtp_error

            else:
                msg = str(e)

            msg_lower = msg.lower()
            if msg_lower.count('tls'):
                msg = 'TLS might be required'

            elif msg_lower.count('authentication'):
                msg = 'authentication error'

            elif msg_lower.count('name or service not known'):
                msg = 'check SMTP server name'

            elif msg_lower.count('connection refused'):
                msg = 'check SMTP port'

            logging.error('notification email failed: %s' % msg, exc_info=True)
            self.finish_json({'error': str(msg)})
Ejemplo n.º 3
0
    def on_media_files(media_files):
        io_loop.stop()
        
        timestamp = time.mktime(moment.timetuple())

        if media_files:
            logging.debug('got media files')
            media_files = [m for m in media_files if abs(m['timestamp'] - timestamp) < timespan] # filter out non-recent media files
            media_files.sort(key=lambda m: m['timestamp'], reverse=True)
            media_files = [os.path.join(camera_config['target_dir'], re.sub('^/', '', m['path'])) for m in media_files]
        
            logging.debug('selected %d pictures' % len(media_files))

        format_dict = {
            'camera': camera_config['@name'],
            'hostname': socket.gethostname(),
            'moment': moment.strftime('%Y-%m-%d %H:%M:%S'),
        }
        
        if settings.LOCAL_TIME_FILE:
            format_dict['timezone'] = tzctl.get_time_zone()

        else:
            format_dict['timezone'] = 'local time'
    
        m = message % format_dict
        s = subject % format_dict
        s = s.replace('\n', ' ')
    
        m += '\n\n'
        m += 'motionEye.'
        
        callback(s, m, media_files)
Ejemplo n.º 4
0
    def on_media_files(media_files):
        io_loop.stop()
        photos = []

        timestamp = time.mktime(moment.timetuple())
        if media_files:
            logging.debug('got media files')
            media_files = [
                m for m in media_files
                if abs(m['timestamp'] - timestamp) < float(timespan)
            ]
            media_files.sort(key=lambda m: m['timestamp'], reverse=True)
            media_files = [
                os.path.join(camera_config['target_dir'],
                             re.sub('^/', '', m['path'])) for m in media_files
            ]
            logging.debug('selected %d pictures' % len(media_files))

        format_dict = {
            'camera': camera_config['camera_name'],
            'hostname': socket.gethostname(),
            'moment': moment.strftime('%Y-%m-%d %H:%M:%S'),
        }

        if settings.LOCAL_TIME_FILE:
            format_dict['timezone'] = tzctl.get_time_zone()

        else:
            format_dict['timezone'] = 'local time'

        logging.debug('creating email message')

        m = message % format_dict

        callback(m, media_files)
Ejemplo n.º 5
0
def send_email_notification(camera_config, files):
    import socket
    import sendmail
    import tzctl
    import smtplib

    logging.debug('sending animation email')

    try:
        subject = sendmail.subjects['motion_end']
        message = sendmail.messages['motion_start']
        format_dict = {
            'camera': camera_config['@name'],
            'hostname': socket.gethostname(),
            'moment': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        }
        if settings.LOCAL_TIME_FILE:
            format_dict['timezone'] = tzctl.get_time_zone()

        else:
            format_dict['timezone'] = 'local time'

        message = message % format_dict
        subject = subject % format_dict

        sendmail.send_mail(
            camera_config['@animation_email_notifications_smtp_server'],
            int(camera_config['@animation_email_notifications_smtp_port']),
            camera_config['@animation_email_notifications_smtp_account'],
            camera_config['@animation_email_notifications_smtp_password'],
            camera_config['@animation_email_notifications_smtp_tls'],
            camera_config['@animation_email_notifications_from'],
            [camera_config['@animation_email_notifications_addresses']],
            subject=subject,
            message=message,
            files=files)

        logging.debug('animation email succeeded')

    except Exception as e:
        if isinstance(e, smtplib.SMTPResponseException):
            msg = e.smtp_error
        else:
            msg = str(e)
        logging.error('animation email failed: %s' % msg, exc_info=True)
Ejemplo n.º 6
0
def queue_motion(camera_id, camera_config):
    try:
        import sendmail
        import socket
        import tzctl
        message = sendmail.messages['motion_start']
        format_dict = {
            'camera': camera_config['@name'],
            'hostname': socket.gethostname(),
            'moment': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        }
        if settings.LOCAL_TIME_FILE:
            format_dict['timezone'] = tzctl.get_time_zone()

        else:
            format_dict['timezone'] = 'local time'

        message = message % format_dict

        # TODO motion email notification rework
        #if camera_config['@animation_email_enabled']:
        #    tasks.add(0, send_email_notification, tag='queue_animation(%s)' % anim_path,
        #              camera_config=camera_config, files=[anim_path])

        # motion telegram notification
        if camera_config['@telegram_enabled'] and camera_config[
                '@telegram_motion_enabled']:

            tasks.add(0,
                      send_telegram_notification,
                      tag='queue_animation(%s)' % camera_id,
                      camera_config=camera_config,
                      message=message)
            #send_telegram_notification(camera_config=camera_config, message=message)

    except Exception as e:
        logging.error('queue motion notification failed: %s' % e.message,
                      exc_info=True)