Search in sources :

Example 16 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId 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 17 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class ProtoTransportEntityService method getDeviceById.

public Device getDeviceById(DeviceId id) {
    TransportProtos.GetDeviceResponseMsg deviceProto = transportService.getDevice(TransportProtos.GetDeviceRequestMsg.newBuilder().setDeviceIdMSB(id.getId().getMostSignificantBits()).setDeviceIdLSB(id.getId().getLeastSignificantBits()).build());
    if (deviceProto == null) {
        return null;
    }
    DeviceProfileId deviceProfileId = new DeviceProfileId(new UUID(deviceProto.getDeviceProfileIdMSB(), deviceProto.getDeviceProfileIdLSB()));
    Device device = new Device();
    device.setId(id);
    device.setDeviceProfileId(deviceProfileId);
    DeviceTransportConfiguration deviceTransportConfiguration = (DeviceTransportConfiguration) dataDecodingEncodingService.decode(deviceProto.getDeviceTransportConfiguration().toByteArray()).orElseThrow(() -> new IllegalStateException("Can't find device transport configuration"));
    DeviceData deviceData = new DeviceData();
    deviceData.setTransportConfiguration(deviceTransportConfiguration);
    device.setDeviceData(deviceData);
    return device;
}
Also used : DeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.DeviceTransportConfiguration) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Device(org.thingsboard.server.common.data.Device) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) DeviceData(org.thingsboard.server.common.data.device.data.DeviceData) UUID(java.util.UUID)

Example 18 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class OtaPackageController method getOtaPackages.

@ApiOperation(value = "Get OTA Package Infos (getOtaPackages)", notes = "Returns a page of OTA Package Info objects owned by tenant. " + PAGE_DATA_PARAMETERS + OTA_PACKAGE_INFO_DESCRIPTION + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = "application/json")
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/otaPackages/{deviceProfileId}/{type}", method = RequestMethod.GET)
@ResponseBody
public PageData<OtaPackageInfo> getOtaPackages(@ApiParam(value = DEVICE_PROFILE_ID_PARAM_DESCRIPTION) @PathVariable("deviceProfileId") String strDeviceProfileId, @ApiParam(value = "OTA Package type.", allowableValues = "FIRMWARE, SOFTWARE") @PathVariable("type") String strType, @ApiParam(value = PAGE_SIZE_DESCRIPTION, required = true) @RequestParam int pageSize, @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true) @RequestParam int page, @ApiParam(value = OTA_PACKAGE_TEXT_SEARCH_DESCRIPTION) @RequestParam(required = false) String textSearch, @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = OTA_PACKAGE_SORT_PROPERTY_ALLOWABLE_VALUES) @RequestParam(required = false) String sortProperty, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(required = false) String sortOrder) throws ThingsboardException {
    checkParameter("deviceProfileId", strDeviceProfileId);
    checkParameter("type", strType);
    try {
        PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
        return checkNotNull(otaPackageService.findTenantOtaPackagesByTenantIdAndDeviceProfileIdAndTypeAndHasData(getTenantId(), new DeviceProfileId(toUUID(strDeviceProfileId)), OtaPackageType.valueOf(strType), pageLink));
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) PageLink(org.thingsboard.server.common.data.page.PageLink) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 19 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class DefaultTbDeviceProfileCache method get.

@Override
public DeviceProfile get(TenantId tenantId, DeviceId deviceId) {
    DeviceProfileId profileId = devicesMap.get(deviceId);
    if (profileId == null) {
        Device device = deviceService.findDeviceById(tenantId, deviceId);
        if (device != null) {
            profileId = device.getDeviceProfileId();
            devicesMap.put(deviceId, profileId);
        } else {
            return null;
        }
    }
    return get(tenantId, profileId);
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Device(org.thingsboard.server.common.data.Device)

Example 20 with DeviceProfileId

use of org.thingsboard.server.common.data.id.DeviceProfileId in project thingsboard by thingsboard.

the class DeviceEdgeProcessor method createDevice.

private Device createDevice(TenantId tenantId, Edge edge, DeviceUpdateMsg deviceUpdateMsg, String deviceName) {
    Device device;
    deviceCreationLock.lock();
    try {
        log.debug("[{}] Creating device entity [{}] from edge [{}]", tenantId, deviceUpdateMsg, edge.getName());
        DeviceId deviceId = new DeviceId(new UUID(deviceUpdateMsg.getIdMSB(), deviceUpdateMsg.getIdLSB()));
        device = deviceService.findDeviceById(tenantId, deviceId);
        boolean created = false;
        if (device == null) {
            device = new Device();
            device.setTenantId(tenantId);
            device.setCreatedTime(Uuids.unixTimestamp(deviceId.getId()));
            created = true;
        }
        // make device private, if edge is public
        device.setCustomerId(getCustomerId(edge));
        device.setName(deviceName);
        device.setType(deviceUpdateMsg.getType());
        if (deviceUpdateMsg.hasLabel()) {
            device.setLabel(deviceUpdateMsg.getLabel());
        }
        if (deviceUpdateMsg.hasAdditionalInfo()) {
            device.setAdditionalInfo(JacksonUtil.toJsonNode(deviceUpdateMsg.getAdditionalInfo()));
        }
        if (deviceUpdateMsg.hasDeviceProfileIdMSB() && deviceUpdateMsg.hasDeviceProfileIdLSB()) {
            DeviceProfileId deviceProfileId = new DeviceProfileId(new UUID(deviceUpdateMsg.getDeviceProfileIdMSB(), deviceUpdateMsg.getDeviceProfileIdLSB()));
            device.setDeviceProfileId(deviceProfileId);
        }
        if (created) {
            deviceValidator.validate(device, Device::getTenantId);
            device.setId(deviceId);
        } else {
            deviceValidator.validate(device, Device::getTenantId);
        }
        Device savedDevice = deviceService.saveDevice(device, false);
        tbClusterService.onDeviceUpdated(savedDevice, created ? null : device, false);
        if (created) {
            DeviceCredentials deviceCredentials = new DeviceCredentials();
            deviceCredentials.setDeviceId(new DeviceId(savedDevice.getUuidId()));
            deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
            deviceCredentials.setCredentialsId(org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric(20));
            deviceCredentialsService.createDeviceCredentials(device.getTenantId(), deviceCredentials);
        }
        createRelationFromEdge(tenantId, edge.getId(), device.getId());
        pushDeviceCreatedEventToRuleEngine(tenantId, edge, device);
        deviceService.assignDeviceToEdge(edge.getTenantId(), device.getId(), edge.getId());
    } finally {
        deviceCreationLock.unlock();
    }
    return device;
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) Device(org.thingsboard.server.common.data.Device) DeviceId(org.thingsboard.server.common.data.id.DeviceId) UUID(java.util.UUID) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Aggregations

DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)33 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)12 UUID (java.util.UUID)9 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 ApiOperation (io.swagger.annotations.ApiOperation)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 Device (org.thingsboard.server.common.data.Device)8 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 IOException (java.io.IOException)3 EntityType (org.thingsboard.server.common.data.EntityType)3 PageLink (org.thingsboard.server.common.data.page.PageLink)3 TransportDeviceInfo (org.thingsboard.server.common.transport.auth.TransportDeviceInfo)3 ByteString (com.google.protobuf.ByteString)2 ExecutionException (java.util.concurrent.ExecutionException)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2