using current PortalBase much of the complexity of logging to AIO goes away

This commit is contained in:
slootsky 2021-06-21 22:15:36 -04:00
parent a66a382bbd
commit 0e86d196cc
2 changed files with 15 additions and 42 deletions

View file

@ -12,6 +12,8 @@ Licensed under the MIT license.
All text above must be included in any redistribution. All text above must be included in any redistribution.
""" """
from adafruit_portalbase import PortalBase
#pylint:disable=missing-super-argument #pylint:disable=missing-super-argument
# Example: # Example:
@ -19,60 +21,28 @@ All text above must be included in any redistribution.
# from aio_handler import AIOHandler # from aio_handler import AIOHandler
# import adafruit_logging as logging # import adafruit_logging as logging
# l = logging.getLogger('aio') # l = logging.getLogger('aio')
# l.addHandler(AIOHandler('test')) # # Pass in the device object based on portal_base (Funhouse, PyPortal, MagTag, etc) as the 2nd parameter
# l.addHandler(AIOHandler('test'), portal_device)
# l.level = logging.ERROR # l.level = logging.ERROR
# l.error("test") # l.error("test")
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_logging import LoggingHandler from adafruit_logging import LoggingHandler
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
class AIOHandler(LoggingHandler): class AIOHandler(LoggingHandler):
def __init__(self, name): def __init__(self, name, portal_device):
"""Create an instance.""" """Create an instance."""
# PyPortal ESP32 Setup self._log_feed_name=f"{name}-logging"
esp32_cs = DigitalInOut(board.ESP_CS) if not issubclass(type(portal_device), PortalBase):
esp32_ready = DigitalInOut(board.ESP_BUSY) raise TypeError("portal_device must be a PortalBase or subclass of PortalBase")
esp32_reset = DigitalInOut(board.ESP_RESET) self._portal_device = portal_device
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
ADAFRUIT_IO_USER = secrets['adafruit_io_user']
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']
# Create an instance of the Adafruit IO REST client
self._io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)
self._name = '{0}-logging'.format(name)
try:
# Get the logging feed from Adafruit IO
self._log_feed = self._io.get_feed(self._name)
except AdafruitIO_RequestError:
# If no logging feed exists, create one
self._log_feed = self._io.create_new_feed(self._name)
def emit(self, level, msg): def emit(self, level, msg):
"""Generate the message and write it to the UART. """Generate the message and write it to the AIO Feed.
:param level: The level at which to log :param level: The level at which to log
:param msg: The core message :param msg: The core message
""" """
self._io.send_data(self._log_feed['key'], self.format(level, msg)) self._portal_device.push_to_io(self._log_feed_name, self.format(level, msg))

View file

@ -1,10 +1,13 @@
import time import time
import random import random
from adafruit_pyportal import PyPortal
from aio_handler import AIOHandler from aio_handler import AIOHandler
import adafruit_logging as logging import adafruit_logging as logging
device=PyPortal()
l = logging.getLogger('aio') l = logging.getLogger('aio')
l.addHandler(AIOHandler('test')) l.addHandler(AIOHandler('test'), device)
def go(): def go():
while True: while True: