Browse Source

Provice decoders super class to ensure properties

pull/34/head
Jan-Jonas Sämann 3 years ago
parent
commit
e5b427f4f2
  1. 97
      tools/rpi/hoymiles/decoders/__init__.py

97
tools/rpi/hoymiles/decoders/__init__.py

@ -2,13 +2,43 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import struct import struct
import crcmod import crcmod
from datetime import timedelta from datetime import datetime, timedelta
f_crc_m = crcmod.predefined.mkPredefinedCrcFun('modbus') f_crc_m = crcmod.predefined.mkPredefinedCrcFun('modbus')
class StatusResponse: class Response:
inverter_ser = None
inverter_name = None
dtu_ser = None
response = None
def __init__(self, *args, **params):
"""
:param bytes response: response payload bytes
"""
self.inverter_ser = params.get('inverter_ser', None)
self.inverter_name = params.get('inverter_name', None)
self.dtu_ser = params.get('dtu_ser', None)
self.response = args[0]
if isinstance(params.get('time_rx', None), datetime):
self.time_rx = params['time_rx']
else:
self.time_rx = datetime.now()
def __dict__(self):
return {
'inverter_ser': self.inverter_ser,
'inverter_name': self.inverter_name,
'dtu_ser': self.dtu_ser}
class StatusResponse(Response):
"""Inverter StatusResponse object""" """Inverter StatusResponse object"""
e_keys = ['voltage','current','power','energy_total','energy_daily'] e_keys = ['voltage','current','power','energy_total','energy_daily','powerfactor']
def __init__(self, *args, **params):
super().__init__(*args, **params)
def unpack(self, fmt, base): def unpack(self, fmt, base):
""" """
@ -77,17 +107,22 @@ class StatusResponse:
:return: dict of properties :return: dict of properties
:rtype: dict :rtype: dict
""" """
data = {} data = super().__dict__()
data['phases'] = self.phases data['phases'] = self.phases
data['strings'] = self.strings data['strings'] = self.strings
data['temperature'] = self.temperature data['temperature'] = self.temperature
data['frequency'] = self.frequency data['frequency'] = self.frequency
data['time'] = self.time_rx
return data return data
class UnknownResponse: class UnknownResponse(Response):
""" """
Debugging helper for unknown payload format Debugging helper for unknown payload format
""" """
def __init__(self, *args, **params):
super().__init__(*args, **params)
@property @property
def hex_ascii(self): def hex_ascii(self):
""" """
@ -291,8 +326,8 @@ class EventsResponse(UnknownResponse):
9000: 'Microinverter is suspected of being stolen' 9000: 'Microinverter is suspected of being stolen'
} }
def __init__(self, response): def __init__(self, *args, **params):
self.response = response super().__init__(*args, **params)
crc_valid = self.valid_crc crc_valid = self.valid_crc
if crc_valid: if crc_valid:
@ -318,13 +353,9 @@ class EventsResponse(UnknownResponse):
class DEBUG_DecodeAny(UnknownResponse): class DEBUG_DecodeAny(UnknownResponse):
"""Default decoder""" """Default decoder"""
def __init__(self, response):
"""
Try interpret and print unknown response data
:param bytes response: response payload bytes def __init__(self, *args, **params):
""" super().__init__(*args, **params)
self.response = response
crc_valid = self.valid_crc crc_valid = self.valid_crc
if crc_valid: if crc_valid:
@ -385,8 +416,8 @@ class DEBUG_DecodeAny(UnknownResponse):
# 1121-Series Intervers, 1 MPPT, 1 Phase # 1121-Series Intervers, 1 MPPT, 1 Phase
class HM300_Decode0B(StatusResponse): class HM300_Decode0B(StatusResponse):
def __init__(self, response): def __init__(self, *args, **params):
self.response = response super().__init__(*args, **params)
@property @property
def dc_voltage_0(self): def dc_voltage_0(self):
@ -422,19 +453,18 @@ class HM300_Decode0B(StatusResponse):
return self.unpack('>H', 26)[0]/10 return self.unpack('>H', 26)[0]/10
class HM300_Decode11(EventsResponse): class HM300_Decode11(EventsResponse):
def __init__(self, response): def __init__(self, *args, **params):
super().__init__(response) super().__init__(*args, **params)
class HM300_Decode12(EventsResponse): class HM300_Decode12(EventsResponse):
def __init__(self, response): def __init__(self, *args, **params):
super().__init__(response) super().__init__(*args, **params)
# 1141-Series Inverters, 2 MPPT, 1 Phase # 1141-Series Inverters, 2 MPPT, 1 Phase
class HM600_Decode0B(StatusResponse): class HM600_Decode0B(StatusResponse):
def __init__(self, response): def __init__(self, *args, **params):
self.response = response super().__init__(*args, **params)
@property @property
def dc_voltage_0(self): def dc_voltage_0(self):
@ -483,20 +513,23 @@ class HM600_Decode0B(StatusResponse):
@property @property
def temperature(self): def temperature(self):
return self.unpack('>H', 38)[0]/10 return self.unpack('>H', 38)[0]/10
@property
def alarm_count(self):
return self.unpack('>H', 40)[0]
class HM600_Decode11(EventsResponse): class HM600_Decode11(EventsResponse):
def __init__(self, response): def __init__(self, *args, **params):
super().__init__(response) super().__init__(*args, **params)
class HM600_Decode12(EventsResponse): class HM600_Decode12(EventsResponse):
def __init__(self, response): def __init__(self, *args, **params):
super().__init__(response) super().__init__(*args, **params)
# 1161-Series Inverters, 4 MPPT, 1 Phase # 1161-Series Inverters, 4 MPPT, 1 Phase
class HM1200_Decode0B(StatusResponse): class HM1200_Decode0B(StatusResponse):
def __init__(self, response): def __init__(self, *args, **params):
self.response = response super().__init__(*args, **params)
@property @property
def dc_voltage_0(self): def dc_voltage_0(self):
@ -579,9 +612,9 @@ class HM1200_Decode0B(StatusResponse):
return self.unpack('>H', 58)[0]/10 return self.unpack('>H', 58)[0]/10
class HM1200_Decode11(EventsResponse): class HM1200_Decode11(EventsResponse):
def __init__(self, response): def __init__(self, *args, **params):
super().__init__(response) super().__init__(*args, **params)
class HM1200_Decode12(EventsResponse): class HM1200_Decode12(EventsResponse):
def __init__(self, response): def __init__(self, *args, **params):
super().__init__(response) super().__init__(*args, **params)

Loading…
Cancel
Save