-
Notifications
You must be signed in to change notification settings - Fork 0
Quick example script for sending telemetry #6
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
Open
wlach
wants to merge
3
commits into
main
Choose a base branch
from
send-telemetry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,75 @@ | ||
import dataclasses | ||
import os | ||
import sys | ||
from typing import List, Union | ||
|
||
import requests | ||
|
||
|
||
VOLTUS_API_URL = os.getenv("VOLTUS_API_URL", "https://api.voltus.co/2022-04-15") | ||
VOLTUS_API_KEY = os.getenv("VOLTUS_API_KEY") | ||
|
||
s = requests.Session() | ||
s.headers.update({"X-Voltus-API-Key": VOLTUS_API_KEY, "Accept": "application/json"}) | ||
|
||
@dataclasses.dataclass | ||
class TelemetryReading: | ||
interval_seconds: int | ||
meter_id: str | ||
timestamp: str | ||
units: str | ||
value: float | ||
|
||
@dataclasses.dataclass | ||
class ControllableLoadReading: | ||
interval_seconds: int | ||
site_id: str | ||
timestamp: str | ||
units: str | ||
value: float | ||
|
||
def _send_telemetry(url: str, datatype: str, readings: Union[List[TelemetryReading], List[ControllableLoadReading]]): | ||
try: | ||
r = s.post(url, json={ | ||
datatype: [dataclasses.asdict(reading) for reading in readings] | ||
}) | ||
if r.status_code != 200: | ||
sys.stderr.write(f"Failed to send telemetry: {r.text}\n") | ||
sys.exit(1) | ||
except requests.exceptions.RequestException as e: | ||
sys.stderr.write(f"Failed to send telemetry: {e}\n") | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 7: | ||
sys.stderr.write("Usage: python send_telemetry.py <telemetry_type> <interval_seconds> <meter_or_site_id> <timestamp> <units> <value>") | ||
sys.exit(1) | ||
|
||
if VOLTUS_API_KEY is None: | ||
sys.stderr.write("VOLTUS_API_KEY environment variable is unset") | ||
sys.exit(1) | ||
|
||
telemetry_type = sys.argv[1] | ||
reading_class = None | ||
if telemetry_type == "telemetry": | ||
url = f"{VOLTUS_API_URL}/telemetry" | ||
reading_class = TelemetryReading | ||
elif telemetry_type == "controllable_load": | ||
url = f"{VOLTUS_API_URL}/telemetry/controllable-load" | ||
reading_class = ControllableLoadReading | ||
else: | ||
sys.stderr.write("Invalid telemetry type. Must be 'telemetry' or 'controllable_load'") | ||
wlach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sys.exit(1) | ||
|
||
# Create a telemetry reading object | ||
reading = reading_class( | ||
int(sys.argv[2]), # interval seconds | ||
sys.argv[3], # site or meter id | ||
timestamp=sys.argv[4], | ||
units=sys.argv[5], | ||
value=float(sys.argv[6]) | ||
) | ||
|
||
# Attempt to send it | ||
_send_telemetry(url, telemetry_type, [reading]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.