Skip to content

Commit

Permalink
feat: add delay handling to serial.py, part of #43
Browse files Browse the repository at this point in the history
  • Loading branch information
oplik0 committed Aug 3, 2020
1 parent f0b7149 commit c41dbf4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions cherrydoor/interface/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime
from math import ceil
from time import sleep
import sys

from motor import motor_asyncio as motor
import aioserial
Expand All @@ -19,9 +20,10 @@ def __init__(self):
self.encoding = config.get("interface", {}).get("encoding", "utf-8")
self.manual_auth = False
self.is_break = False
self.command_funcions = {"CARD": self.card}
self.command_funcions = {"CARD": self.card, "EXIT": sys.exit}
self.terminal_change_stream = None
self.break_times = []
self.delay = 0

def start(self):
try:
Expand Down Expand Up @@ -79,6 +81,8 @@ async def card(self, block0):
"manufacturer-code", "18"
) or await self.authenticate(block0[:10])
auth_mode = "Manufacturer code"
if self.delay:
await asyncio.sleep(self.delay)
await self.writeline(f"AUTH {1 if result else 0}")
self.loop.create_task(self.log_entry(block0, auth_mode, result))
print(f"Authentication {'successful' if result else 'unsuccessful'}")
Expand Down Expand Up @@ -110,17 +114,27 @@ async def settings_listener(self):
pipeline=[
{
"$match": {
"fullDocument.setting": "break_times",
"fullDocument.setting": {"$in": ["break_times", "delay"]},
"operationType": {"$in": ["insert", "update", "replace"]},
}
},
{"$project": {"value": "$fullDocument.value"}},
{
"$project": {
"value": "$fullDocument.value",
"setting": "$fullDocument.setting",
}
},
],
full_document="updateLookup",
) as self.settings_change_stream:
async for change in self.settings_change_stream:
self.break_times = change.get("value", [])
print(f"new break times: {self.break_times}")
setting = change.get("setting", "")
if setting == "break_times":
self.break_times = change.get("value", [])
print(f"new break times: {self.break_times}")
elif setting == "delay":
self.delay = change.get("value", 0)
print(f"new response delay: {self.delay}s")

async def breaks(self):
while True:
Expand Down

0 comments on commit c41dbf4

Please sign in to comment.