|
|
|
@ -52,7 +52,6 @@ import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientProfile; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.client.ResourceValue; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.client.ResultsAnalyzerParameters; |
|
|
|
import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl; |
|
|
|
|
|
|
|
@ -73,9 +72,6 @@ import java.util.concurrent.ConcurrentMap; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.locks.Lock; |
|
|
|
import java.util.concurrent.locks.ReadWriteLock; |
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static org.thingsboard.server.common.transport.util.JsonUtils.getJsonObject; |
|
|
|
@ -107,8 +103,6 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
private ExecutorService executorUpdateRegistered; |
|
|
|
private ExecutorService executorUnRegistered; |
|
|
|
private LwM2mValueConverterImpl converter; |
|
|
|
protected final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); |
|
|
|
protected final Lock writeLock = readWriteLock.writeLock(); |
|
|
|
|
|
|
|
private final TransportService transportService; |
|
|
|
|
|
|
|
@ -162,7 +156,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
if (lwM2MClient != null) { |
|
|
|
SessionInfoProto sessionInfo = this.getValidateSessionInfo(registration); |
|
|
|
if (sessionInfo != null) { |
|
|
|
this.initLwM2mClient (lwM2MClient, sessionInfo); |
|
|
|
this.initLwM2mClient(lwM2MClient, sessionInfo); |
|
|
|
transportService.registerAsyncSession(sessionInfo, new LwM2mSessionMsgListener(this, sessionInfo)); |
|
|
|
transportService.process(sessionInfo, DefaultTransportService.getSessionEventMsg(SessionEvent.OPEN), null); |
|
|
|
transportService.process(sessionInfo, TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null); |
|
|
|
@ -223,7 +217,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void initLwM2mClient (LwM2mClient lwM2MClient, SessionInfoProto sessionInfo) { |
|
|
|
private void initLwM2mClient(LwM2mClient lwM2MClient, SessionInfoProto sessionInfo) { |
|
|
|
lwM2MClient.setDeviceId(new UUID(sessionInfo.getDeviceIdMSB(), sessionInfo.getDeviceIdLSB())); |
|
|
|
lwM2MClient.setProfileId(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB())); |
|
|
|
lwM2MClient.setDeviceName(sessionInfo.getDeviceName()); |
|
|
|
@ -309,7 +303,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
LwM2mClient lwM2MClient = lwM2mClientContext.getLwM2mClient(new UUID(sessionInfo.getSessionIdMSB(), sessionInfo.getSessionIdLSB())); |
|
|
|
LwM2mClientProfile clientProfile = lwM2mClientContext.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB())); |
|
|
|
if (pathIdVer != null && !pathIdVer.isEmpty() && (this.validatePathInAttrProfile(clientProfile, pathIdVer) || this.validatePathInTelemetryProfile(clientProfile, pathIdVer))) { |
|
|
|
ResourceModel resourceModel = lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModel(lwM2MClient.getRegistration(), new LwM2mPath(convertToObjectIdFromIdVer(pathIdVer))); |
|
|
|
ResourceModel resourceModel = lwM2MClient.getResourceModel(pathIdVer); |
|
|
|
if (resourceModel != null && resourceModel.operations.isWritable()) { |
|
|
|
lwM2mTransportRequest.sendAllRequest(lwM2MClient.getRegistration(), pathIdVer, POST_TYPE_OPER_WRITE_REPLACE, |
|
|
|
ContentFormat.TLV.getName(), null, value, this.lwM2mTransportContextServer.getLwM2MTransportConfigServer().getTimeout()); |
|
|
|
@ -360,6 +354,26 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
registrationIdOpt.ifPresent(registrationId -> this.onDeviceUpdateLwM2MClient(registrationId, device, deviceProfileOpt)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param resourceUpdateMsgOpt - |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void onResourceUpdate (Optional<TransportProtos.ResourceUpdateMsg> resourceUpdateMsgOpt) { |
|
|
|
String idVer = resourceUpdateMsgOpt.get().getResourceKey(); // 19_1.0
|
|
|
|
lwM2mClientContext.getLwM2mClients().values().stream().forEach(e -> e.updateResourceModel(idVer, this.lwM2mTransportContextServer.getLwM2MTransportConfigServer().getModelProvider())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @param resourceDeleteMsgOpt - |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void onResourceDelete(Optional<TransportProtos.ResourceDeleteMsg> resourceDeleteMsgOpt) { |
|
|
|
String pathIdVer = resourceDeleteMsgOpt.get().getResourceKey(); // 19_1.0
|
|
|
|
lwM2mClientContext.getLwM2mClients().values().stream().forEach(e -> e.deleteResources(pathIdVer, this.lwM2mTransportContextServer.getLwM2MTransportConfigServer().getModelProvider())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Trigger Server path = "/1/0/8" |
|
|
|
* <p> |
|
|
|
@ -519,10 +533,14 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
*/ |
|
|
|
private void updateResourcesValue(Registration registration, LwM2mResource lwM2mResource, String path) { |
|
|
|
LwM2mClient lwM2MClient = lwM2mClientContext.getLwM2mClientWithReg(registration, null); |
|
|
|
lwM2MClient.updateResourceValue(path, lwM2mResource); |
|
|
|
Set<String> paths = new HashSet<>(); |
|
|
|
paths.add(path); |
|
|
|
this.updateAttrTelemetry(registration, paths); |
|
|
|
if (lwM2MClient.saveResourceValue(path, lwM2mResource, this.lwM2mTransportContextServer.getLwM2MTransportConfigServer().getModelProvider())) { |
|
|
|
Set<String> paths = new HashSet<>(); |
|
|
|
paths.add(path); |
|
|
|
this.updateAttrTelemetry(registration, paths); |
|
|
|
} |
|
|
|
else { |
|
|
|
log.error("Fail update Resource [{}]", lwM2mResource); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -538,12 +556,9 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
JsonObject attributes = new JsonObject(); |
|
|
|
JsonObject telemetries = new JsonObject(); |
|
|
|
try { |
|
|
|
writeLock.lock(); |
|
|
|
this.getParametersFromProfile(attributes, telemetries, registration, paths); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("UpdateAttrTelemetry", e); |
|
|
|
} finally { |
|
|
|
writeLock.unlock(); |
|
|
|
} |
|
|
|
if (attributes.getAsJsonObject().entrySet().size() > 0) |
|
|
|
this.updateParametersOnThingsboard(attributes, DEVICE_ATTRIBUTES_TOPIC, registration); |
|
|
|
@ -558,8 +573,8 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
*/ |
|
|
|
private boolean validatePathInAttrProfile(LwM2mClientProfile clientProfile, String path) { |
|
|
|
try { |
|
|
|
List<String> attributesSet = new Gson().fromJson(clientProfile.getPostAttributeProfile(), new TypeToken<List<String>>() { |
|
|
|
}.getType()); |
|
|
|
List<String> attributesSet = new Gson().fromJson(clientProfile.getPostAttributeProfile(), |
|
|
|
new TypeToken<List<String>>() {}.getType()); |
|
|
|
return attributesSet.stream().anyMatch(p -> p.equals(path)); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Fail Validate Path [{}] ClientProfile.Attribute", path, e); |
|
|
|
@ -717,9 +732,9 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
*/ |
|
|
|
private String getResourceValueToString(LwM2mClient lwM2MClient, String path) { |
|
|
|
LwM2mPath pathIds = new LwM2mPath(convertToObjectIdFromIdVer(path)); |
|
|
|
ResourceValue resourceValue = this.returnResourceValueFromLwM2MClient(lwM2MClient, path); |
|
|
|
LwM2mResource resourceValue = this.returnResourceValueFromLwM2MClient(lwM2MClient, path); |
|
|
|
return resourceValue == null ? null : |
|
|
|
this.converter.convertValue(resourceValue.getResourceValue(), this.lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModelType(lwM2MClient.getRegistration(), pathIds), ResourceModel.Type.STRING, pathIds).toString(); |
|
|
|
this.converter.convertValue(resourceValue.isMultiInstances() ? resourceValue.getValues() : resourceValue.getValue(), resourceValue.getType(), ResourceModel.Type.STRING, pathIds).toString(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -727,10 +742,10 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
* @param path - |
|
|
|
* @return - return value of Resource by idPath |
|
|
|
*/ |
|
|
|
private ResourceValue returnResourceValueFromLwM2MClient(LwM2mClient lwM2MClient, String path) { |
|
|
|
ResourceValue resourceValue = null; |
|
|
|
private LwM2mResource returnResourceValueFromLwM2MClient(LwM2mClient lwM2MClient, String path) { |
|
|
|
LwM2mResource resourceValue = null; |
|
|
|
if (new LwM2mPath(convertToObjectIdFromIdVer(path)).isResource()) { |
|
|
|
resourceValue = lwM2MClient.getResources().get(path); |
|
|
|
resourceValue = lwM2MClient.getResources().get(path).getLwM2mResource(); |
|
|
|
} |
|
|
|
return resourceValue; |
|
|
|
} |
|
|
|
@ -967,9 +982,9 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
*/ |
|
|
|
private String getPathAttributeUpdateProfile(TransportProtos.SessionInfoProto sessionInfo, String name) { |
|
|
|
LwM2mClientProfile profile = lwM2mClientContext.getProfile(new UUID(sessionInfo.getDeviceProfileIdMSB(), sessionInfo.getDeviceProfileIdLSB())); |
|
|
|
Registration registration = lwM2mClientContext.getLwM2MClient(sessionInfo).getRegistration(); |
|
|
|
LwM2mClient lwM2mClient = lwM2mClientContext.getLwM2MClient(sessionInfo); |
|
|
|
return profile.getPostKeyNameProfile().getAsJsonObject().entrySet().stream() |
|
|
|
.filter(e -> e.getValue().getAsString().equals(name) && validateResourceInModel(registration, e.getKey(), false)).findFirst().map(Map.Entry::getKey) |
|
|
|
.filter(e -> e.getValue().getAsString().equals(name) && validateResourceInModel(lwM2mClient, e.getKey(), false)).findFirst().map(Map.Entry::getKey) |
|
|
|
.orElse(""); |
|
|
|
} |
|
|
|
|
|
|
|
@ -1118,7 +1133,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
|
|
|
|
ConcurrentMap<String, String> keyNamesIsWritable = keyNamesMap.entrySet() |
|
|
|
.stream() |
|
|
|
.filter(e -> (attrSet.contains(e.getKey()) && validateResourceInModel(lwM2MClient.getRegistration(), e.getKey(), true))) |
|
|
|
.filter(e -> (attrSet.contains(e.getKey()) && validateResourceInModel(lwM2MClient, e.getKey(), true))) |
|
|
|
.collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue)); |
|
|
|
|
|
|
|
Set<String> namesIsWritable = ConcurrentHashMap.newKeySet(); |
|
|
|
@ -1126,13 +1141,12 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService { |
|
|
|
return new ArrayList<>(namesIsWritable); |
|
|
|
} |
|
|
|
|
|
|
|
private boolean validateResourceInModel(Registration registration, String pathKey, boolean isWritable) { |
|
|
|
ResourceModel resourceModel = lwM2mTransportContextServer.getLwM2MTransportConfigServer().getResourceModel(registration, |
|
|
|
new LwM2mPath(convertToObjectIdFromIdVer(pathKey))); |
|
|
|
private boolean validateResourceInModel(LwM2mClient lwM2mClient, String pathKey, boolean isWritable) { |
|
|
|
ResourceModel resourceModel = lwM2mClient.getResourceModel(pathKey); |
|
|
|
Integer objectId = validateObjectIdFromKey(pathKey); |
|
|
|
String objectVer = validateObjectVerFromKey(pathKey); |
|
|
|
return resourceModel != null && (isWritable ? |
|
|
|
objectId != null && objectVer != null && objectVer.equals(registration.getSupportedVersion(objectId)) && resourceModel.operations.isWritable() : |
|
|
|
objectId != null && objectVer != null && objectVer.equals(registration.getSupportedVersion(objectId))); |
|
|
|
objectId != null && objectVer != null && objectVer.equals(lwM2mClient.getRegistration().getSupportedVersion(objectId)) && resourceModel.operations.isWritable() : |
|
|
|
objectId != null && objectVer != null && objectVer.equals(lwM2mClient.getRegistration().getSupportedVersion(objectId))); |
|
|
|
} |
|
|
|
} |
|
|
|
|