-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMqttClientJava.java
More file actions
122 lines (87 loc) · 2.75 KB
/
MqttClientJava.java
File metadata and controls
122 lines (87 loc) · 2.75 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
package com.clearblade.examples;
import com.clearblade.java.api.*;
import java.util.HashMap;
public class MqttClientJava {
private static MqttClient mqttClient;
private static boolean isInit = false;
public static void main(String[] args) throws ClearBladeException{
initClearBlade();
if (isInit) {
connectToMQTT();
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
ex.getMessage();
}
subscribe("hello");
try {
Thread.sleep(5000);
} catch(InterruptedException ex) {
ex.getMessage();
}
publish("hello", "this is a test");
try {
Thread.sleep(5000);
} catch(InterruptedException ex) {
ex.getMessage();
}
mqttClient.disconnect();
logout();
}
}
private static void initClearBlade() {
InitCallback initCallback = new InitCallback() {
@Override
public void done(boolean results) {
System.out.println("ClearBlade platform initialized");
isInit = true;
}
@Override
public void error(ClearBladeException error) {
isInit = false;
String message = error.getMessage();
System.out.println(message);
}
};
String systemKey = "[SYSTEM KEY GOES HERE]";
String systemSecret = "[SYSTEM SECRET GOES HERE]";
String userEmail = "[USER EMAIL GOES HERE]";
String userPassword = "[USER PASSWORD GOES HERE]";
String platformUrl = "[PLATFORM URL GOES HERE]";
String messagingUrl = "[MESSAGING URL GOES HERE]";
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("email", userEmail);
options.put("password", userPassword);
options.put("platformURL", platformUrl);
options.put("messagingURL", messagingUrl);
ClearBlade.initialize(systemKey, systemSecret, options, initCallback);
}
private static void connectToMQTT() throws ClearBladeException {
mqttClient = new MqttClient("clientID-test", 1);
}
private static void subscribe(String topic) throws ClearBladeException {
MessageCallback messageCallback = new MessageCallback() {
@Override
public void done(String topic, String message){
System.out.println("Topic: " + topic +" Message received: " + message);
}
@Override
public void error(ClearBladeException exception) {
String message = exception.getLocalizedMessage();
System.out.println("CB Subscribe Exception: " + message);
}
};
mqttClient.subscribe(topic, messageCallback);
}
private static void publish(String topic, String payload) throws ClearBladeException {
mqttClient.publish(topic, payload);
}
private static void logout() {
try {
ClearBlade.getAuth().doLogout();
System.out.println("logged out");
} catch (Exception e) {
System.out.println("logout failed: " + e.getMessage());
}
}
}