use of org.openhab.binding.snmp.SnmpBindingProvider in project openhab1-addons by openhab.
the class SnmpBinding method internalReceiveCommand.
/**
* @{inheritDoc
*/
@Override
public void internalReceiveCommand(String itemName, Command command) {
logger.debug("SNMP receive command {} from {}", itemName, command);
SnmpBindingProvider providerCmd = null;
for (SnmpBindingProvider provider : this.providers) {
OID oid = provider.getOID(itemName, command);
if (oid != null) {
providerCmd = provider;
break;
}
}
if (providerCmd == null) {
logger.warn("No match for binding provider [itemName={}, command={}]", itemName, command);
return;
}
logger.debug("SNMP command for {} to {}", itemName, providerCmd.toString());
// Set up the target
CommunityTarget target = new CommunityTarget();
target.setCommunity(providerCmd.getCommunity(itemName, command));
target.setAddress(providerCmd.getAddress(itemName, command));
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(providerCmd.getSnmpVersion(itemName, command));
Variable var = providerCmd.getValue(itemName, command);
OID oid = providerCmd.getOID(itemName, command);
VariableBinding varBind = new VariableBinding(oid, var);
// Create the PDU
PDU pdu = new PDU();
pdu.add(varBind);
pdu.setType(PDU.SET);
pdu.setRequestID(new Integer32(1));
logger.debug("SNMP: Send CMD PDU {} {}", providerCmd.getAddress(itemName, command), pdu);
if (snmp == null) {
logger.error("SNMP: snmp not initialised - aborting request");
} else {
sendPDU(target, pdu);
}
}
use of org.openhab.binding.snmp.SnmpBindingProvider 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.openhab.binding.snmp.SnmpBindingProvider in project openhab1-addons by openhab.
the class SnmpBinding method execute.
/**
* @{inheritDoc
*/
@Override
public void execute() {
for (SnmpBindingProvider provider : providers) {
for (String itemName : provider.getInBindingItemNames()) {
int refreshInterval = provider.getRefreshInterval(itemName);
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean needsUpdate;
if (refreshInterval == 0) {
needsUpdate = false;
} else {
needsUpdate = age >= refreshInterval;
}
if (needsUpdate) {
logger.debug("Item '{}' is about to be refreshed", itemName);
// Set up the target
CommunityTarget target = new CommunityTarget();
target.setCommunity(provider.getCommunity(itemName));
target.setAddress(provider.getAddress(itemName));
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(provider.getSnmpVersion(itemName));
// Create the PDU
PDU pdu = new PDU();
pdu.add(new VariableBinding(provider.getOID(itemName)));
pdu.setType(PDU.GET);
logger.debug("SNMP: Send PDU {} {}", provider.getAddress(itemName), pdu);
if (snmp == null) {
logger.error("SNMP: snmp not initialised - aborting request");
} else {
sendPDU(target, pdu);
}
lastUpdateMap.put(itemName, System.currentTimeMillis());
}
}
}
}
use of org.openhab.binding.snmp.SnmpBindingProvider in project openhab1-addons by openhab.
the class SnmpBinding method updated.
/**
* {@inheritDoc}
*/
@Override
public void updated(Dictionary<String, ?> config) throws ConfigurationException {
boolean mapping = false;
stopListening();
if (config != null) {
mapping = true;
SnmpBinding.community = (String) config.get("community");
if (StringUtils.isBlank(SnmpBinding.community)) {
SnmpBinding.community = "public";
logger.info("didn't find SNMP community configuration -> listen to SNMP community {}", SnmpBinding.community);
}
String portString = (String) config.get("port");
if (StringUtils.isNotBlank(portString) && portString.matches("\\d*")) {
SnmpBinding.port = Integer.valueOf(portString).intValue();
} else {
SnmpBinding.port = SNMP_DEFAULT_PORT;
logger.info("Didn't find SNMP port configuration or configuration is invalid -> listen to SNMP default port {}", SnmpBinding.port);
}
String timeoutString = (String) config.get("timeout");
if (StringUtils.isNotBlank(timeoutString)) {
SnmpBinding.timeout = Integer.valueOf(timeoutString).intValue();
if (SnmpBinding.timeout < 0 | SnmpBinding.retries > 5) {
logger.info("SNMP timeout value is invalid (" + SnmpBinding.timeout + "). Using default value.");
SnmpBinding.timeout = 1500;
}
} else {
SnmpBinding.timeout = 1500;
logger.info("Didn't find SNMP timeout or configuration is invalid -> timeout set to {}", SnmpBinding.timeout);
}
String retriesString = (String) config.get("retries");
if (StringUtils.isNotBlank(retriesString)) {
SnmpBinding.retries = Integer.valueOf(retriesString).intValue();
if (SnmpBinding.retries < 0 | SnmpBinding.retries > 5) {
logger.info("SNMP retries value is invalid (" + SnmpBinding.retries + "). Using default value.");
SnmpBinding.retries = 0;
}
} else {
SnmpBinding.retries = 0;
logger.info("Didn't find SNMP retries or configuration is invalid -> retries set to {}", SnmpBinding.retries);
}
}
for (SnmpBindingProvider provider : providers) {
if (provider.getInBindingItemNames() != null) {
mapping = true;
}
}
// Did we find either a trap request, or any bindings
if (mapping) {
listen();
}
setProperlyConfigured(true);
}
Aggregations