Search in sources :

Example 1 with OtaPackageId

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

Example 2 with OtaPackageId

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

the class DefaultOtaPackageStateService method updateFirmware.

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

Example 3 with OtaPackageId

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

the class DefaultOtaPackageStateService method process.

@Override
public boolean process(ToOtaPackageStateServiceMsg msg) {
    boolean isSuccess = false;
    OtaPackageId targetOtaPackageId = new OtaPackageId(new UUID(msg.getOtaPackageIdMSB(), msg.getOtaPackageIdLSB()));
    DeviceId deviceId = new DeviceId(new UUID(msg.getDeviceIdMSB(), msg.getDeviceIdLSB()));
    TenantId tenantId = TenantId.fromUUID(new UUID(msg.getTenantIdMSB(), msg.getTenantIdLSB()));
    OtaPackageType firmwareType = OtaPackageType.valueOf(msg.getType());
    long ts = msg.getTs();
    Device device = deviceService.findDeviceById(tenantId, deviceId);
    if (device == null) {
        log.warn("[{}] [{}] Device was removed during firmware update msg was queued!", tenantId, deviceId);
    } else {
        OtaPackageId currentOtaPackageId = OtaPackageUtil.getOtaPackageId(device, firmwareType);
        if (currentOtaPackageId == null) {
            DeviceProfile deviceProfile = deviceProfileService.findDeviceProfileById(tenantId, device.getDeviceProfileId());
            currentOtaPackageId = OtaPackageUtil.getOtaPackageId(deviceProfile, firmwareType);
        }
        if (targetOtaPackageId.equals(currentOtaPackageId)) {
            update(device, otaPackageService.findOtaPackageInfoById(device.getTenantId(), targetOtaPackageId), ts);
            isSuccess = true;
        } else {
            log.warn("[{}] [{}] Can`t update firmware for the device, target firmwareId: [{}], current firmwareId: [{}]!", tenantId, deviceId, targetOtaPackageId, currentOtaPackageId);
        }
    }
    return isSuccess;
}
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) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) UUID(java.util.UUID)

Example 4 with OtaPackageId

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

the class DeviceProfileEntity method toData.

@Override
public DeviceProfile toData() {
    DeviceProfile deviceProfile = new DeviceProfile(new DeviceProfileId(this.getUuid()));
    deviceProfile.setCreatedTime(createdTime);
    if (tenantId != null) {
        deviceProfile.setTenantId(TenantId.fromUUID(tenantId));
    }
    deviceProfile.setName(name);
    deviceProfile.setType(type);
    deviceProfile.setImage(image);
    deviceProfile.setTransportType(transportType);
    deviceProfile.setProvisionType(provisionType);
    deviceProfile.setDescription(description);
    deviceProfile.setDefault(isDefault);
    deviceProfile.setProfileData(JacksonUtil.convertValue(profileData, DeviceProfileData.class));
    if (defaultRuleChainId != null) {
        deviceProfile.setDefaultRuleChainId(new RuleChainId(defaultRuleChainId));
    }
    if (defaultDashboardId != null) {
        deviceProfile.setDefaultDashboardId(new DashboardId(defaultDashboardId));
    }
    deviceProfile.setDefaultQueueName(defaultQueueName);
    deviceProfile.setProvisionDeviceKey(provisionDeviceKey);
    if (firmwareId != null) {
        deviceProfile.setFirmwareId(new OtaPackageId(firmwareId));
    }
    if (softwareId != null) {
        deviceProfile.setSoftwareId(new OtaPackageId(softwareId));
    }
    return deviceProfile;
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) DashboardId(org.thingsboard.server.common.data.id.DashboardId) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData)

Example 5 with OtaPackageId

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

the class OtaPackageInfoEntity method toData.

@Override
public OtaPackageInfo toData() {
    OtaPackageInfo otaPackageInfo = new OtaPackageInfo(new OtaPackageId(id));
    otaPackageInfo.setCreatedTime(createdTime);
    otaPackageInfo.setTenantId(TenantId.fromUUID(tenantId));
    if (deviceProfileId != null) {
        otaPackageInfo.setDeviceProfileId(new DeviceProfileId(deviceProfileId));
    }
    otaPackageInfo.setType(type);
    otaPackageInfo.setTitle(title);
    otaPackageInfo.setVersion(version);
    otaPackageInfo.setTag(tag);
    otaPackageInfo.setUrl(url);
    otaPackageInfo.setFileName(fileName);
    otaPackageInfo.setContentType(contentType);
    otaPackageInfo.setChecksumAlgorithm(checksumAlgorithm);
    otaPackageInfo.setChecksum(checksum);
    otaPackageInfo.setDataSize(dataSize);
    otaPackageInfo.setAdditionalInfo(additionalInfo);
    otaPackageInfo.setHasData(hasData);
    return otaPackageInfo;
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo)

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