From b82f63f48dc4f3778a09d180eb2b34c89aefbb02 Mon Sep 17 00:00:00 2001 From: richzzp Date: Sun, 25 Aug 2019 15:35:22 +0800 Subject: [PATCH] add alarm send --- .../leshan/client/californium/LeshanClient.java | 10 ++++++++++ .../californium/impl/CoapRequestBuilder.java | 15 +++++++++++++++ .../impl/LwM2mClientResponseBuilder.java | 5 +++++ .../leshan/client/servers/RegistrationEngine.java | 6 ++++++ .../leshan/client/demo/LeshanClientDemo.java | 14 +++++++++++--- .../src/main/java/org/eclipse/leshan/LwM2mId.java | 1 + .../leshan/core/request/UplinkRequestVisitor.java | 2 ++ .../server/californium/impl/LeshanServer.java | 4 ++++ 8 files changed, 54 insertions(+), 3 deletions(-) diff --git a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/LeshanClient.java b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/LeshanClient.java index 8b7c1f58cf..10c52d28b6 100644 --- a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/LeshanClient.java +++ b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/LeshanClient.java @@ -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; @@ -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 objectEnablers, NetworkConfig coapConfig, Builder dtlsConfigBuilder, @@ -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 diff --git a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/CoapRequestBuilder.java b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/CoapRequestBuilder.java index 20b4d339fd..028786d060 100644 --- a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/CoapRequestBuilder.java +++ b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/CoapRequestBuilder.java @@ -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; @@ -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(); diff --git a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/LwM2mClientResponseBuilder.java b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/LwM2mClientResponseBuilder.java index d81699ba18..b752595779 100644 --- a/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/LwM2mClientResponseBuilder.java +++ b/leshan-client-cf/src/main/java/org/eclipse/leshan/client/californium/impl/LwM2mClientResponseBuilder.java @@ -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; @@ -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()) { diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java index e3c3168961..53d3ee1351 100644 --- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java +++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/servers/RegistrationEngine.java @@ -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 additionalAttributes; @@ -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); diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java index 6cd81e353e..8541cf4ae6 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java @@ -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) { @@ -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 models = ObjectLoader.loadDefault(); @@ -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 enablers = initializer.createAll(); @@ -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(); + } } } diff --git a/leshan-core/src/main/java/org/eclipse/leshan/LwM2mId.java b/leshan-core/src/main/java/org/eclipse/leshan/LwM2mId.java index 6b24cb9059..8dd1ad70a2 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/LwM2mId.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/LwM2mId.java @@ -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 */ diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/request/UplinkRequestVisitor.java b/leshan-core/src/main/java/org/eclipse/leshan/core/request/UplinkRequestVisitor.java index a74e5d32e9..c19bc3c560 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/request/UplinkRequestVisitor.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/request/UplinkRequestVisitor.java @@ -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); diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanServer.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanServer.java index 97bf80a06d..1e98d0e760 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanServer.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanServer.java @@ -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) {