Search in sources :

Example 1 with LwM2MModelConfig

use of org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig in project thingsboard by thingsboard.

the class DefaultLwM2mUplinkMsgHandler method onDeviceProfileUpdate.

// TODO: review and optimize the logic to minimize number of the requests to device.
private void onDeviceProfileUpdate(List<LwM2mClient> clients, Lwm2mDeviceProfileTransportConfiguration oldProfile, DeviceProfile deviceProfile) {
    if (clientContext.profileUpdate(deviceProfile) != null) {
        TelemetryMappingConfiguration oldTelemetryParams = oldProfile.getObserveAttr();
        Set<String> attributeSetOld = oldTelemetryParams.getAttribute();
        Set<String> telemetrySetOld = oldTelemetryParams.getTelemetry();
        Set<String> observeOld = oldTelemetryParams.getObserve();
        Map<String, String> keyNameOld = oldTelemetryParams.getKeyName();
        Map<String, ObjectAttributes> attributeLwm2mOld = oldTelemetryParams.getAttributeLwm2m();
        var newProfile = clientContext.getProfile(deviceProfile.getUuidId());
        TelemetryMappingConfiguration newTelemetryParams = newProfile.getObserveAttr();
        Set<String> attributeSetNew = newTelemetryParams.getAttribute();
        Set<String> telemetrySetNew = newTelemetryParams.getTelemetry();
        Set<String> observeNew = newTelemetryParams.getObserve();
        Map<String, String> keyNameNew = newTelemetryParams.getKeyName();
        Map<String, ObjectAttributes> attributeLwm2mNew = newTelemetryParams.getAttributeLwm2m();
        Set<String> observeToAdd = diffSets(observeOld, observeNew);
        Set<String> observeToRemove = diffSets(observeNew, observeOld);
        Set<String> newObjectsToRead = new HashSet<>();
        Set<String> newObjectsToCancelRead = new HashSet<>();
        if (!attributeSetOld.equals(attributeSetNew)) {
            newObjectsToRead.addAll(diffSets(attributeSetOld, attributeSetNew));
            newObjectsToCancelRead.addAll(diffSets(attributeSetNew, attributeSetOld));
        }
        if (!telemetrySetOld.equals(telemetrySetNew)) {
            newObjectsToRead.addAll(diffSets(telemetrySetOld, telemetrySetNew));
            newObjectsToCancelRead.addAll(diffSets(telemetrySetNew, telemetrySetOld));
        }
        if (!keyNameOld.equals(keyNameNew)) {
            ParametersAnalyzeResult keyNameChange = this.getAnalyzerKeyName(keyNameOld, keyNameNew);
            newObjectsToRead.addAll(keyNameChange.getPathPostParametersAdd());
        }
        ParametersAnalyzeResult analyzerParameters = getAttributesAnalyzer(attributeLwm2mOld, attributeLwm2mNew);
        clients.forEach(client -> {
            LwM2MModelConfig modelConfig = new LwM2MModelConfig(client.getEndpoint());
            modelConfig.getToRead().addAll(diffSets(observeToAdd, newObjectsToRead));
            modelConfig.getToCancelRead().addAll(newObjectsToCancelRead);
            modelConfig.getToCancelObserve().addAll(observeToRemove);
            modelConfig.getToObserve().addAll(observeToAdd);
            Set<String> clientObjects = clientContext.getSupportedIdVerInClient(client);
            Set<String> pathToAdd = analyzerParameters.getPathPostParametersAdd().stream().filter(target -> clientObjects.contains("/" + target.split(LWM2M_SEPARATOR_PATH)[1])).collect(Collectors.toUnmodifiableSet());
            modelConfig.getAttributesToAdd().putAll(pathToAdd.stream().collect(Collectors.toMap(t -> t, attributeLwm2mNew::get)));
            Set<String> pathToRemove = analyzerParameters.getPathPostParametersDel().stream().filter(target -> clientObjects.contains("/" + target.split(LWM2M_SEPARATOR_PATH)[1])).collect(Collectors.toUnmodifiableSet());
            modelConfig.getAttributesToRemove().addAll(pathToRemove);
            modelConfigService.sendUpdates(client, modelConfig);
        });
        // update value in fwInfo
        OtherConfiguration newLwM2mSettings = newProfile.getClientLwM2mSettings();
        OtherConfiguration oldLwM2mSettings = oldProfile.getClientLwM2mSettings();
        if (!newLwM2mSettings.getFwUpdateStrategy().equals(oldLwM2mSettings.getFwUpdateStrategy()) || (StringUtils.isNotEmpty(newLwM2mSettings.getFwUpdateResource()) && !newLwM2mSettings.getFwUpdateResource().equals(oldLwM2mSettings.getFwUpdateResource()))) {
            clients.forEach(lwM2MClient -> otaService.onFirmwareStrategyUpdate(lwM2MClient, newLwM2mSettings));
        }
        if (!newLwM2mSettings.getSwUpdateStrategy().equals(oldLwM2mSettings.getSwUpdateStrategy()) || (StringUtils.isNotEmpty(newLwM2mSettings.getSwUpdateResource()) && !newLwM2mSettings.getSwUpdateResource().equals(oldLwM2mSettings.getSwUpdateResource()))) {
            clients.forEach(lwM2MClient -> otaService.onCurrentSoftwareStrategyUpdate(lwM2MClient, newLwM2mSettings));
        }
    }
}
Also used : LwM2MTransportServerConfig(org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig) LwM2MTelemetryLogService(org.thingsboard.server.transport.lwm2m.server.log.LwM2MTelemetryLogService) TypeToken(com.google.gson.reflect.TypeToken) SW_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_VER_ID) TbLwM2MLatchCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MLatchCallback) LwM2MSessionManager(org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) TbLwM2MReadRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MReadRequest) LwM2MModelConfig(org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig) GsonBuilder(com.google.gson.GsonBuilder) TenantId(org.thingsboard.server.common.data.id.TenantId) LwM2MExecutorAwareService(org.thingsboard.server.transport.lwm2m.server.common.LwM2MExecutorAwareService) TbLwM2MReadCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MReadCallback) Observation(org.eclipse.leshan.core.observation.Observation) Map(java.util.Map) SW_3_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_3_VER_ID) LwM2mClientContext(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) TbLwM2mSecurityStore(org.thingsboard.server.transport.lwm2m.server.store.TbLwM2mSecurityStore) StringUtils(org.thingsboard.server.common.data.StringUtils) OtaPackageUtil(org.thingsboard.server.common.data.ota.OtaPackageUtil) Set(java.util.Set) FW_RESULT_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_RESULT_ID) TbLwM2MWriteAttributesRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttributesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Slf4j(lombok.extern.slf4j.Slf4j) LOG_LWM2M_INFO(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_INFO) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) Lazy(org.springframework.context.annotation.Lazy) LwM2MTransportUtil(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient) LwM2mSingleResource(org.eclipse.leshan.core.node.LwM2mSingleResource) OtherConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.OtherConfiguration) LwM2mVersionedModelProvider(org.thingsboard.server.transport.lwm2m.server.LwM2mVersionedModelProvider) TransportServiceCallback(org.thingsboard.server.common.transport.TransportServiceCallback) Device(org.thingsboard.server.common.data.Device) TbLwM2mTransportComponent(org.thingsboard.server.queue.util.TbLwM2mTransportComponent) ArrayList(java.util.ArrayList) Mode(org.eclipse.leshan.core.request.WriteRequest.Mode) TbLwM2MObserveCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MObserveCallback) ReadCompositeResponse(org.eclipse.leshan.core.response.ReadCompositeResponse) Service(org.springframework.stereotype.Service) LwM2MModelConfigService(org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfigService) ParametersAnalyzeResult(org.thingsboard.server.transport.lwm2m.server.client.ParametersAnalyzeResult) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) TelemetryMappingConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.TelemetryMappingConfiguration) Lwm2mDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration) LOG_LWM2M_ERROR(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_ERROR) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2MTransportUtil.convertOtaUpdateValueToString(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertOtaUpdateValueToString) FW_STATE_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_STATE_ID) LwM2mOtaConvert(org.thingsboard.server.transport.lwm2m.server.LwM2mOtaConvert) LOG_LWM2M_WARN(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.LOG_LWM2M_WARN) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) LwM2MOtaUpdateService(org.thingsboard.server.transport.lwm2m.server.ota.LwM2MOtaUpdateService) LwM2mValueConverterImpl(org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl) FW_DELIVERY_METHOD(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_DELIVERY_METHOD) LwM2MTransportUtil.fromVersionedIdToObjectId(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.fromVersionedIdToObjectId) FW_3_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_3_VER_ID) LwM2MAttributesService(org.thingsboard.server.transport.lwm2m.server.attributes.LwM2MAttributesService) LwM2mTransportServerHelper(org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) JsonObject(com.google.gson.JsonObject) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DonAsynchron(org.thingsboard.common.util.DonAsynchron) LwM2MTransportUtil.convertObjectIdToVersionedId(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertObjectIdToVersionedId) TbLwM2MWriteAttributesCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttributesCallback) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Random(java.util.Random) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) DownlinkRequestCallback(org.thingsboard.server.transport.lwm2m.server.downlink.DownlinkRequestCallback) LwM2mTransportContext(org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext) PreDestroy(javax.annotation.PreDestroy) LwM2MClientState(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientState) Registration(org.eclipse.leshan.server.registration.Registration) Gson(com.google.gson.Gson) ResultsAddKeyValueProto(org.thingsboard.server.transport.lwm2m.server.client.ResultsAddKeyValueProto) SW_STATE_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_STATE_ID) FW_NAME_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_NAME_ID) LWM2M_SEPARATOR_PATH(org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH) LwM2mMultipleResource(org.eclipse.leshan.core.node.LwM2mMultipleResource) DeviceId(org.thingsboard.server.common.data.id.DeviceId) LwM2mDownlinkMsgHandler(org.thingsboard.server.transport.lwm2m.server.downlink.LwM2mDownlinkMsgHandler) SW_NAME_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_NAME_ID) ResponseCode(org.eclipse.leshan.core.ResponseCode) LwM2MClientStateException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException) Collection(java.util.Collection) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) TransportService(org.thingsboard.server.common.transport.TransportService) Collectors(java.util.stream.Collectors) CollectionsUtil.diffSets(org.thingsboard.common.util.CollectionsUtil.diffSets) FW_VER_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.FW_VER_ID) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) TbLwM2MObserveRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MObserveRequest) Optional(java.util.Optional) TbLwM2MDtlsSessionStore(org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MDtlsSessionStore) TbLwM2MCancelObserveCallback(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MCancelObserveCallback) ObjectAttributes(org.thingsboard.server.common.data.device.profile.lwm2m.ObjectAttributes) SW_RESULT_ID(org.thingsboard.server.transport.lwm2m.server.ota.DefaultLwM2MOtaUpdateService.SW_RESULT_ID) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) TbLwM2MCancelObserveRequest(org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MCancelObserveRequest) SessionInfoProto(org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) WriteCompositeRequest(org.eclipse.leshan.core.request.WriteCompositeRequest) TimeUnit(java.util.concurrent.TimeUnit) LwM2mResourceInstance(org.eclipse.leshan.core.node.LwM2mResourceInstance) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) RegistrationStore(org.eclipse.leshan.server.registration.RegistrationStore) Collections(java.util.Collections) OtherConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.OtherConfiguration) ParametersAnalyzeResult(org.thingsboard.server.transport.lwm2m.server.client.ParametersAnalyzeResult) TelemetryMappingConfiguration(org.thingsboard.server.common.data.device.profile.lwm2m.TelemetryMappingConfiguration) ObjectAttributes(org.thingsboard.server.common.data.device.profile.lwm2m.ObjectAttributes) LwM2MTransportUtil.convertOtaUpdateValueToString(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertOtaUpdateValueToString) LwM2MModelConfig(org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig) HashSet(java.util.HashSet)

