Search in sources :

Example 1 with PowerSavingConfiguration

use of org.thingsboard.server.common.data.device.data.PowerSavingConfiguration in project thingsboard by thingsboard.

the class DefaultTransportApiService method getDeviceInfoProto.

private DeviceInfoProto getDeviceInfoProto(Device device) throws JsonProcessingException {
    DeviceInfoProto.Builder builder = DeviceInfoProto.newBuilder().setTenantIdMSB(device.getTenantId().getId().getMostSignificantBits()).setTenantIdLSB(device.getTenantId().getId().getLeastSignificantBits()).setCustomerIdMSB(Optional.ofNullable(device.getCustomerId()).map(customerId -> customerId.getId().getMostSignificantBits()).orElse(0L)).setCustomerIdLSB(Optional.ofNullable(device.getCustomerId()).map(customerId -> customerId.getId().getLeastSignificantBits()).orElse(0L)).setDeviceIdMSB(device.getId().getId().getMostSignificantBits()).setDeviceIdLSB(device.getId().getId().getLeastSignificantBits()).setDeviceName(device.getName()).setDeviceType(device.getType()).setDeviceProfileIdMSB(device.getDeviceProfileId().getId().getMostSignificantBits()).setDeviceProfileIdLSB(device.getDeviceProfileId().getId().getLeastSignificantBits()).setAdditionalInfo(mapper.writeValueAsString(device.getAdditionalInfo()));
    PowerSavingConfiguration psmConfiguration = null;
    switch(device.getDeviceData().getTransportConfiguration().getType()) {
        case LWM2M:
            psmConfiguration = (Lwm2mDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
            break;
        case COAP:
            psmConfiguration = (CoapDeviceTransportConfiguration) device.getDeviceData().getTransportConfiguration();
            break;
    }
    if (psmConfiguration != null) {
        PowerMode powerMode = psmConfiguration.getPowerMode();
        if (powerMode != null) {
            builder.setPowerMode(powerMode.name());
            if (powerMode.equals(PowerMode.PSM)) {
                builder.setPsmActivityTimer(checkLong(psmConfiguration.getPsmActivityTimer()));
            } else if (powerMode.equals(PowerMode.E_DRX)) {
                builder.setEdrxCycle(checkLong(psmConfiguration.getEdrxCycle()));
                builder.setPagingTransmissionWindow(checkLong(psmConfiguration.getPagingTransmissionWindow()));
            }
        }
    }
    return builder.build();
}
Also used : ValidateDeviceCredentialsResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg) TbResource(org.thingsboard.server.common.data.TbResource) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) DeviceCredentialsService(org.thingsboard.server.dao.device.DeviceCredentialsService) ProvisionRequest(org.thingsboard.server.dao.device.provision.ProvisionRequest) TenantId(org.thingsboard.server.common.data.id.TenantId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) DeviceProvisionService(org.thingsboard.server.dao.device.DeviceProvisionService) GetDeviceCredentialsRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetDeviceCredentialsRequestMsg) OtaPackageDataCache(org.thingsboard.server.cache.ota.OtaPackageDataCache) ProvisionDeviceProfileCredentials(org.thingsboard.server.common.data.device.profile.ProvisionDeviceProfileCredentials) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) GetEntityProfileRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileRequestMsg) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) EntityType(org.thingsboard.server.common.data.EntityType) JsonNode(com.fasterxml.jackson.databind.JsonNode) TbResourceService(org.thingsboard.server.service.resource.TbResourceService) DeviceId(org.thingsboard.server.common.data.id.DeviceId) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) PageLink(org.thingsboard.server.common.data.page.PageLink) OtaPackage(org.thingsboard.server.common.data.OtaPackage) ProvisionDeviceRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ProvisionDeviceRequestMsg) StringUtils(org.thingsboard.server.common.data.StringUtils) OtaPackageUtil(org.thingsboard.server.common.data.ota.OtaPackageUtil) GetDeviceRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetDeviceRequestMsg) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TransportApiRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportApiRequestMsg) UUID(java.util.UUID) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) Slf4j(lombok.extern.slf4j.Slf4j) ProvisionDeviceCredentialsData(org.thingsboard.server.common.data.device.credentials.ProvisionDeviceCredentialsData) ValidateDeviceLwM2MCredentialsRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceLwM2MCredentialsRequestMsg) TbClusterService(org.thingsboard.server.cluster.TbClusterService) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Optional(java.util.Optional) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) TbApiUsageStateService(org.thingsboard.server.service.apiusage.TbApiUsageStateService) CustomerId(org.thingsboard.server.common.data.id.CustomerId) OtaPackageService(org.thingsboard.server.dao.ota.OtaPackageService) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) GetOrCreateDeviceFromGatewayResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayResponseMsg) TransportApiResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.TransportApiResponseMsg) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) GetSnmpDevicesRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetSnmpDevicesRequestMsg) Device(org.thingsboard.server.common.data.Device) JacksonUtil(org.thingsboard.common.util.JacksonUtil) CoapDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.CoapDeviceTransportConfiguration) GetOrCreateDeviceFromGatewayRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayRequestMsg) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DataDecodingEncodingService(org.thingsboard.server.common.transport.util.DataDecodingEncodingService) ConcurrentMap(java.util.concurrent.ConcurrentMap) EncryptionUtil(org.thingsboard.server.common.msg.EncryptionUtil) RelationService(org.thingsboard.server.dao.relation.RelationService) TenantProfile(org.thingsboard.server.common.data.TenantProfile) DeviceService(org.thingsboard.server.dao.device.DeviceService) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) ValidateDeviceTokenRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceTokenRequestMsg) ProvisionResponse(org.thingsboard.server.dao.device.provision.ProvisionResponse) PASSWORD_MISMATCH(org.thingsboard.server.service.transport.BasicCredentialsValidationResult.PASSWORD_MISMATCH) GetSnmpDevicesResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetSnmpDevicesResponseMsg) PowerSavingConfiguration(org.thingsboard.server.common.data.device.data.PowerSavingConfiguration) DataConstants(org.thingsboard.server.common.data.DataConstants) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DeviceCredentialsType(org.thingsboard.server.common.data.security.DeviceCredentialsType) DbCallbackExecutorService(org.thingsboard.server.service.executors.DbCallbackExecutorService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) DeviceInfoProto(org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Lwm2mDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.Lwm2mDeviceTransportConfiguration) ValidateDeviceX509CertRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceX509CertRequestMsg) VALID(org.thingsboard.server.service.transport.BasicCredentialsValidationResult.VALID) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) TbDeviceProfileCache(org.thingsboard.server.service.profile.TbDeviceProfileCache) OtaPackageType(org.thingsboard.server.common.data.ota.OtaPackageType) Futures(com.google.common.util.concurrent.Futures) Lock(java.util.concurrent.locks.Lock) PowerMode(org.thingsboard.server.common.data.device.data.PowerMode) PageData(org.thingsboard.server.common.data.page.PageData) TbMsgDataType(org.thingsboard.server.common.msg.TbMsgDataType) GetResourceRequestMsg(org.thingsboard.server.gen.transport.TransportProtos.GetResourceRequestMsg) GetEntityProfileResponseMsg(org.thingsboard.server.gen.transport.TransportProtos.GetEntityProfileResponseMsg) ResourceType(org.thingsboard.server.common.data.ResourceType) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) PowerMode(org.thingsboard.server.common.data.device.data.PowerMode) DeviceInfoProto(org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto) PowerSavingConfiguration(org.thingsboard.server.common.data.device.data.PowerSavingConfiguration)

