This commit is contained in:
Evgeny Zinoviev 2021-05-07 00:34:21 +03:00
commit 47ab3d064d
6 changed files with 128 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
venv
__pycache__
dist
build
inverterd.egg-info
.idea

21
LICENSE Normal file
View 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.

25
README.md Normal file
View File

@ -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

1
inverterd/__init__.py Normal file
View File

@ -0,0 +1 @@
from .inverterd import Client, Format, InverterError

53
inverterd/inverterd.py Normal file
View File

@ -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

22
setup.cfg Normal file
View File

@ -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