|
13 | 13 |
|
14 | 14 |
|
15 | 15 | class KwargsDict(TypedDict): |
16 | | - device_id: int |
17 | 16 | client: ModbusTcpClient_ |
18 | 17 |
|
19 | 18 |
|
20 | 19 | class CHINTCounter(AbstractCounter): |
21 | 20 | def __init__(self, component_config: CHINTCounterSetup, **kwargs: Any) -> None: |
22 | 21 | self.component_config = component_config |
23 | 22 | self.kwargs: KwargsDict = kwargs |
24 | | - self.__modbus_id = component_config.configuration.modbus_id |
25 | | - self.invert = component_config.configuration.invert |
26 | 23 |
|
27 | 24 | def initialize(self) -> None: |
28 | | - self.__device_id: int = self.kwargs['device_id'] |
29 | 25 | self.client: ModbusTcpClient_ = self.kwargs['client'] |
30 | 26 | self.store = get_counter_value_store(self.component_config.id) |
31 | 27 | self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config)) |
| 28 | + self.__modbus_id = self.component_config.configuration.modbus_id |
32 | 29 |
|
33 | 30 | def update(self): |
34 | 31 | powers = voltages = currents = power_factors = None |
35 | 32 | imported_ep = exported_ep = power = frequency = 0 |
36 | | - try: |
37 | | - irat = self.client.read_holding_registers(0x0006, ModbusDataType.INT_16, unit=self.__modbus_id) |
38 | | - urat = self.client.read_holding_registers(0x0007, ModbusDataType.INT_16, unit=self.__modbus_id) |
39 | | - power_ratio = urat*0.1*irat*0.1 |
40 | | - if self.invert: |
41 | | - power_ratio = power_ratio * -1 |
42 | | - frequency = self.client.read_holding_registers(0x2044, ModbusDataType.FLOAT_32, unit=self.__modbus_id)/100 |
43 | | - power = self.client.read_holding_registers(0x2012, |
44 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * power_ratio |
45 | | - powers = [ |
46 | | - self.client.read_holding_registers(0x2014, |
47 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * power_ratio, |
48 | | - self.client.read_holding_registers(0x2016, |
49 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * power_ratio, |
50 | | - self.client.read_holding_registers(0x2018, |
| 33 | + irat = self.client.read_holding_registers(0x0006, ModbusDataType.INT_16, unit=self.__modbus_id) |
| 34 | + urat = self.client.read_holding_registers(0x0007, ModbusDataType.INT_16, unit=self.__modbus_id) |
| 35 | + power_ratio = urat*0.1*irat*0.1 |
| 36 | + |
| 37 | + frequency = self.client.read_holding_registers(0x2044, ModbusDataType.FLOAT_32, unit=self.__modbus_id)/100 |
| 38 | + power = self.client.read_holding_registers(0x2012, |
51 | 39 | ModbusDataType.FLOAT_32, unit=self.__modbus_id) * power_ratio |
52 | | - ] |
53 | | - voltage_ratio = urat*0.1*0.1 |
54 | | - voltages = [ |
55 | | - self.client.read_holding_registers(0x2006, |
56 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * voltage_ratio, |
57 | | - self.client.read_holding_registers(0x2008, |
58 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * voltage_ratio, |
59 | | - self.client.read_holding_registers(0x200A, |
60 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * voltage_ratio |
61 | | - ] |
62 | | - current_ratio = irat*0.001 |
63 | | - currents = [ |
64 | | - self.client.read_holding_registers(0x200C, |
65 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * current_ratio, |
66 | | - self.client.read_holding_registers(0x200E, |
67 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * current_ratio, |
68 | | - self.client.read_holding_registers(0x2010, |
69 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * current_ratio |
70 | | - ] |
71 | | - power_factors = [ |
72 | | - self.client.read_holding_registers(0x202C, ModbusDataType.FLOAT_32, unit=self.__modbus_id) * 0.001, |
73 | | - self.client.read_holding_registers(0x202E, ModbusDataType.FLOAT_32, unit=self.__modbus_id) * 0.001, |
74 | | - self.client.read_holding_registers(0x2030, ModbusDataType.FLOAT_32, unit=self.__modbus_id) * 0.001 |
75 | | - ] |
76 | | - ep_ratio = irat * urat * 100 |
77 | | - imported_ep = self.client.read_holding_registers(0x401E, |
78 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * ep_ratio |
79 | | - exported_ep = self.client.read_holding_registers(0x4028, |
80 | | - ModbusDataType.FLOAT_32, unit=self.__modbus_id) * ep_ratio |
81 | | - if self.invert: |
82 | | - imported_ep, exported_ep = exported_ep, imported_ep |
83 | | - |
84 | | - except Exception: |
85 | | - log.debug("Modbus could not be read.") |
| 40 | + powers = [self.client.read_holding_registers(reg, ModbusDataType.FLOAT_32, unit=self.__modbus_id) * power_ratio |
| 41 | + for reg in [0x2014, 0x2016, 0x2018]] |
| 42 | + voltage_ratio = urat*0.1*0.1 |
| 43 | + voltages = [self.client.read_holding_registers( |
| 44 | + reg, ModbusDataType.FLOAT_32, unit=self.__modbus_id) * voltage_ratio |
| 45 | + for reg in [0x2006, 0x2008, 0x200A]] |
| 46 | + current_ratio = irat*0.001 |
| 47 | + currents = [self.client.read_holding_registers( |
| 48 | + reg, ModbusDataType.FLOAT_32, unit=self.__modbus_id) * current_ratio |
| 49 | + for reg in [0x200C, 0x200E, 0x2010]] |
| 50 | + power_factors = [self.client.read_holding_registers(reg, ModbusDataType.FLOAT_32, unit=self.__modbus_id) * 0.001 |
| 51 | + for reg in [0x202C, 0x202E, 0x2030]] |
| 52 | + ep_ratio = irat * urat * 100 |
| 53 | + imported_ep = self.client.read_holding_registers(0x401E, |
| 54 | + ModbusDataType.FLOAT_32, unit=self.__modbus_id) * ep_ratio |
| 55 | + exported_ep = self.client.read_holding_registers(0x4028, |
| 56 | + ModbusDataType.FLOAT_32, unit=self.__modbus_id) * ep_ratio |
86 | 57 |
|
87 | 58 | counter_state = CounterState( |
88 | 59 | currents=currents, |
|
0 commit comments