Search in sources :

Example 1 with SnmpDeviceProfileTransportConfiguration

use of org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration 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 SnmpDeviceProfileTransportConfiguration

use of org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration in project thingsboard by thingsboard.

the class SnmpTransportContext method updateDeviceSession.

private void updateDeviceSession(DeviceSessionContext sessionContext, Device device, DeviceProfile deviceProfile) {
    log.info("Updating SNMP session for device {}", device.getId());
    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());
        destroyDeviceSession(sessionContext);
        return;
    }
    SnmpDeviceProfileTransportConfiguration newProfileTransportConfiguration = (SnmpDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
    SnmpDeviceTransportConfiguration newDeviceTransportConfiguration = (SnmpDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
    try {
        if (!newProfileTransportConfiguration.equals(sessionContext.getProfileTransportConfiguration())) {
            sessionContext.setProfileTransportConfiguration(newProfileTransportConfiguration);
            sessionContext.initializeTarget(newProfileTransportConfiguration, newDeviceTransportConfiguration);
            snmpTransportService.cancelQueryingTasks(sessionContext);
            snmpTransportService.createQueryingTasks(sessionContext);
        } else if (!newDeviceTransportConfiguration.equals(sessionContext.getDeviceTransportConfiguration())) {
            sessionContext.setDeviceTransportConfiguration(newDeviceTransportConfiguration);
            sessionContext.initializeTarget(newProfileTransportConfiguration, newDeviceTransportConfiguration);
        } else {
            log.trace("Configuration of the device {} was not updated", device);
        }
    } catch (Exception e) {
        log.error("Failed to update session for SNMP device {}: {}", sessionContext.getDeviceId(), e.getMessage());
        destroyDeviceSession(sessionContext);
    }
}
Also used : SnmpDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Aggregations

SnmpDeviceTransportConfiguration (org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration)2 SnmpDeviceProfileTransportConfiguration (org.thingsboard.server.common.data.device.profile.SnmpDeviceProfileTransportConfiguration)2 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)2 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)1 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)1 DeviceSessionContext (org.thingsboard.server.transport.snmp.session.DeviceSessionContext)1