Skip to content

Commit

Permalink
Reorganize and clean up the examples
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Orru <simone.orru@secomind.com>
  • Loading branch information
sorru94 committed Aug 7, 2024
1 parent 26daffc commit 2148f19
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 212 deletions.
296 changes: 152 additions & 144 deletions examples/datastreams/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,174 +21,182 @@
Example showing how to send/receive individual datastreams.
"""
import os
import argparse
import tempfile
import time
from datetime import datetime, timezone
from pathlib import Path
from time import sleep

from termcolor import cprint

from astarte.device import DeviceMqtt

_ROOT_DIR = Path(__file__).parent.absolute()
_INTERFACES_DIR = _ROOT_DIR.joinpath("interfaces")
_PERSISTENCY_DIR = _ROOT_DIR.joinpath(_ROOT_DIR, "build")
if not Path.is_dir(_PERSISTENCY_DIR):
os.makedirs(_PERSISTENCY_DIR)
_DEVICE_ID = "DEVICE ID HERE"
_REALM = "REALM HERE"
_CREDENTIAL_SECRET = "CREDENTIAL SECRET HERE"
_PAIRING_URL = "PAIRING URL HERE"
_IGNORE_SSL_ERRORS = True
_INTERFACES_DIR = Path(__file__).parent.joinpath("interfaces").absolute()
_DEVICE_ID = "<DEVICE_ID>"
_REALM = "<REALM>"
_CREDENTIAL_SECRET = "<CREDENTIAL_SECRET>"
_PAIRING_URL = "<PAIRING_URL>"


def on_connected_cbk(_):
"""
Callback for a connection event.
"""
cprint("Device connected.", color="green", flush=True)
print("Device connected.")


def on_data_received_cbk(device: DeviceMqtt, interface_name: str, path: str, payload: dict):
def on_data_received_cbk(_: DeviceMqtt, interface_name: str, path: str, payload: dict):
"""
Callback for a data reception event.
"""
cprint(
f"Received message for interface: {interface_name} and path: {path}.",
color="cyan",
flush=True,
)
cprint(f" Payload: {payload}", color="cyan", flush=True)
print(f"Received message for interface: {interface_name} and path: {path}.")
print(f" Payload: {payload}")


def on_disconnected_cbk(_, reason: int):
"""
Callback for a disconnection event.
"""
cprint(f"Device disconnected because: {reason}.", color="red", flush=True)
print("Device disconnected" + (f" because: {reason}." if reason else "."))


# If called as a script
if __name__ == "__main__":
# Instance the device
device = DeviceMqtt(
device_id=_DEVICE_ID,
realm=_REALM,
credentials_secret=_CREDENTIAL_SECRET,
pairing_base_url=_PAIRING_URL,
persistency_dir=_PERSISTENCY_DIR,
ignore_ssl_errors=_IGNORE_SSL_ERRORS,
)
# Load all the interfaces
device.add_interfaces_from_dir(_INTERFACES_DIR)
# Set all the callback functions
device.set_events_callbacks(
on_connected=on_connected_cbk,
on_data_received=on_data_received_cbk,
on_disconnected=on_disconnected_cbk,
)
# Connect the device
device.connect()

sleep(1)

# Send the binary blob endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/binaryblob_endpoint",
b"binblob",
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/binaryblobarray_endpoint",
[b"bin", b"blob"],
datetime.now(tz=timezone.utc),
)

# Send the boolean endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/boolean_endpoint",
False,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/booleanarray_endpoint",
[False, True],
datetime.now(tz=timezone.utc),
)

# Send the datetime endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/datetime_endpoint",
datetime.now(tz=timezone.utc),
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/datetimearray_endpoint",
[datetime.now(tz=timezone.utc)],
datetime.now(tz=timezone.utc),
)

# Send the double endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/double_endpoint",
21.3,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/doublearray_endpoint",
[1123.0, 12.232],
datetime.now(tz=timezone.utc),
)

# Send the integer endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/integer_endpoint",
11,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/integerarray_endpoint",
[452, 0],
datetime.now(tz=timezone.utc),
)

# Send the long integer endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/longinteger_endpoint",
2**34,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/longintegerarray_endpoint",
[2**34, 2**35 + 11],
datetime.now(tz=timezone.utc),
)

# Send the string endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/string_endpoint",
"Hello world!",
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/stringarray_endpoint",
["Hello,", " world!"],
datetime.now(tz=timezone.utc),
)

while True:
sleep(5)
# Accept an argument to specify a set time duration for the example
parser = argparse.ArgumentParser(
description="Datastream sample for the Astarte device SDK Python"
)
parser.add_argument(
"-d",
"--duration",
type=int,
default=30,
help="Approximated duration in seconds for the example (default: 30)",
)
args = parser.parse_args()

# Creating a temporary directory
with tempfile.TemporaryDirectory(prefix="python_sdk_examples_") as temp_dir:

# Instantiate the device
device = DeviceMqtt(
device_id=_DEVICE_ID,
realm=_REALM,
credentials_secret=_CREDENTIAL_SECRET,
pairing_base_url=_PAIRING_URL,
persistency_dir=temp_dir,
)
# Load all the interfaces
device.add_interfaces_from_dir(_INTERFACES_DIR)
# Set all the callback functions
device.set_events_callbacks(
on_connected=on_connected_cbk,
on_data_received=on_data_received_cbk,
on_disconnected=on_disconnected_cbk,
)
# Connect the device
device.connect()
while not device.is_connected():
pass

# Send the binary blob endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/binaryblob_endpoint",
b"binblob",
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/binaryblobarray_endpoint",
[b"bin", b"blob"],
datetime.now(tz=timezone.utc),
)

# Send the boolean endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/boolean_endpoint",
False,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/booleanarray_endpoint",
[False, True],
datetime.now(tz=timezone.utc),
)

# Send the datetime endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/datetime_endpoint",
datetime.now(tz=timezone.utc),
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/datetimearray_endpoint",
[datetime.now(tz=timezone.utc)],
datetime.now(tz=timezone.utc),
)

# Send the double endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/double_endpoint",
21.3,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/doublearray_endpoint",
[1123.0, 12.232],
datetime.now(tz=timezone.utc),
)

# Send the integer endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/integer_endpoint",
11,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/integerarray_endpoint",
[452, 0],
datetime.now(tz=timezone.utc),
)

# Send the long integer endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/longinteger_endpoint",
2**34,
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/longintegerarray_endpoint",
[2**34, 2**35 + 11],
datetime.now(tz=timezone.utc),
)

# Send the string endpoints
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/string_endpoint",
"Hello world!",
datetime.now(tz=timezone.utc),
)
device.send(
"org.astarte-platform.python.examples.DeviceDatastream",
"/stringarray_endpoint",
["Hello,", " world!"],
datetime.now(tz=timezone.utc),
)

# Sleep for the example duration
time.sleep(args.duration)

device.disconnect()
Loading

0 comments on commit 2148f19

Please sign in to comment.