Skip to content

Commit

Permalink
Covered everything with tests, and cleaned up all the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nanavuletic committed May 29, 2023
1 parent 4f73013 commit 3ad11fb
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 20 deletions.
35 changes: 28 additions & 7 deletions README.md
Expand Up @@ -16,6 +16,7 @@


```

[![Tests and Coverage](https://github.com/Wolkabout/WolkConnect-Python/actions/workflows/tests-and-coverage.yml/badge.svg?branch=development)](https://github.com/Wolkabout/WolkConnect-Python/actions/workflows/tests-and-coverage.yml) [![PyPI version](https://badge.fury.io/py/wolk-connect.svg)](https://badge.fury.io/py/wolk-connect) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/wolk-connect) ![GitHub](https://img.shields.io/github/license/wolkabout/WolkConnect-Python) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black) [![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/) [![Documentation Status](https://readthedocs.org/projects/wolkconnect-python/badge/?version=latest)](https://wolkconnect-python.readthedocs.io/en/latest/?badge=latest)
----
WolkAbout Python Connector library for connecting devices to WolkAbout IoT platform instance.
Expand All @@ -29,20 +30,23 @@ WolkAbout Python Connector library for connecting devices to WolkAbout IoT platf
There are two ways to install this package

### Installing with pip

```console
python3 -m pip install wolk-connect
```

### Installing from source

Clone this repository from the command line using:

```console
git clone https://github.com/Wolkabout/WolkConnect-Python.git
```

Install dependencies by invoking `python3 -m pip install -r requirements.txt`

Install the package by running:

```console
python3 setup.py install
```
Expand All @@ -51,8 +55,10 @@ python3 setup.py install

### Establishing connection with WolkAbout IoT platform

Create a device on WolkAbout IoT Platform by using the *Simple example* device type that is available on the platform. ``Note that device type can be created by importing `simple_example.json` file as new Device Type.``
This device type fits [main.py](https://github.com/Wolkabout/WolkConnect-Python/blob/master/examples/simple/main.py) and demonstrates the periodic sending of a temperature feed reading.
Create a device on WolkAbout IoT Platform by using the *Simple example* device type that is available on the
platform. ``Note that device type can be created by importing `simple_example.json` file as new Device Type.``
This device type fits [main.py](https://github.com/Wolkabout/WolkConnect-Python/blob/master/examples/simple/main.py) and
demonstrates the periodic sending of a temperature feed reading.

```python
import wolk
Expand All @@ -73,10 +79,10 @@ wolk_device.connect()
### Adding feed values

```python
wolk_device.add_feed_value_sealed(("T", 26.93))
wolk_device.add_feed_value(("T", 26.93))

# or multiple feed value readings
wolk_device.add_feed_value_sealed([("T", 27.11), ("H", 54.34), ("P", 1002.3)])
wolk_device.add_feed_value([("T", 27.11), ("H", 54.34), ("P", 1002.3)])
```

Optionally pass a `timestamp` as `round(time.time()) * 1000`.
Expand All @@ -87,19 +93,33 @@ If `timestamp` is not provided, the library will assign a timestamp before placi

```python
# Add a signel feed reading to the message queue with the timestamp
wolk_device.add_feed_value_sealed(("T", 12.34), 1658315834000)
wolk_device.add_feed_value(("T", 12.34), 1658315834000)

# Add a multi feed reading to the message queue with the timestamp
wolk_device.add_feed_value_sealed([("T", 12.34), ("H", 56.78), ("P", 1022.00)], 1658315834000)
wolk_device.add_feed_value([("T", 12.34), ("H", 56.78), ("P", 1022.00)], 1658315834000)
```

### Data publish strategy

Stored feed values are pushed to WolkAbout IoT platform on demand by calling:

```python
wolk_device.publish()
```

### Adding feed values 'separated'

When adding feed values, the values themselves are persisted, which means when publishing all values will be placed
in a single message and published as a single message.

If you would like to ensure different behavior, where you can add feed values that will be sent as a separate message
from any other feed values, use the alternative method:

```python
# Method arguments are exactly the same as for the `add_feed_value`
wolk_device.add_feed_value_separated([("T", 12.34), ("H", 56.78), ("P", 1022.00)], 1658315834000)
```

### Disconnecting from the platform

```python
Expand All @@ -108,4 +128,5 @@ wolk_device.disconnect()

## Additional functionality

WolkConnect-Python library has integrated additional features which can perform full WolkAbout IoT platform potential. Explore the [examples](https://github.com/Wolkabout/WolkConnect-Python/tree/master/examples/) for more information.
WolkConnect-Python library has integrated additional features which can perform full WolkAbout IoT platform potential.
Explore the [examples](https://github.com/Wolkabout/WolkConnect-Python/tree/master/examples/) for more information.
21 changes: 17 additions & 4 deletions examples/full_feature_set/README.md
Expand Up @@ -101,10 +101,10 @@ wolk_device = (
### Adding feed values

```python
wolk_device.add_feed_value_sealed(("T", 26.93))
wolk_device.add_feed_value(("T", 26.93))

# or multiple feed value readings
wolk_device.add_feed_value_sealed([("T", 27.11), ("H", 54.34), ("P", 1002.3)])
wolk_device.add_feed_value([("T", 27.11), ("H", 54.34), ("P", 1002.3)])
```

Optionally pass a `timestamp` as `round(time.time()) * 1000`.
Expand All @@ -115,10 +115,10 @@ If `timestamp` is not provided, the library will assign a timestamp before placi

```python
# Add a signel feed reading to the message queue with the timestamp
wolk_device.add_feed_value_sealed(("T", 12.34), 1658315834000)
wolk_device.add_feed_value(("T", 12.34), 1658315834000)