Example 2 with LwM2MModelConfig

use of org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig in project thingsboard by thingsboard.

the class TbRedisLwM2MModelConfigStore method getAll.

@Override
public List<LwM2MModelConfig> getAll() {
    try (var connection = connectionFactory.getConnection()) {
        List<LwM2MModelConfig> configs = new ArrayList<>();
        ScanOptions scanOptions = ScanOptions.scanOptions().count(100).match(MODEL_EP + "*").build();
        List<Cursor<byte[]>> scans = new ArrayList<>();
        if (connection instanceof RedisClusterConnection) {
            ((RedisClusterConnection) connection).clusterGetNodes().forEach(node -> {
                scans.add(((RedisClusterConnection) connection).scan(node, scanOptions));
            });
        } else {
            scans.add(connection.scan(scanOptions));
        }
        scans.forEach(scan -> {
            scan.forEachRemaining(key -> {
                byte[] element = connection.get(key);
                configs.add(JacksonUtil.fromBytes(element, LwM2MModelConfig.class));
            });
        });
        return configs;
    }
}
Also used : RedisClusterConnection(org.springframework.data.redis.connection.RedisClusterConnection) ArrayList(java.util.ArrayList) ScanOptions(org.springframework.data.redis.core.ScanOptions) Cursor(org.springframework.data.redis.core.Cursor) LwM2MModelConfig(org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig)

Aggregations

ArrayList (java.util.ArrayList)2 LwM2MModelConfig (org.thingsboard.server.transport.lwm2m.server.model.LwM2MModelConfig)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 TypeToken (com.google.gson.reflect.TypeToken)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Random (java.util.Random)1 Set (java.util.Set)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1