implemented #46

This commit is contained in:
Manuel Kamper 2023-04-09 15:50:05 +00:00
parent 8f126543a8
commit fa02b5cb38
1 changed files with 19 additions and 4 deletions

23
main.py
View File

@ -33,6 +33,15 @@ busline = ADC(28)
triggerline = Pin(15, Pin.OUT)
writerActive = False
# Checks if string is integer
def IsInt(possibleint):
try:
int(possibleint)
except:
return False
else:
return True
# Reboots the Pico W (f.e. in case of an error)
def Reboot():
Logger.LogMessage("Performing Reboot")
@ -240,7 +249,7 @@ async def APIHandling(reader, writer):
else:
req = request.split('/')
stateis = ""
if (len(req) == 3 or len(req) == 4):
if (len(req) == 3 or len(req) == 4 or len(req) == 5):
if (req[1] == secrets['api']):
if (req[2] == "triggerdoor"):
Logger.LogMessage("Triggering Door")
@ -275,12 +284,18 @@ async def APIHandling(reader, writer):
stateis = "Ringing front doorbell"
await TCSBusWriter(configs['frontdoor_ringing_message'])
elif (req[2] == "logs"):
stateis = Logger.LastLogs(50)
if (len(req) >= 4 and req[3] != "json"):
if (IsInt(req[3])):
stateis = Logger.LastLogs(int(req[3]))
else:
stateis = "<b>Error:</b> Parameter for log length not an integer!"
else:
stateis = Logger.LastLogs(50)
else:
stateis = "<b>Error:</b> Unknown command!"
else:
stateis = "<b>Error:</b> API key is invalid!"
if (len(req) == 4 and req[3] == "json"):
if ((len(req) == 4 and req[3] == "json") or (len(req) == 5 and req[4] == "json")):
response = json % stateis
writer.write('HTTP/1.0 200 OK\r\nContent-type: text/json\r\n\r\n')
else:
@ -452,4 +467,4 @@ except KeyboardInterrupt:
Oled.fill(0x0000)
Oled.show()
finally:
asyncio.new_event_loop()
asyncio.new_event_loop()