Skip to content

Commit

Permalink
feat: initial open source release
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuagruenstein committed Apr 16, 2024
0 parents commit 0455c21
Show file tree
Hide file tree
Showing 16 changed files with 1,407 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/semantic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
titleOnly: true
46 changes: 46 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Release
on:
push:
branches: main

jobs:
publish:
runs-on: ubuntu-latest
permissions:
# https://docs.github.com/en/rest/overview/permissions-required-for-github-apps?apiVersion=2022-11-28#metadata
id-token: write
contents: write
steps:
- uses: actions/checkout@v2
with:
ref: main
fetch-depth: 0

- name: Python Semantic Release
id: release
uses: python-semantic-release/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
root_options: "-vv"

- name: Publish package distributions to PyPI
id: pypi-publish

# NOTE: DO NOT wrap the conditional in ${{ }} as it will always evaluate to true.
# See https://github.com/actions/runner/issues/1173
if: steps.release.outputs.released == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
verbose: true

- name: Publish package distributions to GitHub Releases
id: github-release

# NOTE: DO NOT wrap the conditional in ${{ }} as it will always evaluate to true.
# See https://github.com/actions/runner/issues/1173
if: steps.release.outputs.released == 'true'
uses: python-semantic-release/upload-to-gh-release@main
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ steps.release.outputs.tag }}

129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Tutor Intelligence

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# tcp-modbus-aio

[![PyPI version](https://badge.fury.io/py/tcp-modbus-aio.svg)](https://badge.fury.io/py/tcp-modbus-aio)

asyncio client library for tcp modbus devices. built on top of [umodbus](https://pypi.org/project/uModbus/) but extended for more *industrial robustness* and asyncio compat. the [umodbus documentation](https://umodbus.readthedocs.io/en/latest/) is recommended reading to have any hope of using this code.

narrowly constructed for the use cases of [Tutor Intelligence](http://tutorintelligence.com/), but feel free to post an issue or PR if relevant to you.

### usage

create a `TCPModbusClient`. once you have it, you can call the following methods on it:
- `await conn.send_modbus_message(request_function, **kwargs)`: sends a `umodbus.functions.ModbusFunction` to the modbus device and returns the corresponding `ModbusFunction` reply.
- `await conn.test_connection()`: sends a modbus message to the device to ensure it's still operational (currently hardcoded to read coil 0) and return boolean of whether it succeeded. is implemented as a cached awaitable to allow you to spam this call.
- `await conn.clear_tcp_connection()`: kill the current TCP socket (a new one will automatically be created for the next request)
- `await conn.log_watch(msg, memo_key="system_temperature", expiry_period_s=10, hz=1)`: spins up a background coroutine to log the result of that message for the next `expiry_period` seconds at `hz` frequency. `memo_key` is used to allow multiple calls to `log_watch` without having overlapping log watch loops.
- `await conn.close()`: for cleaning up the connection (kills TCP conn and ping loop)
21 changes: 21 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import asyncio

from umodbus.functions import ReadCoils

from tcp_modbus_aio.connection import TCPModbusClient


async def example() -> None:
example_message = ReadCoils()
example_message.starting_address = 0
example_message.quantity = 1

async with TCPModbusClient("192.168.250.204") as conn:
response = await conn.send_modbus_message(example_message)

assert response is not None, "we expect a response from ReadCoils"
print(response.data)


if __name__ == "__main__":
asyncio.run(example())
Loading

0 comments on commit 0455c21

Please sign in to comment.