Skip to content

Commit

Permalink
Fix remaining errors during on-device testing
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7878 committed Apr 30, 2023
1 parent 65de149 commit 0cbe850
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
58 changes: 39 additions & 19 deletions dbus_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
import platform
import sys
import logging
import time
from vedbus import VeDbusService # pylint: disable=E0401
import requests # for http GET #pylint: disable=E0401
from requests.auth import HTTPDigestAuth # pylint: disable=E0401

# victron imports:
import dbus # pylint: disable=E0401
from requests.auth import HTTPDigestAuth #pylint: disable=E0401
import time

# our imports:
import constants
# TODO: This should be refactored to use helper (utils) as a module # pylint: disable=W0511
from helpers import * # pylint: disable=W0401,W0614
from helpers import *

# victron imports:
import dbus #pylint: disable=E0401

if sys.version_info.major == 2:
import gobject # pylint: disable=E0401
import gobject #pylint: disable=E0401
else:
from gi.repository import GLib as gobject # pylint: disable=E0401
from gi.repository import GLib as gobject #pylint: disable=E0401

sys.path.insert(
1,
Expand All @@ -31,6 +29,7 @@
"/opt/victronenergy/dbus-systemcalc-py/ext/velib_python",
),
)
from vedbus import VeDbusService #pylint: disable=E0401

