From 2b81d33bc80c07e42d5f27836c9070e435def7d3 Mon Sep 17 00:00:00 2001 From: bohdan-s Date: Fri, 24 Dec 2021 10:43:57 +1100 Subject: [PATCH] Added HTTP export, massive README update, improved error checking, added commandline options --- Dockerfile | 4 +--- SunGather/config-example.yaml | 4 ++++ SunGather/exports/http.py | 40 +++++++++++++++++++++++++++++++++++ SunGather/version.py | 2 +- requirements.txt | 2 +- 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 SunGather/exports/http.py diff --git a/Dockerfile b/Dockerfile index 7831368..98a63f8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,5 @@ FROM python:3 -ENV IS_DOCKER True - WORKDIR /usr/src/sungather COPY requirements.txt ./ @@ -10,4 +8,4 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt COPY SunGather/ . RUN touch /config/config.yaml -CMD [ "python", "sungather.py" ] \ No newline at end of file +CMD [ "python", "sungather.py -c /config/config.yaml" ] \ No newline at end of file diff --git a/SunGather/config-example.yaml b/SunGather/config-example.yaml index cdcac9c..cfc74ac 100644 --- a/SunGather/config-example.yaml +++ b/SunGather/config-example.yaml @@ -20,6 +20,10 @@ exports: - name: console # Print Registers to console, good for debugging / troubleshooting enabled: False + - name: http + enabled: True # Access at http://localhost:8080 or http://[serverip]:8080 + # port: 8080 # Default port is 8080 + - name: mqtt # Publish Registers to MQTT / Home Assistant host: 192.168.1.200 # port: 1883 diff --git a/SunGather/exports/http.py b/SunGather/exports/http.py new file mode 100644 index 0000000..1991646 --- /dev/null +++ b/SunGather/exports/http.py @@ -0,0 +1,40 @@ +from http.server import BaseHTTPRequestHandler, HTTPServer +from socketserver import ThreadingMixIn +from threading import Thread + +import logging + +class export_http(object): + html_body = "Pending Data retrieval" + def __init__(self): + return + + # Configure HTTP + def configure(self, config): + self.webServer = HTTPServer(('localhost', config.get('port',8080)), MyServer) + t = Thread(target=self.webServer.serve_forever) + t.start() + logging.info("Configured Simple HTTP Server") + + def publish(self, inverter): + #global html_body + + body = "" + for register in inverter: + body = body + f"" + export_http.html_body = body + f"
RegisterValue
{register}{inverter.get(register)}

Total {len(inverter)} registers" + print("Publish HTTP") + return + +class MyServer(BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + self.wfile.write(bytes("SunGather", "utf-8")) + self.wfile.write(bytes("", "utf-8")) + self.wfile.write(bytes(export_http.html_body, "utf-8")) + self.wfile.write(bytes("", "utf-8")) + self.wfile.write(bytes("", "utf-8")) + def log_message(self, format, *args): + pass diff --git a/SunGather/version.py b/SunGather/version.py index 541f859..c12f34c 100644 --- a/SunGather/version.py +++ b/SunGather/version.py @@ -1 +1 @@ -__version__ = '0.1.0' \ No newline at end of file +__version__ = '0.1.1' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index b63ec74..72caf15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ requests>=2.26.0 paho-mqtt>=1.5.1 pymodbus>=2.4.0 SungrowModbusTcpClient>=0.1.5 -SungrowModbusWebClient>=0.2.5 +SungrowModbusWebClient>=0.2.6