use of org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory in project thingsboard by thingsboard.
the class LwM2MTestClient method init.
public void init(Security security, Configuration coapConfig, int port, boolean isRpc, boolean isBootstrap, int shortServerId, int shortServerIdBs, Security securityBs, DefaultLwM2mUplinkMsgHandler defaultLwM2mUplinkMsgHandler, LwM2mClientContext clientContext) throws InvalidDDFFileException, IOException {
Assert.assertNull("client already initialized", leshanClient);
this.defaultLwM2mUplinkMsgHandlerTest = defaultLwM2mUplinkMsgHandler;
this.clientContext = clientContext;
List<ObjectModel> models = new ArrayList<>();
for (String resourceName : resources) {
models.addAll(ObjectLoader.loadDdfFile(LwM2MTestClient.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName), resourceName));
}
LwM2mModel model = new StaticModel(models);
ObjectsInitializer initializer = new ObjectsInitializer(model);
if (securityBs == null) {
initializer.setInstancesForObject(SECURITY, this.lwm2mSecurity = security);
} else {
securityBs.setId(0);
security.setId(1);
LwM2mInstanceEnabler[] instances = new LwM2mInstanceEnabler[] { this.lwm2mSecurityBs = securityBs, this.lwm2mSecurity = security };
initializer.setClassForObject(SECURITY, Security.class);
initializer.setInstancesForObject(SECURITY, instances);
}
if (isBootstrap) {
initializer.setInstancesForObject(SERVER, lwm2mServerBs = new Lwm2mServer(shortServerIdBs, 300));
} else {
if (securityBs == null) {
initializer.setInstancesForObject(SERVER, lwm2mServer = new Lwm2mServer(shortServerId, 300));
} else {
lwm2mServerBs = new Lwm2mServer(shortServerIdBs, 300);
lwm2mServerBs.setId(0);
lwm2mServer = new Lwm2mServer(shortServerId, 300);
lwm2mServer.setId(1);
LwM2mInstanceEnabler[] instances = new LwM2mInstanceEnabler[] { lwm2mServerBs, lwm2mServer };
initializer.setClassForObject(SERVER, Server.class);
initializer.setInstancesForObject(SERVER, instances);
}
}
initializer.setInstancesForObject(DEVICE, lwM2MDevice = new SimpleLwM2MDevice(executor));
initializer.setInstancesForObject(FIRMWARE, fwLwM2MDevice = new FwLwM2MDevice());
initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, swLwM2MDevice = new SwLwM2MDevice());
initializer.setClassForObject(ACCESS_CONTROL, DummyInstanceEnabler.class);
initializer.setInstancesForObject(BINARY_APP_DATA_CONTAINER, lwM2MBinaryAppDataContainer = new LwM2mBinaryAppDataContainer(executor, OBJECT_INSTANCE_ID_0), new LwM2mBinaryAppDataContainer(executor, OBJECT_INSTANCE_ID_1));
locationParams = new LwM2MLocationParams();
locationParams.getPos();
initializer.setInstancesForObject(LOCATION, new LwM2mLocation(locationParams.getLatitude(), locationParams.getLongitude(), locationParams.getScaleFactor(), executor, OBJECT_INSTANCE_ID_0));
initializer.setInstancesForObject(TEMPERATURE_SENSOR, lwM2MTemperatureSensor = new LwM2mTemperatureSensor(executor, OBJECT_INSTANCE_ID_0), new LwM2mTemperatureSensor(executor, OBJECT_INSTANCE_ID_12));
DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(coapConfig);
dtlsConfig.set(DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
DefaultRegistrationEngineFactory engineFactory = new DefaultRegistrationEngineFactory();
engineFactory.setReconnectOnUpdate(false);
engineFactory.setResumeOnConnect(true);
LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
builder.setLocalAddress("0.0.0.0", port);
builder.setObjects(initializer.createAll());
builder.setCoapConfig(coapConfig);
builder.setDtlsConfig(dtlsConfig);
builder.setRegistrationEngineFactory(engineFactory);
builder.setSharedExecutor(executor);
builder.setDecoder(new DefaultLwM2mDecoder(false));
builder.setEncoder(new DefaultLwM2mEncoder(new LwM2mValueConverterImpl(), false));
clientState = ON_INIT;
clientStates = new HashSet<>();
clientStates.add(clientState);
leshanClient = builder.build();
LwM2mClientObserver observer = new LwM2mClientObserver() {
@Override
public void onBootstrapStarted(ServerIdentity bsserver, BootstrapRequest request) {
clientState = ON_BOOTSTRAP_STARTED;
clientStates.add(clientState);
}
@Override
public void onBootstrapSuccess(ServerIdentity bsserver, BootstrapRequest request) {
clientState = ON_BOOTSTRAP_SUCCESS;
clientStates.add(clientState);
}
@Override
public void onBootstrapFailure(ServerIdentity bsserver, BootstrapRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
clientState = ON_BOOTSTRAP_FAILURE;
clientStates.add(clientState);
}
@Override
public void onBootstrapTimeout(ServerIdentity bsserver, BootstrapRequest request) {
clientState = ON_BOOTSTRAP_TIMEOUT;
clientStates.add(clientState);
}
@Override
public void onRegistrationStarted(ServerIdentity server, RegisterRequest request) {
clientState = ON_REGISTRATION_STARTED;
clientStates.add(clientState);
}
@Override
public void onRegistrationSuccess(ServerIdentity server, RegisterRequest request, String registrationID) {
clientState = ON_REGISTRATION_SUCCESS;
clientStates.add(clientState);
}
@Override
public void onRegistrationFailure(ServerIdentity server, RegisterRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
clientState = ON_REGISTRATION_FAILURE;
clientStates.add(clientState);
}
@Override
public void onRegistrationTimeout(ServerIdentity server, RegisterRequest request) {
clientState = ON_REGISTRATION_TIMEOUT;
clientStates.add(clientState);
}
@Override
public void onUpdateStarted(ServerIdentity server, UpdateRequest request) {
clientState = ON_UPDATE_STARTED;
clientStates.add(clientState);
}
@Override
public void onUpdateSuccess(ServerIdentity server, UpdateRequest request) {
clientState = ON_UPDATE_SUCCESS;
clientStates.add(clientState);
}
@Override
public void onUpdateFailure(ServerIdentity server, UpdateRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
clientState = ON_UPDATE_FAILURE;
clientStates.add(clientState);
}
@Override
public void onUpdateTimeout(ServerIdentity server, UpdateRequest request) {
clientState = ON_UPDATE_TIMEOUT;
clientStates.add(clientState);
}
@Override
public void onDeregistrationStarted(ServerIdentity server, DeregisterRequest request) {
clientState = ON_DEREGISTRATION_STARTED;
clientStates.add(clientState);
}
@Override
public void onDeregistrationSuccess(ServerIdentity server, DeregisterRequest request) {
clientState = ON_DEREGISTRATION_SUCCESS;
clientStates.add(clientState);
}
@Override
public void onDeregistrationFailure(ServerIdentity server, DeregisterRequest request, ResponseCode responseCode, String errorMessage, Exception cause) {
clientState = ON_DEREGISTRATION_FAILURE;
clientStates.add(clientState);
}
@Override
public void onDeregistrationTimeout(ServerIdentity server, DeregisterRequest request) {
clientState = ON_DEREGISTRATION_TIMEOUT;
clientStates.add(clientState);
}
@Override
public void onUnexpectedError(Throwable unexpectedError) {
clientState = ON_EXPECTED_ERROR;
clientStates.add(clientState);
}
};
this.leshanClient.addObserver(observer);
if (!isRpc) {
this.start(true);
}
}
Aggregations