use of org.eclipse.leshan.client.californium.LeshanClientBuilder in project leshan by eclipse.
the class LeshanClientDemo method createAndStartClient.
public static void createAndStartClient(String endpoint, String localAddress, int localPort, String secureLocalAddress, int secureLocalPort, boolean needBootstrap, String serverURI, byte[] pskIdentity, byte[] pskKey, Float latitude, Float longitude, float scaleFactor) {
locationInstance = new MyLocation(latitude, longitude, scaleFactor);
// Initialize model
List<ObjectModel> models = ObjectLoader.loadDefault();
models.addAll(ObjectLoader.loadDdfResources("/models", modelPaths));
// Initialize object list
ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(models));
if (needBootstrap) {
if (pskIdentity == null)
initializer.setInstancesForObject(SECURITY, noSecBootstap(serverURI));
else
initializer.setInstancesForObject(SECURITY, pskBootstrap(serverURI, pskIdentity, pskKey));
} else {
if (pskIdentity == null) {
initializer.setInstancesForObject(SECURITY, noSec(serverURI, 123));
initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
} else {
initializer.setInstancesForObject(SECURITY, psk(serverURI, 123, pskIdentity, pskKey));
initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
}
}
initializer.setClassForObject(DEVICE, MyDevice.class);
initializer.setInstancesForObject(LOCATION, locationInstance);
initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, new RandomTemperatureSensor());
List<LwM2mObjectEnabler> enablers = initializer.create(SECURITY, SERVER, DEVICE, LOCATION, OBJECT_ID_TEMPERATURE_SENSOR);
// Create CoAP Config
NetworkConfig coapConfig;
File configFile = new File(NetworkConfig.DEFAULT_FILE_NAME);
if (configFile.isFile()) {
coapConfig = new NetworkConfig();
coapConfig.load(configFile);
} else {
coapConfig = LeshanClientBuilder.createDefaultNetworkConfig();
coapConfig.store(configFile);
}
// Create client
LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
builder.setLocalAddress(localAddress, localPort);
builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);
builder.setObjects(enablers);
builder.setCoapConfig(coapConfig);
// so we can disable the other one.
if (!needBootstrap) {
if (pskIdentity == null)
builder.disableSecuredEndpoint();
else
builder.disableUnsecuredEndpoint();
}
final LeshanClient client = builder.build();
LOG.info("Press 'w','a','s','d' to change reported Location ({},{}).", locationInstance.getLatitude(), locationInstance.getLongitude());
// Start the client
client.start();
// De-register on shutdown and stop client.
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// send de-registration request before destroy
client.destroy(true);
}
});
// Change the location through the Console
try (Scanner scanner = new Scanner(System.in)) {
while (scanner.hasNext()) {
String nextMove = scanner.next();
locationInstance.moveLocation(nextMove);
}
}
}
use of org.eclipse.leshan.client.californium.LeshanClientBuilder in project leshan by eclipse.
the class RegistrationTest method fail_to_create_client_with_same_object_twice.
// TODO not really a registration test
@Test(expected = IllegalArgumentException.class)
public void fail_to_create_client_with_same_object_twice() {
ObjectEnabler objectEnabler = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null);
ObjectEnabler objectEnabler2 = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null);
ArrayList<LwM2mObjectEnabler> objects = new ArrayList<>();
objects.add(objectEnabler);
objects.add(objectEnabler2);
helper.client = new LeshanClientBuilder("test").setObjects(objects).build();
}
use of org.eclipse.leshan.client.californium.LeshanClientBuilder in project leshan by eclipse.
the class SecureIntegrationTestHelper method createPSKClient.
public void createPSKClient() {
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.psk("coaps://" + server.getSecuredAddress().getHostString() + ":" + server.getSecuredAddress().getPort(), 12345, GOOD_PSK_ID.getBytes(StandardCharsets.UTF_8), GOOD_PSK_KEY));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
List<LwM2mObjectEnabler> objects = initializer.createMandatory();
objects.add(initializer.create(2));
InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
builder.setLocalAddress(clientAddress.getHostString(), clientAddress.getPort());
builder.setObjects(objects);
client = builder.build();
}
use of org.eclipse.leshan.client.californium.LeshanClientBuilder in project leshan by eclipse.
the class QueueModeIntegrationTestHelper method createClient.
@Override
public void createClient() {
// Create objects Enabler
ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(createObjectModels()));
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(), 12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.UQ, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "UQ") {
@Override
public ExecuteResponse execute(int resourceid, String params) {
if (resourceid == 4) {
return ExecuteResponse.success();
} else {
return super.execute(resourceid, params);
}
}
});
List<LwM2mObjectEnabler> objects = initializer.createMandatory();
objects.addAll(initializer.create(2, 2000));
// Build Client
LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get());
builder.setObjects(objects);
client = builder.build();
}
use of org.eclipse.leshan.client.californium.LeshanClientBuilder in project leshan by eclipse.
the class IntegrationTestHelper method createClient.
public void createClient() {
// Create objects Enabler
ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(createObjectModels()));
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(), 12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U") {
@Override
public ExecuteResponse execute(int resourceid, String params) {
if (resourceid == 4) {
return ExecuteResponse.success();
} else {
return super.execute(resourceid, params);
}
}
});
List<LwM2mObjectEnabler> objects = initializer.createMandatory();
objects.addAll(initializer.create(2, 2000));
// Build Client
LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get());
builder.setObjects(objects);
client = builder.build();
}
Aggregations