监测平台系统coap集成配置及测试
parent
04445e77ae
commit
2917a7a0e5
@ -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
|
||||||
|
// }
|
||||||
|
}
|
Loading…
Reference in New Issue