Skip to content

Commit 6737765

Browse files
committed
FT017TH.receive: now async, quits after timeout_seconds without valid packet, limit waiting for edge on GDO0 to timeout_seconds (blocks thread), no longer yield None; command wireless-sensor-receive: timeout after one hour without valid packet
1 parent 30a1286 commit 6737765

File tree

9 files changed

+210
-162
lines changed

9 files changed

+210
-162
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- added attribute type hints to `Measurement` class
1010
- declare compatibility with `python3.11`
1111

12+
### Changed
13+
- method `FT017TH.receive`
14+
- now async
15+
- quits after `timeout_seconds` without valid packet
16+
- `gpiod_line_request_rising_edge_events` (within `cc1101` library) blocks
17+
thread for up to `timeout_seconds`
18+
- no longer yields `None`
19+
- command `wireless-sensor-receive`: timeout after one hour without valid packet
20+
1221
### Removed
1322
- compatibility with `python3.5`, `python3.6`, `python3.7` & `python3.8`
1423

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ black = "*"
1111
mypy = "*"
1212
pylint = "*"
1313
pytest = "*"
14+
pytest-asyncio = "*"
1415
pytest-cov = "*"
1516

1617
# python<3.11 compatibility

Pipfile.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@ $ wireless-sensor-receive
3737
### Python Library
3838

3939
```python
40+
import asyncio
41+
4042
import wireless_sensor
4143

42-
sensor = wireless_sensor.FT017TH(gdo0_gpio_line_name=b'GPIO24')
43-
for measurement in sensor.receive(timeout_seconds=600):
44-
if not measurement:
45-
print("invalid packet or timeout")
46-
continue
47-
print(
48-
measurement.decoding_timestamp,
49-
measurement.temperature_degrees_celsius,
50-
measurement.relative_humidity,
51-
)
44+
async def _main():
45+
sensor = wireless_sensor.FT017TH(gdo0_gpio_line_name=b'GPIO24')
46+
async for measurement in sensor.receive(timeout_seconds=600):
47+
print(
48+
measurement.decoding_timestamp,
49+
measurement.temperature_degrees_celsius,
50+
measurement.relative_humidity,
51+
)
52+
53+
asyncio.run(_main())
5254
```

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@
7373
"cc1101==2.7.3",
7474
],
7575
setup_requires=["setuptools_scm"],
76-
tests_require=["pytest"],
76+
tests_require=["pytest", "pytest-asyncio"],
7777
)

tests/test_cli.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import datetime
1919
import logging
20+
import typing
2021
import unittest.mock
2122

2223
import pytest
@@ -27,6 +28,28 @@
2728
# pylint: disable=protected-access
2829

2930

31+
async def _receive_mock(
32+
sensor: wireless_sensor.FT017TH, timeout_seconds: int
33+
) -> typing.AsyncIterator[wireless_sensor.Measurement]:
34+
assert isinstance(sensor, wireless_sensor.FT017TH)
35+
assert timeout_seconds == 3600
36+
yield wireless_sensor.Measurement(
37+
decoding_timestamp=datetime.datetime(2020, 12, 7, 10, 0, 0),
38+
temperature_degrees_celsius=24.1234,
39+
relative_humidity=0.51234,
40+
)
41+
yield wireless_sensor.Measurement(
42+
decoding_timestamp=datetime.datetime(2020, 12, 7, 10, 0, 50),
43+
temperature_degrees_celsius=22.42,
44+
relative_humidity=0.55123,
45+
)
46+
yield wireless_sensor.Measurement(
47+
decoding_timestamp=datetime.datetime(2020, 12, 7, 10, 1, 41),
48+
temperature_degrees_celsius=21.1234,
49+
relative_humidity=0.61234,
50+
)
51+
52+
3053
@pytest.mark.parametrize(
3154
("argv", "root_log_level", "unlock_spi_device", "gdo0_gpio_line_name"),
3255
[
@@ -41,25 +64,7 @@ def test__receive(capsys, argv, root_log_level, unlock_spi_device, gdo0_gpio_lin
4164
with unittest.mock.patch(
4265
"wireless_sensor.FT017TH.__init__", return_value=None
4366
) as init_mock, unittest.mock.patch(
44-
"wireless_sensor.FT017TH.receive",
45-
return_value=[
46-
wireless_sensor.Measurement(
47-
decoding_timestamp=datetime.datetime(2020, 12, 7, 10, 0, 0),
48-
temperature_degrees_celsius=24.1234,
49-
relative_humidity=0.51234,
50-
),
51-
wireless_sensor.Measurement(
52-
decoding_timestamp=datetime.datetime(2020, 12, 7, 10, 0, 50),
53-
temperature_degrees_celsius=22.42,
54-
relative_humidity=0.55123,
55-
),
56-
None, # timeout or error
57-
wireless_sensor.Measurement(
58-
decoding_timestamp=datetime.datetime(2020, 12, 7, 10, 1, 41),
59-
temperature_degrees_celsius=21.1234,
60-
relative_humidity=0.61234,
61-
),
62-
],
67+
"wireless_sensor.FT017TH.receive", _receive_mock
6368
):
6469
with unittest.mock.patch("sys.argv", argv), unittest.mock.patch(
6570
"logging.basicConfig"
@@ -78,4 +83,5 @@ def test__receive(capsys, argv, root_log_level, unlock_spi_device, gdo0_gpio_lin
7883
"2020-12-07T10:00:00\t24.1°C\t51.2%\n"
7984
"2020-12-07T10:00:50\t22.4°C\t55.1%\n"
8085
"2020-12-07T10:01:41\t21.1°C\t61.2%\n"
86+
"timeout\n"
8187
)

0 commit comments

Comments
 (0)