use of org.snmp4j.smi.Variable in project openhab1-addons by openhab.
the class SnmpBinding method dispatchPdu.
private void dispatchPdu(Address address, PDU pdu) {
if (pdu != null & address != null) {
logger.debug("Received PDU from '{}' '{}'", address, pdu);
for (SnmpBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
// Check the IP address
if (!provider.getAddress(itemName).equals(address)) {
continue;
}
// Check the OID
OID oid = provider.getOID(itemName);
Variable variable = pdu.getVariable(oid);
if (variable != null) {
Class<? extends Item> itemType = provider.getItemType(itemName);
// Do any transformations
String value = variable.toString();
try {
value = provider.doTransformation(itemName, value);
} catch (TransformationException e) {
logger.error("Transformation error with item {}: {}", itemName, e);
}
// Change to a state
State state = null;
if (itemType.isAssignableFrom(StringItem.class)) {
state = StringType.valueOf(value);
} else if (itemType.isAssignableFrom(NumberItem.class)) {
state = DecimalType.valueOf(value);
} else if (itemType.isAssignableFrom(SwitchItem.class)) {
state = OnOffType.valueOf(value);
}
if (state != null) {
eventPublisher.postUpdate(itemName, state);
} else {
logger.debug("'{}' couldn't be parsed to a State. Valid State-Types are String and Number", variable.toString());
}
} else {
logger.trace("PDU doesn't contain a variable with OID '{}'", oid.toString());
}
}
}
}
}
use of org.snmp4j.smi.Variable in project opennms by OpenNMS.
the class MapSubAgent method get.
@Override
public VariableBinding get(OID requested) {
if (!m_values.containsKey(requested)) {
return null;
}
OID response = new OID(requested);
Variable value = m_values.get(response);
return new VariableBinding(response, value);
}
use of org.snmp4j.smi.Variable in project opennms by OpenNMS.
the class MapSubAgent method getNext.
@Override
public VariableBinding getNext(OID requested) {
OID successor = nextOID(requested);
SortedMap<OID, Variable> tailMap = m_values.tailMap(successor);
if (tailMap.isEmpty()) {
return null;
}
OID next = tailMap.firstKey();
Variable value = tailMap.get(next);
return new VariableBinding(next, value);
}
use of org.snmp4j.smi.Variable in project opennms by OpenNMS.
the class PropertiesBackedManagedObject method commit.
/** {@inheritDoc} */
@Override
public void commit(final SubRequest request) {
final VariableBinding vb = request.getVariableBinding();
final Variable v = vb.getVariable();
m_vars.put(vb.getOid(), v);
final RequestStatus status = request.getStatus();
status.setPhaseComplete(true);
}
use of org.snmp4j.smi.Variable in project opennms by OpenNMS.
the class PropertiesBackedManagedObject method getVariableFromValueString.
/**
* <p>getVariableFromValueString</p>
*
* @param oidStr a {@link java.lang.String} object.
* @param valStr a {@link java.lang.String} object.
* @return a {@link org.snmp4j.smi.Variable} object.
* @throws SnmpErrorStatusException
*/
private Variable getVariableFromValueString(String oidStr, String valStr) throws SnmpErrorStatusException {
Variable newVar;
if (valStr.startsWith("Wrong Type")) {
String newVal = valStr.replaceFirst("Wrong Type \\(should be .*\\): ", "");
s_log.error("Bad Mib walk has value: '" + valStr + "' using '" + newVal + "'");
valStr = newVal;
}
if ("\"\"".equals(valStr)) {
newVar = new Null();
} else {
String moTypeStr = valStr.substring(0, valStr.indexOf(':'));
String moValStr = valStr.substring(valStr.indexOf(':') + 2);
try {
if (moTypeStr.equals("STRING")) {
if (moValStr.startsWith("\"") && moValStr.endsWith("\"")) {
moValStr = moValStr.substring(1, moValStr.length() - 1);
}
newVar = new OctetString(moValStr);
} else if (moTypeStr.equals("Hex-STRING")) {
newVar = OctetString.fromHexString(moValStr.trim().replace(' ', ':'));
} else if (moTypeStr.equals("INTEGER")) {
newVar = new Integer32(Integer.parseInt(moValStr));
} else if (moTypeStr.equals("Gauge32")) {
newVar = new Gauge32(Long.parseLong(moValStr));
} else if (moTypeStr.equals("Counter32")) {
// a 32 bit counter can be > 2 ^ 31, which is > INTEGER_MAX
newVar = new Counter32(Long.parseLong(moValStr));
} else if (moTypeStr.equals("Counter64")) {
newVar = new Counter64(Long.parseLong(moValStr));
} else if (moTypeStr.equals("Timeticks")) {
Integer ticksInt = Integer.parseInt(moValStr.substring(moValStr.indexOf('(') + 1, moValStr.indexOf(')')));
newVar = new TimeTicks(ticksInt);
} else if (moTypeStr.equals("OID")) {
newVar = new OID(moValStr);
} else if (moTypeStr.equals("IpAddress")) {
newVar = new IpAddress(moValStr.trim());
} else if (moTypeStr.equals("Network Address")) {
newVar = OctetString.fromHexString(moValStr.trim());
} else if (moTypeStr.equals("Responder")) {
newVar = handleDynamicVariable(oidStr, moValStr);
} else {
//newVar = new OctetString(moValStr);
throw new IllegalArgumentException("Unrecognized SNMP Type " + moTypeStr);
}
} catch (SnmpErrorStatusException e) {
throw e;
} catch (Throwable t) {
throw new UndeclaredThrowableException(t, "Could not convert value '" + moValStr + "' of type '" + moTypeStr + "' to SNMP object for OID " + oidStr);
}
}
return newVar;
}
Aggregations