from time import time
from uasyncio import sleep_ms, sleep
from machine import WDT
from rfid_utils import RFID, Buzzer, GLED, RLED, CardFilter, OUT, IN
from webserver import eprint, erfid
from config import settings, udb
async def granted(buz):
GLED.on()
await buz.beep()
GLED.off()
OUT.off()
await sleep(5)
OUT.on()
async def run():
await eprint("User code started")
rfid = RFID()
buz = Buzzer()
GLED.off()
RLED.off()
OUT.on()
c_filter = CardFilter(5)
wdt_enabled = settings.get('wdt')
str_format = settings.get('format')
if wdt_enabled:
wdt = WDT(timeout=10000)
while 1:
if wdt_enabled:
wdt.feed()
await sleep_ms(200)
c_filter.clean(time())
if IN.value() == 0:
await granted(buz)
uid = rfid.read(str_format)
if uid and uid not in c_filter:
c_filter.append(uid)
await erfid(uid)
value = udb.get(uid)
if value is not None:
await eprint(uid + ' granted')
await granted(buz)
else:
RLED.on()
await eprint(uid + ' denied')
await buz.beep(500)
RLED.off()