-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmock-device-for-mqtt.java
178 lines (165 loc) · 5.38 KB
/
mock-device-for-mqtt.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package mockdevice;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
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.persist.MemoryPersistence;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MockDevice4Mqtt {
private static String BROKER_HOST_ADDR = "tcp://192.168.56.4:1883";
private static String CLIENTID = "Mock-Device-ID";
private static String RESP_CLIENTID = "Mock-Device-Response-ID";
private static String ACTIVE_CLIENTID = "Mock-Device-Active-ID";
private static int QOS = 0;
private static String USERNAME = "huaqiao";
private static String PWD = "1234";
private static String CMD_TOPIC = "CommandTopic";
private static String RESPONSE_TOPIC = "ResponseTopic";
private static String DATA_TOPIC = "DataTopic";
private static String PAYLOAD = "{\"name\":\"mqtt-device-01\",\"randnum\":\"520.1314\"}";
private MqttClient client = null;
private ObjectMapper JSONmapper = new ObjectMapper();
private String active = "false";
private final Queue<String> globalQueue = new LinkedBlockingQueue<String>(5);
public static void main(String[] args) {
new MockDevice4Mqtt();
for(;;){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public MockDevice4Mqtt() {
initClient();
new Thread(new Runnable() {
@Override
public void run() {
MqttClient dataClient = null;
try {
dataClient = new MqttClient(BROKER_HOST_ADDR, ACTIVE_CLIENTID,new MemoryPersistence());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(USERNAME);
connOpts.setPassword(PWD.toCharArray());
connOpts.setCleanSession(true);
dataClient.connect(connOpts);
System.out.println("Ready to send actively.");
} catch (MqttException e) {
System.out.println("Can't connect when send actively.");
e.printStackTrace();
}
for (;;) {
Object item = globalQueue.poll();
if (item != null) {
active = (String)item;
}
if ("true".equals(active)) {
try {
Thread.sleep(1000);
dataClient.publish(DATA_TOPIC, PAYLOAD.getBytes(), QOS, false);
System.out.println("send data actively : " + PAYLOAD);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}).start();
}
public void initClient() {
try {
client = new MqttClient(BROKER_HOST_ADDR, CLIENTID,new MemoryPersistence());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(USERNAME);
connOpts.setPassword(PWD.toCharArray());
connOpts.setCleanSession(false);
client.connect(connOpts);
} catch (MqttException e) {
System.out.println("can't connect to broker!");
e.printStackTrace();
}
System.out.println("connect success!");
try {
client.subscribe(CMD_TOPIC,new IMqttMessageListener() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String jsonStr = new String(message.getPayload());
System.out.println("Receive cmd : " + jsonStr);
commandHandler(message.getPayload());
}
});
} catch (MqttException e) {
e.printStackTrace();
}
System.out.println("Start subscribe " + CMD_TOPIC + " topic .");
}
@SuppressWarnings("unchecked")
public void commandHandler(byte[] cmdPaylod) {
Map<String,String> cmdMap = null;
try {
cmdMap = JSONmapper.readValue(cmdPaylod, Map.class);
} catch (Exception e) {
System.out.println("Json convert failed." );
}
String cmd = cmdMap.get("cmd");
String method = cmdMap.get("method");
System.out.println(cmd + " : " + method );
switch (cmd) {
case "ping":
cmdMap.put(cmd, "pong");
break;
case "randnum":
cmdMap.put(cmd, "520.1314");
break;
case "message":
if ("get".equals(method)) {
cmdMap.put(cmd, "Are you ok?");
} else {
cmdMap.put("result", "set success.");
}
break;
case "collect":
if ("get".equals(method)) {
cmdMap.put(cmd, active);
} else {
cmdMap.put("result", "set success.");
globalQueue.add(cmdMap.get("param"));
}
break;
}
try {
responseCommand(RESPONSE_TOPIC,JSONmapper.writeValueAsString(cmdMap));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
public void responseCommand(String topic,String payload) {
MqttClient respClient = null;
try {
respClient = new MqttClient(BROKER_HOST_ADDR, RESP_CLIENTID,new MemoryPersistence());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(USERNAME);
connOpts.setPassword(PWD.toCharArray());
connOpts.setCleanSession(true);
respClient.connect(connOpts);
respClient.publish(topic, payload.getBytes(), QOS, false);
System.out.println("Response Cmd :" + payload);
respClient.disconnect();
} catch (Exception e) {
System.out.println("can't publish msg success.");
e.printStackTrace();
try {
respClient.disconnect();
} catch (MqttException e1) {
System.out.println("can't disconnect client.");
e1.printStackTrace();
}
}
}
}