Simplify adafruit io error logging

This commit is contained in:
Mike Cifelli 2023-02-08 08:30:26 -05:00
parent 47355d820d
commit 97e63124d4
1 changed files with 11 additions and 14 deletions

View File

@ -19,11 +19,7 @@ class AdafruitIO:
self.url = f'https://io.adafruit.com/api/v2/{username}/groups/{group}/data'
def upload(self, reading):
result = urequests.post(
self.url,
json=self.createPayload(reading),
headers=self.headers
)
result = self.uploadReading(reading)
try:
if result.status_code != 200:
@ -34,6 +30,13 @@ class AdafruitIO:
finally:
result.close()
def uploadReading(self, reading):
return urequests.post(
self.url,
json=self.createPayload(reading),
headers=self.headers
)
def createPayload(self, reading):
payload = {
'created_at': reading['timestamp'],
@ -49,17 +52,11 @@ class AdafruitIO:
return payload
def logError(self, result):
reason = 'unknown'
status = result.status_code
reason = result.reason.decode("utf-8")
error_message = self.getErrorMessage(result)
if result.status_code == 429:
reason = 'rate limited'
elif result.status_code == 422 and error_message.find('data created_at may not be in the future') == 0:
reason = 'future reading'
else:
reason = f'{result.status_code} - {result.reason.decode("utf-8")}'
logging.debug(f'upload issue: {reason} - "{error_message}"')
logging.debug(f'Adafruit IO upload issue: {status} {reason} - "{error_message}"')
def getErrorMessage(self, result):
try: