Search in sources :

Example 6 with OtaPackageId

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

the class OtaPackageEntity method toData.

@Override
public OtaPackage toData() {
    OtaPackage otaPackage = new OtaPackage(new OtaPackageId(id));
    otaPackage.setCreatedTime(createdTime);
    otaPackage.setTenantId(TenantId.fromUUID(tenantId));
    if (deviceProfileId != null) {
        otaPackage.setDeviceProfileId(new DeviceProfileId(deviceProfileId));
    }
    otaPackage.setType(type);
    otaPackage.setTitle(title);
    otaPackage.setVersion(version);
    otaPackage.setTag(tag);
    otaPackage.setUrl(url);
    otaPackage.setFileName(fileName);
    otaPackage.setContentType(contentType);
    otaPackage.setChecksumAlgorithm(checksumAlgorithm);
    otaPackage.setChecksum(checksum);
    otaPackage.setDataSize(dataSize);
    if (data != null) {
        otaPackage.setData(ByteBuffer.wrap(data));
        otaPackage.setHasData(true);
    }
    otaPackage.setAdditionalInfo(additionalInfo);
    return otaPackage;
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId)

Example 7 with OtaPackageId

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

the class OtaPackageController method deleteOtaPackage.

@ApiOperation(value = "Delete OTA Package (deleteOtaPackage)", notes = "Deletes the OTA Package. Referencing non-existing OTA Package Id will cause an error. " + "Can't delete the OTA Package if it is referenced by existing devices or device profile." + TENANT_AUTHORITY_PARAGRAPH, produces = "application/json")
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/otaPackage/{otaPackageId}", method = RequestMethod.DELETE)
@ResponseBody
public void deleteOtaPackage(@ApiParam(value = OTA_PACKAGE_ID_PARAM_DESCRIPTION) @PathVariable("otaPackageId") String strOtaPackageId) throws ThingsboardException {
    checkParameter(OTA_PACKAGE_ID, strOtaPackageId);
    try {
        OtaPackageId otaPackageId = new OtaPackageId(toUUID(strOtaPackageId));
        OtaPackageInfo info = checkOtaPackageInfoId(otaPackageId, Operation.DELETE);
        otaPackageService.deleteOtaPackage(getTenantId(), otaPackageId);
        logEntityAction(otaPackageId, info, null, ActionType.DELETED, null, strOtaPackageId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.OTA_PACKAGE), null, null, ActionType.DELETED, e, strOtaPackageId);
        throw handleException(e);
    }
}
Also used : OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) 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 8 with OtaPackageId

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

the class AbstractDeviceEntity method toDevice.

protected Device toDevice() {
    Device device = new Device(new DeviceId(getUuid()));
    device.setCreatedTime(createdTime);
    if (tenantId != null) {
        device.setTenantId(TenantId.fromUUID(tenantId));
    }
    if (customerId != null) {
        device.setCustomerId(new CustomerId(customerId));
    }
    if (deviceProfileId != null) {
        device.setDeviceProfileId(new DeviceProfileId(deviceProfileId));
    }
    if (firmwareId != null) {
        device.setFirmwareId(new OtaPackageId(firmwareId));
    }
    if (softwareId != null) {
        device.setSoftwareId(new OtaPackageId(softwareId));
    }
    device.setDeviceData(JacksonUtil.convertValue(deviceData, DeviceData.class));
    device.setName(name);
    device.setType(type);
    device.setLabel(label);
    device.setAdditionalInfo(additionalInfo);
    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) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) DeviceData(org.thingsboard.server.common.data.device.data.DeviceData) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 9 with OtaPackageId

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

the class DefaultTransportApiService method handle.

