-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from pimoroni/feature/influxdb-destination
added support for influxdb
- Loading branch information
Showing
5 changed files
with
96 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from enviro.helpers import get_config | ||
import urequests, time | ||
|
||
def url_encode(t): | ||
result = "" | ||
for c in t: | ||
# no encoding needed for character | ||
if c.isalpha() or c.isdigit() or c in ["-", "_", "."]: | ||
result += c | ||
elif c == " ": | ||
result += "+" | ||
else: | ||
result += f"%{ord(c):02X}" | ||
return result | ||
|
||
def upload_reading(reading): | ||
bucket = get_config("influxdb_bucket") | ||
|
||
payload = "" | ||
for key, value in reading["readings"].items(): | ||
if payload != "": | ||
payload += "\n" | ||
timestamp = reading["timestamp"] | ||
|
||
year = int(timestamp[0:4]) | ||
month = int(timestamp[5:7]) | ||
day = int(timestamp[8:10]) | ||
hour = int(timestamp[11:13]) | ||
minute = int(timestamp[14:16]) | ||
second = int(timestamp[17:19]) | ||
timestamp = time.mktime((year, month, day, hour, minute, second, 0, 0)) | ||
|
||
nickname = reading["nickname"] | ||
payload += f"{key},device={nickname} value={value}" | ||
|
||
influxdb_token = get_config("influxdb_token") | ||
headers = { | ||
"Authorization": f"Token {influxdb_token}" | ||
} | ||
|
||
url = get_config("influxdb_url") | ||
org = get_config("influxdb_org") | ||
url += f"/api/v2/write?precision=s&org={url_encode(org)}&bucket={url_encode(bucket)}" | ||
|
||
try: | ||
# post reading data to http endpoint | ||
result = urequests.post(url, headers=headers, data=payload) | ||
result.close() | ||
return result.status_code == 204 # why 204? we'll never know... | ||
except: | ||
pass | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters