saving events and simple endpoint

This commit is contained in:
Blake Blackshear 2020-10-31 06:43:29 -05:00
parent be1fcbbdf8
commit 5512bb2e06
2 changed files with 22 additions and 21 deletions

View File

@ -91,15 +91,18 @@ flask_db = FlaskDB(app)
db = flask_db.database db = flask_db.database
log = logging.getLogger('werkzeug') log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR) log.setLevel(logging.ERROR)
peewee_log = logging.getLogger('peewee')
peewee_log.addHandler(logging.StreamHandler())
peewee_log.setLevel(logging.DEBUG)
class Event(flask_db.Model): class Event(flask_db.Model):
id = CharField(null=False, primary_key=True, max_length=30) id = CharField(null=False, primary_key=True, max_length=30)
label = CharField(index=True, max_length=20) label = CharField(index=True, max_length=20)
camera = CharField(index=True, max_length=20) camera = CharField(index=True, max_length=20)
start_time = DateTimeField(), start_time = DateTimeField()
end_time = DateTimeField(), end_time = DateTimeField()
top_score = FloatField(), top_score = FloatField()
false_positive = BooleanField(), false_positive = BooleanField()
zones = JSONField() zones = JSONField()
def init_db(): def init_db():
@ -303,7 +306,7 @@ def main():
camera_process['process'].start() camera_process['process'].start()
print(f"Camera process started for {name}: {camera_process['process'].pid}") print(f"Camera process started for {name}: {camera_process['process'].pid}")
event_processor = EventProcessor(CONFIG, camera_process_info, CACHE_DIR, CLIPS_DIR, event_queue, stop_event) event_processor = EventProcessor(CONFIG, camera_process_info, CACHE_DIR, CLIPS_DIR, event_queue, stop_event, Event)
event_processor.start() event_processor.start()
object_processor = TrackedObjectProcessor(CONFIG['cameras'], client, MQTT_TOPIC_PREFIX, tracked_objects_queue, event_queue, stop_event) object_processor = TrackedObjectProcessor(CONFIG['cameras'], client, MQTT_TOPIC_PREFIX, tracked_objects_queue, event_queue, stop_event)
@ -353,9 +356,7 @@ def main():
@app.route('/events') @app.route('/events')
def events(): def events():
events = Event.select().dicts() events = Event.select()
# if events is None:
# return jsonify([])
return jsonify([model_to_dict(e) for e in events]) return jsonify([model_to_dict(e) for e in events])
@app.route('/debug/stats') @app.route('/debug/stats')

View File

@ -7,10 +7,9 @@ import json
import datetime import datetime
import subprocess as sp import subprocess as sp
import queue import queue
from tinydb import TinyDB
class EventProcessor(threading.Thread): class EventProcessor(threading.Thread):
def __init__(self, config, camera_processes, cache_dir, clip_dir, event_queue, stop_event): def __init__(self, config, camera_processes, cache_dir, clip_dir, event_queue, stop_event, Event):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.config = config self.config = config
self.camera_processes = camera_processes self.camera_processes = camera_processes
@ -20,7 +19,7 @@ class EventProcessor(threading.Thread):
self.event_queue = event_queue self.event_queue = event_queue
self.events_in_process = {} self.events_in_process = {}
self.stop_event = stop_event self.stop_event = stop_event
self.db = TinyDB(f"{os.path.join(self.clip_dir, 'events')}.json") self.Event = Event
def refresh_cache(self): def refresh_cache(self):
cached_files = os.listdir(self.cache_dir) cached_files = os.listdir(self.cache_dir)
@ -178,16 +177,17 @@ class EventProcessor(threading.Thread):
self.events_in_process[event_data['id']] = event_data self.events_in_process[event_data['id']] = event_data
if event_type == 'end': if event_type == 'end':
self.db.insert({ self.Event.create(
'id': event_data['id'], id=event_data['id'],
'label': event_data['label'], label=event_data['label'],
'camera': camera, camera=camera,
'start_time': event_data['start_time'], start_time=event_data['start_time'],
'end_time': event_data['end_time'], end_time=event_data['end_time'],
'top_score': event_data['top_score'], top_score=event_data['top_score'],
'false_positive': event_data['false_positive'], false_positive=event_data['false_positive'],
'zones': list(event_data['entered_zones']) zones=list(event_data['entered_zones'])
}) )
if len(self.cached_clips) > 0 and not event_data['false_positive']: if len(self.cached_clips) > 0 and not event_data['false_positive']:
self.create_clip(camera, event_data, save_clips_config.get('pre_capture', 30)) self.create_clip(camera, event_data, save_clips_config.get('pre_capture', 30))
del self.events_in_process[event_data['id']] del self.events_in_process[event_data['id']]