Search in sources :

Example 1 with ChecksumAlgorithm

use of org.thingsboard.server.common.data.ota.ChecksumAlgorithm 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)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 OtaPackage (org.thingsboard.server.common.data.OtaPackage)1 OtaPackageInfo (org.thingsboard.server.common.data.OtaPackageInfo)1 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)1 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)1 ChecksumAlgorithm (org.thingsboard.server.common.data.ota.ChecksumAlgorithm)1