# Add a multi feed reading to the message queue with the timestamp
wolk_device.add_feed_value_sealed([("T", 12.34), ("H", 56.78), ("P", 1022.00)], 1658315834000)
wolk_device.add_feed_value([("T", 12.34), ("H", 56.78), ("P", 1022.00)], 1658315834000)
```

### Data publish strategy
Expand All @@ -128,6 +128,19 @@ Stored feed values are pushed to WolkAbout IoT platform on demand by calling:
wolk_device.publish()
```

### Adding feed values 'separated'

When adding feed values, the values themselves are persisted, which means when publishing all values will be placed
in a single message and published as a single message.

If you would like to ensure different behavior, where you can add feed values that will be sent as a separate message
from any other feed values, use the alternative method:

```python
# Method arguments are exactly the same as for the `add_feed_value`
wolk_device.add_feed_value_separated([("T", 12.34), ("H", 56.78), ("P", 1022.00)], 1658315834000)
```

### Disconnecting from the platform

```python
Expand Down
7 changes: 4 additions & 3 deletions examples/full_feature_set/main.py
Expand Up @@ -35,6 +35,7 @@
print(f"Failed to import WolkConnect: '{e}'")
raise e


# NOTE: Enable debug logging by uncommenting the following line
# Optionally, as a second argument pass a file name
# wolk.logging_config("debug")
Expand Down Expand Up @@ -177,7 +178,7 @@ def incoming_feed_value_handler(
while True:
try:
# Add feed values to outbound message queue
wolk_device.add_feed_value_sealed(
wolk_device.add_feed_value(
[
(switch_feed.reference, switch_feed.value),
(heart_beat.reference, heart_beat.value),
Expand All @@ -187,12 +188,12 @@ def incoming_feed_value_handler(
# Generate a random value
temperature = random.randint(-20, 80)
# Add a feed reading to the message queue
wolk_device.add_feed_value_sealed(("T", temperature))
wolk_device.add_feed_value(("T", temperature))

# Generate random value for the newly registered feed
new_feed = random.randint(0, 100)
# Add feed value reading of the new feed to message queue
wolk_device.add_feed_value_sealed(("NF", new_feed))
wolk_device.add_feed_value(("NF", new_feed))

# Publish all queued messages
wolk_device.publish()
Expand Down
2 changes: 1 addition & 1 deletion examples/pull/main.py
Expand Up @@ -116,7 +116,7 @@ def incoming_feed_value_handler(
time.sleep(heart_beat.value / 10)

# Add feed values to outbound message queue
wolk_device.add_feed_value_sealed(
wolk_device.add_feed_value(
[
(switch_feed.reference, switch_feed.value),
(heart_beat.reference, heart_beat.value),
Expand Down
2 changes: 1 addition & 1 deletion examples/register_feed_and_attribute/main.py
Expand Up @@ -75,7 +75,7 @@ def main() -> None:
# Generate random value for the newly registered feed
new_feed = random.randint(0, 100)
# Add feed value reading of the new feed to message queue
wolk_device.add_feed_value_sealed(("NF", new_feed))
wolk_device.add_feed_value(("NF", new_feed))
print(f'Publishing "NF": {new_feed}')
# Publish queued messages
wolk_device.publish()
Expand Down
11 changes: 10 additions & 1 deletion test/test_wolk_connect.py
Expand Up @@ -1446,12 +1446,21 @@ def test_request_timestamp_when_not_none(self):
def test_add_feed_value(self):
"""Test add feed value."""
self.wolk_device.logger.setLevel(logging.CRITICAL)
self.wolk_device.readings_persistence.store_reading = MagicMock()

self.wolk_device.add_feed_value(("foo", "bar"))

self.wolk_device.readings_persistence.store_reading.assert_called_once()

def test_add_feed_value_separated(self):
"""Test add feed value separated."""
self.wolk_device.logger.setLevel(logging.CRITICAL)
self.wolk_device.message_queue.put = MagicMock()
self.wolk_device.message_factory.make_from_feed_value = MagicMock(
return_value=True
)

self.wolk_device.add_feed_value_sealed("foo", "bar")
self.wolk_device.add_feed_value_separated(("foo", "bar"))

self.wolk_device.message_queue.put.assert_called_once()

Expand Down
5 changes: 2 additions & 3 deletions wolk/wolk_connect.py
Expand Up @@ -421,10 +421,9 @@ def add_feed_value(
self.logger.debug(
f"Adding feed value: reading: {reading}, timestamp = {timestamp}"
)

self.readings_persistence.store_reading(reading, timestamp)

def add_feed_value_sealed(
def add_feed_value_separated(
self,
reading: Union[Reading, List[Reading]],
timestamp: Optional[int] = None,
Expand All @@ -443,7 +442,7 @@ def add_feed_value_sealed(
denote when the reading occurred. By default, the current system
provided time will be assigned to a reading.
The sealed variant will ensure that these reading values get sent
The separated variant will ensure that these reading values get sent
as a separate message, independent of any other feed values that
have been added to the object.
Expand Down

0 comments on commit 3ad11fb

Please sign in to comment.