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

Add push io #2

Merged
merged 3 commits into from
May 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions adafruit_pyportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,11 @@ def image_converter_url(image_url, width, height, color_depth=16):
width, height,
color_depth, image_url)

def io_push(self, feed, data):
def io_push(self, feed_key, data):
# pylint: disable=line-too-long
"""Push data to an adafruit.io feed

:param str feed: Name of feed to push data to.
:param str feed_key: Name of feed to push data to.
:param data: data to send to feed

"""
Expand All @@ -668,18 +668,32 @@ def io_push(self, feed, data):
aio_username = secrets['aio_username']
aio_key = secrets['aio_key']
except KeyError:
raise KeyError("\n\n")
raise KeyError("Adafruit IO secrets are kept in secrets.py, please add them there!\n\n")

wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None)
io_connect = RESTClient(aio_username, aio_key, wifi)
io_client = RESTClient(aio_username, aio_key, wifi)

try:
feed_id = io_connect.get_feed(feed)
except AdafruitIO_RequestError:
# If no feed exists, create one
feed_id = io_connect.create_new_feed(feed)

io_connect.send_data(feed_id['key'], data)
while True:
try:
feed_id = io_client.get_feed(feed_key)
except AdafruitIO_RequestError:
# If no feed exists, create one
feed_id = io_client.create_new_feed(feed_key)
except RuntimeError as exception:
print("An error occured, retrying! 1 -", exception)
continue
break

while True:
try:
io_client.send_data(feed_id['key'], data)
except RuntimeError as exception:
print("An error occured, retrying! 2 -", exception)
continue
except NameError as exception:
print(feed_id['key'], data, exception)
continue
break

def fetch(self, refresh_url=None):
"""Fetch data from the url we initialized with, perfom any parsing,
Expand Down