diff options
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | LICENSE | 21 | ||||
-rw-r--r-- | README.md | 25 | ||||
-rw-r--r-- | inverterd/__init__.py | 1 | ||||
-rw-r--r-- | inverterd/inverterd.py | 53 | ||||
-rw-r--r-- | setup.cfg | 22 |
6 files changed, 128 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..935015f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +venv +__pycache__ +dist +build +inverterd.egg-info +.idea
\ No newline at end of file @@ -0,0 +1,21 @@ +MIT License + +Copyright (C) 2021 Evgeny Zinoviev + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..106605b --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# inverterd-client + +This is a Python [inverterd](https://github.com/gch1p/inverter-tools) client library. + +## Installation + +It's available on Pypi: + +``` +pip install inverterd +``` + +## Usage example +```python +from inverterd import Client + +c = Client(8305, '127.0.0.1') +c.format('json') +print(c.exec('get-status')) +print(c.exec('get-year-generated', (2021,))) +``` + +## License + +MIT
\ No newline at end of file diff --git a/inverterd/__init__.py b/inverterd/__init__.py new file mode 100644 index 0000000..531a1fc --- /dev/null +++ b/inverterd/__init__.py @@ -0,0 +1 @@ +from .inverterd import Client, Format, InverterError
\ No newline at end of file diff --git a/inverterd/inverterd.py b/inverterd/inverterd.py new file mode 100644 index 0000000..9f07c2e --- /dev/null +++ b/inverterd/inverterd.py @@ -0,0 +1,53 @@ +import socket + +from enum import Enum + + +class Format(Enum): + JSON = 'json' + TABLE = 'table' + SIMPLE_TABLE = 'simple-table' + + +class Client: + def __init__(self, port=8305, host='127.0.0.1'): + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.connect((host, port)) + + def _write(self, line): + self.sock.sendall((line+'\r\n').encode()) + + def _read(self): + buf = bytearray() + while True: + buf.extend(self.sock.recv(256)) + if b'\r\n\r\n' in buf: + break + + response = buf.decode().strip().split("\r\n") + status = response.pop(0) + if status not in ('ok', 'err'): + raise InverterError(f"Unexpected status '{status}'") + + if status == 'err': + raise InverterError(response[0] if len(response) >= 0 else "Unknown error") + + return '\r\n'.join(response) + + def protocol(self, v): + self._write(f'v {v}') + return self._read() + + def format(self, format): + self._write(f'format {format}') + return self._read() + + def exec(self, command, arguments=()): + buf = f'exec {command}' + for arg in arguments: + buf += f' {arg}' + self._write(buf) + return self._read() + + +class InverterError(RuntimeError): pass
\ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..1140bcc --- /dev/null +++ b/setup.cfg @@ -0,0 +1,22 @@ +[metadata] +name = inverterd +version = 1.0.0 +author = Evgeny Zinoviev +author_email = me@ch1p.io +description = +long_description = file: README.md +license = MIT +license_file = LICENSE +long_description_content_type = text/markdown +url = https://github.com/gch1p/inverterd-client.git +project_urls = + Bug Tracker = https://github.com/gch1p/inverterd-client/issues +classifiers = + Programming Language :: Python :: 3 + License :: OSI Approved :: MIT License + Operating System :: OS Independent + +[options] +packages = inverterd +python_requires = >=3.6 +include_package_data = True
\ No newline at end of file |