|
|
|
@ -21,11 +21,15 @@ import org.eclipse.leshan.core.SecurityMode; |
|
|
|
import org.eclipse.leshan.core.model.ResourceModel; |
|
|
|
import org.eclipse.leshan.core.node.LwM2mPath; |
|
|
|
import org.eclipse.leshan.server.registration.Registration; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
|
import org.thingsboard.server.common.data.device.data.PowerMode; |
|
|
|
import org.thingsboard.server.common.data.device.profile.Lwm2mDeviceProfileTransportConfiguration; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|
|
|
import org.thingsboard.server.common.transport.TransportDeviceProfileCache; |
|
|
|
import org.thingsboard.server.common.transport.TransportServiceCallback; |
|
|
|
import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; |
|
|
|
import org.thingsboard.server.gen.transport.TransportProtos; |
|
|
|
import org.thingsboard.server.queue.util.AfterStartUp; |
|
|
|
@ -34,9 +38,11 @@ import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; |
|
|
|
import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.ota.LwM2MOtaUpdateService; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.session.LwM2MSessionManager; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.store.TbLwM2MClientStore; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.store.TbMainSecurityStore; |
|
|
|
import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2MUplinkMsgHandler; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collection; |
|
|
|
@ -64,6 +70,14 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { |
|
|
|
private final TbLwM2MClientStore clientStore; |
|
|
|
private final LwM2MSessionManager sessionManager; |
|
|
|
private final TransportDeviceProfileCache deviceProfileCache; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Lazy |
|
|
|
private DefaultLwM2MUplinkMsgHandler defaultLwM2MUplinkMsgHandler; |
|
|
|
@Autowired |
|
|
|
@Lazy |
|
|
|
private LwM2MOtaUpdateService otaUpdateService; |
|
|
|
|
|
|
|
private final Map<String, LwM2mClient> lwM2mClientsByEndpoint = new ConcurrentHashMap<>(); |
|
|
|
private final Map<String, LwM2mClient> lwM2mClientsByRegistrationId = new ConcurrentHashMap<>(); |
|
|
|
private final Map<UUID, Lwm2mDeviceProfileTransportConfiguration> profiles = new ConcurrentHashMap<>(); |
|
|
|
@ -158,6 +172,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { |
|
|
|
} |
|
|
|
client.setRegistration(registration); |
|
|
|
clientStore.put(client); |
|
|
|
sendMsgsAfterSleeping(client); |
|
|
|
} finally { |
|
|
|
client.unlock(); |
|
|
|
} |
|
|
|
@ -248,6 +263,28 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { |
|
|
|
//TODO: implement
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void sendMsgsAfterSleeping(LwM2mClient lwM2MClient) { |
|
|
|
if (LwM2MClientState.REGISTERED.equals(lwM2MClient.getState())) { |
|
|
|
PowerMode powerMode = lwM2MClient.getPowerMode(); |
|
|
|
if (powerMode == null) { |
|
|
|
Lwm2mDeviceProfileTransportConfiguration deviceProfile = getProfile(lwM2MClient.getProfileId()); |
|
|
|
powerMode = deviceProfile.getClientLwM2mSettings().getPowerMode(); |
|
|
|
} |
|
|
|
|
|
|
|
if (PowerMode.PSM.equals(powerMode) || PowerMode.E_DRX.equals(powerMode)) { |
|
|
|
defaultLwM2MUplinkMsgHandler.initAttributes(lwM2MClient); |
|
|
|
TransportProtos.TransportToDeviceActorMsg persistentRpcRequestMsg = TransportProtos.TransportToDeviceActorMsg |
|
|
|
.newBuilder() |
|
|
|
.setSessionInfo(lwM2MClient.getSession()) |
|
|
|
.setSendPendingRPC(TransportProtos.SendPendingRPCMsg.newBuilder().build()) |
|
|
|
.build(); |
|
|
|
context.getTransportService().process(persistentRpcRequestMsg, TransportServiceCallback.EMPTY); |
|
|
|
otaUpdateService.init(lwM2MClient); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Collection<LwM2mClient> getLwM2mClients() { |
|
|
|
return lwM2mClientsByEndpoint.values(); |
|
|
|
@ -261,22 +298,23 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { |
|
|
|
@Override |
|
|
|
public Lwm2mDeviceProfileTransportConfiguration getProfile(Registration registration) { |
|
|
|
UUID profileId = getClientByEndpoint(registration.getEndpoint()).getProfileId(); |
|
|
|
Lwm2mDeviceProfileTransportConfiguration result = doGetAndCache(profileId); |
|
|
|
return doGetAndCache(profileId); |
|
|
|
} |
|
|
|
|
|
|
|
private Lwm2mDeviceProfileTransportConfiguration doGetAndCache(UUID profileId) { |
|
|
|
Lwm2mDeviceProfileTransportConfiguration result = profiles.get(profileId); |
|
|
|
if (result == null) { |
|
|
|
log.debug("[{}] Fetching profile [{}]", registration.getEndpoint(), profileId); |
|
|
|
log.debug("Fetching profile [{}]", profileId); |
|
|
|
DeviceProfile deviceProfile = deviceProfileCache.get(new DeviceProfileId(profileId)); |
|
|
|
if (deviceProfile != null) { |
|
|
|
profileUpdate(deviceProfile); |
|
|
|
result = doGetAndCache(profileId); |
|
|
|
result = profileUpdate(deviceProfile); |
|
|
|
} else { |
|
|
|
log.info("Device profile was not found! Most probably device profile [{}] has been removed from the database.", profileId); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private Lwm2mDeviceProfileTransportConfiguration doGetAndCache(UUID profileId) { |
|
|
|
return profiles.get(profileId); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Lwm2mDeviceProfileTransportConfiguration profileUpdate(DeviceProfile deviceProfile) { |
|
|
|
Lwm2mDeviceProfileTransportConfiguration clientProfile = LwM2mTransportUtil.toLwM2MClientProfile(deviceProfile); |
|
|
|
|