Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data server example #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ COPY . /app
WORKDIR /app
RUN python3 setup.py install

ENTRYPOINT [ "/usr/local/bin/python3", "example.py" ]
#Install requirements
RUN pip3 install -r requirements.txt

ENTRYPOINT [ "/usr/local/bin/python3", "data_server.py" ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need a strategy what the entrypoint will be like for the docker image :-) Maybe create something more versatile that can dispatch to the intended command?
I stayed with the default example when I introduced it so that it can still be used as a "command line tool" for instance.

16 changes: 16 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3'

services:
solaredge-modbus-exporter:
#image: your-container-registry/solaredge-modbus:latest # Replace with your image details
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000" # Map container port 5000 to host port 5000
environment:
MODBUS_HOST: "PLACE_YOUR_IP_HERE" # Replace with your Modbus host
MODBUS_PORT: "1502" # Replace with your Modbus port
MODBUS_TIMEOUT: "1" # Replace with your Modbus timeout
MODBUS_UNIT: "1" # Replace with your Modbus unit
restart: always
56 changes: 56 additions & 0 deletions data_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3

import os
import json
from flask import Flask, jsonify

import solaredge_modbus

app = Flask(__name__)

global inverter

@app.route('/', methods=['GET'])
def get_values():
global inverter

values = {}
values = inverter.read_all()
meters = inverter.meters()
batteries = inverter.batteries()
values["meters"] = {}
values["batteries"] = {}

for meter, params in meters.items():
meter_values = params.read_all()
values["meters"][meter] = meter_values

for battery, params in batteries.items():
battery_values = params.read_all()
values["batteries"][battery] = battery_values

return jsonify(values)

def parse_environment_variables():
host = os.environ.get("MODBUS_HOST", "localhost")
port = os.environ.get("MODBUS_PORT", "502")
timeout = os.environ.get("MODBUS_TIMEOUT", "1")
unit = os.environ.get("MODBUS_UNIT", "1")

return {
"host": host,
"port": port,
"timeout": timeout,
"unit": unit,
}

args = parse_environment_variables() # Parse environment variables
inverter = solaredge_modbus.Inverter(
host=args["host"],
port=int(args["port"]),
timeout=int(args["timeout"]),
unit=int(args["unit"])
)

if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flask==2.3.2