Search in sources :

Example 16 with OtaPackage

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

Example 17 with OtaPackage

use of org.thingsboard.server.common.data.OtaPackage 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 18 with OtaPackage

use of org.thingsboard.server.common.data.OtaPackage in project thingsboard by thingsboard.

the class BaseDeviceProfileServiceTest method testSaveDeviceProfileWithFirmware.

@Test
public void testSaveDeviceProfileWithFirmware() {
    DeviceProfile deviceProfile = this.createDeviceProfile(tenantId, "Device Profile");
    DeviceProfile savedDeviceProfile = deviceProfileService.saveDeviceProfile(deviceProfile);
    Assert.assertNotNull(savedDeviceProfile);
    Assert.assertNotNull(savedDeviceProfile.getId());
    Assert.assertTrue(savedDeviceProfile.getCreatedTime() > 0);
    Assert.assertEquals(deviceProfile.getName(), savedDeviceProfile.getName());
    Assert.assertEquals(deviceProfile.getDescription(), savedDeviceProfile.getDescription());
    Assert.assertEquals(deviceProfile.getProfileData(), savedDeviceProfile.getProfileData());
    Assert.assertEquals(deviceProfile.isDefault(), savedDeviceProfile.isDefault());
    Assert.assertEquals(deviceProfile.getDefaultRuleChainId(), savedDeviceProfile.getDefaultRuleChainId());
    OtaPackage firmware = new OtaPackage();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(savedDeviceProfile.getId());
    firmware.setType(FIRMWARE);
    firmware.setTitle("my firmware");
    firmware.setVersion("v1.0");
    firmware.setFileName("test.txt");
    firmware.setContentType("text/plain");
    firmware.setChecksumAlgorithm(ChecksumAlgorithm.SHA256);
    firmware.setChecksum("4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a");
    firmware.setData(ByteBuffer.wrap(new byte[] { 1 }));
    firmware.setDataSize(1L);
    OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
    deviceProfile.setFirmwareId(savedFirmware.getId());
    deviceProfileService.saveDeviceProfile(savedDeviceProfile);
    DeviceProfile foundDeviceProfile = deviceProfileService.findDeviceProfileById(tenantId, savedDeviceProfile.getId());
    Assert.assertEquals(savedDeviceProfile.getName(), foundDeviceProfile.getName());
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) OtaPackage(org.thingsboard.server.common.data.OtaPackage) Test(org.junit.Test)

Example 19 with OtaPackage

use of org.thingsboard.server.common.data.OtaPackage in project thingsboard by thingsboard.

the class BaseOtaPackageServiceTest method testSaveFirmwareWithInvalidDeviceProfileId.

@Test
public void testSaveFirmwareWithInvalidDeviceProfileId() {
    OtaPackage firmware = new OtaPackage();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(new DeviceProfileId(Uuids.timeBased()));
    firmware.setType(FIRMWARE);
    firmware.setTitle(TITLE);
    firmware.setVersion(VERSION);
    firmware.setFileName(FILE_NAME);
    firmware.setContentType(CONTENT_TYPE);
    firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
    firmware.setChecksum(CHECKSUM);
    firmware.setData(DATA);
    thrown.expect(DataValidationException.class);
    thrown.expectMessage("OtaPackage is referencing to non-existent device profile!");
    otaPackageService.saveOtaPackage(firmware);
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) OtaPackage(org.thingsboard.server.common.data.OtaPackage) Test(org.junit.Test)

Example 20 with OtaPackage

use of org.thingsboard.server.common.data.OtaPackage in project thingsboard by thingsboard.

the class BaseOtaPackageServiceTest method testSaveFirmwareWithEmptyTenant.

@Test
public void testSaveFirmwareWithEmptyTenant() {
    OtaPackage firmware = new OtaPackage();
    firmware.setDeviceProfileId(deviceProfileId);
    firmware.setType(FIRMWARE);
    firmware.setTitle(TITLE);
    firmware.setVersion(VERSION);
    firmware.setFileName(FILE_NAME);
    firmware.setContentType(CONTENT_TYPE);
    firmware.setChecksumAlgorithm(CHECKSUM_ALGORITHM);
    firmware.setChecksum(CHECKSUM);
    firmware.setData(DATA);
    thrown.expect(DataValidationException.class);
    thrown.expectMessage("OtaPackage should be assigned to tenant!");
    otaPackageService.saveOtaPackage(firmware);
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) Test(org.junit.Test)

Aggregations

OtaPackage (org.thingsboard.server.common.data.OtaPackage)29 Test (org.junit.Test)21 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)5 OtaPackageInfo (org.thingsboard.server.common.data.OtaPackageInfo)5 Device (org.thingsboard.server.common.data.Device)4 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)4 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)3 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 DeviceProfileId (org.thingsboard.server.common.data.id.DeviceProfileId)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 MessagingException (javax.mail.MessagingException)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)1 DashboardInfo (org.thingsboard.server.common.data.DashboardInfo)1