diff --git a/camdetect.py b/camdetect.py index 79abad5..251ef30 100755 --- a/camdetect.py +++ b/camdetect.py @@ -51,6 +51,7 @@ SOURCE_NAME = os.environ["SOURCE_NAME"] SLIDE_WINDOW = 2 DCT_BLOCK_SIZE = 8 UPLOAD_FRAMESKIP = 3 +CLOCK_SKEW_TOLERANCE = timedelta(seconds=3) # Percentage of blocks active to consider movement in whole frame THRESHOLD_RATIO = int(os.getenv("THRESHOLD_RATIO", "5")) @@ -273,11 +274,14 @@ async def motion_detector(reference_frame, download_queue, upload_queue): # Handle event start if motion_detected and not event_id: result = await app.ctx.coll.find_one_and_update({ - "timestamp": dt, # TODO: Account for clock skew + "@timestamp": { + "$lte": dt + CLOCK_SKEW_TOLERANCE, + "$gte": dt - CLOCK_SKEW_TOLERANCE, + }, "source": SOURCE_NAME, }, { "$setOnInsert": { - "timestamp": dt, + "@timestamp": dt, "source": SOURCE_NAME, "event": "motion-detected", "started": dt, @@ -552,8 +556,10 @@ async def setup_db(app, loop): app.ctx.db = AsyncIOMotorClient(MONGO_URI).get_default_database() app.ctx.coll = app.ctx.db[MONGO_COLLECTION] app.ctx.coll.create_index([ - ("timestamp", pymongo.ASCENDING), - ("source", pymongo.ASCENDING)], unique=True) + ("@timestamp", pymongo.ASCENDING)]) + app.ctx.coll.create_index([ + ("source", pymongo.ASCENDING), + ("@timestamp", pymongo.ASCENDING)], unique=True) app.ctx.last_frame = None app.ctx.event_frame = asyncio.Event() app.ctx.event_id = None