ソースを参照

测试coap协议

xucaiqin 1 年間 前
コミット
277b684da9

+ 172 - 0
Californium3.properties

@@ -0,0 +1,172 @@
+# Random factor for initial CoAP acknowledge timeout.
+# Default: 1.5
+COAP.ACK_INIT_RANDOM=1.5
+# Initial CoAP acknowledge timeout.
+# Default: 2[s]
+COAP.ACK_TIMEOUT=2[s]
+# Scale factor for CoAP acknowledge backoff-timeout.
+# Default: 2.0
+COAP.ACK_TIMEOUT_SCALE=2.0
+# Enable automatic failover on "entity too large" response.
+# Default: true
+COAP.BLOCKWISE_ENTITY_TOO_LARGE_AUTO_FAILOVER=true
+# Interval to validate lifetime of blockwise status.
+# Default: 5[s]
+COAP.BLOCKWISE_STATUS_INTERVAL=5[s]
+# Lifetime of blockwise status.
+# Default: 5[min]
+COAP.BLOCKWISE_STATUS_LIFETIME=5[min]
+# Use block2 option strictly, even if block2 is not required.
+# Default: false
+COAP.BLOCKWISE_STRICT_BLOCK2_OPTION=false
+# CoAP port.
+# Default: 5683
+COAP.COAP_PORT=5683
+# CoAP DTLS port.
+# Default: 5684
+COAP.COAP_SECURE_PORT=5684
+# Congestion-Control algorithm (still experimental).
+# [NULL, COCOA, COCOA_STRONG, BASIC_RTO, LINUX_RTO, PEAKHOPPER_RTO].
+# Default: NULL
+COAP.CONGESTION_CONTROL_ALGORITHM=NULL
+# Crop rotation period.
+# Default: 247[s]
+COAP.CROP_ROTATION_PERIOD=247[s]
+# Deduplicator algorithm.
+# [MARK_AND_SWEEP, PEERS_MARK_AND_SWEEP, CROP_ROTATION, NO_DEDUPLICATOR].
+# Default: MARK_AND_SWEEP
+COAP.DEDUPLICATOR=MARK_AND_SWEEP
+# Automatic replace entries in deduplicator.
+# Default: true
+COAP.DEDUPLICATOR_AUTO_REPLACE=true
+# CoAP maximum exchange lifetime for CON requests.
+# Default: 247[s]
+COAP.EXCHANGE_LIFETIME=247[s]
+# Timespan a multicast server may spread the response.
+# Default: 5[s]
+COAP.LEISURE=5[s]
+# Mark and sweep interval.
+# Default: 10[s]
+COAP.MARK_AND_SWEEP_INTERVAL=10[s]
+# Maximum number of active peers.
+# Default: 150000
+COAP.MAX_ACTIVE_PEERS=150000
+# Maximum transmission latency for messages.
+# Default: 100[s]
+COAP.MAX_LATENCY=100[s]
+# Maximum payload size.
+# Default: 1024
+COAP.MAX_MESSAGE_SIZE=1024
+# Maximum inactive period of peer.
+# Default: 10[min]
+COAP.MAX_PEER_INACTIVITY_PERIOD=10[min]
+# Maximum size of resource body. 0 to disable transparent blockwise
+# mode.
+# Default: 8192
+COAP.MAX_RESOURCE_BODY_SIZE=8192
+# Maximum number of CoAP retransmissions.
+# Default: 4
+COAP.MAX_RETRANSMIT=4
+# Maximum server response delay.
+# Default: 250[s]
+COAP.MAX_SERVER_RESPONSE_DELAY=250[s]
+# Maximum time to wait for ACK or RST after the first transmission of
+# a CON message.
+# Default: 93[s]
+COAP.MAX_TRANSMIT_WAIT=93[s]
+# MID tracker.
+# [NULL, GROUPED, MAPBASED].
+# Default: GROUPED
+COAP.MID_TACKER=GROUPED
+# Number of MID tracker groups.
+# Default: 16
+COAP.MID_TRACKER_GROUPS=16
+# Base MID for multicast requests.
+# Default: 65000
+COAP.MULTICAST_BASE_MID=65000
+# CoAP maximum lifetime for NON requests.
+# Default: 145[s]
+COAP.NON_LIFETIME=145[s]
+# Interval time to check notifications receiver using a CON message.
+# Default: 2[min]
+COAP.NOTIFICATION_CHECK_INTERVAL=2[min]
+# Interval counter to check notifications receiver using a CON message.
+# Default: 100
+COAP.NOTIFICATION_CHECK_INTERVAL_COUNT=100
+# Additional time (backoff) to the max-age option
+# for waiting for the next notification before reregister.
+# Default: 2[s]
+COAP.NOTIFICATION_REREGISTRATION_BACKOFF=2[s]
+# Maximum concurrent transmissions.
+# Default: 1
+COAP.NSTART=1
+# Maximum messages kept per peer for PEERS_MARK_AND_SWEEP.
+# Default: 64
+COAP.PEERS_MARK_AND_SWEEP_MESSAGES=64
+# Preferred blocksize for blockwise transfer.
+# Default: 512
+COAP.PREFERRED_BLOCK_SIZE=512
+# Probing rate to peers, which didn't response before. Currently not
+# used.
+# Default: 1.0
+COAP.PROBING_RATE=1.0
+# Protocol stage thread count.
+# Default: 1
+COAP.PROTOCOL_STAGE_THREAD_COUNT=16
+# Response matching mode.
+# [STRICT, RELAXED, PRINCIPAL, PRINCIPAL_IDENTITY].
+# Default: STRICT
+COAP.RESPONSE_MATCHING=STRICT
+# Number of block per TCP-blockwise bulk transfer.
+# Default: 1
+COAP.TCP_NUMBER_OF_BULK_BLOCKS=4
+# Limit of token size.
+# Default: 8
+COAP.TOKEN_SIZE_LIMIT=8
+# Use message off-loading, when data is not longer required.
+# Default: false
+COAP.USE_MESSAGE_OFFLOADING=false
+# Use initially a random value for MID.
+# Default: true
+COAP.USE_RANDOM_MID_START=true
+# Health status interval. 0 to disable the health status.
+# Default: 0[ms]
+SYS.HEALTH_STATUS_INTERVAL=0[ms]
+# TLS client authentication mode.
+# [NONE, WANTED, NEEDED].
+# Default: WANTED
+TCP.CLIENT_AUTHENTICATION_MODE=WANTED
+# TCP connection idle timeout.
+# Default: 10[s]
+TCP.CONNECTION_IDLE_TIMEOUT=10[s]
+# TCP connect timeout.
+# Default: 10[s]
+TCP.CONNECT_TIMEOUT=10[s]
+# TLS handshake timeout.
+# Default: 10[s]
+TCP.HANDSHAKE_TIMEOUT=10[s]
+# TLS session timeout.
+# Default: 1[h]
+TCP.SESSION_TIMEOUT=1[h]
+# TLS verifies the server certificate's subjects.
+# Default: true
+TCP.VERIFY_SERVER_CERTIFICATES_SUBJECT=true
+# Number of TCP worker threads.
+# Default: 1
+TCP.WORKER_THREADS=1
+# Maximum number of pending outgoing messages.
+# Default: 2147483647
+UDP.CONNECTOR_OUT_CAPACITY=2147483647
+# Maxium size of UDP datagram.
+# Default: 2048
+UDP.DATAGRAM_SIZE=2048
+# Number of UDP receiver threads.
+# Default: 1
+UDP.RECEIVER_THREAD_COUNT=2
+# UDP receive-buffer size.
+UDP.RECEIVE_BUFFER_SIZE=
+# Number of UDP sender threads.
+# Default: 1
+UDP.SENDER_THREAD_COUNT=2
+# UDP send-buffer size.
+UDP.SEND_BUFFER_SIZE=

