Commit c230e3fdcd65111f22b93f088174ffcfdef5092c

Authored by nickAS21
Committed by Andrew Shvayka
1 parent 793e43e9

Lwm2m: front: add key to profile - refactoring-light start2

... ... @@ -581,7 +581,6 @@ transport:
581 581 request_pool_size: "${LWM2M_REQUEST_POOL_SIZE:100}"
582 582 request_error_pool_size: "${LWM2M_REQUEST_ERROR_POOL_SIZE:10}"
583 583 registered_pool_size: "${LWM2M_REGISTERED_POOL_SIZE:10}"
584   - client_update_value_after_connect: "${CLIENT_UPDATE_VALUE_AFTER_CONNECT:false}"
585 584 update_registered_pool_size: "${LWM2M_UPDATE_REGISTERED_POOL_SIZE:10}"
586 585 un_registered_pool_size: "${LWM2M_UN_REGISTERED_POOL_SIZE:10}"
587 586 secure:
... ...
... ... @@ -36,7 +36,7 @@ import org.nustaq.serialization.FSTConfiguration;
36 36 import org.thingsboard.server.common.data.DeviceProfile;
37 37 import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration;
38 38 import org.thingsboard.server.common.transport.TransportServiceCallback;
39   -import org.thingsboard.server.transport.lwm2m.server.client.AttrTelemetryObserveValue;
  39 +import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientProfile;
