Search in sources :

Example 1 with OtaPackageInfo

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

the class BaseOtaPackageControllerTest method testSaveFirmware.

@Test
public void testSaveFirmware() throws Exception {
    SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest();
    firmwareInfo.setDeviceProfileId(deviceProfileId);
    firmwareInfo.setType(FIRMWARE);
    firmwareInfo.setTitle(TITLE);
    firmwareInfo.setVersion(VERSION);
    firmwareInfo.setUsesUrl(false);
    OtaPackageInfo savedFirmwareInfo = save(firmwareInfo);
    Assert.assertNotNull(savedFirmwareInfo);
    Assert.assertNotNull(savedFirmwareInfo.getId());
    Assert.assertTrue(savedFirmwareInfo.getCreatedTime() > 0);
    Assert.assertEquals(savedTenant.getId(), savedFirmwareInfo.getTenantId());
    Assert.assertEquals(firmwareInfo.getTitle(), savedFirmwareInfo.getTitle());
    Assert.assertEquals(firmwareInfo.getVersion(), savedFirmwareInfo.getVersion());
    savedFirmwareInfo.setAdditionalInfo(JacksonUtil.newObjectNode());
    save(new SaveOtaPackageInfoRequest(savedFirmwareInfo, false));
    OtaPackageInfo foundFirmwareInfo = doGet("/api/otaPackage/info/" + savedFirmwareInfo.getId().getId().toString(), OtaPackageInfo.class);
    Assert.assertEquals(foundFirmwareInfo.getTitle(), savedFirmwareInfo.getTitle());
}
Also used : OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) SaveOtaPackageInfoRequest(org.thingsboard.server.common.data.SaveOtaPackageInfoRequest) Test(org.junit.Test)

Example 2 with OtaPackageInfo

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

the class BaseOtaPackageControllerTest method testFindTenantFirmwares.

@Test
public void testFindTenantFirmwares() throws Exception {
    List<OtaPackageInfo> otaPackages = new ArrayList<>();
    for (int i = 0; i < 165; i++) {
        SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest();
        firmwareInfo.setDeviceProfileId(deviceProfileId);
        firmwareInfo.setType(FIRMWARE);
        firmwareInfo.setTitle(TITLE);
        firmwareInfo.setVersion(VERSION + i);
        firmwareInfo.setUsesUrl(false);
        OtaPackageInfo savedFirmwareInfo = save(firmwareInfo);
        if (i > 100) {
            MockMultipartFile testData = new MockMultipartFile("file", FILE_NAME, CONTENT_TYPE, DATA.array());
            OtaPackageInfo savedFirmware = savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, CHECKSUM_ALGORITHM);
            otaPackages.add(savedFirmware);
        } else {
            otaPackages.add(savedFirmwareInfo);
        }
    }
    List<OtaPackageInfo> loadedFirmwares = new ArrayList<>();
    PageLink pageLink = new PageLink(24);
    PageData<OtaPackageInfo> pageData;
    do {
        pageData = doGetTypedWithPageLink("/api/otaPackages?", new TypeReference<>() {
        }, pageLink);
        loadedFirmwares.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(otaPackages, idComparator);
    Collections.sort(loadedFirmwares, idComparator);
    Assert.assertEquals(otaPackages, loadedFirmwares);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SaveOtaPackageInfoRequest(org.thingsboard.server.common.data.SaveOtaPackageInfoRequest) Test(org.junit.Test)

Example 3 with OtaPackageInfo

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

the class BaseOtaPackageControllerTest method testFindFirmwareById.

@Test
public void testFindFirmwareById() throws Exception {
    SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest();
    firmwareInfo.setDeviceProfileId(deviceProfileId);
    firmwareInfo.setType(FIRMWARE);
    firmwareInfo.setTitle(TITLE);
    firmwareInfo.setVersion(VERSION);
    firmwareInfo.setUsesUrl(false);
    OtaPackageInfo savedFirmwareInfo = save(firmwareInfo);
    MockMultipartFile testData = new MockMultipartFile("file", FILE_NAME, CONTENT_TYPE, DATA.array());
    OtaPackageInfo savedFirmware = savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, CHECKSUM_ALGORITHM);
    OtaPackage foundFirmware = doGet("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString(), OtaPackage.class);
    Assert.assertNotNull(foundFirmware);
    Assert.assertEquals(savedFirmware, new OtaPackageInfo(foundFirmware));
    Assert.assertEquals(DATA, foundFirmware.getData());
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) OtaPackage(org.thingsboard.server.common.data.OtaPackage) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) SaveOtaPackageInfoRequest(org.thingsboard.server.common.data.SaveOtaPackageInfoRequest) Test(org.junit.Test)

Example 4 with OtaPackageInfo

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

the class BaseOtaPackageControllerTest method testFindTenantFirmwaresByHasData.

@Test
public void testFindTenantFirmwaresByHasData() throws Exception {
    List<OtaPackageInfo> otaPackagesWithData = new ArrayList<>();
    List<OtaPackageInfo> allOtaPackages = new ArrayList<>();
    for (int i = 0; i < 165; i++) {
        SaveOtaPackageInfoRequest firmwareInfo = new SaveOtaPackageInfoRequest();
        firmwareInfo.setDeviceProfileId(deviceProfileId);
        firmwareInfo.setType(FIRMWARE);
        firmwareInfo.setTitle(TITLE);
        firmwareInfo.setVersion(VERSION + i);
        firmwareInfo.setUsesUrl(false);
        OtaPackageInfo savedFirmwareInfo = save(firmwareInfo);
        if (i > 100) {
            MockMultipartFile testData = new MockMultipartFile("file", FILE_NAME, CONTENT_TYPE, DATA.array());
            OtaPackageInfo savedFirmware = savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, CHECKSUM_ALGORITHM);
            savedFirmwareInfo = new OtaPackageInfo(savedFirmware);
            otaPackagesWithData.add(savedFirmwareInfo);
        }
        allOtaPackages.add(savedFirmwareInfo);
    }
    List<OtaPackageInfo> loadedOtaPackagesWithData = new ArrayList<>();
    PageLink pageLink = new PageLink(24);
    PageData<OtaPackageInfo> pageData;
    do {
        pageData = doGetTypedWithPageLink("/api/otaPackages/" + deviceProfileId.toString() + "/FIRMWARE?", new TypeReference<>() {
        }, pageLink);
        loadedOtaPackagesWithData.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    List<OtaPackageInfo> allLoadedOtaPackages = new ArrayList<>();
    pageLink = new PageLink(24);
    do {
        pageData = doGetTypedWithPageLink("/api/otaPackages?", new TypeReference<>() {
        }, pageLink);
        allLoadedOtaPackages.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(otaPackagesWithData, idComparator);
    Collections.sort(allOtaPackages, idComparator);
    Collections.sort(loadedOtaPackagesWithData, idComparator);
    Collections.sort(allLoadedOtaPackages, idComparator);
    Assert.assertEquals(otaPackagesWithData, loadedOtaPackagesWithData);
    Assert.assertEquals(allOtaPackages, allLoadedOtaPackages);
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SaveOtaPackageInfoRequest(org.thingsboard.server.common.data.SaveOtaPackageInfoRequest) Test(org.junit.Test)

Example 5 with OtaPackageInfo

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

the class DefaultOtaPackageStateService method send.

private void send(TenantId tenantId, DeviceId deviceId, OtaPackageId firmwareId, long ts, OtaPackageType firmwareType) {
    ToOtaPackageStateServiceMsg msg = ToOtaPackageStateServiceMsg.newBuilder().setTenantIdMSB(tenantId.getId().getMostSignificantBits()).setTenantIdLSB(tenantId.getId().getLeastSignificantBits()).setDeviceIdMSB(deviceId.getId().getMostSignificantBits()).setDeviceIdLSB(deviceId.getId().getLeastSignificantBits()).setOtaPackageIdMSB(firmwareId.getId().getMostSignificantBits()).setOtaPackageIdLSB(firmwareId.getId().getLeastSignificantBits()).setType(firmwareType.name()).setTs(ts).build();
    OtaPackageInfo firmware = otaPackageService.findOtaPackageInfoById(tenantId, firmwareId);
    if (firmware == null) {
        log.warn("[{}] Failed to send firmware update because firmware was already deleted", firmwareId);
        return;
    }
    TopicPartitionInfo tpi = new TopicPartitionInfo(otaPackageStateMsgProducer.getDefaultTopic(), null, null, false);
    otaPackageStateMsgProducer.send(tpi, new TbProtoQueueMsg<>(UUID.randomUUID(), msg), null);
    List<TsKvEntry> telemetry = new ArrayList<>();
    telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTargetTelemetryKey(firmware.getType(), TITLE), firmware.getTitle())));
    telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTargetTelemetryKey(firmware.getType(), VERSION), firmware.getVersion())));
    if (StringUtils.isNotEmpty(firmware.getTag())) {
        telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTargetTelemetryKey(firmware.getType(), TAG), firmware.getTag())));
    }
    telemetry.add(new BasicTsKvEntry(ts, new LongDataEntry(getTargetTelemetryKey(firmware.getType(), TS), ts)));
    telemetry.add(new BasicTsKvEntry(ts, new StringDataEntry(getTelemetryKey(firmware.getType(), STATE), OtaPackageUpdateStatus.QUEUED.name())));
    telemetryService.saveAndNotify(tenantId, deviceId, telemetry, new FutureCallback<>() {

        @Override
        public void onSuccess(@Nullable Void tmp) {
            log.trace("[{}] Success save firmware status!", deviceId);
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("[{}] Failed to save firmware status!", deviceId, t);
        }
    });
}
Also used : ToOtaPackageStateServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToOtaPackageStateServiceMsg) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) ArrayList(java.util.ArrayList) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry)

Aggregations

OtaPackageInfo (org.thingsboard.server.common.data.OtaPackageInfo)31 Test (org.junit.Test)18 SaveOtaPackageInfoRequest (org.thingsboard.server.common.data.SaveOtaPackageInfoRequest)8 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)6 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)6 ArrayList (java.util.ArrayList)5 OtaPackage (org.thingsboard.server.common.data.OtaPackage)5 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)4 PageLink (org.thingsboard.server.common.data.page.PageLink)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 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 Cache (org.springframework.cache.Cache)2 OtaPackageDataCache (org.thingsboard.server.cache.ota.OtaPackageDataCache)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HashMap (java.util.HashMap)1