监测平台系统coap集成配置及测试

master
马雪伟 3 months ago
parent 441b390210
commit 080933782e

@ -96,6 +96,11 @@
<artifactId>jts-core</artifactId> <artifactId>jts-core</artifactId>
<version>1.19.0</version> <version>1.19.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
<version>3.12.1</version>
</dependency>
</dependencies> </dependencies>

@ -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);
}
}

@ -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<Object, Object> 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
// }
}

@ -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;
}
}
Loading…
Cancel
Save