summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-08-03 22:59:20 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-08-03 22:59:20 +0300
commitfffb6d3ba9982938acad4a657706788eb683bc12 (patch)
tree76dca585f6d532fafa529136cbd6e7afd3f88432
iniital
-rw-r--r--README.md26
-rwxr-xr-xinverter-http-proxy50
2 files changed, 76 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e07b49a
--- /dev/null
+++ b/README.md
@@ -0,0 +1,26 @@
+# inverter-http-proxy
+
+HTTP interface for [inverterd](https://github.com/gch1p/inverter-tools).
+
+## Dependencides
+
+- Python 3
+- `aiohttp` (from Pypi)
+- `inverterd` (from Pypi)
+
+## Usage
+
+Assuming inverterd host is `192.168.1.2`:
+
+```
+inverter-http-proxy --inverter-host 192.168.1.2 --host 127.0.0.1 --port 8080
+```
+
+Then open https://127.0.0.1/get-status/ in the browser. You can replace `get-status`
+with any command supported by inverterd.
+
+Command arguments are not supported at the moment.
+
+## License
+
+MIT \ No newline at end of file
diff --git a/inverter-http-proxy b/inverter-http-proxy
new file mode 100755
index 0000000..b0ec7dd
--- /dev/null
+++ b/inverter-http-proxy
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+import logging, json
+
+from aiohttp import web
+from inverterd import Format, Client as InverterClient, InverterError
+from argparse import ArgumentParser
+
+routes = web.RouteTableDef()
+inv_host = None
+inv_port = None
+
+
+@routes.get('/{command}/')
+async def variable_handler(request):
+ command = request.match_info['command']
+
+ inverter = InverterClient(host=inv_host, port=inv_port)
+ inverter.connect()
+ inverter.format(Format.JSON)
+
+ try:
+ response = inverter.exec(command)
+ except InverterError as e:
+ response = str(e)
+
+ try:
+ response = json.loads(response)
+ except json.decoder.JSONDecodeError:
+ response = {'error': response}
+
+ return web.json_response(response, dumps=lambda x: json.dumps(x, separators=(',', ':')))
+
+
+if __name__ == '__main__':
+ parser = ArgumentParser()
+ parser.add_argument('--inverter-host', required=True, type=str)
+ parser.add_argument('--inverter-port', type=int, default=8305)
+ parser.add_argument('--host', default='0.0.0.0', type=str)
+ parser.add_argument('--port', default=8080, type=int)
+ args = parser.parse_args()
+
+ inv_host = args.inverter_host
+ inv_port = args.inverter_port
+
+ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
+ level=logging.INFO)
+
+ app = web.Application()
+ app.add_routes(routes)
+ web.run_app(app)