forked from tencentyun/iot-device-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoor.java
155 lines (126 loc) · 5.33 KB
/
Door.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
package main.scenarized;
import com.tencent.iot.hub.device.java.core.common.Status;
import com.tencent.iot.hub.device.java.core.mqtt.TXMqttActionCallBack;
import com.tencent.iot.hub.device.java.core.mqtt.TXMqttConnection;
import com.tencent.iot.hub.device.java.core.mqtt.TXMqttConstants;
import com.tencent.iot.hub.device.java.core.util.AsymcSslUtils;
import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Door {
public static final String TAG = Door.class.getSimpleName();
private static final Logger LOG = LoggerFactory.getLogger(Door.class);
/**
* 产品ID
*/
private static final String PRODUCT_ID = "YOUR_PRODUCT_ID";
/**
* 设备名称
*/
private static final String DEVICE_NAME = "YOUR_DEVICE_NAME";
/**
* 密钥
*/
private static final String SECRET_KEY = "YOUR_SECRET_KEY";
/**
* 设备证书名
*/
private static final String DEVICE_CERT_NAME = "YOUR_DEVICE_NAME_cert.crt";
/**
* 设备私钥文件名
*/
private static final String DEVICE_KEY_NAME = "YOUR_DEVICE_NAME_private.key";
private static final String COME_HOME_MESSAGE = "{\"action\": \"come_home\", \"targetDevice\": \"" + Airconditioner.DEVICE_NAME + "\"}";
private static final String LEAVE_HOME_MESSAGE = "{\"action\": \"leave_home\", \"targetDevice\": \"" + Airconditioner.DEVICE_NAME + "\"}";
private TXMqttConnection mqttConnection;
private MqttConnectOptions options;
public Door() {
}
/**
* 进门
*/
public void enterRoom() {
if (mqttConnection == null) {
mqttConnection = new TXMqttConnection(PRODUCT_ID, DEVICE_NAME, SECRET_KEY, new DoorMqttActionCallBack());
options = new MqttConnectOptions();
options.setConnectionTimeout(8);
options.setKeepAliveInterval(240);
options.setAutomaticReconnect(true);
options.setSocketFactory(AsymcSslUtils.getSocketFactoryByAssetsFile(DEVICE_CERT_NAME, DEVICE_KEY_NAME));
mqttConnection.connect(options, null);
DisconnectedBufferOptions bufferOptions = new DisconnectedBufferOptions();
bufferOptions.setBufferEnabled(true);
bufferOptions.setBufferSize(1024);
bufferOptions.setDeleteOldestMessages(true);
mqttConnection.setBufferOpts(bufferOptions);
}
if (mqttConnection.getConnectStatus().equals(TXMqttConstants.ConnectStatus.kConnected)) {
MqttMessage message = new MqttMessage();
message.setPayload(COME_HOME_MESSAGE.getBytes());
String topic = String.format("%s/%s/%s", PRODUCT_ID, DEVICE_NAME, "event");
mqttConnection.publish(topic, message, null);
} else {
//mqttConnection.connect(options, null);
}
}
/**
* 出门
*/
public void leaveRoom() {
if (null == mqttConnection) {
LOG.error("please enter room first!");
return;
}
MqttMessage message = new MqttMessage();
message.setPayload(LEAVE_HOME_MESSAGE.getBytes());
String topic = String.format("%s/%s/%s", PRODUCT_ID, DEVICE_NAME, "event");
mqttConnection.publish(topic, message, null);
closeConnection();
}
public void closeConnection() {
if (null != mqttConnection) {
mqttConnection.disConnect(null);
mqttConnection = null;
}
}
private class DoorMqttActionCallBack extends TXMqttActionCallBack {
@Override
public void onConnectCompleted(Status status, boolean reconnect, Object userContext, String msg) {
LOG.info("onConnectCompleted:" + msg);
if (status.equals(Status.OK)) {
if (!reconnect) {
MqttMessage message = new MqttMessage();
message.setPayload(COME_HOME_MESSAGE.getBytes());
String topic = String.format("%s/%s/%s", PRODUCT_ID, DEVICE_NAME, "event");
mqttConnection.publish(topic, message, null);
}
}
}
@Override
public void onConnectionLost(Throwable cause) {
String logInfo = String.format("onConnectionLost, cause[%s]", cause.toString());
LOG.info(logInfo);
}
@Override
public void onDisconnectCompleted(Status status, Object userContext, String msg) {
String logInfo = String.format("onDisconnectCompleted, status[%s], msg[%s]", status.name(), msg);
LOG.info(logInfo);
}
@Override
public void onSubscribeCompleted(Status status, IMqttToken token, Object userContext, String msg) {
String logInfo = String.format("onSubscribeCompleted, status[%s], message[%s]", status.name(), msg);
if (Status.ERROR == status) {
LOG.error(logInfo);
} else {
LOG.info(logInfo);
}
}
@Override
public void onPublishCompleted(Status status, IMqttToken token, Object userContext, String msg) {
super.onPublishCompleted(status, token, userContext, msg);
}
}
}