Search in sources :

Example 1 with SnmpBindingProvider

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);
    }
}
Also used : PDU(org.snmp4j.PDU) Integer32(org.snmp4j.smi.Integer32) Variable(org.snmp4j.smi.Variable) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 2 with SnmpBindingProvider

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());
                }
            }
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) TransformationException(org.openhab.core.transform.TransformationException) Variable(org.snmp4j.smi.Variable) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) State(org.openhab.core.types.State) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID)

Example 3 with SnmpBindingProvider

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());
            }
        }
    }
}
Also used : PDU(org.snmp4j.PDU) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) OctetString(org.snmp4j.smi.OctetString) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 4 with SnmpBindingProvider

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);
}
Also used : SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) OctetString(org.snmp4j.smi.OctetString)

Aggregations

SnmpBindingProvider (org.openhab.binding.snmp.SnmpBindingProvider)4 OctetString (org.snmp4j.smi.OctetString)3 CommunityTarget (org.snmp4j.CommunityTarget)2 PDU (org.snmp4j.PDU)2 OID (org.snmp4j.smi.OID)2 Variable (org.snmp4j.smi.Variable)2 VariableBinding (org.snmp4j.smi.VariableBinding)2 NumberItem (org.openhab.core.library.items.NumberItem)1 TransformationException (org.openhab.core.transform.TransformationException)1 State (org.openhab.core.types.State)1 Integer32 (org.snmp4j.smi.Integer32)1