-
Notifications
You must be signed in to change notification settings - Fork 409
Getting Started : Client
Here you will see how to code your own LWM2M client with Leshan.
To develop your client you need to use the leshan-client-cf
module.
To know the last 1.x version.
<dependencies>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-client-cf</artifactId>
<version><!-- use the last version --></version>
</dependency>
<!-- add any slf4j backend, here we use one of the smallest one: slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>runtime</scope>
</dependency>
</dependencies>
You need to use Leshan builder to create client.
String endpoint = "..." ; // choose an endpoint name
LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
LeshanClient client = builder.build();
client.start();
If you execute this code (with internet access), you should see something like this in your log :
[...] INFO ... - Leshan client[endpoint:myClient] started at coap://0.0.0.0/0.0.0.0:46535 coaps://0.0.0.0/0.0.0.0:36490
[...] INFO ... - Trying to register to coap://leshan.eclipseprojects.io:5683 ...
[...] INFO ... - Registered with location '/rd/w50JjnP76l'.
[...] INFO ... - Next registration update in 270.0s...
That's means that you are successfully register your client to the leshan sandbox. You can check if you see your device in the web UI and you can play with it (Read or Write on Device object).
By default the Leshan client is created with mandatory objects (Security, Server and Device). Internally LeshanClientBuilder
uses the ObjectsInitializer
to create those objects, like this :
// create objects
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://leshan.eclipseprojects.io:5683", 12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, 5 * 60, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", "model12345", "12345", "U"));
// add it to the client
builder.setObjects(initializer.createAll());
We provide only 3 really simple object implementation and you will need to create your own LWM2M objects.
To do that you need to create a class which implement LwM2mInstanceEnabler
or BaseInstanceEnabler
.
public class ConnectivityStatistics extends BaseInstanceEnabler {
@Override
public ReadResponse read(ServerIdentity identity, int resourceid) {
switch (resourceid) {
case 0:
return ReadResponse.success(resourceid, getSmsTxCounter());
}
return ReadResponse.notFound();
}
@Override
public WriteResponse write(ServerIdentity identity, int resourceid, LwM2mResource value) {
switch (resourceid) {
case 15:
setCollectionPeriod((Long) value.getValue());
return WriteResponse.success();
}
return WriteResponse.notFound();
}
@Override
public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
switch (resourceid) {
case 12:
start();
return ExecuteResponse.success();
}
return ExecuteResponse.notFound();
}
Now we add it to your client:
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://leshan.eclipseprojects.io:5683", 12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, 5 * 60, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", "model12345", "12345", "U"));
initializer.setInstancesForObject(7, new ConnectivityStatistics());
// add it to the client
builder.setObjects(initializer.createAll());
You can have a look to the code of our client demo: LeshanClientDemo
in leshan-client-demo
.
All contributions you make to our web site (including this wiki) are governed by our Terms of Use, so please take the time to actually read it. Your interactions with the Eclipse Foundation web properties and any information you may provide us about yourself are governed by our Privacy Policy.