@ -162,15 +162,12 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
if ( lwM2MClient ! = null ) {
SessionInfoProto sessionInfo = this . getValidateSessionInfo ( registration ) ;
if ( sessionInfo ! = null ) {
lwM2MClient . setDeviceId ( new UUID ( sessionInfo . getDeviceIdMSB ( ) , sessionInfo . getDeviceIdLSB ( ) ) ) ;
lwM2MClient . setProfileId ( new UUID ( sessionInfo . getDeviceProfileIdMSB ( ) , sessionInfo . getDeviceProfileIdLSB ( ) ) ) ;
lwM2MClient . setDeviceName ( sessionInfo . getDeviceName ( ) ) ;
lwM2MClient . setDeviceProfileName ( sessionInfo . getDeviceType ( ) ) ;
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 ) ;
this . sentLogsToThingsboard ( LOG_LW2M_INFO + ": Client create after Registration" , registration ) ;
this . initLwM2mFromClientValue ( registration , lwM2MClient ) ;
this . sendLogsToThingsboard ( LOG_LW2M_INFO + ": Client create after Registration" , registration ) ;
} else {
log . error ( "Client: [{}] onRegistered [{}] name [{}] sessionInfo " , registration . getId ( ) , registration . getEndpoint ( ) , null ) ;
}
@ -194,6 +191,11 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
SessionInfoProto sessionInfo = this . getValidateSessionInfo ( registration ) ;
if ( sessionInfo ! = null ) {
this . checkInactivity ( sessionInfo ) ;
LwM2mClient lwM2MClient = this . lwM2mClientContext . getLwM2MClient ( sessionInfo ) ;
if ( lwM2MClient . getDeviceId ( ) = = null & & lwM2MClient . getProfileId ( ) = = null ) {
initLwM2mClient ( lwM2MClient , sessionInfo ) ;
}
log . info ( "Client: [{}] updatedReg [{}] name [{}] profile " , registration . getId ( ) , registration . getEndpoint ( ) , sessionInfo . getDeviceType ( ) ) ;
} else {
log . error ( "Client: [{}] updatedReg [{}] name [{}] sessionInfo " , registration . getId ( ) , registration . getEndpoint ( ) , null ) ;
@ -213,7 +215,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
executorUnRegistered . submit ( ( ) - > {
try {
this . setCancelObservations ( registration ) ;
this . sent LogsToThingsboard ( LOG_LW2M_INFO + ": Client unRegistration" , registration ) ;
this . send LogsToThingsboard ( LOG_LW2M_INFO + ": Client unRegistration" , registration ) ;
this . closeClientSession ( registration ) ;
} catch ( Throwable t ) {
log . error ( "[{}] endpoint [{}] error Unable un registration." , registration . getEndpoint ( ) , t ) ;
@ -221,6 +223,13 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
} ) ;
}
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 ( ) ) ;
lwM2MClient . setDeviceProfileName ( sessionInfo . getDeviceType ( ) ) ;
}
private void closeClientSession ( Registration registration ) {
SessionInfoProto sessionInfo = this . getValidateSessionInfo ( registration ) ;
if ( sessionInfo ! = null ) {
@ -283,7 +292,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
}
/ * *
* Update - sent request in change value resources in Client
* Update - send request in change value resources in Client
* Path to resources from profile equal keyName or from ModelObject equal name
* Only for resources : isWritable & & isPresent as attribute in profile - > LwM2MClientProfile ( format : CamelCase )
* Delete - nothing *
@ -308,13 +317,13 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
log . error ( "Resource path - [{}] value - [{}] is not Writable and cannot be updated" , pathIdVer , value ) ;
String logMsg = String . format ( "%s: attributeUpdate: Resource path - %s value - %s is not Writable and cannot be updated" ,
LOG_LW2M_ERROR , pathIdVer , value ) ;
this . sent LogsToThingsboard ( logMsg , lwM2MClient . getRegistration ( ) ) ;
this . send LogsToThingsboard ( logMsg , lwM2MClient . getRegistration ( ) ) ;
}
} else {
log . error ( "Attribute name - [{}] value - [{}] is not present as attribute in profile and cannot be updated" , de . getKey ( ) , value ) ;
String logMsg = String . format ( "%s: attributeUpdate: attribute name - %s value - %s is not present as attribute in profile and cannot be updated" ,
LOG_LW2M_ERROR , de . getKey ( ) , value ) ;
this . sent LogsToThingsboard ( logMsg , lwM2MClient . getRegistration ( ) ) ;
this . send LogsToThingsboard ( logMsg , lwM2MClient . getRegistration ( ) ) ;
}
} ) ;
} else if ( msg . getSharedDeletedCount ( ) > 0 ) {
@ -422,7 +431,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
* @param msg - text msg
* @param registration - Id of Registration LwM2M Client
* /
public void sent LogsToThingsboard ( String msg , Registration registration ) {
public void send LogsToThingsboard ( String msg , Registration registration ) {
if ( msg ! = null ) {
JsonObject telemetries = new JsonObject ( ) ;
telemetries . addProperty ( LOG_LW2M_TELEMETRY , msg ) ;
@ -433,7 +442,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
/ * *
* // !!! Ok
* Prepare Sent to Thigsboard callback - Attribute or Telemetry
* Prepare send to Thigsboard callback - Attribute or Telemetry
*
* @param msg - JsonArray : [ { name : value } ]
* @param topicName - Api Attribute or Telemetry
@ -442,7 +451,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
public void updateParametersOnThingsboard ( JsonElement msg , String topicName , Registration registration ) {
SessionInfoProto sessionInfo = this . getValidateSessionInfo ( registration ) ;
if ( sessionInfo ! = null ) {
lwM2mTransportContextServer . sent ParametersOnThingsboard ( msg , topicName , sessionInfo ) ;
lwM2mTransportContextServer . send ParametersOnThingsboard ( msg , topicName , sessionInfo ) ;
} else {
log . error ( "Client: [{}] updateParametersOnThingsboard [{}] sessionInfo " , registration , null ) ;
}
@ -517,7 +526,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
}
/ * *
* Sent Attribute and Telemetry to Thingsboard
* send Attribute and Telemetry to Thingsboard
* # 1 - get AttrName / TelemetryName with value from LwM2MClient :
* - - resourceId = = path from LwM2MClientProfile . postAttributeProfile / postTelemetryProfile / postObserveProfile
* - - AttrName / TelemetryName = = resourceName from ModelObject . objectModel , value from ModelObject . instance . resource ( resourceId )
@ -594,17 +603,17 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
result = JacksonUtil . fromString ( lwM2MClientProfile . getPostObserveProfile ( ) . toString ( ) , new TypeReference < > ( ) {
} ) ;
}
Set < String > pathSent = ConcurrentHashMap . newKeySet ( ) ;
Set < String > pathSend = ConcurrentHashMap . newKeySet ( ) ;
result . forEach ( target - > {
// #1.1
String [ ] resPath = target . split ( "/" ) ;
String instance = "/" + resPath [ 1 ] + "/" + resPath [ 2 ] ;
if ( clientInstances ! = null & & clientInstances . size ( ) > 0 & & clientInstances . contains ( instance ) ) {
pathSent . add ( target ) ;
pathSend . add ( target ) ;
}
} ) ;
lwM2MClient . getPendingRequests ( ) . addAll ( pathSent ) ;
pathSent . forEach ( target - > lwM2mTransportRequest . sendAllRequest ( registration , target , typeOper , ContentFormat . TLV . getName ( ) ,
lwM2MClient . getPendingRequests ( ) . addAll ( pathSend ) ;
pathSend . forEach ( target - > lwM2mTransportRequest . sendAllRequest ( registration , target , typeOper , ContentFormat . TLV . getName ( ) ,
null , null , this . lwM2mTransportContextServer . getLwM2MTransportConfigServer ( ) . getTimeout ( ) ) ) ;
if ( GET_TYPE_OPER_OBSERVE . equals ( typeOper ) ) {
lwM2MClient . initValue ( this , null ) ;
@ -745,15 +754,15 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
* # 3 . 2 Telemetry isChange ( add & del )
* # 3 . 3 KeyName isChange ( add )
* # 4 update
* # 4 . 1 add If # 3 isChange , then analyze and update Value in Transport form Client and sent Value to thingsboard
* # 4 . 1 add If # 3 isChange , then analyze and update Value in Transport form Client and send Value to thingsboard
* # 4 . 2 del
* - - if add attributes includes del telemetry - result del for observe
* # 5
* # 5 . 1 Observe isChange ( add & del )
* # 5 . 2 Observe . add
* - - path Attr / Telemetry includes newObserve and does not include oldObserve : sent Request observe to Client
* - - path Attr / Telemetry includes newObserve and does not include oldObserve : send Request observe to Client
* # 5 . 3 Observe . del
* - - different between newObserve and oldObserve : sent Request cancel observe to client
* - - different between newObserve and oldObserve : send Request cancel observe to client
*
* @param registrationIds -
* @param deviceProfile -
@ -778,20 +787,20 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
JsonObject keyNameNew = lwM2MClientProfileNew . getPostKeyNameProfile ( ) ;
// #3
ResultsAnalyzerParameters sent AttrToThingsboard = new ResultsAnalyzerParameters ( ) ;
ResultsAnalyzerParameters send AttrToThingsboard = new ResultsAnalyzerParameters ( ) ;
// #3.1
if ( ! attributeOld . equals ( attributeNew ) ) {
ResultsAnalyzerParameters postAttributeAnalyzer = this . getAnalyzerParameters ( new Gson ( ) . fromJson ( attributeOld , new TypeToken < Set < String > > ( ) {
} . getType ( ) ) , attributeSetNew ) ;
sent AttrToThingsboard . getPathPostParametersAdd ( ) . addAll ( postAttributeAnalyzer . getPathPostParametersAdd ( ) ) ;
sent AttrToThingsboard . getPathPostParametersDel ( ) . addAll ( postAttributeAnalyzer . getPathPostParametersDel ( ) ) ;
send AttrToThingsboard . getPathPostParametersAdd ( ) . addAll ( postAttributeAnalyzer . getPathPostParametersAdd ( ) ) ;
send AttrToThingsboard . getPathPostParametersDel ( ) . addAll ( postAttributeAnalyzer . getPathPostParametersDel ( ) ) ;
}
// #3.2
if ( ! telemetryOld . equals ( telemetryNew ) ) {
ResultsAnalyzerParameters postTelemetryAnalyzer = this . getAnalyzerParameters ( new Gson ( ) . fromJson ( telemetryOld , new TypeToken < Set < String > > ( ) {
} . getType ( ) ) , telemetrySetNew ) ;
sent AttrToThingsboard . getPathPostParametersAdd ( ) . addAll ( postTelemetryAnalyzer . getPathPostParametersAdd ( ) ) ;
sent AttrToThingsboard . getPathPostParametersDel ( ) . addAll ( postTelemetryAnalyzer . getPathPostParametersDel ( ) ) ;
send AttrToThingsboard . getPathPostParametersAdd ( ) . addAll ( postTelemetryAnalyzer . getPathPostParametersAdd ( ) ) ;
send AttrToThingsboard . getPathPostParametersDel ( ) . addAll ( postTelemetryAnalyzer . getPathPostParametersDel ( ) ) ;
}
// #3.3
if ( ! keyNameOld . equals ( keyNameNew ) ) {
@ -799,24 +808,24 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
} . getType ( ) ) ,
new Gson ( ) . fromJson ( keyNameNew . toString ( ) , new TypeToken < ConcurrentHashMap < String , String > > ( ) {
} . getType ( ) ) ) ;
sent AttrToThingsboard . getPathPostParametersAdd ( ) . addAll ( keyNameChange . getPathPostParametersAdd ( ) ) ;
send AttrToThingsboard . getPathPostParametersAdd ( ) . addAll ( keyNameChange . getPathPostParametersAdd ( ) ) ;
}
// #4.1 add
if ( sent AttrToThingsboard . getPathPostParametersAdd ( ) . size ( ) > 0 ) {
if ( send AttrToThingsboard . getPathPostParametersAdd ( ) . size ( ) > 0 ) {
// update value in Resources
registrationIds . forEach ( registrationId - > {
// LeshanServer lwServer = leshanServer;
Registration registration = lwM2mClientContext . getRegistration ( registrationId ) ;
this . readResourceValueObserve ( registration , sent AttrToThingsboard . getPathPostParametersAdd ( ) , GET_TYPE_OPER_READ ) ;
// sent attr/telemetry to tingsboard for new path
this . updateAttrTelemetry ( registration , sent AttrToThingsboard . getPathPostParametersAdd ( ) ) ;
this . readResourceValueObserve ( registration , send AttrToThingsboard . getPathPostParametersAdd ( ) , GET_TYPE_OPER_READ ) ;
// send attr/telemetry to tingsboard for new path
this . updateAttrTelemetry ( registration , send AttrToThingsboard . getPathPostParametersAdd ( ) ) ;
} ) ;
}
// #4.2 del
if ( sent AttrToThingsboard . getPathPostParametersDel ( ) . size ( ) > 0 ) {
ResultsAnalyzerParameters sent AttrToThingsboardDel = this . getAnalyzerParameters ( sent AttrToThingsboard . getPathPostParametersAdd ( ) , sent AttrToThingsboard . getPathPostParametersDel ( ) ) ;
sent AttrToThingsboard . setPathPostParametersDel ( sent AttrToThingsboardDel . getPathPostParametersDel ( ) ) ;
if ( send AttrToThingsboard . getPathPostParametersDel ( ) . size ( ) > 0 ) {
ResultsAnalyzerParameters send AttrToThingsboardDel = this . getAnalyzerParameters ( send AttrToThingsboard . getPathPostParametersAdd ( ) , send AttrToThingsboard . getPathPostParametersDel ( ) ) ;
send AttrToThingsboard . setPathPostParametersDel ( send AttrToThingsboardDel . getPathPostParametersDel ( ) ) ;
}
// #5.1
@ -826,17 +835,17 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
//#5.2 add
// path Attr/Telemetry includes newObserve
attributeSetOld . addAll ( telemetrySetOld ) ;
ResultsAnalyzerParameters sent ObserveToClientOld = this . getAnalyzerParametersIn ( attributeSetOld , observeSetOld ) ; // add observe
ResultsAnalyzerParameters send ObserveToClientOld = this . getAnalyzerParametersIn ( attributeSetOld , observeSetOld ) ; // add observe
attributeSetNew . addAll ( telemetrySetNew ) ;
ResultsAnalyzerParameters sent ObserveToClientNew = this . getAnalyzerParametersIn ( attributeSetNew , observeSetNew ) ; // add observe
ResultsAnalyzerParameters send ObserveToClientNew = this . getAnalyzerParametersIn ( attributeSetNew , observeSetNew ) ; // add observe
// does not include oldObserve
ResultsAnalyzerParameters postObserveAnalyzer = this . getAnalyzerParameters ( sent ObserveToClientOld . getPathPostParametersAdd ( ) , sent ObserveToClientNew . getPathPostParametersAdd ( ) ) ;
// sent Request observe to Client
ResultsAnalyzerParameters postObserveAnalyzer = this . getAnalyzerParameters ( send ObserveToClientOld . getPathPostParametersAdd ( ) , send ObserveToClientNew . getPathPostParametersAdd ( ) ) ;
// send Request observe to Client
registrationIds . forEach ( registrationId - > {
Registration registration = lwM2mClientContext . getRegistration ( registrationId ) ;
this . readResourceValueObserve ( registration , postObserveAnalyzer . getPathPostParametersAdd ( ) , GET_TYPE_OPER_OBSERVE ) ;
// 5.3 del
// sent Request cancel observe to Client
// send Request cancel observe to Client
this . cancelObserveIsValue ( registration , postObserveAnalyzer . getPathPostParametersDel ( ) ) ;
} ) ;
}
@ -877,7 +886,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
/ * *
* Update Resource value after change RezAttrTelemetry in config Profile
* sent response Read to Client and add path to pathResAttrTelemetry in LwM2MClient . getAttrTelemetryObserveValue ( )
* send response Read to Client and add path to pathResAttrTelemetry in LwM2MClient . getAttrTelemetryObserveValue ( )
*
* @param registration - Registration LwM2M Client
* @param targets - path Resources = = [ "/2/0/0" , "/2/0/1" ]
@ -967,7 +976,7 @@ public class LwM2mTransportServiceImpl implements LwM2mTransportService {
* # 1 Get path resource by result attributesResponse
* # 1 . 1 If two names have equal path = > last time attribute
* # 2 . 1 if there is a difference in values between the current resource values and the shared attribute values
* = > sent to client Request Update of value ( new value from shared attribute )
* = > send to client Request Update of value ( new value from shared attribute )
* and LwM2MClient . delayedRequests . add ( path )
* # 2 . 1 if there is not a difference in values between the current resource values and the shared attribute values
*