Search in sources :

Example 11 with OtaPackageId

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

the class OtaPackageController method saveOtaPackageData.

@ApiOperation(value = "Save OTA Package data (saveOtaPackageData)", notes = "Update the OTA Package. Adds the date to the existing OTA Package Info" + TENANT_AUTHORITY_PARAGRAPH, produces = "application/json")
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/otaPackage/{otaPackageId}", method = RequestMethod.POST)
@ResponseBody
public OtaPackageInfo saveOtaPackageData(@ApiParam(value = OTA_PACKAGE_ID_PARAM_DESCRIPTION) @PathVariable(OTA_PACKAGE_ID) String strOtaPackageId, @ApiParam(value = "OTA Package checksum. For example, '0xd87f7e0c'") @RequestParam(required = false) String checksum, @ApiParam(value = "OTA Package checksum algorithm.", allowableValues = OTA_PACKAGE_CHECKSUM_ALGORITHM_ALLOWABLE_VALUES) @RequestParam(CHECKSUM_ALGORITHM) String checksumAlgorithmStr, @ApiParam(value = "OTA Package data.") @RequestBody MultipartFile file) throws ThingsboardException {
    checkParameter(OTA_PACKAGE_ID, strOtaPackageId);
    checkParameter(CHECKSUM_ALGORITHM, checksumAlgorithmStr);
    try {
        OtaPackageId otaPackageId = new OtaPackageId(toUUID(strOtaPackageId));
        OtaPackageInfo info = checkOtaPackageInfoId(otaPackageId, Operation.READ);
        OtaPackage otaPackage = new OtaPackage(otaPackageId);
        otaPackage.setCreatedTime(info.getCreatedTime());
        otaPackage.setTenantId(getTenantId());
        otaPackage.setDeviceProfileId(info.getDeviceProfileId());
        otaPackage.setType(info.getType());
        otaPackage.setTitle(info.getTitle());
        otaPackage.setVersion(info.getVersion());
        otaPackage.setTag(info.getTag());
        otaPackage.setAdditionalInfo(info.getAdditionalInfo());
        ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.valueOf(checksumAlgorithmStr.toUpperCase());
        byte[] bytes = file.getBytes();
        if (StringUtils.isEmpty(checksum)) {
            checksum = otaPackageService.generateChecksum(checksumAlgorithm, ByteBuffer.wrap(bytes));
        }
        otaPackage.setChecksumAlgorithm(checksumAlgorithm);
        otaPackage.setChecksum(checksum);
        otaPackage.setFileName(file.getOriginalFilename());
        otaPackage.setContentType(file.getContentType());
        otaPackage.setData(ByteBuffer.wrap(bytes));
        otaPackage.setDataSize((long) bytes.length);
        OtaPackageInfo savedOtaPackage = otaPackageService.saveOtaPackage(otaPackage);
        logEntityAction(savedOtaPackage.getId(), savedOtaPackage, null, ActionType.UPDATED, null);
        return savedOtaPackage;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.OTA_PACKAGE), null, null, ActionType.UPDATED, e, strOtaPackageId);
        throw handleException(e);
    }
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) ChecksumAlgorithm(org.thingsboard.server.common.data.ota.ChecksumAlgorithm) 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 12 with OtaPackageId

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

the class DefaultOtaPackageStateService method updateSoftware.

private void updateSoftware(Device device, Device oldDevice) {
    OtaPackageId newSoftwareId = device.getSoftwareId();
    if (newSoftwareId == null) {
        DeviceProfile newDeviceProfile = deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
        newSoftwareId = newDeviceProfile.getSoftwareId();
    }
    if (oldDevice != null) {
        OtaPackageId oldSoftwareId = oldDevice.getSoftwareId();
        if (oldSoftwareId == null) {
            DeviceProfile oldDeviceProfile = deviceProfileService.findDeviceProfileById(oldDevice.getTenantId(), oldDevice.getDeviceProfileId());
            oldSoftwareId = oldDeviceProfile.getSoftwareId();
        }
        if (newSoftwareId != null) {
            if (!newSoftwareId.equals(oldSoftwareId)) {
                // Device was updated and new firmware is different from previous firmware.
                send(device.getTenantId(), device.getId(), newSoftwareId, System.currentTimeMillis(), SOFTWARE);
            }
        } else if (oldSoftwareId != null) {
            // Device was updated and new firmware is not set.
            remove(device, SOFTWARE);
        }
    } else if (newSoftwareId != null) {
        // Device was created and firmware is defined.
        send(device.getTenantId(), device.getId(), newSoftwareId, System.currentTimeMillis(), SOFTWARE);
    }
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId)

Example 13 with OtaPackageId

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

the class BaseOtaPackageService method saveOtaPackage.

@Override
public OtaPackage saveOtaPackage(OtaPackage otaPackage) {
    log.trace("Executing saveOtaPackage [{}]", otaPackage);
    otaPackageValidator.validate(otaPackage, OtaPackageInfo::getTenantId);
    try {
        OtaPackageId otaPackageId = otaPackage.getId();
        if (otaPackageId != null) {
            Cache cache = cacheManager.getCache(OTA_PACKAGE_CACHE);
            cache.evict(toOtaPackageInfoKey(otaPackageId));
            otaPackageDataCache.evict(otaPackageId.toString());
        }
        return otaPackageDao.save(otaPackage.getTenantId(), otaPackage);
    } catch (Exception t) {
        ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
        if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("ota_package_tenant_title_version_unq_key")) {
            throw new DataValidationException("OtaPackage with such title and version already exists!");
        } else {
            throw t;
        }
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Cache(org.springframework.cache.Cache) OtaPackageDataCache(org.thingsboard.server.cache.ota.OtaPackageDataCache)

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