Example 2 with PowerSavingConfiguration

use of org.thingsboard.server.common.data.device.data.PowerSavingConfiguration in project thingsboard by thingsboard.

the class DefaultCoapClientContext method onUplink.

private void onUplink(TbCoapClientState client, boolean notifyOtherServers, long uplinkTs) {
    PowerMode powerMode = client.getPowerMode();
    PowerSavingConfiguration profileSettings = null;
    if (powerMode == null) {
        var clientProfile = getProfile(client.getProfileId());
        if (clientProfile.isPresent()) {
            profileSettings = clientProfile.get().getClientSettings();
            if (profileSettings != null) {
                powerMode = profileSettings.getPowerMode();
            }
        }
    }
    if (powerMode == null || PowerMode.DRX.equals(powerMode)) {
        client.updateLastUplinkTime(uplinkTs);
        return;
    }
    client.lock();
    try {
        long uplinkTime = client.updateLastUplinkTime(uplinkTs);
        long timeout = getTimeout(client, powerMode, profileSettings);
        Future<Void> sleepTask = client.getSleepTask();
        if (sleepTask != null) {
            sleepTask.cancel(false);
        }
        Future<Void> task = transportContext.getScheduler().schedule(() -> {
            if (uplinkTime == client.getLastUplinkTime()) {
                asleep(client);
            }
            return null;
        }, timeout, TimeUnit.MILLISECONDS);
        client.setSleepTask(task);
        if (notifyOtherServers && partitionService.countTransportsByType(DataConstants.COAP_TRANSPORT_NAME) > 1) {
            transportService.notifyAboutUplink(getNewSyncSession(client), TransportProtos.UplinkNotificationMsg.newBuilder().setUplinkTs(uplinkTime).build(), TransportServiceCallback.EMPTY);
        }
    } finally {
        client.unlock();
    }
}
Also used : PowerMode(org.thingsboard.server.common.data.device.data.PowerMode) PowerSavingConfiguration(org.thingsboard.server.common.data.device.data.PowerSavingConfiguration)

