Skip to content

Commit

Permalink
symcon.py: accept multi-inverter input from pvdata.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sellth committed Jan 10, 2021
1 parent d5c61e5 commit 2d6e3d0
Showing 1 changed file with 41 additions and 44 deletions.
85 changes: 41 additions & 44 deletions features/symcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
2018-12-23 Tommi2Day
2020-09-22 Tommi2Day fix empty pv data
2021-01-07 sellth added support for multiple inverters
Configuration:
[FEATURE-symcon]
Expand Down Expand Up @@ -55,14 +56,10 @@ def run(emparts, config):

# mqtt client settings
myhostname = platform.node()

symcon_last_update = time.time()

url = 'http://' + host + ':' + str(port) + emhook

# last aupdate
symcon_last_update = time.time()

serial = emparts['serial']
data = {}
for f in fields.split(','):
Expand Down Expand Up @@ -118,6 +115,8 @@ def run(emparts, config):
pvhook = config.get('pvhook')
pvfields = config.get('pvfields', 'AC Power,daily yield')
if None in [pvhook, pvfields]: return
pvurl = 'http://' + host + ':' + str(port) + pvhook

try:
from features.pvdata import pv_data
except:
Expand All @@ -129,25 +128,6 @@ def run(emparts, config):
print("Symcon EM: No PV Data")

return
serial = pv_data['serial']
pvurl = 'http://' + host + ':' + str(port) + pvhook
pvpower = pv_data.get("AC Power")
if None in [serial, pvpower]: return
for f in pvfields.split(','):
data[f] = pv_data.get(f, 0)

data['timestamp'] = symcon_last_update
data['sender'] = myhostname
data['serial'] = str(serial)
pvpayload = json.dumps(data)

# prepare request
pvreq = urllib.request.Request(pvurl)
pvreq.add_header('Content-Type', 'application/json; charset=utf-8')
pvdataasbytes = pvpayload.encode('utf-8') # needs to be bytes
pvreq.add_header('Content-Length', str(len(pvdataasbytes)))
# print(dataasbytes)
pvreq.add_header("User-Agent", "SMWR")

# prepare auth
if None not in [user, password]:
Expand All @@ -159,28 +139,45 @@ def run(emparts, config):
if symcon_debug > 2:
print('Symcon PV: use Basic auth')

# send it

try:
response = urllib.request.urlopen(pvreq, data=pvdataasbytes, timeout=int(timeout))
# prepare request
pvreq = urllib.request.Request(pvurl)
pvreq.add_header('Content-Type', 'application/json; charset=utf-8')
pvreq.add_header("User-Agent", "SMWR")

except urllib.error.HTTPError as e:
if symcon_debug > 0:
print('Symcon PV : HTTPError: {%s} to %s ' % (format(e.reason), pvurl))
pass
except urllib.error.URLError as e:
if symcon_debug > 0:
print('Symcon PV: URLError: {%s} to %s ' % (format(e.reason), pvurl))
pass
except Exception as e:
if symcon_debug > 0:
print("Symcon PV: Error from symcon request")
print(e)
pass
else:
if symcon_debug > 0:
print("Symcon PV: data published %s:%s to %s" % (
format(time.strftime("%H:%M:%S", time.localtime(symcon_last_update))), pvpayload, pvurl))
for inv in pv_data:
serial = inv.get("serial")
if serial is not None:
for f in pvfields.split(','):
data[f] = inv.get(f, 0)

data['timestamp'] = symcon_last_update
data['sender'] = myhostname
data['serial'] = str(serial)
pvpayload = json.dumps(data)
pvdataasbytes = pvpayload.encode('utf-8') # needs to be bytes
pvreq.add_header('Content-Length', str(len(pvdataasbytes)))
# print(dataasbytes)

# send it
try:
response = urllib.request.urlopen(pvreq, data=pvdataasbytes, timeout=int(timeout))
except urllib.error.HTTPError as e:
if symcon_debug > 0:
print('Symcon PV : HTTPError: {%s} to %s ' % (format(e.reason), pvurl))
pass
except urllib.error.URLError as e:
if symcon_debug > 0:
print('Symcon PV: URLError: {%s} to %s ' % (format(e.reason), pvurl))
pass
except Exception as e:
if symcon_debug > 0:
print("Symcon PV: Error from symcon request")
print(e)
pass
else:
if symcon_debug > 0:
print("Symcon PV: data published %s:%s to %s" % (
format(time.strftime("%H:%M:%S", time.localtime(symcon_last_update))), pvpayload, pvurl))


def stopping(emparts, config):
Expand Down

0 comments on commit 2d6e3d0

Please sign in to comment.