-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserApp.java
More file actions
135 lines (119 loc) · 4.41 KB
/
Copy pathUserApp.java
File metadata and controls
135 lines (119 loc) · 4.41 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.internal.wire.MqttReceivedMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
/**
* @author Yatin Kewale
*
*/
public class ReceivingApp implements MqttCallback {
public static final String DEFAULT_PROTOCOL = "tcp";
// public static final String DEFAULT_HOST = "test.mosquitto.org";
public static final String DEFAULT_HOST = "iot.eclipse.org";
public static final int DEFAULT_PORT = 1883;
private static final Logger _Logger = Logger.getLogger(ReceivingApp.class.getName());
// params
private String _clientID;
private String _protocol;
private String _host;
private int _port;
private String _brokerAddr;
private MqttClient _client;
// constructors
public ReceivingApp() {
super();
_clientID = MqttClient.generateClientId();
_protocol = DEFAULT_PROTOCOL;
_host = DEFAULT_HOST;
_port = DEFAULT_PORT;
_brokerAddr = DEFAULT_PROTOCOL + "://" + DEFAULT_HOST + ":" + DEFAULT_PORT;
_Logger.info("Broker Address: " + _brokerAddr);
}
// public methods
public boolean disconnect() {
boolean success = false;
try {
_client.disconnect();
_Logger.info("Disconnected from Broker" + _brokerAddr);
success = true;
} catch (Exception e) {
_Logger.log(Level.SEVERE, "Failed to disconnect from broker: " + _brokerAddr);
}
return success;
}
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String topic, MqttMessage msg) throws Exception { //public method to get the received messages and check the source
System.out.println(topic + " is the group topic with message " + msg);
if (topic == "/MDAS") {
//_client.subscribe(topic);
System.out.println(" Janvi's security system sending");
} else if (topic == "/SafeDoor") {
//_client.subscribe(topic);
System.out.println(" Akash's security system sending data");
} else if(topic=="RAESSL"){
System.out.println(" Its my own data");
}
else System.out.println("Some common topic we have ");
// TODO Auto-generated method stub
}
public boolean connect() {
MemoryPersistence persistence = new MemoryPersistence();
boolean success = false;
try {
System.out.println("in MQTT CLIENT=============================");
// System.out.println("Got this "+ path);
_client = new MqttClient(_brokerAddr, _clientID, persistence); //creating new MQTT object with required parameters
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
_Logger.info("Connecting to a broker: " + _brokerAddr);
_client.setCallback(this);
_client.connect(connOpts);
_Logger.info("Connected to a broker: " + _brokerAddr);
long waitMillis = 650L;
_Logger.info("Waiting at least " + waitMillis + " seconds for ping...");
try {
Thread.sleep(waitMillis);
} catch (Exception e) {
// ignore
}
// success = true;
System.out.println("Out of MQTT CLIENT=============================");
} catch (MqttException e) {
_Logger.log(Level.SEVERE, "Failed to connect to broker: " + _brokerAddr);
}
return success;// TODO Auto-generated method stub
}
//public method which takes a string as argument and subscribes to the topic mentioned by the string
public boolean subscribeMessage(String topic) throws MqttException {
{
try {
_Logger.info("Subscribing to " + topic);
_client.subscribeWithResponse(topic);
_client.subscribe(topic); //subscribing to various topics for test
// _client.subscribe("SafeDoor");
Thread.sleep(2000);
// _client.unsubscribe(topic);
// _Logger.info("Client Unsubscribed to topic: " + topic);
} catch (Exception e) {
_Logger.log(Level.SEVERE, "Failed to subscribe sync message", e);
}
}
return true;
}
}