@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
* You may obtain a copy of the License at
*
*
* http : //www.apache.org/licenses/LICENSE-2.0
* http : //www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing , software
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* distributed under the License is distributed on an "AS IS" BASIS ,
@ -38,16 +38,14 @@ import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse ;
import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse ;
import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto ;
import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto ;
import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto ;
import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto ;
import org.thingsboard.server.transport.lwm2m.server.LwM2mQueuedRequest ;
import java.io.Serializable ;
import java.util.Collection ;
import java.util.Collection ;
import java.util.Map ;
import java.util.Map ;
import java.util.Optional ;
import java.util.Optional ;
import java.util.Queue ;
import java.util.Set ;
import java.util.Set ;
import java.util.UUID ;
import java.util.UUID ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.concurrent.ConcurrentLinkedQueue ;
import java.util.concurrent.locks.Lock ;
import java.util.concurrent.locks.Lock ;
import java.util.concurrent.locks.ReentrantLock ;
import java.util.concurrent.locks.ReentrantLock ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
@ -60,48 +58,38 @@ import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.f
import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.getVerFromPathIdVerOrId ;
import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.getVerFromPathIdVerOrId ;
@Slf4j
@Slf4j
public class LwM2mClient implements Cloneable {
public class LwM2mClient implements Serializable {
private static final long serialVersionUID = 8793482946289222623L ;
private final String nodeId ;
private final String nodeId ;
@Getter
@Getter
private final String endpoint ;
private final String endpoint ;
private final Lock lock ;
@Getter
@Setter
private LwM2MClientState state ;
@Getter
private final Map < String , ResourceValue > resources ;
@Getter
private final Map < String , TsKvProto > sharedAttributes ;
@Getter
private final Queue < LwM2mQueuedRequest > queuedRequests ;
private transient final Lock lock = new ReentrantLock ( ) ;
//TODO: define custom serialization of those fields.
@Getter
@Getter
private String deviceName ;
private transient final Map < String , ResourceValue > resources ;
@Getter
@Getter
private String deviceProfileName ;
private final Map < String , TsKvProto > sharedAttributes ;
@Getter
private PowerMode powerMode ;
@Getter
private String identity ;
@Getter
private SecurityInfo securityInfo ;
@Getter
@Getter
private TenantId tenantId ;
private TenantId tenantId ;
@Getter
@Getter
private UUID profileId ;
@Getter
private UUID deviceId ;
private UUID deviceId ;
@Getter
@Getter
@Setter
private LwM2MClientState state ;
@Getter
private SessionInfoProto session ;
private SessionInfoProto session ;
@Getter
@Getter
private UUID profileId ;
private PowerMode powerMode ;
@Getter
@Getter
@Setter
@Setter
private Registration registration ;
private Registration registration ;
private ValidateDeviceCredentialsResponse credentials ;
public Object clone ( ) throws CloneNotSupportedException {
public Object clone ( ) throws CloneNotSupportedException {
return super . clone ( ) ;
return super . clone ( ) ;
}
}
@ -109,23 +97,16 @@ public class LwM2mClient implements Cloneable {
public LwM2mClient ( String nodeId , String endpoint ) {
public LwM2mClient ( String nodeId , String endpoint ) {
this . nodeId = nodeId ;
this . nodeId = nodeId ;
this . endpoint = endpoint ;
this . endpoint = endpoint ;
this . lock = new ReentrantLock ( ) ;
this . sharedAttributes = new ConcurrentHashMap < > ( ) ;
this . sharedAttributes = new ConcurrentHashMap < > ( ) ;
this . resources = new ConcurrentHashMap < > ( ) ;
this . resources = new ConcurrentHashMap < > ( ) ;
this . queuedRequests = new ConcurrentLinkedQueue < > ( ) ;
this . state = LwM2MClientState . CREATED ;
this . state = LwM2MClientState . CREATED ;
}
}
public void init ( String identity , SecurityInfo securityInfo , ValidateDeviceCredentialsResponse credentials , UUID sessionId ) {
public void init ( ValidateDeviceCredentialsResponse credentials , UUID sessionId ) {
this . identity = identity ;
this . securityInfo = securityInfo ;
this . credentials = credentials ;
this . session = createSession ( nodeId , sessionId , credentials ) ;
this . session = createSession ( nodeId , sessionId , credentials ) ;
this . tenantId = new TenantId ( new UUID ( session . getTenantIdMSB ( ) , session . getTenantIdLSB ( ) ) ) ;
this . tenantId = new TenantId ( new UUID ( session . getTenantIdMSB ( ) , session . getTenantIdLSB ( ) ) ) ;
this . deviceId = new UUID ( session . getDeviceIdMSB ( ) , session . getDeviceIdLSB ( ) ) ;
this . deviceId = new UUID ( session . getDeviceIdMSB ( ) , session . getDeviceIdLSB ( ) ) ;
this . profileId = new UUID ( session . getDeviceProfileIdMSB ( ) , session . getDeviceProfileIdLSB ( ) ) ;
this . profileId = new UUID ( session . getDeviceProfileIdMSB ( ) , session . getDeviceProfileIdLSB ( ) ) ;
this . deviceName = session . getDeviceName ( ) ;
this . deviceProfileName = session . getDeviceType ( ) ;
this . powerMode = credentials . getDeviceInfo ( ) . getPowerMode ( ) ;
this . powerMode = credentials . getDeviceInfo ( ) . getPowerMode ( ) ;
}
}
@ -140,10 +121,9 @@ public class LwM2mClient implements Cloneable {
public void onDeviceUpdate ( Device device , Optional < DeviceProfile > deviceProfileOpt ) {
public void onDeviceUpdate ( Device device , Optional < DeviceProfile > deviceProfileOpt ) {
SessionInfoProto . Builder builder = SessionInfoProto . newBuilder ( ) . mergeFrom ( session ) ;
SessionInfoProto . Builder builder = SessionInfoProto . newBuilder ( ) . mergeFrom ( session ) ;
this . deviceId = device . getUuidId ( ) ;
this . deviceId = device . getUuidId ( ) ;
this . deviceName = device . getName ( ) ;
builder . setDeviceIdMSB ( deviceId . getMostSignificantBits ( ) ) ;
builder . setDeviceIdMSB ( deviceId . getMostSignificantBits ( ) ) ;
builder . setDeviceIdLSB ( deviceId . getLeastSignificantBits ( ) ) ;
builder . setDeviceIdLSB ( deviceId . getLeastSignificantBits ( ) ) ;
builder . setDeviceName ( deviceName ) ;
builder . setDeviceName ( device . get Name( ) ) ;
deviceProfileOpt . ifPresent ( deviceProfile - > updateSession ( deviceProfile , builder ) ) ;
deviceProfileOpt . ifPresent ( deviceProfile - > updateSession ( deviceProfile , builder ) ) ;
this . session = builder . build ( ) ;
this . session = builder . build ( ) ;
this . powerMode = ( ( Lwm2mDeviceTransportConfiguration ) device . getDeviceData ( ) . getTransportConfiguration ( ) ) . getPowerMode ( ) ;
this . powerMode = ( ( Lwm2mDeviceTransportConfiguration ) device . getDeviceData ( ) . getTransportConfiguration ( ) ) . getPowerMode ( ) ;
@ -156,11 +136,10 @@ public class LwM2mClient implements Cloneable {
}
}
private void updateSession ( DeviceProfile deviceProfile , SessionInfoProto . Builder builder ) {
private void updateSession ( DeviceProfile deviceProfile , SessionInfoProto . Builder builder ) {
this . deviceProfileName = deviceProfile . getName ( ) ;
this . profileId = deviceProfile . getUuidId ( ) ;
this . profileId = deviceProfile . getUuidId ( ) ;
builder . setDeviceProfileIdMSB ( profileId . getMostSignificantBits ( ) ) ;
builder . setDeviceProfileIdMSB ( profileId . getMostSignificantBits ( ) ) ;
builder . setDeviceProfileIdLSB ( profileId . getLeastSignificantBits ( ) ) ;
builder . setDeviceProfileIdLSB ( profileId . getLeastSignificantBits ( ) ) ;
builder . setDeviceType ( this . deviceProfileName ) ;
builder . setDeviceType ( deviceProfile . getName ( ) ) ;
}
}
private SessionInfoProto createSession ( String nodeId , UUID sessionId , ValidateDeviceCredentialsResponse msg ) {
private SessionInfoProto createSession ( String nodeId , UUID sessionId , ValidateDeviceCredentialsResponse msg ) {