-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceAuthExample.java
More file actions
96 lines (70 loc) · 3.01 KB
/
DeviceAuthExample.java
File metadata and controls
96 lines (70 loc) · 3.01 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
package com.clearblade.examples;
import com.clearblade.java.api.*;
import com.clearblade.java.api.auth.DeviceAuth;
public class DeviceAuthExample {
public static InitOptions makeInitOptions() {
String platformUrl = System.getenv("PLATFORM_URL");
String messagingUrl = System.getenv("MESSAGING_URL");
String systemKey = System.getenv("SYSTEM_KEY");
String deviceName = System.getenv("DEVICE_NAME");
String activeKey = System.getenv("ACTIVE_KEY");
InitOptions options = new InitOptions();
if (platformUrl != null && platformUrl.length() > 0) {
System.out.println("Found PLATFORM_URL");
options.setPlatformUrl(platformUrl);
}
if (messagingUrl != null && messagingUrl.length() > 0) {
System.out.println("Found MESSAGING_URL");
options.setMessagingUrl(messagingUrl);
}
options.setAuth(new DeviceAuth(systemKey, deviceName, activeKey));
return options;
}
public static void initializeClearBlade() {
String systemKey = System.getenv("SYSTEM_KEY");
String systemSecret = System.getenv("SYSTEM_SECRET");
InitCallback initCb = new InitCallback() {
@Override
public void done(boolean results) {
System.out.print("done initializing\n");
}
@Override
public void error(ClearBladeException exception) {
System.err.printf("error initializing: %s\n", exception);
exception.getCause().printStackTrace();
}
};
InitOptions options = makeInitOptions();
ClearBlade.initialize(systemKey, systemSecret, options, initCb);
}
public static void main(String[] args) throws InterruptedException, ClearBladeException {
initializeClearBlade();
String identifier = "deviceClientID";
String firstTopic = "firstDeviceMessagingTopic";
String secondTopic = "secondDeviceMessagingTopic";
MqttClient mqttClient = new MqttClient(identifier, 1);
mqttClient.subscribe(firstTopic, new MessageCallback() {
@Override
public void done(String topic, String message){
System.out.printf("first received %s: %s%n", topic, message);
}
});
mqttClient.subscribe(secondTopic, new MessageCallback() {
@Override
public void done(String topic, String message) {
System.out.printf("second received %s: %s%n", topic, message);
}
});
System.out.println("Publishing...");
for (int idx = 0; idx < 100; idx++) {
try {
mqttClient.publish(firstTopic, String.format("messageBody %d", idx));
mqttClient.publish(secondTopic, String.format("messageBody %d", idx));
} catch (ClearBladeException e) {
System.out.println(e.getMessage());
}
Thread.sleep(1000);
}
mqttClient.disconnect();
}
}