-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIotClient.java
114 lines (79 loc) · 3.27 KB
/
IotClient.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
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
import com.amazonaws.services.iot.client.AWSIotMqttClient;
import com.amazonaws.services.iot.client.AWSIotQos;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.zip.GZIPOutputStream;
import java.util.Properties;
public class IotClient {
private static Properties prop;
static {
try(InputStream input = IotClient.class.getClassLoader().getResourceAsStream("config.properties")){
prop = new Properties();
if (input == null) {
throw new RuntimeException("Sorry, unable to find config.properties");
}
prop.load(input);
} catch (Exception e){
e.printStackTrace();
}
}
public static class Message {
public Message(String message) {
this.message = message;
}
String message;
}
public static void main(String[] args) throws Exception {
AWSIotMqttClient client = getClientForWebsocket();
// optional parameters can be set before connect()
client.connect();
String[] topics = prop.getProperty("topics").split(",");
for (String topic : topics) {
String[] jsonMessageFiles = prop.getProperty("jsonFiles").split(",");
String dir = prop.getProperty("jsonFilesDirectory");
for (String jsonMessageFile : jsonMessageFiles) {
JSONObject jsonObject = readJsonFile(dir + jsonMessageFile);
jsonObject.replace("date", getFormattedDate());
byte[] compressed = compress(jsonObject.toString());
client.publish(topic, AWSIotQos.QOS0, compressed);
}
}
client.disconnect();
}
private static byte[] compress(String str) throws Exception {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toByteArray();
}
private static String getFormattedDate() {
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
return formatter.format(java.time.Instant.now());
}
private static JSONObject readJsonFile(String filename) throws Exception {
//JSON parser object to parse read file
JSONParser jsonParser = new JSONParser();
FileReader reader = new FileReader(filename);
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONObject pdvEvent = (JSONObject) obj;
System.out.println(pdvEvent);
return pdvEvent;
}
private static AWSIotMqttClient getClientForWebsocket() throws IOException {
String clientEndpoint = prop.getProperty("clientEndpoint");
String clientId = prop.getProperty("clientId");
String awsAccessKeyId = prop.getProperty("awsAccessKeyId");
String awsSecretAccessKey = prop.getProperty("awsSecretAccessKey");
return new AWSIotMqttClient(clientEndpoint, clientId, awsAccessKeyId, awsSecretAccessKey);
}
}