diff --git a/Dockerfile b/Dockerfile index e7ad160..7831368 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,13 @@ FROM python:3 +ENV IS_DOCKER True + WORKDIR /usr/src/sungather COPY requirements.txt ./ -RUN pip install --no-cache-dir -r requirements.txt +RUN pip install --no-cache-dir --upgrade -r requirements.txt COPY SunGather/ . -RUN touch /usr/src/sungather/config.yaml +RUN touch /config/config.yaml CMD [ "python", "sungather.py" ] \ No newline at end of file diff --git a/SunGather/config-example.yaml b/SunGather/config-example.yaml index c2e5865..cdcac9c 100644 --- a/SunGather/config-example.yaml +++ b/SunGather/config-example.yaml @@ -1,19 +1,20 @@ inverter: host: 192.168.1.100 - # port: 502 # Default for modbus is 502, for http is 8082 - # slave: 0x01 # Default is 0x01 - # timeout: 10 # Default is 10 - # scan_interval: 30 # Default is 30 - connection: sungrow # options: modbus, sungrow, http - # model: "SG7.0RT" # This is autodected on startup, only needed if detection issues or for testing - # See model list here: https://github.com/bohdan-s/SunGather#supported + # port: 502 # Default for modbus is 502, for http is 8082 + # slave: 0x01 # Default is 0x01 + # timeout: 10 # Default is 10 + # scan_interval: 30 # Default is 30 + connection: sungrow # options: modbus, sungrow, http + # model: "SG7.0RT" # This is autodected on startup, only needed if detection issues or for testing + # See model list here: https://github.com/bohdan-s/SunGather#supported # smart_meter: True # Default is False, Set to true if inverter supports reading grind / house consumption + # use_local_time: False # Default False, Uses Inveter time, if try uses PC time when updating timestamps (e.g. PVOutput) # hybrid: False # Default false, if you have a Hybrid (battery) set to True # logging: 30 # 10 = Debug, 20 = Info, 30 = Warnning (default), 40 = Error # level: 1 # 0 = Model and Solar Generation, - # 1 (default) = Useful data, all required for exports, - # 2 everything your Inverter supports, - # 3 Everything from every register + # 1 (default) = Useful data, all required for exports, + # 2 everything your Inverter supports, + # 3 Everything from every register exports: - name: console # Print Registers to console, good for debugging / troubleshooting diff --git a/SunGather/exports/pvoutput.py b/SunGather/exports/pvoutput.py index 8110974..8746ae1 100644 --- a/SunGather/exports/pvoutput.py +++ b/SunGather/exports/pvoutput.py @@ -1,6 +1,6 @@ import logging -import datetime import requests +import datetime # Configure PVOutput class export_pvoutput(object): @@ -63,7 +63,7 @@ class export_pvoutput(object): """ at_least_one_of = set(["v1", "v2", "v3", "v4"]) - now = datetime.datetime.now() + now = datetime.datetime.strptime(inverter.get('timestamp'), "%Y-%m-%d %H:%M:%S") if self.latest_run: # Spread out our publishes over the hour based on the rate limit diff --git a/SunGather/sungather.py b/SunGather/sungather.py index b46900f..351439d 100644 --- a/SunGather/sungather.py +++ b/SunGather/sungather.py @@ -5,12 +5,14 @@ from SungrowModbusWebClient import SungrowModbusWebClient from pymodbus.client.sync import ModbusTcpClient from threading import Thread from version import __version__ +from datetime import datetime import importlib import logging import sys import yaml import time +import os logging.basicConfig( format='%(asctime)s %(levelname)-8s %(message)s', @@ -20,21 +22,34 @@ logging.info(f'Starting SunGather {__version__}') #opts = sys.getopt(sys.argv[1:],"hi:o:",["ifile=","ofile="]) +IS_DOCKER = os.environ.get('IS_DOCKER', False) + +# If we are in docker, get config.py from a easy location to map via volumes +if IS_DOCKER: + try: + config = yaml.safe_load(open('/config/config.yaml')) + logging.info(f"Loaded config: /config/config.yaml") + except Exception as err: + logging.error(f"Failed: Loading config: /config/config.yaml {err}") + sys.exit(1) +else: + try: + config = yaml.safe_load(open('config.yaml')) + logging.info(f"Loaded config: {os.getcwd()}/config.yaml") + except Exception as err: + logging.error(f"Failed: Loading config: {os.getcwd()}/config.yaml {err}") + sys.exit(1) + try: - config = yaml.safe_load(open('config.yaml')) - logging.info(f"Loaded config: config.yaml") + registers = yaml.safe_load(open('registers.yaml')) + logging.info(f"Loaded registers: {os.getcwd()}/registers.yaml") except Exception as err: - logging.error(f"Failed: Loading config: config.yaml {err}") + logging.error(f"Failed: Loading registers: {os.getcwd()}/registers.yaml {err}") sys.exit(1) logging.getLogger().setLevel(config['inverter'].get('logging',30)) -try: - registers = yaml.safe_load(open('registers.yaml')) - logging.info(f"Loaded registers: registers.yaml") -except Exception as err: - logging.error(f"Failed: Loading registers: registers.yaml {err}") - sys.exit(1) + exports = [] if config.get('exports'): @@ -250,23 +265,34 @@ def scrape_inverter(): except Exception: pass - try: - inverter["timestamp"] = "%s-%s-%s %s:%02d:%02d" % ( - inverter["year"], - inverter["month"], - inverter["day"], - inverter["hour"], - inverter["minute"], - inverter["second"], - ) + if config['inverter'].get('use_local_time',False): + inverter["timestamp"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + logging.debug(f'Using Local Computer Time: {inverter.get("timestamp")}') del inverter["year"] del inverter["month"] del inverter["day"] del inverter["hour"] del inverter["minute"] del inverter["second"] - except Exception: - pass + else: + try: + inverter["timestamp"] = "%s-%s-%s %s:%02d:%02d" % ( + inverter["year"], + inverter["month"], + inverter["day"], + inverter["hour"], + inverter["minute"], + inverter["second"], + ) + logging.debug(f'Using Inverter Time: {inverter.get("timestamp")}') + del inverter["year"] + del inverter["month"] + del inverter["day"] + del inverter["hour"] + del inverter["minute"] + del inverter["second"] + except Exception: + pass client.close() return True