-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegration.java
63 lines (40 loc) · 1.61 KB
/
Integration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//COAP inside MQTT
package com.iot.coap.demo;
import java.util.UUID;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A simple CoAP Synchronous Client implemented using Californium Java Library
* The client Observe a target resource for 10 Seconds and then cancel the request and ends the execution
*/
public class Integration {
private final static Logger logger = LoggerFactory.getLogger(Integration.class);
//private static String SERVER_URI = "tcp://iot.eclipse.org:1883";
private static String SERVER_URI = "tcp://127.0.0.1:1883";
public static void main(String [ ] args) throws MqttException {
logger.info("SimpleProducer started ...");
try{
String publisherId = UUID.randomUUID().toString();
IMqttClient publisher = new MqttClient(SERVER_URI ,publisherId);
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
publisher.connect(options);
logger.info("Connected !");
//this is the heart of the MQTT function- it is publish
CoapObservingClientProcess coapObservingClientProcess = new CoapObservingClientProcess(publisher);
coapObservingClientProcess.call();
logger.info("Message Sent !");
publisher.disconnect();
publisher.close();
logger.info("Disconnected !");
}catch (Exception e){
e.printStackTrace();
}
}
}