[snmp] nullness improvements (#9038)

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K 2020-11-16 02:24:02 +01:00 committed by GitHub
parent 1e5aaf948c
commit 09b0e3fb1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 39 deletions

View File

@ -100,18 +100,21 @@ public class SnmpServiceImpl implements SnmpService {
}
private void shutdownSnmp() throws IOException {
DefaultUdpTransportMapping transport = this.transport;
if (transport != null) {
transport.close();
transport = null;
this.transport = null;
}
Snmp snmp = this.snmp;
if (snmp != null) {
snmp.close();
snmp = null;
this.snmp = null;
}
}
@Override
public void addCommandResponder(CommandResponder listener) {
Snmp snmp = this.snmp;
if (snmp != null) {
snmp.addCommandResponder(listener);
}
@ -120,6 +123,7 @@ public class SnmpServiceImpl implements SnmpService {
@Override
public void removeCommandResponder(CommandResponder listener) {
Snmp snmp = this.snmp;
if (snmp != null) {
snmp.removeCommandResponder(listener);
}
@ -129,6 +133,7 @@ public class SnmpServiceImpl implements SnmpService {
@Override
public void send(PDU pdu, Target target, @Nullable Object userHandle, ResponseListener listener)
throws IOException {
Snmp snmp = this.snmp;
if (snmp != null) {
snmp.send(pdu, target, userHandle, listener);
logger.trace("send {} to {}", pdu, target);

View File

@ -207,9 +207,9 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe
logger.trace("{} received {}", thing.getUID(), response);
response.getVariableBindings().forEach(variable -> {
OID oid = variable.getOid();
Variable value = variable.getVariable();
updateChannels(oid, value, readChannelSet);
if (variable != null) {
updateChannels(variable.getOid(), variable.getVariable(), readChannelSet);
}
});
}
@ -237,9 +237,9 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe
if ((pdu.getType() == PDU.TRAP || pdu.getType() == PDU.V1TRAP) && config.community.equals(community)
&& targetAddressString.equals(address)) {
pdu.getVariableBindings().forEach(variable -> {
OID oid = variable.getOid();
Variable value = variable.getVariable();
updateChannels(oid, value, trapChannelSet);
if (variable != null) {
updateChannels(variable.getOid(), variable.getVariable(), trapChannelSet);
}
});
}
}
@ -247,60 +247,65 @@ public class SnmpTargetHandler extends BaseThingHandler implements ResponseListe
private @Nullable SnmpInternalChannelConfiguration getChannelConfigFromChannel(Channel channel) {
SnmpChannelConfiguration config = channel.getConfiguration().as(SnmpChannelConfiguration.class);
SnmpDatatype datatype;
String oid = config.oid;
if (oid == null) {
logger.warn("oid must not be null");
return null;
}
SnmpDatatype datatype = config.datatype; // maybe null, override later
Variable onValue = null;
Variable offValue = null;
State exceptionValue = UnDefType.UNDEF;
if (CHANNEL_TYPE_UID_NUMBER.equals(channel.getChannelTypeUID())) {
if (config.datatype == null) {
if (datatype == null) {
datatype = SnmpDatatype.INT32;
} else if (config.datatype == SnmpDatatype.IPADDRESS || config.datatype == SnmpDatatype.STRING) {
} else if (datatype == SnmpDatatype.IPADDRESS || datatype == SnmpDatatype.STRING) {
return null;
} else {
datatype = config.datatype;
}
if (config.exceptionValue != null) {
exceptionValue = DecimalType.valueOf(config.exceptionValue);
String configExceptionValue = config.exceptionValue;
if (configExceptionValue != null) {
exceptionValue = DecimalType.valueOf(configExceptionValue);
}
} else if (CHANNEL_TYPE_UID_STRING.equals(channel.getChannelTypeUID())) {
if (config.datatype == null) {
if (datatype == null) {
datatype = SnmpDatatype.STRING;
} else if (config.datatype != SnmpDatatype.IPADDRESS && config.datatype != SnmpDatatype.STRING
&& config.datatype != SnmpDatatype.HEXSTRING) {
} else if (datatype != SnmpDatatype.IPADDRESS && datatype != SnmpDatatype.STRING
&& datatype != SnmpDatatype.HEXSTRING) {
return null;
} else {
datatype = config.datatype;
}
if (config.exceptionValue != null) {
exceptionValue = StringType.valueOf(config.exceptionValue);
String configExceptionValue = config.exceptionValue;
if (configExceptionValue != null) {
exceptionValue = StringType.valueOf(configExceptionValue);
}
} else if (CHANNEL_TYPE_UID_SWITCH.equals(channel.getChannelTypeUID())) {
if (config.datatype == null) {
if (datatype == null) {
datatype = SnmpDatatype.UINT32;
} else {
datatype = config.datatype;
}
try {
if (config.onvalue != null) {
onValue = convertDatatype(new StringType(config.onvalue), config.datatype);
final String configOnValue = config.onvalue;
if (configOnValue != null) {
onValue = convertDatatype(new StringType(configOnValue), datatype);
}
if (config.offvalue != null) {
offValue = convertDatatype(new StringType(config.offvalue), config.datatype);
final String configOffValue = config.offvalue;
if (configOffValue != null) {
offValue = convertDatatype(new StringType(configOffValue), datatype);
}
} catch (IllegalArgumentException e) {
logger.warn("illegal value configuration for channel {}", channel.getUID());
return null;
}
if (config.exceptionValue != null) {
exceptionValue = OnOffType.from(config.exceptionValue);
String configExceptionValue = config.exceptionValue;
if (configExceptionValue != null) {
exceptionValue = OnOffType.from(configExceptionValue);
}
} else {
logger.warn("unknown channel type found for channel {}", channel.getUID());
return null;
}
return new SnmpInternalChannelConfiguration(channel.getUID(), new OID(config.oid), config.mode, datatype,
onValue, offValue, exceptionValue, config.doNotLogException);
return new SnmpInternalChannelConfiguration(channel.getUID(), new OID(oid), config.mode, datatype, onValue,
offValue, exceptionValue, config.doNotLogException);
}
private void generateChannelConfigs() {

View File

@ -12,6 +12,8 @@
*/
package org.openhab.binding.snmp.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.snmp.internal.SnmpChannelMode;
import org.openhab.binding.snmp.internal.SnmpDatatype;
@ -20,14 +22,15 @@ import org.openhab.binding.snmp.internal.SnmpDatatype;
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class SnmpChannelConfiguration {
public String oid;
public @Nullable String oid;
public SnmpChannelMode mode = SnmpChannelMode.READ;
public SnmpDatatype datatype;
public @Nullable SnmpDatatype datatype;
public String onvalue;
public String offvalue;
public String exceptionValue;
public @Nullable String onvalue;
public @Nullable String offvalue;
public @Nullable String exceptionValue;
public boolean doNotLogException = false;
}

View File

@ -12,11 +12,14 @@
*/
package org.openhab.binding.snmp.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* The {@link SnmpServiceConfiguration} class contains fields mapping binding configuration parameters.
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class SnmpServiceConfiguration {
public int port = 0;
}

View File

@ -12,6 +12,8 @@
*/
package org.openhab.binding.snmp.internal.config;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.snmp.internal.SnmpProtocolVersion;
/**
@ -19,8 +21,9 @@ import org.openhab.binding.snmp.internal.SnmpProtocolVersion;
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class SnmpTargetConfiguration {
public String hostname;
public @Nullable String hostname;
public int port = 161;
public String community = "public";
public int refresh = 60;