Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occasionally calling get_strftime() results in an error with self._wifi.requests being NoneType #91

Open
richteel opened this issue May 18, 2023 · 0 comments

Comments

@richteel
Copy link

richteel commented May 18, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant