Skip to content

Commit

Permalink
add alarm send
Browse files Browse the repository at this point in the history
  • Loading branch information
richzzp committed Aug 25, 2019
1 parent 91f4a83 commit b82f63f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.leshan.client.observer.LwM2mClientObserver;
import org.eclipse.leshan.client.observer.LwM2mClientObserverDispatcher;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.servers.AlarmEngine;
import org.eclipse.leshan.client.servers.BootstrapHandler;
import org.eclipse.leshan.client.servers.RegistrationEngine;
import org.eclipse.leshan.core.californium.EndpointFactory;
Expand All @@ -53,11 +54,14 @@ public class LeshanClient implements LwM2mClient {

private final CoapServer clientSideServer;
private final CaliforniumLwM2mRequestSender requestSender;
//private final CaliforniumLwM2mRequestSender requestSender2;
private final RegistrationEngine engine;
private final BootstrapHandler bootstrapHandler;
private final LwM2mClientObserverDispatcher observers;
public static AlarmEngine alarmEngine;

private final CaliforniumEndpointsManager endpointsManager;
//private final CaliforniumEndpointsManager endpointsManager2;

public LeshanClient(String endpoint, InetSocketAddress localAddress,
List<? extends LwM2mObjectEnabler> objectEnablers, NetworkConfig coapConfig, Builder dtlsConfigBuilder,
Expand Down Expand Up @@ -112,6 +116,12 @@ protected Resource createRoot() {
engine = new RegistrationEngine(endpoint, this.objectEnablers, endpointsManager, requestSender,
bootstrapHandler, observers, additionalAttributes);

// Create sender for alarm
// Create EndpointHandler for alarm
//endpointsManager2 = new CaliforniumEndpointsManager(clientSideServer, localAddress, coapConfig, dtlsConfigBuilder, endpointFactory);
//requestSender2 = new CaliforniumLwM2mRequestSender(endpointsManager2);
//alarmEngine = new AlarmEngine(endpoint, endpointsManager2, this.objectEnablers, requestSender2);
alarmEngine = new AlarmEngine(endpoint, endpointsManager, this.objectEnablers, requestSender);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import java.util.Map.Entry;

import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.CoAP.Code;
import org.eclipse.californium.core.coap.CoAP.Type;
import org.eclipse.californium.elements.AddressEndpointContext;
import org.eclipse.leshan.Link;
import org.eclipse.leshan.core.request.AlarmRequest;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.core.request.BootstrapRequest;
import org.eclipse.leshan.core.request.ContentFormat;
Expand All @@ -39,6 +42,18 @@ public CoapRequestBuilder(InetSocketAddress serverAddress) {
this.serverAddress = serverAddress;
}

@Override
public void visit(AlarmRequest request) {
coapRequest = new Request(Code.POST, Type.NON);
buildRequestSettings();
coapRequest.getOptions().addUriPath("alarm");
coapRequest.getOptions().addUriQuery("ep=" + request.getEndpointName());

Link[] objectLinks = request.getObjectLinks();
if (objectLinks != null)
coapRequest.setPayload(Link.serialize(objectLinks));
}

@Override
public void visit(BootstrapRequest request) {
coapRequest = Request.newPost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.eclipse.leshan.core.californium.ResponseCodeUtil.toLwM2mResponseCode;

import org.eclipse.californium.core.coap.Response;
import org.eclipse.leshan.core.request.AlarmRequest;
import org.eclipse.leshan.core.request.BootstrapRequest;
import org.eclipse.leshan.core.request.DeregisterRequest;
import org.eclipse.leshan.core.request.LwM2mRequest;
Expand All @@ -41,6 +42,10 @@ public LwM2mClientResponseBuilder(Response coapResponse) {
this.coapResponse = coapResponse;
}

@Override
public void visit(AlarmRequest request) {
}

@Override
public void visit(RegisterRequest request) {
if (coapResponse.isError()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ private static enum Status {
SUCCESS, FAILURE, TIMEOUT
}

// restore the last registered DM server
public static Server regServer;

// device state
private final String endpoint;
private final Map<String, String> additionalAttributes;
Expand Down Expand Up @@ -247,6 +250,9 @@ private Status register(Server server) throws InterruptedException {
// Update every lifetime period
long delay = calculateNextUpdate(dmInfo.lifetime);
scheduleUpdate(server, registrationID, delay);

// Save DM server info
regServer = server;

if (observer != null) {
observer.onRegistrationSuccess(server, registrationID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ public class LeshanClientDemo {

private static final Logger LOG = LoggerFactory.getLogger(LeshanClientDemo.class);

private final static String[] modelPaths = new String[] { "3303.xml" };
private final static String[] modelPaths = new String[] { "3303.xml", "alarm.xml" };

private static final int OBJECT_ID_TEMPERATURE_SENSOR = 3303;
private final static String DEFAULT_ENDPOINT = "LeshanClientDemo";
private final static String USAGE = "java -jar leshan-client-demo.jar [OPTION]\n\n";

private static MyLocation locationInstance;
public static MyAlarm alarmInst;

public static void main(final String[] args) {

Expand Down Expand Up @@ -318,6 +319,7 @@ public static void createAndStartClient(String endpoint, String localAddress, in
Float latitude, Float longitude, float scaleFactor) throws CertificateEncodingException {

locationInstance = new MyLocation(latitude, longitude, scaleFactor);
alarmInst = new MyAlarm();

// Initialize model
List<ObjectModel> models = ObjectLoader.loadDefault();
Expand Down Expand Up @@ -360,6 +362,7 @@ public static void createAndStartClient(String endpoint, String localAddress, in
}
initializer.setInstancesForObject(DEVICE, new MyDevice());
initializer.setInstancesForObject(LOCATION, locationInstance);
initializer.setInstancesForObject(ALARM, alarmInst);
initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, new RandomTemperatureSensor());
List<LwM2mObjectEnabler> enablers = initializer.createAll();

Expand Down Expand Up @@ -429,13 +432,18 @@ public void run() {
client.destroy(true); // send de-registration request before destroy
}
});

// Change the location through the Console
try (Scanner scanner = new Scanner(System.in)) {
while (scanner.hasNext()) {
String nextMove = scanner.next();
locationInstance.moveLocation(nextMove);

alarmInst.SendAlarm(100);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1 change: 1 addition & 0 deletions leshan-core/src/main/java/org/eclipse/leshan/LwM2mId.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface LwM2mId {
public static final int LOCATION = 6;
public static final int CONNECTIVITY_STATISTICS = 7;
public static final int SOFTWARE_MANAGEMENT = 9;
public static final int ALARM = 20010;

/* SECURITY RESOURCES */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* A visitor to visit an Uplink Lightweight M2M request.
*/
public interface UplinkRequestVisitor {
void visit(AlarmRequest request);

void visit(RegisterRequest request);

void visit(UpdateRequest request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ protected Resource createRoot() {
new RegistrationHandler(this.registrationService, authorizer, registrationIdProvider));
coapServer.add(rdResource);

// define /alarm resource
AlarmResource alarmResource = new AlarmResource();
coapServer.add(alarmResource);

// create sender
// notify applications of LWM2M client coming online/offline
if (noQueueMode) {
Expand Down

0 comments on commit b82f63f

Please sign in to comment.