adafruit / adafruit/Adafruit_CircuitPython_PortalBase
Occasionally calling get_strftime() results in an error with self._wifi.requests being NoneType
- Dominant language
- Python
- Stars
- 17
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
Sometimes when getting the time, the following error occurs:
Hardware is PyPortal Adafruit Product ID: 4116
------ START: Error Message ------
File "adafruit_portalbase/network.py", line 208, in get_strftime
AttributeError: 'NoneType' object has no attribute 'get'
------ END: Error Message ------
Line 208: response = self._wifi.requests.get(api_url, timeout=10)
Not certain why this happens but it is not every time. Perhaps one in 5 connection attempts.
My workaround has been to catch all exceptions when attempting to get the time.
```
def getTime(lastRefreshedTime, secondsToUpdate):
if pyportal is None:
return None
if (not lastRefreshedTime) or (time.monotonic() - lastRefreshedTime) > secondsToUpdate:
eventWindow.statusDateTime.text = "Updating Time"
try:
print("INFO: Getting time from internet!")
if (not lastRefreshedTime):
eventWindow.changeBackground(
eventWindow.IMG_FILE_CONNECTING_BACKGROUND, False)
start = time.time()
pyportal.get_local_time() # pyportal.get_local_time(false) non-blocking?
print(f"INFO: Getting time took {time.time() - start} seconds.")
lastRefreshedTime = time.monotonic()
print("INFO: Success the time has been set.")
except RuntimeError as e:
print(f"WARN: Some error occured, retrying!\r\n{e}")
if (not lastRefreshedTime):
eventWindow.changeBackground(
eventWindow.IMG_FILE_CONNECT_FAILED_BACKGROUND, False)
# Use try catch here as there is an error in adafruit_portalbase
# File "adafruit_portalbase/network.py", line 208, in get_strftime
# AttributeError: 'NoneType' object has no attribute 'get'
except Exception as e:
print(f"ERROR: Failed to getTime\r\n{e}")
eventWindow.changeBackground(
eventWindow.IMG_FILE_CONNECT_FAILED_BACKGROUND, False)
removePastEvents()
return lastRefreshedTime
```
Full code with the workaround is available at https://github.com/richteel/PyPortal_Events. Direct link to the code.py file is https://github.com/richteel/PyPortal_Events/blob/main/PyPortal/code.py.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in adafruit_portalbase/network.py at get_strftime, especially line 208, and trace where self._wifi.requests is initialized and can become None. Reproduce the intermittent PyPortal time request failure if possible, then verify the completed change no longer raises an AttributeError when getting the time.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100