Search in sources :

Example 1 with OtaPackageType

use of org.thingsboard.server.common.data.ota.OtaPackageType 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 2 with OtaPackageType

use of org.thingsboard.server.common.data.ota.OtaPackageType 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 3 with OtaPackageType

use of org.thingsboard.server.common.data.ota.OtaPackageType in project thingsboard by thingsboard.

the class DefaultOtaPackageStateService method update.

private void update(Device device, OtaPackageInfo otaPackage, long ts) {
    TenantId tenantId = device.getTenantId();
    DeviceId deviceId = device.getId();
    OtaPackageType otaPackageType = otaPackage.getType();
    BasicTsKvEntry status = new BasicTsKvEntry(System.currentTimeMillis(), new StringDataEntry(getTelemetryKey(otaPackageType, STATE), OtaPackageUpdateStatus.INITIATED.name()));
    telemetryService.saveAndNotify(tenantId, deviceId, Collections.singletonList(status), new FutureCallback<>() {

        @Override
        public void onSuccess(@Nullable Void tmp) {
            log.trace("[{}] Success save telemetry with target {} for device!", deviceId, otaPackage);
            updateAttributes(device, otaPackage, ts, tenantId, deviceId, otaPackageType);
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("[{}] Failed to save telemetry with target {} for device!", deviceId, otaPackage, t);
            updateAttributes(device, otaPackage, ts, tenantId, deviceId, otaPackageType);
        }
    });
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) DeviceId(org.thingsboard.server.common.data.id.DeviceId)

Example 4 with OtaPackageType

use of org.thingsboard.server.common.data.ota.OtaPackageType in project thingsboard by thingsboard.

the class OtaPackageTransportResource method processAccessTokenRequest.

private void processAccessTokenRequest(CoapExchange exchange, Request request) {
    Optional<DeviceTokenCredentials> credentials = decodeCredentials(request);
    if (credentials.isEmpty()) {
        exchange.respond(CoAP.ResponseCode.UNAUTHORIZED);
        return;
    }
    transportService.process(DeviceTransportType.COAP, TransportProtos.ValidateDeviceTokenRequestMsg.newBuilder().setToken(credentials.get().getCredentialsId()).build(), new CoapDeviceAuthCallback(exchange, (msg, deviceProfile) -> {
        getOtaPackageCallback(msg, exchange, otaPackageType);
    }));
}
Also used : CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Response(org.eclipse.californium.core.coap.Response) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Exchange(org.eclipse.californium.core.network.Exchange) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) StringUtils(org.thingsboard.server.common.data.StringUtils) UUID(java.util.UUID) TenantId(org.thingsboard.server.common.data.id.TenantId) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Request(org.eclipse.californium.core.coap.Request) Resource(org.eclipse.californium.core.server.resources.Resource) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) CoAP(org.eclipse.californium.core.coap.CoAP) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Optional(java.util.Optional) ValidateDeviceCredentialsResponse(org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse) SessionInfoCreator(org.thingsboard.server.common.transport.auth.SessionInfoCreator) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback)

Aggregations

DeviceId (org.thingsboard.server.common.data.id.DeviceId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 OtaPackageType (org.thingsboard.server.common.data.ota.OtaPackageType)4 UUID (java.util.UUID)3 Device (org.thingsboard.server.common.data.Device)2 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)2 OtaPackageId (org.thingsboard.server.common.data.id.OtaPackageId)2 List (java.util.List)1 Optional (java.util.Optional)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CoAP (org.eclipse.californium.core.coap.CoAP)1 Request (org.eclipse.californium.core.coap.Request)1 Response (org.eclipse.californium.core.coap.Response)1 Exchange (org.eclipse.californium.core.network.Exchange)1 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)1 Resource (org.eclipse.californium.core.server.resources.Resource)1 DeviceTransportType (org.thingsboard.server.common.data.DeviceTransportType)1 OtaPackage (org.thingsboard.server.common.data.OtaPackage)1 OtaPackageInfo (org.thingsboard.server.common.data.OtaPackageInfo)1 StringUtils (org.thingsboard.server.common.data.StringUtils)1