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; + } +}