ApplicationRunnerImpl.java
6.89 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
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
179
180
181
182
183
184
185
186
187
188
package com.shunzhi.mqtt2kanban;
import com.shunzhi.mqtt2kanban.service.UserSeerviceImp;
import com.shunzhi.mqtt2kanban.utils.Tools;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Set;
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
private static String accessKey;
private static String sign;
public static MqttClient mqttClient;
public static String groupId;
public static String topic;
private static int qosLevel;
public static boolean isConnect;
private Set<String> set = new HashSet<String>();
@Autowired
UserSeerviceImp userSeerviceImp;
@Override
public void run(ApplicationArguments args) throws Exception {
initMq();
}
/**
* 初始化MQ
*/
private void initMq() {
final String brokerUrl = "tcp://post-cn-4590mq2hr03.mqtt.aliyuncs.com:1883";
groupId = "GID_HFJSIURFHAQO110";
topic = "Topic_Quene_Test";
qosLevel = 1;
final Boolean cleanSession = false;
String clientId = groupId + "@@@9ED96FB6D72C1698";
accessKey = "UimvLVp0Wj90P88u";
String secretKey = "TE4rZenITG27tiQqHx9qINjx71Nws7";
final MemoryPersistence memoryPersistence = new MemoryPersistence();
if (null == mqttClient) {
try {
mqttClient = new MqttClient(brokerUrl, clientId, memoryPersistence);
} catch (MqttException e) {
e.printStackTrace();
}
MqttConnectOptions connOpts = new MqttConnectOptions();
//cal the sign as password,sign=BASE64(MAC.SHA1(groupId,secretKey))
try {
sign = Tools.macSignature(clientId.split("@@@")[0], secretKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
connOpts.setUserName(accessKey);
connOpts.setPassword(sign.toCharArray());
connOpts.setCleanSession(cleanSession);
connOpts.setKeepAliveInterval(90);
connOpts.setAutomaticReconnect(true);
mqttClient.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
isConnect = true;
System.out.println("connect success");
}
@Override
public void connectionLost(Throwable throwable) {
isConnect = false;
System.out.println("connect lost:" + throwable.toString());
throwable.printStackTrace();
initMq();//初始化
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
System.out.println("receive msg from topic " + s + " , body is " + new String(mqttMessage.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
//this notice make sense when qos >0
// System.out.println("send msg succeed");
}
});
try {
mqttClient.connect(connOpts);
sendMq();
} catch (MqttException e) {
System.out.println("mqtt:" + e.toString());
e.printStackTrace();
}
/*while (true){
sendMessageTest("528C8E6CD4A3C659","zy105387",0);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("connect success:"+e);
e.printStackTrace();
}
}*/
}
}
private void sendMq() {
String recvClientId = groupId + "@@@09EA8EB5142F2964";
final String p2pSendTopic = topic + "/p2p/" + recvClientId;
String content = "{\"cmd\":\"37\",\"clientId\":\"\",\"data\": \"6:00-22:00\"}";
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qosLevel);
System.out.println("发送内容:" + p2pSendTopic);
if (null != mqttClient) {
try {
mqttClient.publish(p2pSendTopic, message);
} catch (MqttException e) {
e.printStackTrace();
}
}
}
/**
* @param equipmentSN 控制器SN号
* @param consumerNO userid
* @param inOrOut 进出状态
*/
public void sendMessage(String equipmentSN, String consumerNO, int inOrOut) {
try {
String recvClientId = groupId + "@@@";
if (null != Mqtt2kanbanApplication.deviceList) {
for (int i = 0; i < Mqtt2kanbanApplication.deviceList.size(); i++) {
String devices = Mqtt2kanbanApplication.deviceList.get(i);
if (devices.contains(equipmentSN))
recvClientId += devices.split(",")[0];
}
final String p2pSendTopic = topic + "/p2p/" + recvClientId;
String data = "{\"userId\":\"" + consumerNO + "\",\"inOrOut\":" + inOrOut + "}";
String content = "{\"cmd\":\"34\",\"clientId\":\"\",\"data\":" + data + "}";
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qosLevel);
System.out.println("发送内容:" + p2pSendTopic);
if (null != mqttClient)
mqttClient.publish(p2pSendTopic, message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMessageTest(String equipmentSN, String consumerNO, int inOrOut) {
try {
String recvClientId = groupId + "@@@"+equipmentSN;
final String p2pSendTopic = topic + "/p2p/" + recvClientId;
String data = "{\"userId\":\"" + consumerNO + "\",\"inOrOut\":" + inOrOut + "}";
String content = "{\"cmd\":\"34\",\"clientId\":\"\",\"data\":" + data + "}";
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qosLevel);
System.out.println("发送内容:" + p2pSendTopic+" "+message);
if (null != mqttClient)
mqttClient.publish(p2pSendTopic, message);
} catch (Exception e) {
e.printStackTrace();
}
}
}