Search in sources :

Example 1 with SnmpCommunicationConfig

use of org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig in project thingsboard by thingsboard.

the class SnmpTransportService method onToDeviceRpcRequest.

public void onToDeviceRpcRequest(DeviceSessionContext sessionContext, TransportProtos.ToDeviceRpcRequestMsg toDeviceRpcRequestMsg) {
    SnmpMethod snmpMethod = SnmpMethod.valueOf(toDeviceRpcRequestMsg.getMethodName());
    JsonObject params = JsonConverter.parse(toDeviceRpcRequestMsg.getParams()).getAsJsonObject();
    String key = Optional.ofNullable(params.get("key")).map(JsonElement::getAsString).orElse(null);
    String value = Optional.ofNullable(params.get("value")).map(JsonElement::getAsString).orElse(null);
    if (value == null && snmpMethod == SnmpMethod.SET) {
        throw new IllegalArgumentException("Value must be specified for SNMP method 'SET'");
    }
    SnmpCommunicationConfig communicationConfig = sessionContext.getProfileTransportConfiguration().getCommunicationConfigs().stream().filter(config -> config.getSpec() == SnmpCommunicationSpec.TO_DEVICE_RPC_REQUEST).findFirst().orElseThrow(() -> new IllegalArgumentException("No communication config found with RPC spec"));
    SnmpMapping snmpMapping = communicationConfig.getAllMappings().stream().filter(mapping -> mapping.getKey().equals(key)).findFirst().orElseThrow(() -> new IllegalArgumentException("No SNMP mapping found in the config for specified key"));
    String oid = snmpMapping.getOid();
    DataType dataType = snmpMapping.getDataType();
    PDU request = pduService.createSingleVariablePdu(sessionContext, snmpMethod, oid, value, dataType);
    RequestInfo requestInfo = new RequestInfo(toDeviceRpcRequestMsg.getRequestId(), communicationConfig.getSpec(), communicationConfig.getAllMappings());
    sendRequest(sessionContext, request, requestInfo);
}
Also used : SnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig) RepeatingQueryingSnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.RepeatingQueryingSnmpCommunicationConfig) SnmpMapping(org.thingsboard.server.common.data.transport.snmp.SnmpMapping) PDU(org.snmp4j.PDU) SnmpMethod(org.thingsboard.server.common.data.transport.snmp.SnmpMethod) JsonObject(com.google.gson.JsonObject) DataType(org.thingsboard.server.common.data.kv.DataType) OctetString(org.snmp4j.smi.OctetString)

Example 2 with SnmpCommunicationConfig

use of org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig in project thingsboard by thingsboard.

the class PduService method createPdu.

public PDU createPdu(DeviceSessionContext sessionContext, SnmpCommunicationConfig communicationConfig, Map<String, String> values) {
    PDU pdu = setUpPdu(sessionContext);
    pdu.setType(communicationConfig.getMethod().getCode());
    pdu.addAll(communicationConfig.getAllMappings().stream().filter(mapping -> values.isEmpty() || values.containsKey(mapping.getKey())).map(mapping -> Optional.ofNullable(values.get(mapping.getKey())).map(value -> {
        Variable variable = toSnmpVariable(value, mapping.getDataType());
        return new VariableBinding(new OID(mapping.getOid()), variable);
    }).orElseGet(() -> new VariableBinding(new OID(mapping.getOid())))).collect(Collectors.toList()));
    return pdu;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) ScopedPDU(org.snmp4j.ScopedPDU) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) HashMap(java.util.HashMap) SnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig) PDU(org.snmp4j.PDU) DataType(org.thingsboard.server.common.data.kv.DataType) Service(org.springframework.stereotype.Service) VariableBinding(org.snmp4j.smi.VariableBinding) Map(java.util.Map) Null(org.snmp4j.smi.Null) SnmpProtocolVersion(org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion) SnmpMapping(org.thingsboard.server.common.data.transport.snmp.SnmpMapping) Integer32(org.snmp4j.smi.Integer32) OctetString(org.snmp4j.smi.OctetString) Collectors(java.util.stream.Collectors) SnmpMethod(org.thingsboard.server.common.data.transport.snmp.SnmpMethod) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TbSnmpTransportComponent(org.thingsboard.server.queue.util.TbSnmpTransportComponent) Optional(java.util.Optional) Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) DeviceSessionContext(org.thingsboard.server.transport.snmp.session.DeviceSessionContext) Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Aggregations

JsonObject (com.google.gson.JsonObject)2 PDU (org.snmp4j.PDU)2 OctetString (org.snmp4j.smi.OctetString)2 DataType (org.thingsboard.server.common.data.kv.DataType)2 SnmpMapping (org.thingsboard.server.common.data.transport.snmp.SnmpMapping)2 SnmpMethod (org.thingsboard.server.common.data.transport.snmp.SnmpMethod)2 SnmpCommunicationConfig (org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Slf4j (lombok.extern.slf4j.Slf4j)1 ScopedPDU (org.snmp4j.ScopedPDU)1 Integer32 (org.snmp4j.smi.Integer32)1 Null (org.snmp4j.smi.Null)1 OID (org.snmp4j.smi.OID)1 Variable (org.snmp4j.smi.Variable)1