|
12 | 12 | All text above must be included in any redistribution. |
13 | 13 | """ |
14 | 14 |
|
| 15 | +from adafruit_portalbase import PortalBase |
| 16 | + |
15 | 17 | #pylint:disable=missing-super-argument |
16 | 18 |
|
17 | 19 | # Example: |
18 | 20 | # |
19 | 21 | # from aio_handler import AIOHandler |
20 | 22 | # import adafruit_logging as logging |
21 | 23 | # l = logging.getLogger('aio') |
22 | | -# l.addHandler(AIOHandler('test')) |
| 24 | +# # Pass in the device object based on portal_base (Funhouse, PyPortal, MagTag, etc) as the 2nd parameter |
| 25 | +# l.addHandler(AIOHandler('test'), portal_device) |
23 | 26 | # l.level = logging.ERROR |
24 | 27 | # l.error("test") |
25 | 28 |
|
26 | | -import board |
27 | | -import busio |
28 | | -from digitalio import DigitalInOut |
29 | | -import neopixel |
30 | 29 | from adafruit_logging import LoggingHandler |
31 | | -from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager |
32 | | -from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError |
33 | | - |
34 | | -try: |
35 | | - from secrets import secrets |
36 | | -except ImportError: |
37 | | - print("WiFi secrets are kept in secrets.py, please add them there!") |
38 | | - raise |
39 | | - |
40 | 30 |
|
41 | 31 | class AIOHandler(LoggingHandler): |
42 | 32 |
|
43 | | - def __init__(self, name): |
| 33 | + def __init__(self, name, portal_device): |
44 | 34 | """Create an instance.""" |
45 | | - # PyPortal ESP32 Setup |
46 | | - esp32_cs = DigitalInOut(board.ESP_CS) |
47 | | - esp32_ready = DigitalInOut(board.ESP_BUSY) |
48 | | - esp32_reset = DigitalInOut(board.ESP_RESET) |
49 | | - spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
50 | | - esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
51 | | - status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) |
52 | | - wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
53 | | - |
54 | | - # Set your Adafruit IO Username and Key in secrets.py |
55 | | - # (visit io.adafruit.com if you need to create an account, |
56 | | - # or if you need your Adafruit IO key.) |
57 | | - ADAFRUIT_IO_USER = secrets['adafruit_io_user'] |
58 | | - ADAFRUIT_IO_KEY = secrets['adafruit_io_key'] |
59 | | - |
60 | | - # Create an instance of the Adafruit IO REST client |
61 | | - self._io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) |
| 35 | + self._log_feed_name=f"{name}-logging" |
| 36 | + if not issubclass(type(portal_device), PortalBase): |
| 37 | + raise TypeError("portal_device must be a PortalBase or subclass of PortalBase") |
| 38 | + self._portal_device = portal_device |
62 | 39 |
|
63 | | - self._name = '{0}-logging'.format(name) |
64 | | - try: |
65 | | - # Get the logging feed from Adafruit IO |
66 | | - self._log_feed = self._io.get_feed(self._name) |
67 | | - except AdafruitIO_RequestError: |
68 | | - # If no logging feed exists, create one |
69 | | - self._log_feed = self._io.create_new_feed(self._name) |
70 | 40 |
|
71 | 41 | def emit(self, level, msg): |
72 | | - """Generate the message and write it to the UART. |
| 42 | + """Generate the message and write it to the AIO Feed. |
73 | 43 |
|
74 | 44 | :param level: The level at which to log |
75 | 45 | :param msg: The core message |
76 | 46 |
|
77 | 47 | """ |
78 | | - self._io.send_data(self._log_feed['key'], self.format(level, msg)) |
| 48 | + self._portal_device.push_to_io(self._log_feed_name, self.format(level, msg)) |
0 commit comments