|
|
|
@ -42,6 +42,7 @@ import org.eclipse.leshan.core.response.ObserveResponse; |
|
|
|
import org.eclipse.leshan.core.response.ReadCompositeResponse; |
|
|
|
import org.eclipse.leshan.core.response.ReadResponse; |
|
|
|
import org.eclipse.leshan.server.registration.Registration; |
|
|
|
import org.eclipse.leshan.server.registration.RegistrationStore; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.thingsboard.common.util.DonAsynchron; |
|
|
|
@ -52,6 +53,7 @@ import org.thingsboard.server.common.data.device.data.lwm2m.ObjectAttributes; |
|
|
|
import org.thingsboard.server.common.data.device.data.lwm2m.OtherConfiguration; |
|
|
|
import org.thingsboard.server.common.data.device.data.lwm2m.TelemetryMappingConfiguration; |
|
|
|
import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.ota.OtaPackageUtil; |
|
|
|
import org.thingsboard.server.common.transport.TransportService; |
|
|
|
@ -85,10 +87,10 @@ import org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttrib |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteAttributesRequest; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.log.LwM2MTelemetryLogService; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.ota.LwM2MOtaUpdateService; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.rpc.LwM2MRpcRequestHandler; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MDtlsSessionStore; |
|
|
|
import org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2mSecurityStore; |
|
|
|
import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
@ -147,6 +149,8 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl |
|
|
|
private final LwM2mClientContext clientContext; |
|
|
|
private final LwM2mDownlinkMsgHandler defaultLwM2MDownlinkMsgHandler; |
|
|
|
private final LwM2mVersionedModelProvider modelProvider; |
|
|
|
private final RegistrationStore registrationStore; |
|
|
|
private final TbLwM2mSecurityStore securityStore; |
|
|
|
|
|
|
|
public DefaultLwM2mUplinkMsgHandler(TransportService transportService, |
|
|
|
LwM2MTransportServerConfig config, |
|
|
|
@ -159,7 +163,9 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl |
|
|
|
@Lazy LwM2mDownlinkMsgHandler defaultLwM2MDownlinkMsgHandler, |
|
|
|
LwM2mTransportContext context, |
|
|
|
TbLwM2MDtlsSessionStore sessionStore, |
|
|
|
LwM2mVersionedModelProvider modelProvider) { |
|
|
|
LwM2mVersionedModelProvider modelProvider, |
|
|
|
RegistrationStore registrationStore, |
|
|
|
TbLwM2mSecurityStore securityStore) { |
|
|
|
this.transportService = transportService; |
|
|
|
this.sessionManager = sessionManager; |
|
|
|
this.attributesService = attributesService; |
|
|
|
@ -172,6 +178,8 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl |
|
|
|
this.context = context; |
|
|
|
this.sessionStore = sessionStore; |
|
|
|
this.modelProvider = modelProvider; |
|
|
|
this.registrationStore = registrationStore; |
|
|
|
this.securityStore = securityStore; |
|
|
|
} |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
@ -277,26 +285,27 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl |
|
|
|
* @param observations - !!! Warn: if have not finishing unReg, then this operation will be finished on next Client`s connect |
|
|
|
*/ |
|
|
|
public void unReg(Registration registration, Collection<Observation> observations) { |
|
|
|
executor.submit(() -> { |
|
|
|
LwM2mClient client = clientContext.getClientByEndpoint(registration.getEndpoint()); |
|
|
|
try { |
|
|
|
logService.log(client, LOG_LWM2M_INFO + ": Client unRegistration"); |
|
|
|
clientContext.unregister(client, registration); |
|
|
|
SessionInfoProto sessionInfo = client.getSession(); |
|
|
|
if (sessionInfo != null) { |
|
|
|
sessionManager.deregister(sessionInfo); |
|
|
|
sessionStore.remove(registration.getEndpoint()); |
|
|
|
log.info("Client close session: [{}] unReg [{}] name [{}] profile ", registration.getId(), registration.getEndpoint(), sessionInfo.getDeviceType()); |
|
|
|
} else { |
|
|
|
log.error("Client close session: [{}] unReg [{}] name [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null); |
|
|
|
} |
|
|
|
} catch (LwM2MClientStateException stateException) { |
|
|
|
log.info("[{}] delete registration: [{}] {}.", registration.getEndpoint(), stateException.getState(), stateException.getMessage()); |
|
|
|
} catch (Throwable t) { |
|
|
|
log.error("[{}] endpoint [{}] error Unable un registration.", registration.getEndpoint(), t); |
|
|
|
logService.log(client, LOG_LWM2M_ERROR + String.format(": Client Unable un Registration, %s", t.getMessage())); |
|
|
|
executor.submit(() -> doUnReg(registration, clientContext.getClientByEndpoint(registration.getEndpoint()))); |
|
|
|
} |
|
|
|
|
|
|
|
private void doUnReg(Registration registration, LwM2mClient client) { |
|
|
|
try { |
|
|
|
logService.log(client, LOG_LWM2M_INFO + ": Client unRegistration"); |
|
|
|
clientContext.unregister(client, registration); |
|
|
|
SessionInfoProto sessionInfo = client.getSession(); |
|
|
|
if (sessionInfo != null) { |
|
|
|
sessionManager.deregister(sessionInfo); |
|
|
|
sessionStore.remove(registration.getEndpoint()); |
|
|
|
log.info("Client close session: [{}] unReg [{}] name [{}] profile ", registration.getId(), registration.getEndpoint(), sessionInfo.getDeviceType()); |
|
|
|
} else { |
|
|
|
log.error("Client close session: [{}] unReg [{}] name [{}] sessionInfo ", registration.getId(), registration.getEndpoint(), null); |
|
|
|
} |
|
|
|
}); |
|
|
|
} catch (LwM2MClientStateException stateException) { |
|
|
|
log.info("[{}] delete registration: [{}] {}.", registration.getEndpoint(), stateException.getState(), stateException.getMessage()); |
|
|
|
} catch (Throwable t) { |
|
|
|
log.error("[{}] endpoint [{}] error Unable un registration.", registration.getEndpoint(), t); |
|
|
|
logService.log(client, LOG_LWM2M_ERROR + String.format(": Client Unable un Registration, %s", t.getMessage())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -394,6 +403,11 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onDeviceDelete(DeviceId deviceId) { |
|
|
|
clearAndUnregister(clientContext.getClientByDeviceId(deviceId.getId())); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onResourceUpdate(TransportProtos.ResourceUpdateMsg resourceUpdateMsgOpt) { |
|
|
|
String idVer = resourceUpdateMsgOpt.getResourceKey(); |
|
|
|
@ -902,8 +916,8 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void onToTransportUpdateCredentials(SessionInfoProto sessionInfo, TransportProtos.ToTransportUpdateCredentialsProto updateCredentials) { |
|
|
|
log.info("[{}] idList [{}] valueList updateCredentials", updateCredentials.getCredentialsIdList(), updateCredentials.getCredentialsValueList()); |
|
|
|
this.clientContext.removeCredentials(sessionInfo); |
|
|
|
log.info("[{}] updateCredentials", sessionInfo); |
|
|
|
clearAndUnregister(clientContext.getClientBySessionInfo(sessionInfo)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -980,4 +994,16 @@ public class DefaultLwM2mUplinkMsgHandler extends LwM2MExecutorAwareService impl |
|
|
|
.setLastActivityTime(System.currentTimeMillis()) |
|
|
|
.build(), TransportServiceCallback.EMPTY); |
|
|
|
} |
|
|
|
|
|
|
|
private void clearAndUnregister(LwM2mClient client) { |
|
|
|
client.lock(); |
|
|
|
try { |
|
|
|
Registration registration = client.getRegistration(); |
|
|
|
doUnReg(registration, client); |
|
|
|
securityStore.remove(registration.getEndpoint(), registration.getId()); |
|
|
|
registrationStore.removeRegistration(registration.getId()); |
|
|
|
} finally { |
|
|
|
client.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|