mirror of
https://github.com/danieldemus/openhab-addons
synced 2026-07-31 13:34:22 +02:00
[sungrow] Added configuration option for modbus read retries (#19186)
* Added configuration option for modbus retries. Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
This commit is contained in:
@@ -50,9 +50,10 @@ You just have to select the configured bridge and optional configure the polling
|
||||
|
||||
### Sungrow Inverter (`sungrow-inverter`)
|
||||
|
||||
| Name | Type | Description | Default | Required | Advanced |
|
||||
|--------------|---------|--------------------------------------|---------|----------|----------|
|
||||
| pollInterval | integer | Interval the device is polled in ms. | 5000 | yes | no |
|
||||
| Name | Type | Description | Default | Required | Advanced |
|
||||
|---------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------|---------|----------|----------|
|
||||
| pollInterval | integer | Interval the device is polled in ms. | 5000 | yes | no |
|
||||
| maxTries | integer | Specifies how many times the binding should retry reading data if a read attempt fails. <br/>Set to `1` to disable retries and use a single attempt. | 3 | yes | no |
|
||||
|
||||
## Channels
|
||||
|
||||
|
||||
+1
@@ -23,4 +23,5 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
public class SungrowInverterConfiguration {
|
||||
|
||||
public int pollInterval;
|
||||
public int maxTries;
|
||||
}
|
||||
|
||||
+15
-10
@@ -49,12 +49,13 @@ public class SungrowInverterHandler extends BaseModbusThingHandler {
|
||||
private final Deque<SungrowInverterRegisters> registers;
|
||||
private final ModbusReadRequestBlueprint blueprint;
|
||||
|
||||
public ModbusRequest(Deque<SungrowInverterRegisters> registers, int slaveId) {
|
||||
public ModbusRequest(Deque<SungrowInverterRegisters> registers, int slaveId, int tries) {
|
||||
this.registers = registers;
|
||||
this.blueprint = initReadRequest(registers, slaveId);
|
||||
this.blueprint = initReadRequest(registers, slaveId, tries);
|
||||
}
|
||||
|
||||
private ModbusReadRequestBlueprint initReadRequest(Deque<SungrowInverterRegisters> registers, int slaveId) {
|
||||
private ModbusReadRequestBlueprint initReadRequest(Deque<SungrowInverterRegisters> registers, int slaveId,
|
||||
int tries) {
|
||||
int firstRegister = registers.getFirst().getRegisterNumber();
|
||||
int lastRegister = registers.getLast().getRegisterNumber();
|
||||
int length = lastRegister - firstRegister + registers.getLast().getRegisterCount();
|
||||
@@ -65,14 +66,13 @@ public class SungrowInverterHandler extends BaseModbusThingHandler {
|
||||
ModbusReadFunctionCode.READ_INPUT_REGISTERS, //
|
||||
firstRegister - 1, //
|
||||
length, //
|
||||
TRIES //
|
||||
tries //
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(SungrowInverterHandler.class);
|
||||
|
||||
private static final int TRIES = 1;
|
||||
private List<ModbusRequest> modbusRequests = new ArrayList<>();
|
||||
|
||||
public SungrowInverterHandler(Thing thing) {
|
||||
@@ -82,7 +82,7 @@ public class SungrowInverterHandler extends BaseModbusThingHandler {
|
||||
/**
|
||||
* Splits the SungrowInverterRegisters into multiple ModbusRequest, to ensure the max request size.
|
||||
*/
|
||||
private List<ModbusRequest> buildRequests() {
|
||||
private List<ModbusRequest> buildRequests(int tries) {
|
||||
final List<ModbusRequest> requests = new ArrayList<>();
|
||||
Deque<SungrowInverterRegisters> currentRequest = new ArrayDeque<>();
|
||||
int currentRequestFirstRegister = 0;
|
||||
@@ -96,7 +96,7 @@ public class SungrowInverterHandler extends BaseModbusThingHandler {
|
||||
int sizeWithRegisterAdded = channel.getRegisterNumber() - currentRequestFirstRegister
|
||||
+ channel.getRegisterCount();
|
||||
if (sizeWithRegisterAdded > ModbusConstants.MAX_REGISTERS_READ_COUNT) {
|
||||
requests.add(new ModbusRequest(currentRequest, getSlaveId()));
|
||||
requests.add(new ModbusRequest(currentRequest, getSlaveId(), tries));
|
||||
currentRequest = new ArrayDeque<>();
|
||||
|
||||
currentRequest.add(channel);
|
||||
@@ -108,7 +108,7 @@ public class SungrowInverterHandler extends BaseModbusThingHandler {
|
||||
}
|
||||
|
||||
if (!currentRequest.isEmpty()) {
|
||||
requests.add(new ModbusRequest(currentRequest, getSlaveId()));
|
||||
requests.add(new ModbusRequest(currentRequest, getSlaveId(), tries));
|
||||
}
|
||||
logger.debug("Created {} modbus request templates.", requests.size());
|
||||
return requests;
|
||||
@@ -137,9 +137,14 @@ public class SungrowInverterHandler extends BaseModbusThingHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
this.updateStatus(ThingStatus.UNKNOWN);
|
||||
if (config.maxTries <= 0) {
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
||||
"Invalid Maximum Tries When Reading: " + config.maxTries);
|
||||
return;
|
||||
}
|
||||
|
||||
this.modbusRequests = this.buildRequests();
|
||||
this.updateStatus(ThingStatus.UNKNOWN);
|
||||
this.modbusRequests = this.buildRequests(config.maxTries);
|
||||
|
||||
for (ModbusRequest request : modbusRequests) {
|
||||
registerRegularPoll( //
|
||||
|
||||
@@ -10,6 +10,13 @@
|
||||
<description>Time between polling the data in ms.</description>
|
||||
<default>5000</default>
|
||||
</parameter>
|
||||
<parameter name="maxTries" type="integer" min="1">
|
||||
<label>Maximum Tries When Reading</label>
|
||||
<default>3</default>
|
||||
<description>Specifies how many times the binding should retry reading data if a read attempt fails.
|
||||
Set to `1` to
|
||||
disable retries and use a single attempt.</description>
|
||||
</parameter>
|
||||
</config-description>
|
||||
|
||||
</config-description:config-descriptions>
|
||||
|
||||
+2
-1
@@ -4,7 +4,8 @@ thing-type.modbus.sungrowInverter.label = Sungrow inverter
|
||||
thing-type.modbus.sungrowInverter.description = Sungrow inverter connected via Modbus.
|
||||
|
||||
# thing types config
|
||||
|
||||
thing-type.config.sungrow.inverter.maxTries.label = Maximum Tries When Reading
|
||||
thing-type.config.sungrow.inverter.maxTries.description = Specifies how many times the binding should retry reading data if a read attempt fails. Set to `1` to disable retries and use a single attempt.
|
||||
thing-type.config.sungrow.inverter.pollInterval.label = Poll Interval
|
||||
thing-type.config.sungrow.inverter.pollInterval.description = Time between polling the data in ms.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user