-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "radiacode" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
description = "Library for RadiaCode-101" | ||
authors = ["Maxim Andreev <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Примеры использования библиотеки | ||
|
||
Устанавливаются с пакетом если указать `pip install radiacode[examples]` (вместо `pip install radiacode`) | ||
|
||
У каждого примера есть справка по `--help` | ||
|
||
|
||
### 1. [basic.py](./basic.py) | ||
Минимальный пример, показывающий соединение с устройством по USB или Bluetooth и получение серийного номера, спектра и измерений числа частиц/дозы | ||
``` | ||
$ python3 -m radiacode-examples.basic --bluetooth-mac 52:43:01:02:03:04 | ||
``` | ||
|
||
### 2. [webserver.py](./webserver.py) & [webserver.html](./webserver.html) | ||
Спектр и число частиц/доза в веб интерфейсе с автоматическим обновлением | ||
``` | ||
$ python3 -m radiacode-examples.webserver --bluetooth-mac 52:43:01:02:03:04 --listen-port 8080 | ||
``` | ||
|
||
|
||
### 3. [narodmon.py](./narodmon.py) | ||
Отправка измерений в сервис [народный мониторинг narodmon.ru](https://narodmon.ru) | ||
``` | ||
$ python3 -m radiacode-examples.narodmon --bluetooth-mac 52:43:01:02:03:04 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import argparse | ||
import asyncio | ||
import time | ||
|
||
import aiohttp | ||
|
||
from radiacode import CountRate, DoseRate, RadiaCode | ||
|
||
|
||
def sensors_data(rc_conn): | ||
databuf = rc_conn.data_buf() | ||
|
||
last_countrate, last_doserate = None, None | ||
for v in databuf: | ||
if isinstance(v, CountRate): | ||
if last_countrate is None or last_countrate.dt < v.dt: | ||
last_countrate = v | ||
elif isinstance(v, DoseRate): | ||
if last_doserate is None or last_doserate.dt < v.dt: | ||
last_doserate = v | ||
|
||
ret = [] | ||
if last_countrate is not None: | ||
ret.append( | ||
{ | ||
'id': 'S1', | ||
'name': 'CountRate', | ||
'value': last_countrate.count_rate, | ||
'unit': 'CPS', | ||
'time': int(last_countrate.dt.timestamp()), | ||
} | ||
) | ||
if last_doserate is not None: | ||
ret.append( | ||
{ | ||
'id': 'S2', | ||
'name': 'R_DoseRate', | ||
'value': 1000000 * last_doserate.dose_rate, | ||
'unit': 'μR/h', | ||
'time': int(last_doserate.dt.timestamp()), | ||
} | ||
) | ||
return ret | ||
|
||
|
||
async def send_data(d): | ||
# use aiohttp because we already have it as dependency in webserver.py, don't want add 'requests' here | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post('https://narodmon.ru/json', json=d) as resp: | ||
return await resp.text() | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--bluetooth-mac', type=str, required=True, help='MAC address of radiascan device') | ||
parser.add_argument('--connection', choices=['usb', 'bluetooth'], default='bluetooth', help='device connection type') | ||
parser.add_argument('--interval', type=int, required=False, default=600, help='send interval, seconds') | ||
args = parser.parse_args() | ||
|
||
if args.connection == 'usb': | ||
print('will use USB connection') | ||
rc_conn = RadiaCode() | ||
else: | ||
print('will use Bluetooth connection') | ||
rc_conn = RadiaCode(bluetooth_mac=args.bluetooth_mac) | ||
|
||
device_data = { | ||
'mac': args.bluetooth_mac.replace(':', '-'), | ||
'name': 'RadiaCode-101', | ||
} | ||
|
||
while True: | ||
d = { | ||
'devices': [ | ||
{ | ||
**device_data, | ||
'sensors': sensors_data(rc_conn), | ||
}, | ||
], | ||
} | ||
print(f'Sending {d}') | ||
|
||
try: | ||
r = asyncio.run(send_data(d)) | ||
print(f'NarodMon Response: {r}') | ||
except Exception as ex: | ||
print(f'NarodMon send error: {ex}') | ||
|
||
time.sleep(args.interval) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |