Search in sources :

Example 11 with OtaPackage

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

the class BaseOtaPackageServiceTest method testSaveFirmware.

@Test
public void testSaveFirmware() {
    OtaPackage firmware = new OtaPackage();
    firmware.setTenantId(tenantId);
    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);
    firmware.setDataSize(DATA_SIZE);
    OtaPackage savedFirmware = otaPackageService.saveOtaPackage(firmware);
    Assert.assertNotNull(savedFirmware);
    Assert.assertNotNull(savedFirmware.getId());
    Assert.assertTrue(savedFirmware.getCreatedTime() > 0);
    Assert.assertEquals(firmware.getTenantId(), savedFirmware.getTenantId());
    Assert.assertEquals(firmware.getTitle(), savedFirmware.getTitle());
    Assert.assertEquals(firmware.getFileName(), savedFirmware.getFileName());
    Assert.assertEquals(firmware.getContentType(), savedFirmware.getContentType());
    Assert.assertEquals(firmware.getData(), savedFirmware.getData());
    savedFirmware.setAdditionalInfo(JacksonUtil.newObjectNode());
    otaPackageService.saveOtaPackage(savedFirmware);
    OtaPackage foundFirmware = otaPackageService.findOtaPackageById(tenantId, savedFirmware.getId());
    Assert.assertEquals(foundFirmware.getTitle(), savedFirmware.getTitle());
    otaPackageService.deleteOtaPackage(tenantId, savedFirmware.getId());
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) Test(org.junit.Test)

Example 12 with OtaPackage

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

the class BaseOtaPackageServiceTest method testSaveFirmwareInfoAndUpdateWithData.

@Test
public void testSaveFirmwareInfoAndUpdateWithData() {
    OtaPackageInfo firmwareInfo = new OtaPackageInfo();
    firmwareInfo.setTenantId(tenantId);
    firmwareInfo.setDeviceProfileId(deviceProfileId);
    firmwareInfo.setType(FIRMWARE);
    firmwareInfo.setTitle(TITLE);
    firmwareInfo.setVersion(VERSION);
    OtaPackageInfo savedFirmwareInfo = otaPackageService.saveOtaPackageInfo(firmwareInfo, false);
    Assert.assertNotNull(savedFirmwareInfo);
    Assert.assertNotNull(savedFirmwareInfo.getId());
    Assert.assertTrue(savedFirmwareInfo.getCreatedTime() > 0);
    Assert.assertEquals(firmwareInfo.getTenantId(), savedFirmwareInfo.getTenantId());
    Assert.assertEquals(firmwareInfo.getTitle(), savedFirmwareInfo.getTitle());
    OtaPackage firmware = new OtaPackage(savedFirmwareInfo.getId());
    firmware.setCreatedTime(firmwareInfo.getCreatedTime());
    firmware.setTenantId(tenantId);
    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);
    firmware.setDataSize(DATA_SIZE);
    otaPackageService.saveOtaPackage(firmware);
    savedFirmwareInfo = otaPackageService.findOtaPackageInfoById(tenantId, savedFirmwareInfo.getId());
    savedFirmwareInfo.setAdditionalInfo(JacksonUtil.newObjectNode());
    otaPackageService.saveOtaPackageInfo(savedFirmwareInfo, false);
    OtaPackage foundFirmware = otaPackageService.findOtaPackageById(tenantId, firmware.getId());
    firmware.setAdditionalInfo(JacksonUtil.newObjectNode());
    Assert.assertEquals(foundFirmware.getTitle(), firmware.getTitle());
    Assert.assertTrue(foundFirmware.isHasData());
    otaPackageService.deleteOtaPackage(tenantId, savedFirmwareInfo.getId());
}
Also used : OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) Test(org.junit.Test)

Example 13 with OtaPackage

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

the class OtaPackageDataValidator method validateUpdate.

@Override
protected void validateUpdate(TenantId tenantId, OtaPackage otaPackage) {
    OtaPackage otaPackageOld = otaPackageDao.findById(tenantId, otaPackage.getUuidId());
    validateUpdate(otaPackage, otaPackageOld);
    if (otaPackageOld.getData() != null && !otaPackageOld.getData().equals(otaPackage.getData())) {
        throw new DataValidationException("Updating otaPackage data is prohibited!");
    }
    if (otaPackageOld.getData() == null && otaPackage.getData() != null) {
        DefaultTenantProfileConfiguration profileConfiguration = (DefaultTenantProfileConfiguration) tenantProfileCache.get(tenantId).getProfileData().getConfiguration();
        long maxOtaPackagesInBytes = profileConfiguration.getMaxOtaPackagesInBytes();
        validateMaxSumDataSizePerTenant(tenantId, otaPackageDao, maxOtaPackagesInBytes, otaPackage.getDataSize(), OTA_PACKAGE);
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) OtaPackage(org.thingsboard.server.common.data.OtaPackage) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration)

Example 14 with OtaPackage

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

the class BaseDeviceServiceTest method testSaveDeviceWithFirmware.

@Test
public void testSaveDeviceWithFirmware() {
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("My device");
    device.setType("default");
    Device savedDevice = deviceService.saveDevice(device);
    Assert.assertNotNull(savedDevice);
    Assert.assertNotNull(savedDevice.getId());
    Assert.assertTrue(savedDevice.getCreatedTime() > 0);
    Assert.assertEquals(device.getTenantId(), savedDevice.getTenantId());
    Assert.assertNotNull(savedDevice.getCustomerId());
    Assert.assertEquals(NULL_UUID, savedDevice.getCustomerId().getId());
    Assert.assertEquals(device.getName(), savedDevice.getName());
    DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(tenantId, savedDevice.getId());
    Assert.assertNotNull(deviceCredentials);
    Assert.assertNotNull(deviceCredentials.getId());
    Assert.assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId());
    Assert.assertEquals(DeviceCredentialsType.ACCESS_TOKEN, deviceCredentials.getCredentialsType());
    Assert.assertNotNull(deviceCredentials.getCredentialsId());
    Assert.assertEquals(20, deviceCredentials.getCredentialsId().length());
    OtaPackage firmware = new OtaPackage();
    firmware.setTenantId(tenantId);
    firmware.setDeviceProfileId(device.getDeviceProfileId());
    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);
    savedDevice.setFirmwareId(savedFirmware.getId());
    deviceService.saveDevice(savedDevice);
    Device foundDevice = deviceService.findDeviceById(tenantId, savedDevice.getId());
    Assert.assertEquals(foundDevice.getName(), savedDevice.getName());
}
Also used : Device(org.thingsboard.server.common.data.Device) OtaPackage(org.thingsboard.server.common.data.OtaPackage) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 15 with OtaPackage

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

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