summaryrefslogtreecommitdiff
path: root/inverter-http-proxy
blob: 64b922728763751659f752865c9de5bf49cae14f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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']

    args = []
    if 'args' in request.query:
        args = request.query['args'].split(',')

    inverter = InverterClient(host=inv_host, port=inv_port)
    inverter.connect()
    inverter.format(Format.JSON)

    try:
        response = inverter.exec(command, arguments=args)
    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, host=args.host, port=args.port)