Search in sources :

Example 1 with SnmpMethod

use of org.thingsboard.server.common.data.transport.snmp.SnmpMethod 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)

Aggregations

JsonObject (com.google.gson.JsonObject)1 PDU (org.snmp4j.PDU)1 OctetString (org.snmp4j.smi.OctetString)1 DataType (org.thingsboard.server.common.data.kv.DataType)1 SnmpMapping (org.thingsboard.server.common.data.transport.snmp.SnmpMapping)1 SnmpMethod (org.thingsboard.server.common.data.transport.snmp.SnmpMethod)1 RepeatingQueryingSnmpCommunicationConfig (org.thingsboard.server.common.data.transport.snmp.config.RepeatingQueryingSnmpCommunicationConfig)1 SnmpCommunicationConfig (org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig)1