class PvInverterRegistry(type):
'''Run a registry for all PV Inverter'''
Expand Down Expand Up @@ -132,6 +131,11 @@ def __init__(
# Set Mode to 2 to show it as ON
# 2=On;4=Off;5=Eco
self._dbusservice.add_path("/Mode", 2)
#set the SystemState flaf to 9=Inverting
#/SystemState/State -> 0: Off
# -> 1: Low power
# -> 9: Inverting
self._dbusservice.add_path("/State", 9)

# add path values to dbus
for path, settings in self._paths.items():
Expand Down Expand Up @@ -204,10 +208,6 @@ def _read_config_template(self, template_number):
self.custtotal_factor = config[f"TEMPLATE{template_number}"]["CUST_Total_Mult"]
self.custvoltage = config[f"TEMPLATE{template_number}"]["CUST_Voltage"].split(
"/")
self.custdcvoltage = config[f"TEMPLATE{template_number}"]["CUST_DCVoltage"].split(
"/")
self.custcurrent = config[f"TEMPLATE{template_number}"]["CUST_Current"].split(
"/")
self.custapipath = config[f"TEMPLATE{template_number}"]["CUST_API_PATH"]
self.serial = str(config[f"TEMPLATE{template_number}"]["CUST_SN"])
self.pollinginterval = int(
Expand All @@ -228,6 +228,22 @@ def _read_config_template(self, template_number):
self.digestauth = is_true(get_config_value(
config, "DigestAuth", "TEMPLATE", template_number, False))

try:
self.custcurrent= config[f"TEMPLATE{template_number}"]["CUST_Current"].split(
"/")
except Exception: # pylint: disable=broad-except
# set to undefined because get_nested will solve this to 0
self.custcurrent = "[undefined]"
logging.debug("CUST_Current not set")

try:
self.custdcvoltage = config[f"TEMPLATE{template_number}"]["CUST_DCVoltage"].split(
"/")
except Exception: # pylint: disable=broad-except
# set to undefined because get_nested will solve this to 0
self.custdcvoltage = "[undefined]"
logging.debug("CUST_DCVoltage not set")

try:
self.max_age_ts = int(config["DEFAULT"]["MagAgeTsLastSuccess"])
except ValueError as ex:
Expand Down Expand Up @@ -425,7 +441,7 @@ def fetch_ahoy_record_data(self):
def fetch_url(self, url, try_number=1):
'''Fetch JSON data from url. Throw an exception on any error. Only return on success.'''
try:
logging.debug("calling %s with timeout=%f", url, self.httptimeout)
logging.debug("calling %s with timeout=%s", url, self.httptimeout)
if self.digestauth:
logging.debug("using Digest access authentication...")
json_str = requests.get(url=url, auth=HTTPDigestAuth(
Expand Down Expand Up @@ -601,7 +617,7 @@ def get_values_for_inverter(self):
voltage = get_ahoy_field_by_name(
meter_data, self.pvinverternumber, "U_AC")
dc_voltage = get_ahoy_field_by_name(
meter_data, self.pvinverternumber, "U_DC")
meter_data, self.pvinverternumber, "U_DC", False)
current = get_ahoy_field_by_name(
meter_data, self.pvinverternumber, "I_AC")

Expand Down Expand Up @@ -651,13 +667,17 @@ def set_dbus_values(self):
self._dbusservice["/Ac/Out/L1/V"] = voltage
self._dbusservice["/Ac/Out/L1/I"] = current
self._dbusservice["/Dc/0/Voltage"] = dc_voltage
state = None
if current > 0:

try:
float_current = float(current)
except ValueError:
float_current = 0

This comment has been minimized.

Copy link
@dsteinkopf

dsteinkopf Apr 30, 2023

Collaborator

I am not quite sure if this is a good idea.
This might have to do with #72. But maybe I don't have enogh overview at the moment.


if float_current > 0:
state = 9

This comment has been minimized.

Copy link
@dsteinkopf

dsteinkopf Apr 30, 2023

Collaborator

Would you mind giving this state a more speaking name? Who's state is this?

else:
state = 0
self._dbusservice["/State"] = state

logging.debug("Inverter #%d Voltage (/Ac/Out/L1/V): %s",
self.pvinverternumber, voltage)
logging.debug("Inverter #%d Current (/Ac/Out/L1/I): %s",
Expand Down
21 changes: 16 additions & 5 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_nested(meter_data, path):
value = 0
return value

def get_ahoy_field_by_name(meter_data, actual_inverter, fieldname):
def get_ahoy_field_by_name(meter_data, actual_inverter, fieldname, ch0_fld_names = True):

This comment has been minimized.

Copy link
@dsteinkopf

dsteinkopf Apr 30, 2023

Collaborator

"ch0_fld_names" is a flag this use a name that helps knowing it is a flag. e.g. use_ch0_fld_names.

This comment has been minimized.

Copy link
@dsteinkopf

dsteinkopf Apr 30, 2023

Collaborator

And please some comment about its semantic. Why do we need it? -> Maybe this helps finding a better name for the flag.

This comment has been minimized.

Copy link
@0x7878

0x7878 May 2, 2023

Author Collaborator

This is used for the Ahoy JSON format. All AC Parameters go to ch0_fld_names whereas DC Parameter from Inverter 0 - n goes to fld_names. Renamed it to use_ch0_fld_names

This comment has been minimized.

Copy link
@dsteinkopf

dsteinkopf May 3, 2023

Collaborator

Ah, yes, I remember(ed) now.

'''get the value by name instead of list index'''
# fetch value from record call:
# - but there seem to be more than one value per type and Inverter, and we don't know which one to take
Expand All @@ -51,10 +51,21 @@ def get_ahoy_field_by_name(meter_data, actual_inverter, fieldname):
# return val
# raise ValueError(f"Fieldname {fieldname} not found in meter_data.")

ac_data_field_names = meter_data["ch0_fld_names"]
data_index = ac_data_field_names.index(fieldname)
ac_channel_index = 0
return meter_data["inverter"][actual_inverter]["ch"][ac_channel_index][data_index]
data = None

if(ch0_fld_names):
data_field_names = meter_data["ch0_fld_names"]
data_index = data_field_names.index(fieldname)
ac_channel_index = 0
data = meter_data["inverter"][actual_inverter]["ch"][ac_channel_index][data_index]
else:
data_field_names = meter_data["fld_names"]
data_index = data_field_names.index(fieldname)
# TODO - check if this channel has to be adjusted
dc_channel_index = 0 + 1 # 1 = DC1, 2 = DC2 etc.

This comment has been minimized.

Copy link
@dsteinkopf

dsteinkopf Apr 30, 2023

Collaborator

0+1 ? Maybe the above mentioned comment/explanation already answers this question.

This comment has been minimized.

Copy link
@0x7878

0x7878 May 2, 2023

Author Collaborator

stupid leftover from a variable...

data = meter_data["inverter"][actual_inverter]["ch"][dc_channel_index][data_index]

return data

def is_true(val):
'''helper function to test for different true values'''
Expand Down

0 comments on commit 0cbe850

Please sign in to comment.