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

Implement inverter.py own logger / Python 3.9 retro-compatibility / Restore .connected attribute #35

Merged
merged 8 commits into from
Apr 3, 2024
25 changes: 12 additions & 13 deletions sun2000_modbus/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ def decode_bitfield(value):


def decode(value, data_type):
match data_type:
case DataType.STRING:
return decode_string(value)
case DataType.UINT16_BE | DataType.UINT32_BE:
return decode_uint_be(value)
case DataType.INT16_BE | DataType.INT32_BE:
return decode_int_be(value)
case DataType.BITFIELD16 | DataType.BITFIELD32:
return decode_bitfield(value)
case DataType.MULTIDATA:
return value
case _:
raise ValueError("Unknown register type")
if data_type == DataType.STRING:
return decode_string(value)
elif data_type == DataType.UINT16_BE or data_type == DataType.UINT32_BE:
return decode_uint_be(value)
elif data_type == DataType.INT16_BE or data_type == DataType.INT32_BE:
return decode_int_be(value)
elif data_type == DataType.BITFIELD16 or data_type == DataType.BITFIELD32:
return decode_bitfield(value)
elif data_type == DataType.MULTIDATA:
return value
else:
raise ValueError("Unknown register type")
30 changes: 21 additions & 9 deletions sun2000_modbus/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

from . import datatypes

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
log_format = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
handler = logging.StreamHandler()
handler.setFormatter(log_format)
logger.addHandler(handler)


class Sun2000:
Expand All @@ -20,10 +25,10 @@ def connect(self):
self.inverter.connect()
time.sleep(self.wait)
if self.isConnected():
logging.info('Successfully connected to inverter')
logger.info('Successfully connected to inverter')
return True
else:
logging.error('Connection to inverter failed')
logger.error('Connection to inverter failed')
return False

def disconnect(self):
Expand All @@ -37,17 +42,21 @@ def isConnected(self):
"""Check if underlying tcp socket is open"""
return self.inverter.is_socket_open()

@property
def connected(self):
return self.isConnected()

def read_raw_value(self, register):
if not self.isConnected():
raise ValueError('Inverter is not connected')

try:
register_value = self.inverter.read_holding_registers(register.value.address, register.value.quantity, unit=self.unit)
if type(register_value) == ModbusIOException:
logging.error("Inverter unit did not respond")
logger.error("Inverter unit did not respond")
raise register_value
except ConnectionException:
logging.error("A connection error occurred")
logger.error("A connection error occurred")
raise

return datatypes.decode(register_value.encode()[1:], register.value.data_type)
Expand All @@ -60,11 +69,14 @@ def read(self, register):
else:
return raw_value / register.value.gain

def read_formatted(self, register):
def read_formatted(self, register, use_locale=False):
value = self.read(register)

if register.value.unit is not None:
return f'{value} {register.value.unit}'
if use_locale:
return f'{value:n} {register.value.unit}'
else:
return f'{value} {register.value.unit}'
elif register.value.mapping is not None:
return register.value.mapping.get(value, 'undefined')
else:
Expand All @@ -86,10 +98,10 @@ def read_range(self, start_address, quantity=0, end_address=0):
try:
register_range_value = self.inverter.read_holding_registers(start_address, quantity, unit=self.unit)
if type(register_range_value) == ModbusIOException:
logging.error("Inverter unit did not respond")
logger.error("Inverter unit did not respond")
raise register_range_value
except ConnectionException:
logging.error("A connection error occurred")
logger.error("A connection error occurred")
raise

return datatypes.decode(register_range_value.encode()[1:], datatypes.DataType.MULTIDATA)
Loading