|
1 | 1 | # avenieca-python (WIP!) |
2 | | -Python SDK for AveniECA |
| 2 | +Python SDK for publishing state signals to the AveniECA suite. |
3 | 3 |
|
4 | | -## run |
5 | | -```bash |
6 | | -python consumer.py localhost:9092 testtopic |
| 4 | +## Usage |
| 5 | + |
| 6 | +### Stream continuously to a topic |
| 7 | +```python |
| 8 | +import os |
| 9 | +import numpy as np |
| 10 | +from avenieca.utils import Config |
| 11 | +from avenieca.utils import Signal |
| 12 | +from avenieca.producers import Stream |
| 13 | + |
| 14 | +# Define a handler that returns a signal dict like |
| 15 | +# the sample in avenieca.utils.signal |
| 16 | + |
| 17 | +def handler(): |
| 18 | + Signal["valence"] = 10 |
| 19 | + Signal["state"] = np.array([0.2, 0.3, 0.8]) |
| 20 | + return Signal |
| 21 | + |
| 22 | +# Initialize Kafka configuration for the Stream |
| 23 | +Config["bootstrap_servers"] = os.environ["KAFKA_URL"] |
| 24 | +Config["topic"] = "left_wheel" #digital twin subscriber-topic |
| 25 | + |
| 26 | +#Initialize the Stream object with a sync_rate |
| 27 | +# (the rate at which to publish signals). |
| 28 | +stream = Stream(config=Config, sync_rate=1) |
| 29 | +stream.publish(handler) |
7 | 30 | ``` |
| 31 | + |
| 32 | +### Publish one signal as an event |
| 33 | +```python |
| 34 | +import os |
| 35 | +import numpy as np |
| 36 | +from avenieca.utils import Config |
| 37 | +from avenieca.utils import Signal |
| 38 | +from avenieca.producers import Event |
| 39 | + |
| 40 | +# Initialize Kafka configuration for the Event |
| 41 | +Config["bootstrap_servers"] = os.environ["KAFKA_URL"] |
| 42 | +Config["topic"] = "left_wheel" #digital twin subscriber-topic |
| 43 | + |
| 44 | +# Define the signal |
| 45 | +Signal["valence"] = 9 |
| 46 | +Signal["state"] = np.array([0.2, 0.3, 0.8]) |
| 47 | + |
| 48 | +event = Event(config=Config) |
| 49 | +event.publish(Signal) |
| 50 | +``` |
| 51 | + |
| 52 | +### Consume from kafka topic |
| 53 | +```python |
| 54 | +import os |
| 55 | +import numpy as np |
| 56 | +from avenieca.utils import Config |
| 57 | +from avenieca.utils.signal import get_state_as_list, get_state_as_array |
| 58 | +from avenieca.consumer import Consumer |
| 59 | + |
| 60 | +# Initialize Kafka configuration for the Event |
| 61 | +Config["bootstrap_servers"] = os.environ["KAFKA_URL"] |
| 62 | +Config["topic"] = "left_wheel" #digital twin subscriber-topic |
| 63 | + |
| 64 | +# Define a handler to process incoming messages |
| 65 | +def handler(data): |
| 66 | + valence = data["valence"] |
| 67 | + state = data["state"] |
| 68 | + assert valence == 10 |
| 69 | + assert state == "[0.2, 0.3, 0.8]" |
| 70 | + |
| 71 | +client = Consumer(config=Config) |
| 72 | +client.consume(handler, True) # pass in handler |
| 73 | + |
| 74 | +# You can use util functions in your handler to |
| 75 | +# convert the state signal from byte string to |
| 76 | +# np.ndarray or python list |
| 77 | +def handler(data): |
| 78 | + assert data["valence"] == 10 |
| 79 | + assert data["state"] == "[0.2, 0.3, 0.8]" |
| 80 | + get_state_as_list(data) |
| 81 | + assert data["state"] == [0.2, 0.3, 0.8] |
| 82 | + |
| 83 | +def handler(data): |
| 84 | + assert data["valence"] == 10 |
| 85 | + assert data["state"] == "[0.2, 0.3, 0.8]" |
| 86 | + get_state_as_array(data) |
| 87 | + assert True, np.array_equal(data["state"], np.array([0.2, 0.3, 0.8])) |
| 88 | +``` |
| 89 | + |
| 90 | +## Tests |
8 | 91 | ```bash |
9 | | -python producer.py localhost:9092 testtwin1 |
| 92 | +python -m pytest test/ |
10 | 93 | ``` |
11 | 94 |
|
0 commit comments