+ 6 - 1
iot-dependencies/pom.xml

@@ -74,6 +74,7 @@
         <jimureport.version>1.5.8</jimureport.version>
         <jimureport.version>1.5.8</jimureport.version>
         <xercesImpl.version>2.12.2</xercesImpl.version>
         <xercesImpl.version>2.12.2</xercesImpl.version>
         <weixin-java.version>4.5.5.B</weixin-java.version>
         <weixin-java.version>4.5.5.B</weixin-java.version>
+        <coap.version>3.11.0</coap.version>
     </properties>
     </properties>
 
 
     <dependencyManagement>
     <dependencyManagement>
@@ -198,7 +199,11 @@
                 <artifactId>iot-starter-biz-dict</artifactId>
                 <artifactId>iot-starter-biz-dict</artifactId>
                 <version>${revision}</version>
                 <version>${revision}</version>
             </dependency>
             </dependency>
-
+            <dependency>
+                <groupId>org.eclipse.californium</groupId>
+                <artifactId>californium-core</artifactId>
+                <version>${coap.version}</version>
+            </dependency>
             <dependency>
             <dependency>
                 <groupId>com.alibaba</groupId>
                 <groupId>com.alibaba</groupId>
                 <artifactId>druid-spring-boot-3-starter</artifactId>
                 <artifactId>druid-spring-boot-3-starter</artifactId>

+ 37 - 0
iot-framework/iot-starter-coap/pom.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.middle.platform</groupId>
+        <artifactId>iot-framework</artifactId>
+        <version>1.0.0</version>
+    </parent>
+
+    <artifactId>iot-starter-coap</artifactId>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>com.middle.platform</groupId>
+            <artifactId>iot-common-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.eclipse.californium</groupId>
+            <artifactId>californium-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-autoconfigure</artifactId>
+        </dependency>
+    </dependencies>
+</project>

+ 16 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/CoapApplication.java

@@ -0,0 +1,16 @@
+package com.middle.platform.coap;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 14:12:08
+ */
+@SpringBootApplication
+public class CoapApplication {
+    public static void main(String[] args) {
+        // 启动 Spring Boot 应用
+        SpringApplication.run(CoapApplication.class, args);
+    }
+}

+ 18 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/config/CoapProperties.java

@@ -0,0 +1,18 @@
+package com.middle.platform.coap.config;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 09:10:39
+ */
+@Getter
+@Setter
+@AutoConfiguration
+@ConfigurationProperties(prefix = "coap")
+public class CoapProperties {
+    private String host;
+}

+ 21 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/core/AResource.java

@@ -0,0 +1,21 @@
+package com.middle.platform.coap.core;
+
+import org.eclipse.californium.core.CoapResource;
+import org.eclipse.californium.core.server.resources.CoapExchange;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 14:03:41
+ */
+@Service
+public class AResource extends CoapResource {
+    public AResource() {
+        super("12123");
+    }
+
+    @Override
+    public void handleGET(CoapExchange exchange) {
+        exchange.respond("12312312312");
+    }
+}

+ 21 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/core/AResource2.java

@@ -0,0 +1,21 @@
+package com.middle.platform.coap.core;
+
+import org.eclipse.californium.core.CoapResource;
+import org.eclipse.californium.core.server.resources.CoapExchange;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 14:03:41
+ */
+@Service
+public class AResource2 extends CoapResource {
+    public AResource2() {
+        super("312");
+    }
+
+    @Override
+    public void handleGET(CoapExchange exchange) {
+        exchange.respond("12312312312");
+    }
+}

+ 11 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/core/Bootstrap.java

@@ -0,0 +1,11 @@
+package com.middle.platform.coap.core;
+
+import org.springframework.boot.web.embedded.netty.NettyWebServer;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 09:16:43
+ */
+public interface Bootstrap {
+
+}

+ 37 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/core/starter/Client.java

@@ -0,0 +1,37 @@
+package com.middle.platform.coap.core.starter;
+
+import org.eclipse.californium.core.CoapClient;
+import org.eclipse.californium.core.CoapResponse;
+import org.eclipse.californium.core.Utils;
+import org.eclipse.californium.core.config.CoapConfig;
+import org.eclipse.californium.elements.config.Configuration;
+
+import java.net.URI;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 09:35:03
+ */
+public class Client {
+    public static void main(String[] args) throws Exception {
+        hello();
+    }
+
+    public static void hello() throws Exception {
+        Configuration.ModuleDefinitionsProvider definitions = CoapConfig.DEFINITIONS;
+        Configuration.addDefaultModule(definitions);
+
+        // 创建一个资源请求hello资源,注意默认端口为5683
+        URI uri = new URI("localhost:5683/12");
+        CoapClient client = new CoapClient(uri);
+        for (int i = 0; i < 10; i++) {
+            CoapResponse response = client.get();
+            if(response !=null){
+                // 打印格式良好的输出
+                System.out.println(Utils.prettyPrint(response));
+            }
+        }
+
+    }
+
+}

+ 32 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/core/starter/Server.java

@@ -0,0 +1,32 @@
+package com.middle.platform.coap.core.starter;
+
+import lombok.RequiredArgsConstructor;
+import org.eclipse.californium.core.CoapServer;
+import org.eclipse.californium.core.config.CoapConfig;
+import org.eclipse.californium.core.server.resources.Resource;
+import org.eclipse.californium.elements.config.Configuration;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 09:22:46
+ */
+@Component
+@RequiredArgsConstructor
+public class Server {
+    private final List<Resource> list;
+
+    public void start() {
+        Configuration.ModuleDefinitionsProvider definitions = CoapConfig.DEFINITIONS;
+        Configuration.addDefaultModule(definitions);
+        Configuration standard = Configuration.getStandard();
+        // 创建CoAP服务器
+        CoapServer server = new CoapServer(standard);
+        server.add(list.toArray(new Resource[0]));
+        // 启动服务器
+        server.start();
+    }
+
+}

+ 22 - 0
iot-framework/iot-starter-coap/src/main/java/com/middle/platform/coap/run/Runner.java

@@ -0,0 +1,22 @@
+package com.middle.platform.coap.run;
+
+import com.middle.platform.coap.core.starter.Server;
+import jakarta.annotation.Resource;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 14:12:40
+ */
+@Component
+public class Runner implements ApplicationRunner {
+    @Resource
+    private Server server;
+
+    @Override
+    public void run(ApplicationArguments args) throws Exception {
+        server.start();
+    }
+}

+ 1 - 0
iot-framework/pom.xml

@@ -29,6 +29,7 @@
         <module>iot-starter-biz-dict</module>
         <module>iot-starter-biz-dict</module>
         <module>iot-starter-excel</module>
         <module>iot-starter-excel</module>
         <module>iot-starter-kafka</module>
         <module>iot-starter-kafka</module>
+        <module>iot-starter-coap</module>
     </modules>
     </modules>
 
 
     <properties>
     <properties>