Commit 4287d838be199f2a37463089748fc1359fa75506

Authored by Andrii Shvaika
1 parent ef54e558

Refactoring or requests to the client

... ... @@ -5,7 +5,7 @@
5 5 * you may not use this file except in compliance with the License.
6 6 * You may obtain a copy of the License at
7 7 *
8   - * http://www.apache.org/licenses/LICENSE-2.0
  8 + * http://www.apache.org/licenses/LICENSE-2.0
9 9 *
10 10 * Unless required by applicable law or agreed to in writing, software
11 11 * distributed under the License is distributed on an "AS IS" BASIS,
... ... @@ -740,8 +740,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
740 740 }
741 741 }
742 742
743   - private void initReadAttrTelemetryObserveToClient(LwM2mClient lwM2MClient,
744   - LwM2mTypeOper typeOper, Set<String> clientObjects) {
  743 + private void initReadAttrTelemetryObserveToClient(LwM2mClient lwM2MClient, LwM2mTypeOper typeOper, Set<String> clientObjects) {
745 744 LwM2mClientProfile lwM2MClientProfile = clientContext.getProfile(lwM2MClient.getProfileId());
746 745 Set<String> result = null;
747 746 ConcurrentHashMap<String, Object> params = null;
... ... @@ -762,28 +761,42 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
762 761 params = this.getPathForWriteAttributes(lwM2MClientProfile.getPostAttributeLwm2mProfile());
763 762 result = params.keySet();
764 763 }
765   - if (result != null && !result.isEmpty()) {
766   - // #1
767   - Set<String> pathSend = result.stream().filter(target -> {
768   - return target.split(LWM2M_SEPARATOR_PATH).length < 3 ?
769   - clientObjects.contains("/" + target.split(LWM2M_SEPARATOR_PATH)[1]) :
770   - clientObjects.contains("/" + target.split(LWM2M_SEPARATOR_PATH)[1] + "/" + target.split(LWM2M_SEPARATOR_PATH)[2]);
771   - }
  764 + sendRequestsToClient(lwM2MClient, typeOper, clientObjects, result, params);
  765 + }
  766 +
  767 + private void sendRequestsToClient(LwM2mClient lwM2MClient, LwM2mTypeOper operationType, Set<String> supportedObjectIds, Set<String> desiredObjectIds, ConcurrentHashMap<String, Object> params) {
  768 + if (desiredObjectIds != null && !desiredObjectIds.isEmpty()) {
  769 + Set<String> targetObjectIds = desiredObjectIds.stream().filter(target -> isSupportedTargetId(supportedObjectIds, target)
772 770 ).collect(Collectors.toUnmodifiableSet());
773   - if (!pathSend.isEmpty()) {
774   - lwM2MClient.getPendingReadRequests().addAll(pathSend);
775   - ConcurrentHashMap<String, Object> finalParams = params;
776   - pathSend.forEach(target -> {
777   - lwM2mTransportRequest.sendAllRequest(lwM2MClient, target, typeOper,
778   - finalParams != null ? finalParams.get(target) : null, this.config.getTimeout(), null);
  771 + if (!targetObjectIds.isEmpty()) {
  772 + //TODO: remove this side effect?
  773 + lwM2MClient.getPendingReadRequests().addAll(targetObjectIds);
  774 + targetObjectIds.forEach(target -> {
  775 + Object additionalParams = params != null ? params.get(target) : null;
  776 + lwM2mTransportRequest.sendAllRequest(lwM2MClient, target, operationType, additionalParams, this.config.getTimeout(), null);
779 777 });
780   - if (OBSERVE.equals(typeOper)) {
  778 + if (OBSERVE.equals(operationType)) {
781 779 lwM2MClient.initReadValue(this, null);
782 780 }
783 781 }
784 782 }
785 783 }
786 784
  785 + private boolean isSupportedTargetId(Set<String> supportedIds, String targetId) {
  786 + String[] targetIdParts = targetId.split(LWM2M_SEPARATOR_PATH);
  787 + if (targetIdParts.length <= 1) {
  788 + return false;
  789 + }
  790 + String targetIdSearch = targetIdParts[0];
  791 + for (int i = 1; i < targetIdParts.length; i++) {
  792 + targetIdSearch += "/" + targetIdParts[i];
  793 + if (supportedIds.contains(targetIdSearch)) {
  794 + return true;
  795 + }
  796 + }
  797 + return false;
  798 + }
  799 +
787 800 private ConcurrentHashMap<String, Object> getPathForWriteAttributes(JsonObject objectJson) {
788 801 ConcurrentHashMap<String, Object> pathAttributes = new Gson().fromJson(objectJson.toString(),
789 802 new TypeToken<ConcurrentHashMap<String, Object>>() {
... ...