private ListenableFuture<TransportApiResponseMsg> handle(TransportProtos.GetOtaPackageRequestMsg requestMsg) {
    TenantId tenantId = TenantId.fromUUID(new UUID(requestMsg.getTenantIdMSB(), requestMsg.getTenantIdLSB()));
    DeviceId deviceId = new DeviceId(new UUID(requestMsg.getDeviceIdMSB(), requestMsg.getDeviceIdLSB()));
    OtaPackageType otaPackageType = OtaPackageType.valueOf(requestMsg.getType());
    Device device = deviceService.findDeviceById(tenantId, deviceId);
    if (device == null) {
        return getEmptyTransportApiResponseFuture();
    }
    OtaPackageId otaPackageId = OtaPackageUtil.getOtaPackageId(device, otaPackageType);
    if (otaPackageId == null) {
        DeviceProfile deviceProfile = deviceProfileCache.find(device.getDeviceProfileId());
        otaPackageId = OtaPackageUtil.getOtaPackageId(deviceProfile, otaPackageType);
    }
    TransportProtos.GetOtaPackageResponseMsg.Builder builder = TransportProtos.GetOtaPackageResponseMsg.newBuilder();
    if (otaPackageId == null) {
        builder.setResponseStatus(TransportProtos.ResponseStatus.NOT_FOUND);
    } else {
        OtaPackageInfo otaPackageInfo = otaPackageService.findOtaPackageInfoById(tenantId, otaPackageId);
        if (otaPackageInfo == null) {
            builder.setResponseStatus(TransportProtos.ResponseStatus.NOT_FOUND);
        } else if (otaPackageInfo.hasUrl()) {
            builder.setResponseStatus(TransportProtos.ResponseStatus.FAILURE);
            log.trace("[{}] Can`t send OtaPackage with URL data!", otaPackageInfo.getId());
        } else {
            builder.setResponseStatus(TransportProtos.ResponseStatus.SUCCESS);
            builder.setOtaPackageIdMSB(otaPackageId.getId().getMostSignificantBits());
            builder.setOtaPackageIdLSB(otaPackageId.getId().getLeastSignificantBits());
            builder.setType(otaPackageInfo.getType().name());
            builder.setTitle(otaPackageInfo.getTitle());
            builder.setVersion(otaPackageInfo.getVersion());
            builder.setFileName(otaPackageInfo.getFileName());
            builder.setContentType(otaPackageInfo.getContentType());
            if (!otaPackageDataCache.has(otaPackageId.toString())) {
                OtaPackage otaPackage = otaPackageService.findOtaPackageById(tenantId, otaPackageId);
                otaPackageDataCache.put(otaPackageId.toString(), otaPackage.getData().array());
            }
        }
    }
    return Futures.immediateFuture(TransportApiResponseMsg.newBuilder().setOtaPackageResponseMsg(builder.build()).build());
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) TenantId(org.thingsboard.server.common.data.id.TenantId) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) UUID(java.util.UUID)

Example 10 with OtaPackageId

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

the class OtaPackageController method downloadOtaPackage.

@ApiOperation(value = "Download OTA Package (downloadOtaPackage)", notes = "Download OTA Package based on the provided OTA Package Id." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority( 'TENANT_ADMIN')")
@RequestMapping(value = "/otaPackage/{otaPackageId}/download", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<org.springframework.core.io.Resource> downloadOtaPackage(@ApiParam(value = OTA_PACKAGE_ID_PARAM_DESCRIPTION) @PathVariable(OTA_PACKAGE_ID) String strOtaPackageId) throws ThingsboardException {
    checkParameter(OTA_PACKAGE_ID, strOtaPackageId);
    try {
        OtaPackageId otaPackageId = new OtaPackageId(toUUID(strOtaPackageId));
        OtaPackage otaPackage = checkOtaPackageId(otaPackageId, Operation.READ);
        if (otaPackage.hasUrl()) {
            return ResponseEntity.badRequest().build();
        }
        ByteArrayResource resource = new ByteArrayResource(otaPackage.getData().array());
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + otaPackage.getFileName()).header("x-filename", otaPackage.getFileName()).contentLength(resource.contentLength()).contentType(parseMediaType(otaPackage.getContentType())).body(resource);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) ByteArrayResource(org.springframework.core.io.ByteArrayResource) 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)

Aggregations

OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)13 OtaPackageInfo (org.thingsboard.server.common.data.OtaPackageInfo)6 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)5 OtaPackage (org.thingsboard.server.common.data.OtaPackage)4 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)4 ApiOperation (io.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 Device (org.thingsboard.server.common.data.Device)3 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)3 DeviceId (org.thingsboard.server.common.data.id.DeviceId)3 UUID (java.util.UUID)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 Cache (org.springframework.cache.Cache)2 OtaPackageDataCache (org.thingsboard.server.cache.ota.OtaPackageDataCache)2 TenantId (org.thingsboard.server.common.data.id.TenantId)2 OtaPackageType (org.thingsboard.server.common.data.ota.OtaPackageType)2 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1