Example 3 with PowerSavingConfiguration

use of org.thingsboard.server.common.data.device.data.PowerSavingConfiguration in project thingsboard by thingsboard.

the class DefaultCoapClientContext method isDownlinkAllowed.

private boolean isDownlinkAllowed(TbCoapClientState client) {
    PowerMode powerMode = client.getPowerMode();
    PowerSavingConfiguration profileSettings = null;
    if (powerMode == null) {
        var clientProfile = getProfile(client.getProfileId());
        if (clientProfile.isPresent()) {
            profileSettings = clientProfile.get().getClientSettings();
            if (profileSettings != null) {
                powerMode = profileSettings.getPowerMode();
            }
        }
    }
    if (powerMode == null || PowerMode.DRX.equals(powerMode)) {
        return true;
    }
    client.lock();
    long timeSinceLastUplink = System.currentTimeMillis() - client.getLastUplinkTime();
    try {
        if (PowerMode.PSM.equals(powerMode)) {
            Long psmActivityTimer = client.getPsmActivityTimer();
            if (psmActivityTimer == null && profileSettings != null) {
                psmActivityTimer = profileSettings.getPsmActivityTimer();
            }
            if (psmActivityTimer == null || psmActivityTimer == 0L) {
                psmActivityTimer = config.getPsmActivityTimer();
            }
            return timeSinceLastUplink <= psmActivityTimer;
        } else {
            Long pagingTransmissionWindow = client.getPagingTransmissionWindow();
            if (pagingTransmissionWindow == null && profileSettings != null) {
                pagingTransmissionWindow = profileSettings.getPagingTransmissionWindow();
            }
            if (pagingTransmissionWindow == null || pagingTransmissionWindow == 0L) {
                pagingTransmissionWindow = config.getPagingTransmissionWindow();
            }
            boolean allowed = timeSinceLastUplink <= pagingTransmissionWindow;
            if (!allowed) {
                return client.checkFirstDownlink();
            } else {
                return true;
            }
        }
    } finally {
        client.unlock();
    }
}
Also used : PowerMode(org.thingsboard.server.common.data.device.data.PowerMode) PowerSavingConfiguration(org.thingsboard.server.common.data.device.data.PowerSavingConfiguration)

Aggregations

PowerMode (org.thingsboard.server.common.data.device.data.PowerMode)3 PowerSavingConfiguration (org.thingsboard.server.common.data.device.data.PowerSavingConfiguration)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 ByteString (com.google.protobuf.ByteString)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Service (org.springframework.stereotype.Service)1