Created
January 10, 2018 07:40
-
-
Save bolddp/d4f13efe502d0915c550e4fc429aeb0a to your computer and use it in GitHub Desktop.
Lwm2mServer sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.eclipse.leshan.client.object.Server; | |
import org.eclipse.leshan.core.request.BindingMode; | |
import org.eclipse.leshan.core.response.ExecuteResponse; | |
import com.husqvarnagroup.dss.husqiot.common.lwm2m.Lwm2mConstants; | |
import com.husqvarnagroup.dss.iot.sensim.simulation.sensor.Sensor; | |
import com.husqvarnagroup.dss.iot.sensim.simulation.sensor.SensorState; | |
public class Lwm2mServerInstance extends Server { | |
private static final int CLIENT_STOP_DELAY = 3000; | |
private final Sensor sensor; | |
public Lwm2mServerInstance(final Sensor sensor, final int shortServerId, final long lifetime, final BindingMode binding, | |
final boolean notifyWhenDisable) { | |
super(shortServerId, lifetime, binding, notifyWhenDisable); | |
this.sensor = sensor; | |
} | |
@Override | |
public ExecuteResponse execute(final int resourceid, final String params) { | |
if (resourceid == Lwm2mConstants.Server.v1_0.RESOURCE_REGISTRATION_UPDATE_TRIGGER) { | |
sensor.getClient().triggerRegistrationUpdate(); | |
LOG.info("Trigger registration update received (sensor: {})", sensor.getEndpointName()); | |
return ExecuteResponse.success(); | |
} else if (resourceid == Lwm2mConstants.Server.v1_0.RESOURCE_DISABLE) { | |
// Disconnect the sensor | |
LOG.info("Disable received (sensor: {})", sensor.getEndpointName()); | |
new java.util.Timer().schedule( | |
new java.util.TimerTask() { | |
@Override | |
public void run() { | |
// Stop the client and update state | |
sensor.getClient().stop(true); | |
} | |
}, CLIENT_STOP_DELAY); | |
return ExecuteResponse.success(); | |
} | |
return super.execute(resourceid, params); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Sensor { | |
private LeshanClient client; | |
public Sensor() { | |
// Setup objects initializer | |
final ObjectsInitializer initializer = new ObjectsInitializer(objectModel); | |
... | |
initializer.setInstancesForObject(SERVER, new Lwm2mServerInstance(this, 123, 30, BindingMode.U, false)); | |
... add other object instances | |
// Create client | |
final LeshanClientBuilder builder = new LeshanClientBuilder(sensor.getEndpointName()); | |
builder.setLocalAddress(null, 0); | |
builder.setLocalSecureAddress(null, 0); | |
builder.setObjects(enablers); | |
builder.setCoapConfig(NetworkConfig.getStandard()); | |
client = builder.build(); | |
} | |
public LeshanClient getClient() { | |
return client; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment