From 080933782ec0e4a1f87ae42b9c82d990420793f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E9=9B=AA=E4=BC=9F?= Date: Fri, 30 Aug 2024 13:33:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E6=B5=8B=E5=B9=B3=E5=8F=B0=E7=B3=BB?= =?UTF-8?q?=E7=BB=9Fcoap=E9=9B=86=E6=88=90=E9=85=8D=E7=BD=AE=E5=8F=8A?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-modules/hw-data-process/pom.xml | 5 ++ .../coap/coapHandler/ResourceHandle.java | 33 +++++++++++++ .../dataprocess/coap/coapHandler/test.java | 47 +++++++++++++++++++ .../dataprocess/coap/config/CoapConfig.java | 21 +++++++++ 4 files changed, 106 insertions(+) create mode 100644 ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/ResourceHandle.java create mode 100644 ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/test.java create mode 100644 ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/config/CoapConfig.java diff --git a/ruoyi-modules/hw-data-process/pom.xml b/ruoyi-modules/hw-data-process/pom.xml index d2f099a..178197b 100644 --- a/ruoyi-modules/hw-data-process/pom.xml +++ b/ruoyi-modules/hw-data-process/pom.xml @@ -96,6 +96,11 @@ jts-core 1.19.0 + + org.eclipse.californium + californium-core + 3.12.1 + diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/ResourceHandle.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/ResourceHandle.java new file mode 100644 index 0000000..6a37459 --- /dev/null +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/ResourceHandle.java @@ -0,0 +1,33 @@ +package com.ruoyi.dataprocess.coap.coapHandler; + +import org.eclipse.californium.core.CoapResource; +import org.eclipse.californium.core.coap.CoAP; +import org.eclipse.californium.core.coap.OptionSet; +import org.eclipse.californium.core.server.resources.CoapExchange; + +import java.net.InetAddress; +import java.nio.charset.StandardCharsets; + +public class ResourceHandle extends CoapResource { + public ResourceHandle(String name) { + super(name); + } + + @Override + public void handleGET(CoapExchange exchange) { + exchange.respond(CoAP.ResponseCode._UNKNOWN_SUCCESS_CODE,"get请求",50); + InetAddress sourceAddress = exchange.getSourceAddress(); + System.out.println(sourceAddress); + OptionSet requestOptions = exchange.getRequestOptions(); + System.out.println(requestOptions); + System.out.println(exchange.getQueryParameter("a")); + } + + @Override + public void handlePOST(CoapExchange exchange) { + System.out.println(new String(exchange.getRequestPayload(), StandardCharsets.UTF_8)); + exchange.respond(CoAP.ResponseCode.CONTENT,"回复成功",50); + } + + +} diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/test.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/test.java new file mode 100644 index 0000000..25fc141 --- /dev/null +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/coapHandler/test.java @@ -0,0 +1,47 @@ +package com.ruoyi.dataprocess.coap.coapHandler; + +import net.dreamlu.iot.mqtt.codec.MqttPublishMessage; +import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate; +import org.eclipse.californium.core.CoapClient; +import org.eclipse.californium.core.CoapHandler; +import org.eclipse.californium.core.CoapResponse; +import org.eclipse.californium.elements.exception.ConnectorException; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; + +@RestController +public class test { + @GetMapping("test") + public void test() throws ConnectorException, IOException { + CoapClient coapClient = new CoapClient("coap://10.11.40.122:5683/shidu"); + coapClient.setURI("coap://10.11.40.122:5683/shidu?a=b"); + CoapResponse coapResponse1 = coapClient.get(40); + System.out.println(new String(coapResponse1.getPayload(),StandardCharsets.UTF_8)); + HashMap map = new HashMap<>(); + map.put("1","发送1"); + map.put("2","发送2"); + map.put("3","发送4"); + coapClient.post(new CoapHandler() { +// 对成功响应返回进行处理 + @Override + public void onLoad(CoapResponse coapResponse) { + + } +// 对失败响应处理 + @Override + public void onError() { + + } + },map.toString(), 40); +// System.out.println(new String(coapResponse.getPayload(), StandardCharsets.UTF_8)); + } + +// @GetMapping("mqtt") +// public void mqttSend(){ +// new mica +// } +} diff --git a/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/config/CoapConfig.java b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/config/CoapConfig.java new file mode 100644 index 0000000..d34d6d0 --- /dev/null +++ b/ruoyi-modules/hw-data-process/src/main/java/com/ruoyi/dataprocess/coap/config/CoapConfig.java @@ -0,0 +1,21 @@ +package com.ruoyi.dataprocess.coap.config; + +import com.ruoyi.dataprocess.coap.coapHandler.ResourceHandle; +import org.apache.commons.math3.ml.neuralnet.Network; +import org.eclipse.californium.core.CoapServer; +import org.eclipse.californium.core.network.Endpoint; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +public class CoapConfig { + @Bean(initMethod = "start") + public CoapServer coapServer(){ + CoapServer coapServer = new CoapServer(); +// 根据需求添加resource接收消息处理,name区别 + coapServer.add(new ResourceHandle("shidu")); + coapServer.start(); + return coapServer; + } +}