40 40 import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClient;
41 41
42 42 import java.io.File;
... ... @@ -70,7 +70,8 @@ public class LwM2MTransportHandler {
70 70
71 71 public static final long DEFAULT_TIMEOUT = 2 * 60 * 1000L; // 2min in ms
72 72 public static final String OBSERVE_ATTRIBUTE_TELEMETRY = "observeAttr";
73   - public static final String KEYNAME = "keyName";
  73 + public static final String CLIENT_LWM2M_SETTINGS = "clientLwM2mSettings";
  74 + public static final String KEY_NAME = "keyName";
74 75 public static final String OBSERVE = "observe";
75 76 public static final String BOOTSTRAP = "bootstrap";
76 77 public static final String SERVERS = "servers";
... ... @@ -187,18 +188,22 @@ public class LwM2MTransportHandler {
187 188 return null;
188 189 }
189 190
190   - public static AttrTelemetryObserveValue getNewProfileParameters(JsonObject profilesConfigData) {
191   - AttrTelemetryObserveValue attrTelemetryObserveValue = new AttrTelemetryObserveValue();
192   - attrTelemetryObserveValue.setPostKeyNameProfile(profilesConfigData.get(KEYNAME).getAsJsonObject());
193   - attrTelemetryObserveValue.setPostAttributeProfile(profilesConfigData.get(ATTRIBUTE).getAsJsonArray());
194   - attrTelemetryObserveValue.setPostTelemetryProfile(profilesConfigData.get(TELEMETRY).getAsJsonArray());
195   - attrTelemetryObserveValue.setPostObserveProfile(profilesConfigData.get(OBSERVE).getAsJsonArray());
196   - return attrTelemetryObserveValue;
  191 + public static LwM2MClientProfile getNewProfileParameters(JsonObject profilesConfigData) {
  192 + LwM2MClientProfile lwM2MClientProfile = new LwM2MClientProfile();
  193 + lwM2MClientProfile.setPostClientLwM2mSettings(profilesConfigData.get(CLIENT_LWM2M_SETTINGS).getAsJsonObject());
  194 + lwM2MClientProfile.setPostKeyNameProfile(profilesConfigData.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(KEY_NAME).getAsJsonObject());
  195 + lwM2MClientProfile.setPostAttributeProfile(profilesConfigData.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(ATTRIBUTE).getAsJsonArray());
  196 + lwM2MClientProfile.setPostTelemetryProfile(profilesConfigData.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(TELEMETRY).getAsJsonArray());
  197 + lwM2MClientProfile.setPostObserveProfile(profilesConfigData.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(OBSERVE).getAsJsonArray());
  198 + return lwM2MClientProfile;
197 199 }
198 200
199 201 /**
200 202 * @return deviceProfileBody with Observe&Attribute&Telemetry From Thingsboard
201   - * Example: with pathResource (use only pathResource)
  203 + * Example:
  204 + * property: {"clientLwM2mSettings": {
  205 + * clientUpdateValueAfterConnect: false;
  206 + * }
202 207 * property: "observeAttr"
203 208 * {"keyName": {
204 209 * "/3/0/1": "modelNumber",
... ... @@ -209,15 +214,14 @@ public class LwM2MTransportHandler {
209 214 * "telemetry":["/1/0/1","/2/0/1","/6/0/1"],
210 215 * "observe":["/2/0","/2/0/0","/4/0/2"]}
211 216 */
212   - public static JsonObject getObserveAttrTelemetryFromThingsboard(DeviceProfile deviceProfile) {
  217 + public static LwM2MClientProfile getLwM2MClientProfileFromThingsboard(DeviceProfile deviceProfile) {
213 218 if (deviceProfile != null && ((Lwm2mDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration()).getProperties().size() > 0) {
214   -// Lwm2mDeviceProfileTransportConfiguration lwm2mDeviceProfileTransportConfiguration = (Lwm2mDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration();
215   - Object observeAttr = ((Lwm2mDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration()).getProperties();
  219 + Object profile = ((Lwm2mDeviceProfileTransportConfiguration) deviceProfile.getProfileData().getTransportConfiguration()).getProperties();
216 220 try {
217 221 ObjectMapper mapper = new ObjectMapper();
218   - String observeAttrStr = mapper.writeValueAsString(observeAttr);
219   - JsonObject objectMsg = (observeAttrStr != null) ? validateJson(observeAttrStr) : null;
220   - return (getValidateCredentialsBodyFromThingsboard(objectMsg)) ? objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject() : null;
  222 + String profileStr = mapper.writeValueAsString(profile);
  223 + JsonObject profileJson = (profileStr != null) ? validateJson(profileStr) : null;
  224 + return (getValidateCredentialsBodyFromThingsboard(profileJson)) ? LwM2MTransportHandler.getNewProfileParameters(profileJson) : null;
221 225 } catch (IOException e) {
222 226 log.error("", e);
223 227 }
... ... @@ -240,15 +244,23 @@ public class LwM2MTransportHandler {
240 244 return null;
241 245 }
242 246
  247 + public static boolean getClientUpdateValueAfterConnect (LwM2MClientProfile profile) {
  248 + return profile.getPostClientLwM2mSettings().getAsJsonObject().has("clientUpdateValueAfterConnect") &&
  249 + profile.getPostClientLwM2mSettings().getAsJsonObject().get("clientUpdateValueAfterConnect").getAsBoolean();
  250 + }
  251 +
243 252 private static boolean getValidateCredentialsBodyFromThingsboard(JsonObject objectMsg) {
244 253 return (objectMsg != null &&
245 254 !objectMsg.isJsonNull() &&
  255 + objectMsg.has(CLIENT_LWM2M_SETTINGS) &&
  256 + !objectMsg.get(CLIENT_LWM2M_SETTINGS).isJsonNull() &&
  257 + objectMsg.get(CLIENT_LWM2M_SETTINGS).isJsonObject() &&
246 258 objectMsg.has(OBSERVE_ATTRIBUTE_TELEMETRY) &&
247 259 !objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).isJsonNull() &&
248 260 objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).isJsonObject() &&
249   - objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().has(KEYNAME) &&
250   - !objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(KEYNAME).isJsonNull() &&
251   - objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(KEYNAME).isJsonObject() &&
  261 + objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().has(KEY_NAME) &&
  262 + !objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(KEY_NAME).isJsonNull() &&
  263 + objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(KEY_NAME).isJsonObject() &&
252 264 objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().has(ATTRIBUTE) &&
253 265 !objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(ATTRIBUTE).isJsonNull() &&
254 266 objectMsg.get(OBSERVE_ATTRIBUTE_TELEMETRY).getAsJsonObject().get(ATTRIBUTE).isJsonArray() &&
... ...
... ... @@ -226,7 +226,7 @@ public class LwM2MTransportRequest {
226 226 String msg = String.format(LOG_LW2M_ERROR + ": sendRequest: Resource path - %s msg No SendRequest to Client", target);
227 227 service.sentLogsToThingsboard(msg, registration);
228 228 log.error("[{}] - [{}] No SendRequest", target);
229   - this.handleResponseError(registration, target, lwM2MClient, isDelayedUpdate);
  229 + this.handleResponseError(registration, target, lwM2MClient, true);
230 230
231 231 }
232 232 }
... ...
... ... @@ -21,11 +21,10 @@ import com.google.gson.JsonElement;
21 21 import com.google.gson.JsonObject;
22 22 import lombok.extern.slf4j.Slf4j;
23 23 import org.eclipse.leshan.core.model.ResourceModel;
24   -import org.eclipse.leshan.core.node.LwM2mMultipleResource;
25 24 import org.eclipse.leshan.core.node.LwM2mObject;
26 25 import org.eclipse.leshan.core.node.LwM2mObjectInstance;
27 26 import org.eclipse.leshan.core.node.LwM2mPath;
28   -import org.eclipse.leshan.core.node.LwM2mSingleResource;
  27 +import org.eclipse.leshan.core.node.LwM2mResource;
29 28 import org.eclipse.leshan.core.observation.Observation;
30 29 import org.eclipse.leshan.core.request.ContentFormat;
31 30 import org.eclipse.leshan.core.request.WriteRequest;
... ... @@ -47,8 +46,8 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionEvent;
47 46 import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto;
48 47 import org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCredentialsProto;
49 48 import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg;
50   -import org.thingsboard.server.transport.lwm2m.server.client.AttrTelemetryObserveValue;
51 49 import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClient;
  50 +import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientProfile;
52 51 import org.thingsboard.server.transport.lwm2m.server.client.ResourceValue;
53 52 import org.thingsboard.server.transport.lwm2m.server.client.ResultsAnalyzerParameters;
54 53 import org.thingsboard.server.transport.lwm2m.server.secure.LwM2mInMemorySecurityStore;
... ... @@ -71,7 +70,6 @@ import java.util.concurrent.ConcurrentMap;
71 70 import java.util.concurrent.ExecutorService;
72 71 import java.util.concurrent.Executors;
73 72 import java.util.concurrent.TimeUnit;
74   -import java.util.concurrent.atomic.AtomicBoolean;
75 73 import java.util.concurrent.locks.Lock;
76 74 import java.util.concurrent.locks.ReadWriteLock;
77 75 import java.util.concurrent.locks.ReentrantReadWriteLock;
... ... @@ -153,8 +151,8 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
153 151 lwM2MClient.setLwM2MTransportServiceImpl(this);
154 152 lwM2MClient.setSessionUuid(UUID.randomUUID());
155 153 this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client Registered", registration);
156   - this.setLwM2mFromClientValue(lwServer, registration, lwM2MClient);
157   - lwM2MClient.setClientUpdateValueAfterConnect(this.context.getCtxServer().isClientUpdateValueAfterConnect());
  154 + LwM2MClientProfile lwM2MClientProfile = lwM2mInMemorySecurityStore.getProfile(registration.getId());
  155 + this.setLwM2mFromClientValue(lwServer, registration, lwM2MClient, lwM2MClientProfile);
158 156 SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration);
159 157 if (sessionInfo != null) {
160 158 lwM2MClient.setDeviceUuid(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB()));
... ... @@ -165,7 +163,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
165 163 transportService.process(sessionInfo, DefaultTransportService.getSessionEventMsg(SessionEvent.OPEN), null);
166 164 transportService.process(sessionInfo, TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null);
167 165 this.sentLogsToThingsboard(LOG_LW2M_INFO + ": Client create after Registration", registration);
168   - if (this.context.getCtxServer().isClientUpdateValueAfterConnect()) {
  166 + if (LwM2MTransportHandler.getClientUpdateValueAfterConnect(lwM2MClientProfile)) {
169 167 this.putDelayedUpdateResourcesThingsboard(lwM2MClient);
170 168 }
171 169 } else {
... ... @@ -255,7 +253,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
255 253 * Removes a profile if not used in sessions
256 254 */
257 255 private void syncSessionsAndProfiles() {
258   - Map<UUID, AttrTelemetryObserveValue> profilesClone = lwM2mInMemorySecurityStore.getProfiles().entrySet()
  256 + Map<UUID, LwM2MClientProfile> profilesClone = lwM2mInMemorySecurityStore.getProfiles().entrySet()
259 257 .stream()
260 258 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
261 259 profilesClone.forEach((k, v) -> {
... ... @@ -285,9 +283,26 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
285 283 * @param registration - Registration LwM2M Client
286 284 * @param lwM2MClient - object with All parameters off client
287 285 */
288   - private void setLwM2mFromClientValue(LeshanServer lwServer, Registration registration, LwM2MClient lwM2MClient) {
  286 + private void setLwM2mFromClientValue(LeshanServer lwServer, Registration registration, LwM2MClient lwM2MClient, LwM2MClientProfile lwM2MClientProfile) {
289 287 // #1.1
290 288 // get all instances in client
  289 + Set<String> clientInstances = this.getAllInstancesInClient(registration);
  290 + if (clientInstances != null && LwM2MTransportHandler.getClientUpdateValueAfterConnect(lwM2MClientProfile)) {
  291 + lwM2MClient.getPendingRequests().addAll(clientInstances);
  292 + // #2
  293 + clientInstances.forEach(path -> {
  294 + lwM2MTransportRequest.sendAllRequest(lwServer, registration, path, GET_TYPE_OPER_READ, ContentFormat.TLV.getName(),
  295 + lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false);
  296 + });
  297 + } else {
  298 + // #1.2
  299 + this.onSentObserveToClient(lwServer, registration);
  300 + }
  301 +
  302 + }
  303 +
  304 + // get all instances in client
  305 + private Set<String> getAllInstancesInClient(Registration registration) {
291 306 Set<String> clientInstances = ConcurrentHashMap.newKeySet();
292 307 Arrays.stream(registration.getObjectLinks()).forEach(url -> {
293 308 LwM2mPath pathIds = new LwM2mPath(url.getUrl());
... ... @@ -295,27 +310,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
295 310 clientInstances.add(url.getUrl());
296 311 }
297 312 });
298   - if (this.context.getCtxServer().isClientUpdateValueAfterConnect()) {
299   - lwM2MClient.getPendingRequests().addAll(clientInstances);
300   - } else {
301   - // #1.2
302   - UUID profileUUid = lwM2mInMemorySecurityStore.getSessions().get(registration.getId()).getProfileUuid();
303   - JsonArray observeValue = lwM2mInMemorySecurityStore.getProfiles().get(profileUUid).getPostObserveProfile();
304   - observeValue.forEach(path -> {
305   - String[] resPath = path.getAsString().split("/");
306   - String instance = "/" + resPath[1] + "/" + resPath[2];
307   - if (clientInstances.contains(instance)) {
308   - lwM2MClient.getPendingRequests().add(path.getAsString());
309   - }
310   - }
311   - );
312   - }
313   -
314   - // #2
315   - lwM2MClient.getPendingRequests().forEach(path -> {
316   - lwM2MTransportRequest.sendAllRequest(lwServer, registration, path, GET_TYPE_OPER_READ, ContentFormat.TLV.getName(),
317   - lwM2MClient, null, null, this.context.getCtxServer().getTimeout(), false);
318   - });
  313 + return (clientInstances.size() > 0) ? clientInstances : null;
319 314 }
320 315
321 316 /**
... ... @@ -463,7 +458,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
463 458 * @return ArrayList keyNames from profile attr resources shared!!!! && IsWritable
464 459 */
465 460 private List<String> getNamesAttrFromProfileIsWritable(LwM2MClient lwM2MClient) {
466   - AttrTelemetryObserveValue profile = lwM2mInMemorySecurityStore.getProfile(lwM2MClient.getProfileUuid());
  461 + LwM2MClientProfile profile = lwM2mInMemorySecurityStore.getProfile(lwM2MClient.getProfileUuid());
467 462 Set attrSet = new Gson().fromJson(profile.getPostAttributeProfile(), Set.class);
468 463 ConcurrentMap<String, String> keyNamesMap = new Gson().fromJson(profile.getPostKeyNameProfile().toString(), ConcurrentHashMap.class);
469 464
... ... @@ -484,7 +479,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
484 479 * #1 - get AttrName/TelemetryName with value:
485 480 * #1.1 from Client
486 481 * #1.2 from LwM2MClient:
487   - * -- resourceId == path from AttrTelemetryObserveValue.postAttributeProfile/postTelemetryProfile/postObserveProfile
  482 + * -- resourceId == path from LwM2MClientProfile.postAttributeProfile/postTelemetryProfile/postObserveProfile
488 483 * -- AttrName/TelemetryName == resourceName from ModelObject.objectModel, value from ModelObject.instance.resource(resourceId)
489 484 * #2 - set Attribute/Telemetry
490 485 *
... ... @@ -539,8 +534,8 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
539 534 * (attributes/telemetry): new {name(Attr/Telemetry):value}
540 535 */
541 536 private void getParametersFromProfile(JsonObject attributes, JsonObject telemetry, Registration registration, Set<String> path) {
542   - AttrTelemetryObserveValue attrTelemetryObserveValue = lwM2mInMemorySecurityStore.getProfiles().get(lwM2mInMemorySecurityStore.getSessions().get(registration.getId()).getProfileUuid());
543   - attrTelemetryObserveValue.getPostAttributeProfile().forEach(p -> {
  537 + LwM2MClientProfile lwM2MClientProfile = lwM2mInMemorySecurityStore.getProfiles().get(lwM2mInMemorySecurityStore.getSessions().get(registration.getId()).getProfileUuid());
  538 + lwM2MClientProfile.getPostAttributeProfile().forEach(p -> {
544 539 LwM2mPath pathIds = new LwM2mPath(p.getAsString().toString());
545 540 if (pathIds.isResource()) {
546 541 if (path == null || path.contains(p.getAsString())) {
... ... @@ -548,7 +543,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
548 543 }
549 544 }
550 545 });
551   - attrTelemetryObserveValue.getPostTelemetryProfile().forEach(p -> {
  546 + lwM2MClientProfile.getPostTelemetryProfile().forEach(p -> {
552 547 LwM2mPath pathIds = new LwM2mPath(p.getAsString().toString());
553 548 if (pathIds.isResource()) {
554 549 if (path == null || path.contains(p.getAsString())) {
... ... @@ -609,20 +604,20 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
609 604 if (lwServer.getObservationService().getObservations(registration).size() > 0) {
610 605 this.setCancelObservations(lwServer, registration);
611 606 }
612   - UUID profileUUid = lwM2mInMemorySecurityStore.getSessions().get(registration.getId()).getProfileUuid();
613   - AttrTelemetryObserveValue attrTelemetryObserveValue = lwM2mInMemorySecurityStore.getProfiles().get(profileUUid);
614   - attrTelemetryObserveValue.getPostObserveProfile().forEach(p -> {
  607 + LwM2MClientProfile lwM2MClientProfile = lwM2mInMemorySecurityStore.getProfile(registration.getId());
  608 + Set<String> clientInstances = this.getAllInstancesInClient(registration);
  609 + lwM2MClientProfile.getPostObserveProfile().forEach(p -> {
615 610 // #1.1
616   - String target = (getValidateObserve(attrTelemetryObserveValue.getPostAttributeProfile(), p.getAsString().toString())) ?
617   - p.getAsString().toString() : (getValidateObserve(attrTelemetryObserveValue.getPostTelemetryProfile(), p.getAsString().toString())) ?
618   - p.getAsString().toString() : null;
619   - if (target != null) {
  611 + String target = p.getAsString().toString();
  612 + String[] resPath = target.split("/");
  613 + String instance = "/" + resPath[1] + "/" + resPath[2];
  614 + if (clientInstances.contains(instance)) {
620 615 // #2
621   - if (this.getResourceValueToString(lwM2mInMemorySecurityStore.getSessions().get(registration.getId()), target) != null) {
622   - lwM2MTransportRequest.sendAllRequest(lwServer, registration, target, GET_TYPE_OPER_OBSERVE,
623   - null, null, null, null, this.context.getCtxServer().getTimeout(),
624   - false);
625   - }
  616 +// if (this.getResourceValueToString(lwM2mInMemorySecurityStore.getSessions().get(registration.getId()), target) != null) {
  617 + lwM2MTransportRequest.sendAllRequest(lwServer, registration, target, GET_TYPE_OPER_OBSERVE,
  618 + null, null, null, null, this.context.getCtxServer().getTimeout(),
  619 + false);
  620 +// }
626 621 }
627 622 });
628 623 }
... ... @@ -648,19 +643,19 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
648 643 * @param path - recourse from postObserveProfile
649 644 * @return rez - true if path observe is in attribute/telemetry
650 645 */
651   - private boolean getValidateObserve(JsonElement parameters, String path) {
652   - AtomicBoolean rez = new AtomicBoolean(false);
653   - if (parameters.isJsonArray()) {
654   - parameters.getAsJsonArray().forEach(p -> {
655   - if (p.getAsString().toString().equals(path)) rez.set(true);
656   - }
657   - );
658   - } else if (parameters.isJsonObject()) {
659   - rez.set((parameters.getAsJsonObject().entrySet()).stream().map(json -> json.toString())
660   - .filter(path::equals).findAny().orElse(null) != null);
661   - }
662   - return rez.get();
663   - }
  646 +// private boolean getValidateObserve(JsonElement parameters, String path) {
  647 +// AtomicBoolean rez = new AtomicBoolean(false);
  648 +// if (parameters.isJsonArray()) {
  649 +// parameters.getAsJsonArray().forEach(p -> {
  650 +// if (p.getAsString().toString().equals(path)) rez.set(true);
  651 +// }
  652 +// );
  653 +// } else if (parameters.isJsonObject()) {
  654 +// rez.set((parameters.getAsJsonObject().entrySet()).stream().map(json -> json.toString())
  655 +// .filter(path::equals).findAny().orElse(null) != null);
  656 +// }
  657 +// return rez.get();
  658 +// }
664 659
665 660 /**
666 661 * Sending observe value to thingsboard from ObservationListener.onResponse: object, instance, SingleResource or MultipleResource
... ... @@ -673,15 +668,15 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
673 668 public void onObservationResponse(Registration registration, String path, ReadResponse response) {
674 669 if (response.getContent() != null) {
675 670 if (response.getContent() instanceof LwM2mObject) {
676   -// LwM2mObject content = (LwM2mObject) response.getContent();
  671 + LwM2mObject lwM2mObject = (LwM2mObject) response.getContent();
677 672 } else if (response.getContent() instanceof LwM2mObjectInstance) {
678   -// LwM2mObjectInstance content = (LwM2mObjectInstance) response.getContent();
679   - } else if (response.getContent() instanceof LwM2mSingleResource) {
680   - LwM2mSingleResource content = (LwM2mSingleResource) response.getContent();
681   - this.onObservationSetResourcesValue(registration, content.getValue(), null, path);
682   - } else if (response.getContent() instanceof LwM2mMultipleResource) {
683   - LwM2mMultipleResource content = (LwM2mMultipleResource) response.getContent();
684   - this.onObservationSetResourcesValue(registration, null, content.getValues(), path);
  673 + LwM2mObjectInstance lwM2mObjectInstance = (LwM2mObjectInstance) response.getContent();
  674 + } else if (response.getContent() instanceof LwM2mResource) {
  675 + LwM2mResource lwM2mResource = (LwM2mResource) response.getContent();
  676 + this.onObservationSetResourcesValue(registration, lwM2mResource, path);
  677 +// } else if (response.getContent() instanceof LwM2mMultipleResource) {
  678 +// LwM2mMultipleResource resource = (LwM2mMultipleResource) response.getContent();
  679 +// this.onObservationSetResourcesValue(registration, null, resource.getValues(), path);
685 680 }
686 681 }
687 682 }
... ... @@ -692,39 +687,17 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
692 687 * #2 Update new Resources (replace old Resource Value on new Resource Value)
693 688 *
694 689 * @param registration - Registration LwM2M Client
695   - * @param value - LwM2mSingleResource response.getContent()
696   - * @param values - LwM2mSingleResource response.getContent()
  690 + * @param - LwM2mSingleResource response.getContent()
  691 + * @param - LwM2mSingleResource response.getContent()
697 692 * @param path - resource
698 693 */
699   - private void onObservationSetResourcesValue(Registration registration, Object value, Map<Integer, ?> values, String path) {
700   - boolean isChange = false;
701   - try {
702   - writeLock.lock();
703   - // #1
704   - LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null);
705   - LwM2mPath pathIds = new LwM2mPath(path);
706   - ResourceModel.Type resModelType = context.getCtxServer().getResourceModelType(registration, pathIds);
707   - ResourceValue resValueOld = lwM2MClient.getResources().get(path);
708   - // #2
709   - if (resValueOld.isMultiInstances() && !values.toString().equals(resValueOld.getResourceValue().toString())) {
710   - lwM2MClient.getResources().get(path).setValues(values);
711   - isChange = true;
712   - } else if (!LwM2MTransportHandler.equalsResourceValue(resValueOld.getValue(), value, resModelType, pathIds)) {
713   - lwM2MClient.getResources().get(path).setValue(value);
714   - log.warn("upDateResize: [{}] [{}] [{}] [{}]", lwM2MClient.getEndPoint(), lwM2MClient.getResources().size(), value, path);
715   - isChange = true;
716   - }
717   - } catch (Exception e) {
718   - log.error("#1_1 Update ResourcesValue after Observation is unsuccessfully path: [{}] value: [{}] [{}]", path, value, e.toString());
719   - } finally {
720   - writeLock.unlock();
721   - }
722   -
723   - if (isChange) {
724   - Set<String> paths = new HashSet<>();
725   - paths.add(path);
726   - this.updateAttrTelemetry(registration, false, paths);
727   - }
  694 + private void onObservationSetResourcesValue(Registration registration, LwM2mResource lwM2mResource, String path) {
  695 + LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null);
  696 + lwM2MClient.updateResourceValue(path, lwM2mResource);
  697 + log.warn("upDateResize: [{}] [{}] [{}]", lwM2MClient.getEndPoint(), lwM2MClient.getResources().size(), path);
  698 + Set<String> paths = new HashSet<>();
  699 + paths.add(path);
  700 + this.updateAttrTelemetry(registration, false, paths);
728 701 }
729 702
730 703 /**
... ... @@ -738,7 +711,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
738 711 /**
739 712 * Update - sent request in change value resources in Client
740 713 * Path to resources from profile equal keyName or from ModelObject equal name
741   - * Only for resources: isWritable && isPresent as attribute in profile -> AttrTelemetryObserveValue (format: CamelCase)
  714 + * Only for resources: isWritable && isPresent as attribute in profile -> LwM2MClientProfile (format: CamelCase)
742 715 * Delete - nothing *
743 716 *
744 717 * @param msg -
... ... @@ -750,7 +723,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
750 723 String path = this.getPathAttributeUpdate(sessionInfo, de.getKey());
751 724 String value = de.getValue().getAsString();
752 725 LwM2MClient lwM2MClient = lwM2mInMemorySecurityStore.getSession(new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB())).entrySet().iterator().next().getValue();
753   - AttrTelemetryObserveValue profile = lwM2mInMemorySecurityStore.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
  726 + LwM2MClientProfile profile = lwM2mInMemorySecurityStore.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
754 727 ResourceModel resourceModel = context.getCtxServer().getResourceModel(lwM2MClient.getRegistration(), new LwM2mPath(path));
755 728 if (!path.isEmpty() && (this.validatePathInAttrProfile(profile, path) || this.validatePathInTelemetryProfile(profile, path))) {
756 729 if (resourceModel != null && resourceModel.operations.isWritable()) {
... ... @@ -775,7 +748,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
775 748
776 749 /**
777 750 * Get path to resource from profile equal keyName or from ModelObject equal name
778   - * Only for resource: isWritable && isPresent as attribute in profile -> AttrTelemetryObserveValue (format: CamelCase)
  751 + * Only for resource: isWritable && isPresent as attribute in profile -> LwM2MClientProfile (format: CamelCase)
779 752 *
780 753 * @param sessionInfo -
781 754 * @param name -
... ... @@ -792,7 +765,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
792 765 * @param path -
793 766 * @return true if path isPresent in postAttributeProfile
794 767 */
795   - private boolean validatePathInAttrProfile(AttrTelemetryObserveValue profile, String path) {
  768 + private boolean validatePathInAttrProfile(LwM2MClientProfile profile, String path) {
796 769 Set<String> attributesSet = new Gson().fromJson(profile.getPostAttributeProfile(), Set.class);
797 770 return attributesSet.stream().filter(p -> p.equals(path)).findFirst().isPresent();
798 771 }
... ... @@ -802,7 +775,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
802 775 * @param path -
803 776 * @return true if path isPresent in postAttributeProfile
804 777 */
805   - private boolean validatePathInTelemetryProfile(AttrTelemetryObserveValue profile, String path) {
  778 + private boolean validatePathInTelemetryProfile(LwM2MClientProfile profile, String path) {
806 779 Set<String> telemetriesSet = new Gson().fromJson(profile.getPostTelemetryProfile(), Set.class);
807 780 return telemetriesSet.stream().filter(p -> p.equals(path)).findFirst().isPresent();
808 781 }
... ... @@ -816,7 +789,7 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
816 789 * @return -
817 790 */
818 791 private String getPathAttributeUpdateProfile(TransportProtos.SessionInfoProto sessionInfo, String name) {
819   - AttrTelemetryObserveValue profile = lwM2mInMemorySecurityStore.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
  792 + LwM2MClientProfile profile = lwM2mInMemorySecurityStore.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB()));
820 793 return profile.getPostKeyNameProfile().getAsJsonObject().entrySet().stream()
821 794 .filter(e -> e.getValue().getAsString().equals(name)).findFirst().map(Map.Entry::getKey)
822 795 .orElse("");
... ... @@ -830,14 +803,16 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
830 803 * @param request -
831 804 */
832 805 public void onAttributeUpdateOk(Registration registration, String path, WriteRequest request, boolean isDelayedUpdate) {
833   - ResourceModel resource = context.getCtxServer().getResourceModel(registration, new LwM2mPath(path));
834   - if (resource.multiple) {
835   - this.onObservationSetResourcesValue(registration, null, ((LwM2mSingleResource) request.getNode()).getValues(), path);
836   - } else {
837   - this.onObservationSetResourcesValue(registration, ((LwM2mSingleResource) request.getNode()).getValue(), null, path);
  806 +// ResourceModel resource = context.getCtxServer().getResourceModel(registration, new LwM2mPath(path));
  807 +// if (resource.multiple) {
  808 + this.onObservationSetResourcesValue(registration, ((LwM2mResource) request.getNode()), path);
  809 +// } else {
  810 +// this.onObservationSetResourcesValue(registration, ((LwM2mSingleResource) request.getNode()).getValue(), null, path);
  811 +// }
  812 + if (isDelayedUpdate) {
  813 + lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null)
  814 + .onSuccessOrErrorDelayedRequests(request.getPath().toString());
838 815 }
839   - if (isDelayedUpdate) lwM2mInMemorySecurityStore.getLwM2MClientWithReg(registration, null)
840   - .onSuccessOrErrorDelayedRequests(request.getPath().toString());
841 816 }
842 817
843 818 /**
... ... @@ -909,24 +884,24 @@ public class LwM2MTransportServiceImpl implements LwM2MTransportService {
909 884 */
910 885 private void onDeviceUpdateChangeProfile(Set<String> registrationIds, DeviceProfile deviceProfile) {
911 886
912   - AttrTelemetryObserveValue attrTelemetryObserveValueOld = lwM2mInMemorySecurityStore.getProfiles().get(deviceProfile.getUuidId());
  887 + LwM2MClientProfile lwM2MClientProfileOld = lwM2mInMemorySecurityStore.getProfiles().get(deviceProfile.getUuidId());
913 888 if (lwM2mInMemorySecurityStore.addUpdateProfileParameters(deviceProfile)) {
914 889
915 890 // #1
916   - JsonArray attributeOld = attrTelemetryObserveValueOld.getPostAttributeProfile();
  891 + JsonArray attributeOld = lwM2MClientProfileOld.getPostAttributeProfile();
917 892 Set attributeSetOld = new Gson().fromJson(attributeOld, Set.class);
918   - JsonArray telemetryOld = attrTelemetryObserveValueOld.getPostTelemetryProfile();
  893 + JsonArray telemetryOld = lwM2MClientProfileOld.getPostTelemetryProfile();
919 894 Set telemetrySetOld = new Gson().fromJson(telemetryOld, Set.class);
920   - JsonArray observeOld = attrTelemetryObserveValueOld.getPostObserveProfile();
921   - JsonObject keyNameOld = attrTelemetryObserveValueOld.getPostKeyNameProfile();
  895 + JsonArray observeOld = lwM2MClientProfileOld.getPostObserveProfile();
  896 + JsonObject keyNameOld = lwM2MClientProfileOld.getPostKeyNameProfile();
922 897
923   - AttrTelemetryObserveValue attrTelemetryObserveValueNew = lwM2mInMemorySecurityStore.getProfiles().get(deviceProfile.getUuidId());
924   - JsonArray attributeNew = attrTelemetryObserveValueNew.getPostAttributeProfile();
  898 + LwM2MClientProfile lwM2MClientProfileNew = lwM2mInMemorySecurityStore.getProfiles().get(deviceProfile.getUuidId());
  899 + JsonArray attributeNew = lwM2MClientProfileNew.getPostAttributeProfile();
925 900 Set attributeSetNew = new Gson().fromJson(attributeNew, Set.class);
926   - JsonArray telemetryNew = attrTelemetryObserveValueNew.getPostTelemetryProfile();
  901 + JsonArray telemetryNew = lwM2MClientProfileNew.getPostTelemetryProfile();
927 902 Set telemetrySetNew = new Gson().fromJson(telemetryNew, Set.class);
928   - JsonArray observeNew = attrTelemetryObserveValueNew.getPostObserveProfile();
929   - JsonObject keyNameNew = attrTelemetryObserveValueNew.getPostKeyNameProfile();
  903 + JsonArray observeNew = lwM2MClientProfileNew.getPostObserveProfile();
  904 + JsonObject keyNameNew = lwM2MClientProfileNew.getPostKeyNameProfile();
930 905
931 906 // #3
932 907 ResultsAnalyzerParameters sentAttrToThingsboard = new ResultsAnalyzerParameters();
... ...
... ... @@ -59,7 +59,6 @@ public class LwM2MClient implements Cloneable {
59 59 private Set<Integer> delayedRequestsId;
60 60 private Map<String, LwM2mResponse> responses;
61 61 private final LwM2mValueConverterImpl converter;
62   - private boolean clientUpdateValueAfterConnect;
63 62
64 63 public Object clone() throws CloneNotSupportedException {
65 64 return super.clone();
... ... @@ -114,7 +113,7 @@ public class LwM2MClient implements Cloneable {
114 113 if (this.responses.size() == 0) this.responses = new ConcurrentHashMap<>();
115 114 }
116 115
117   - private void updateResourceValue(String pathRez, LwM2mResource rez) {
  116 + public void updateResourceValue(String pathRez, LwM2mResource rez) {
118 117 if (rez instanceof LwM2mMultipleResource){
119 118 this.resources.put(pathRez, new ResourceValue(rez.getValues(), null, true));
120 119 }
... ...
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/LwM2MClientProfile.java renamed from common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/client/AttrTelemetryObserveValue.java
... ... @@ -20,7 +20,14 @@ import com.google.gson.JsonObject;
20 20 import lombok.Data;
21 21
22 22 @Data
23   -public class AttrTelemetryObserveValue {
  23 +public class LwM2MClientProfile {
  24 + /**
  25 + * {"clientLwM2mSettings": {
  26 + * clientUpdateValueAfterConnect: false;
  27 + * }
  28 + **/
  29 + JsonObject postClientLwM2mSettings;
  30 +
24 31 /**
25 32 * {"keyName": {
26 33 * "/3/0/1": "modelNumber",
... ...
... ... @@ -15,7 +15,6 @@
15 15 */
16 16 package org.thingsboard.server.transport.lwm2m.server.secure;
17 17
18   -import com.google.gson.JsonObject;
19 18 import lombok.extern.slf4j.Slf4j;
20 19 import org.eclipse.leshan.core.util.Hex;
21 20 import org.eclipse.leshan.server.californium.LeshanServer;
... ... @@ -32,8 +31,8 @@ import org.thingsboard.server.transport.lwm2m.secure.LwM2MSecurityMode;
32 31 import org.thingsboard.server.transport.lwm2m.secure.LwM2mCredentialsSecurityInfoValidator;
33 32 import org.thingsboard.server.transport.lwm2m.secure.ReadResultSecurityStore;
34 33 import org.thingsboard.server.transport.lwm2m.server.LwM2MTransportHandler;
35   -import org.thingsboard.server.transport.lwm2m.server.client.AttrTelemetryObserveValue;
36 34 import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClient;
  35 +import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientProfile;
37 36 import org.thingsboard.server.transport.lwm2m.utils.TypeServer;
38 37
39 38 import java.util.Collection;
... ... @@ -60,7 +59,7 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore {
60 59 private final Lock readLock = readWriteLock.readLock();
61 60 private final Lock writeLock = readWriteLock.writeLock();
62 61 private final Map<String /** registrationId */, LwM2MClient> sessions = new ConcurrentHashMap<>();
63   - private Map<UUID /** profileUUid */, AttrTelemetryObserveValue> profiles = new ConcurrentHashMap<>();
  62 + private Map<UUID /** profileUUid */, LwM2MClientProfile> profiles = new ConcurrentHashMap<>();
64 63 private SecurityStoreListener listener;
65 64
66 65 @Autowired
... ... @@ -228,23 +227,28 @@ public class LwM2mInMemorySecurityStore extends InMemorySecurityStore {
228 227 return this.sessions;
229 228 }
230 229
231   - public Map<UUID, AttrTelemetryObserveValue> getProfiles() {
  230 + public Map<UUID, LwM2MClientProfile> getProfiles() {
232 231 return this.profiles;
233 232 }
234 233
235   - public AttrTelemetryObserveValue getProfile(UUID profileUuId) {
  234 + public LwM2MClientProfile getProfile(UUID profileUuId) {
236 235 return this.profiles.get(profileUuId);
237 236 }
238 237
239   - public Map<UUID, AttrTelemetryObserveValue> setProfiles(Map<UUID, AttrTelemetryObserveValue> profiles) {
  238 + public LwM2MClientProfile getProfile(String registrationId) {
  239 + UUID profileUUid = this.getSessions().get(registrationId).getProfileUuid();
  240 + return this.getProfiles().get(profileUUid);
  241 + }
  242 +
  243 + public Map<UUID, LwM2MClientProfile> setProfiles(Map<UUID, LwM2MClientProfile> profiles) {
240 244 return this.profiles = profiles;
241 245 }
242 246
243 247 public boolean addUpdateProfileParameters(DeviceProfile deviceProfile) {
244   - JsonObject profilesConfigData = LwM2MTransportHandler.getObserveAttrTelemetryFromThingsboard(deviceProfile);
245   - if (profilesConfigData != null) {
246   - profiles.put(deviceProfile.getUuidId(), LwM2MTransportHandler.getNewProfileParameters(profilesConfigData));
  248 + LwM2MClientProfile lwM2MClientProfile = LwM2MTransportHandler.getLwM2MClientProfileFromThingsboard(deviceProfile);
  249 + if (lwM2MClientProfile != null) {
  250 + profiles.put(deviceProfile.getUuidId(), lwM2MClientProfile);
247 251 }
248   - return (profilesConfigData != null);
  252 + return (lwM2MClientProfile != null);
249 253 }
250 254 }
... ...
... ... @@ -119,10 +119,6 @@ public class LwM2MTransportConfigServer {
119 119 private int registeredPoolSize;
120 120
121 121 @Getter
122   - @Value("${transport.lwm2m.client_update_value_after_connect:}")
123   - private boolean clientUpdateValueAfterConnect;
124   -
125   - @Getter
126 122 @Value("${transport.lwm2m.update_registered_pool_size:}")
127 123 private int updateRegisteredPoolSize;
128 124
... ...
... ... @@ -26,7 +26,9 @@
26 26 { count: +lwm2mDeviceProfileFormGroup.get('clientUpdateValueAfterConnect').value,
27 27 value: lwm2mDeviceProfileFormGroup.get('clientUpdateValueAfterConnect').value }) | async }}"
28 28 matTooltipPosition="above">
29   - {{ 'device-profile.lwm2m.client-update-value-after-connect' | translate }}
  29 + {{ translate.get('device-profile.lwm2m.client-update-value-after-connect',
  30 + { count: +lwm2mDeviceProfileFormGroup.get('clientUpdateValueAfterConnect').value,
  31 + value: lwm2mDeviceProfileFormGroup.get('clientUpdateValueAfterConnect').value }) | async }}
30 32 </mat-checkbox>
31 33 </div>
32 34 <div class="mat-padding" style="padding-top: 0">
... ...
... ... @@ -1103,8 +1103,8 @@
1103 1103 "schedule-time-to": "To",
1104 1104 "schedule-days-of-week-required": "At least one day of week should be selected.",
1105 1105 "lwm2m": {
1106   - "client-update-value-after-connect": "Query client resource values after connecting ",
1107   - "client-update-value-after-connect-tip": "{ count, plural, 1 {After connecting the Client, we get All resource values for all objects} other {Once connected to the client, requests are sent to the observer from the profile configuration.} }",
  1106 + "client-update-value-after-connect": "{ count, plural, 1 {Request to the client after registration for All resource values} other {Request to the client after registration to read values only as attributes or telemetry} }",
  1107 + "client-update-value-after-connect-tip": "{ count, plural, 1 {Request to the client after registration to read all resource values for all objects} other {Request to the client after registration to read the values of the resources marked as attribute or telemetry from the profile configuration.} }",
1108 1108 "object-list": "Object list",
1109 1109 "object-list-empty": "No objects selected.",
1110 1110 "no-objects-matching": "No objects matching '{{object}}' were found.",
... ...