|
| 1 | +OSH Connect Tutorial |
| 2 | +==================== |
| 3 | +OSH Connect for Python is a straightforward library for interacting with OpenSensorHub using OGC API Connected Systems. |
| 4 | +This tutorial will help guide you through a few simple examples to get you started with OSH Connect. |
| 5 | + |
| 6 | +OSH Connect Installation |
| 7 | +-------------------------- |
| 8 | +OSH Connect can be installed using `pip`. To install the latest version of OSH Connect, run the following command: |
| 9 | + |
| 10 | +.. code-block:: bash |
| 11 | +
|
| 12 | + pip install git+https://github.com/Botts-Innovative-Research/OSHConnect-Python.git |
| 13 | +
|
| 14 | +Or, if you prefer `poetry`: |
| 15 | + |
| 16 | +.. code-block:: bash |
| 17 | +
|
| 18 | + poetry add git+https://github.com/Botts-Innovative-Research/OSHConnect-Python.git |
| 19 | +
|
| 20 | +Creating an instance of OSHConnect |
| 21 | +--------------------------------------- |
| 22 | +The intended method of interacting with OpenSensorHub is through the `OSHConnect` class. |
| 23 | +To this you must first create an instance of `OSHConnect`: |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + from oshconnect.oshconnectapi import OSHConnect, TemporalModes |
| 28 | +
|
| 29 | + connect_app = OSHConnect(name='OSHConnect', playback_mode=TemporalModes.REAL_TIME) |
| 30 | +
|
| 31 | +.. tip:: |
| 32 | + |
| 33 | + The `name` parameter is optional, but can be useful for debugging purposes. |
| 34 | + The playback mode determines how the data is retrieved from OpenSensorHub. |
| 35 | + |
| 36 | +The next step is to add a `Node` to the `OSHConnect` instance. A `Node` is a representation of a server that you want to connect to. |
| 37 | +The OSHConnect instance can support multiple Nodes at once. |
| 38 | + |
| 39 | +Adding a Node to an OSHConnect instance |
| 40 | +----------------------------------------- |
| 41 | +.. code-block:: python |
| 42 | +
|
| 43 | + from oshconnect.oshconnectapi import OSHConnect, TemporalModes |
| 44 | + from oshconnect.osh_connect_datamodels import Node |
| 45 | +
|
| 46 | + connect_app = OSHConnect(name='OSHConnect', playback_mode=TemporalModes.REAL_TIME) |
| 47 | + node = Node(protocol='http', address="localhost", port=8585, username="test", password="test") |
| 48 | + connect_app.add_node(node) |
| 49 | +
|
| 50 | +System Discovery |
| 51 | +----------------------------------------- |
| 52 | +Once you have added a Node to the OSHConnect instance, you can discover the systems that are available on that Node. |
| 53 | +This is done by calling the `discover_systems()` method on the OSHConnect instance. |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + connect_app.discover_systems() |
| 58 | +
|
| 59 | +Datastream Discovery |
| 60 | +----------------------------------------- |
| 61 | +Once you have discovered the systems that are available on a Node, you can discover the datastreams that are available to those |
| 62 | +systems. This is done by calling the `discover_datastreams` method on the OSHConnect instance. |
| 63 | + |
| 64 | +.. code-block:: python |
| 65 | +
|
| 66 | + connect_app.discover_datastreams() |
| 67 | +
|
| 68 | +Playing back data |
| 69 | +----------------------------------------- |
| 70 | +Once you have discovered the datastreams that are available on a Node, you can play back the data from those datastreams. |
| 71 | +This is done by calling the `playback_streams` method on the OSHConnect instance. |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | +
|
| 75 | + connect_app.playback_streams() |
| 76 | +
|
| 77 | +Accessing data |
| 78 | +----------------------------------------- |
| 79 | +To access the data retrieved from the datastreams, you need to access the messages available to the OSHConnect instance. |
| 80 | +Calling the `get_messages` method on the OSHConnect instance will return a list of `MessageWrapper` objects that contain individual |
| 81 | +observations. |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + messages = connect_app.get_messages() |
| 86 | +
|
| 87 | + for message in messages: |
| 88 | + print(message) |
| 89 | +
|
| 90 | + # or, to access the individual observations |
| 91 | + for message in messages: |
| 92 | + for observation in message.observations: |
| 93 | + do_something_with(observation) |
0 commit comments