Search in sources :

Example 1 with DeviceSessionContext

use of org.thingsboard.server.transport.snmp.session.DeviceSessionContext in project thingsboard by thingsboard.

the class SnmpTransportContext method establishDeviceSession.

private void establishDeviceSession(Device device) {
    if (device == null)
        return;
    log.info("Establishing SNMP session for device {}", device.getId());
    DeviceProfileId deviceProfileId = device.getDeviceProfileId();
    DeviceProfile deviceProfile = deviceProfileCache.get(deviceProfileId);
    DeviceCredentials credentials = protoEntityService.getDeviceCredentialsByDeviceId(device.getId());
    if (credentials.getCredentialsType() != DeviceCredentialsType.ACCESS_TOKEN) {
        log.warn("[{}] Expected credentials type is {} but found {}", device.getId(), DeviceCredentialsType.ACCESS_TOKEN, credentials.getCredentialsType());
        return;
    }
    SnmpDeviceProfileTransportConfiguration profileTransportConfiguration = (SnmpDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
    SnmpDeviceTransportConfiguration deviceTransportConfiguration = (SnmpDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
    DeviceSessionContext deviceSessionContext;
    try {
        deviceSessionContext = new DeviceSessionContext(device, deviceProfile, credentials.getCredentialsId(), profileTransportConfiguration, deviceTransportConfiguration, this);
        registerSessionMsgListener(deviceSessionContext);
    } catch (Exception e) {
        log.error("Failed to establish session for SNMP device {}: {}", device.getId(), e.toString());
        return;
    }
    sessions.put(device.getId(), deviceSessionContext);
    snmpTransportService.createQueryingTasks(deviceSessionContext);
    log.info("Established SNMP device session for device {}", device.getId());
}
Also used : SnmpDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) DeviceSessionContext(org.thingsboard.server.transport.snmp.session.DeviceSessionContext) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 2 with DeviceSessionContext

use of org.thingsboard.server.transport.snmp.session.DeviceSessionContext in project thingsboard by thingsboard.

the class SnmpTransportContext method onDeviceUpdatedOrCreated.

@EventListener(DeviceUpdatedEvent.class)
public void onDeviceUpdatedOrCreated(DeviceUpdatedEvent deviceUpdatedEvent) {
    Device device = deviceUpdatedEvent.getDevice();
    log.trace("Got creating or updating device event for device {}", device);
    DeviceTransportType transportType = Optional.ofNullable(device.getDeviceData().getTransportConfiguration()).map(DeviceTransportConfiguration::getType).orElse(null);
    if (!allSnmpDevicesIds.contains(device.getId())) {
        if (transportType != DeviceTransportType.SNMP) {
            return;
        }
        allSnmpDevicesIds.add(device.getId());
        if (balancingService.isManagedByCurrentTransport(device.getId().getId())) {
            establishDeviceSession(device);
        }
    } else {
        if (balancingService.isManagedByCurrentTransport(device.getId().getId())) {
            DeviceSessionContext sessionContext = sessions.get(device.getId());
            if (transportType == DeviceTransportType.SNMP) {
                if (sessionContext != null) {
                    updateDeviceSession(sessionContext, device, deviceProfileCache.get(device.getDeviceProfileId()));
                } else {
                    establishDeviceSession(device);
                }
            } else {
                log.trace("Transport type was changed to {}", transportType);
                destroyDeviceSession(sessionContext);
            }
        }
    }
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceSessionContext(org.thingsboard.server.transport.snmp.session.DeviceSessionContext) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) EventListener(org.springframework.context.event.EventListener)

Example 3 with DeviceSessionContext

use of org.thingsboard.server.transport.snmp.session.DeviceSessionContext 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

DeviceSessionContext (org.thingsboard.server.transport.snmp.session.DeviceSessionContext)3 SnmpDeviceTransportConfiguration (org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration)2 JsonObject (com.google.gson.JsonObject)1 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 PDU (org.snmp4j.PDU)1 ScopedPDU (org.snmp4j.ScopedPDU)1 Integer32 (org.snmp4j.smi.Integer32)1 Null (org.snmp4j.smi.Null)1 OID (org.snmp4j.smi.OID)1 OctetString (org.snmp4j.smi.OctetString)1 Variable (org.snmp4j.smi.Variable)1 VariableBinding (org.snmp4j.smi.VariableBinding)1 EventListener (org.springframework.context.event.EventListener)1