refactor(mqtt): 使用MqttManager中的MQTT_TOPIC_COMMAND常量
将订阅主题从CommonConfig.MQTT_TOPIC_COMMAND改为MqttManager.MQTT_TOPIC_COMMAND
This commit is contained in:
@@ -136,7 +136,7 @@ public class BaseApplication extends Application {
|
||||
// 初始化 MQTT 并连接 Broker
|
||||
MqttManager.init(this);
|
||||
MqttManager.getInstance().connect();
|
||||
MqttManager.getInstance().subscribe(CommonConfig.MQTT_TOPIC_COMMAND, 1);
|
||||
MqttManager.getInstance().subscribe(MqttManager.MQTT_TOPIC_COMMAND, 1);
|
||||
SherpaOnnxTtsManager.getInstance().init(this);
|
||||
AlarmManagerHelper.rescheduleAllAlarms(this);
|
||||
|
||||
|
||||
@@ -3,15 +3,6 @@ package com.ttstd.dialer.config;
|
||||
public class CommonConfig {
|
||||
public static final String MMKV_ID = "InterProcessKV";
|
||||
|
||||
/*MQTT 配置*/
|
||||
public static final String MQTT_URL = "mqtt://175.178.213.60:1883";
|
||||
public static final String MQTT_CLIENT_ID = "ttstd-dialer-android";
|
||||
public static final String MQTT_USERNAME = "";
|
||||
public static final String MQTT_PASSWORD = "";
|
||||
/*订阅主题*/
|
||||
public static final String MQTT_TOPIC_COMMAND = "ttstd/device/command";
|
||||
/*发布主题*/
|
||||
public static final String MQTT_TOPIC_STATUS = "ttstd/device/status";
|
||||
|
||||
public static final String CONTACT_HOME_PAGE = "contact_home_page_key";
|
||||
|
||||
|
||||
@@ -37,6 +37,16 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
public class MqttManager {
|
||||
private static final String TAG = "MqttManager";
|
||||
|
||||
/*MQTT 配置*/
|
||||
public static final String MQTT_URL = "mqtt://175.178.213.60:1883";
|
||||
public static final String MQTT_CLIENT_ID = "ttstd-dialer-android";
|
||||
public static final String MQTT_USERNAME = "";
|
||||
public static final String MQTT_PASSWORD = "";
|
||||
/*订阅主题*/
|
||||
public static final String MQTT_TOPIC_COMMAND = "ttstd/device/command";
|
||||
/*发布主题*/
|
||||
public static final String MQTT_TOPIC_STATUS = "ttstd/device/status";
|
||||
|
||||
/**
|
||||
* 收到 MQTT 消息的事件 key,消息体为 {@link MqttMessage}
|
||||
*/
|
||||
@@ -52,6 +62,16 @@ public class MqttManager {
|
||||
*/
|
||||
private static final long HEARTBEAT_INTERVAL_MS = 30_000L;
|
||||
|
||||
/**
|
||||
* 重连退避初始间隔(毫秒)
|
||||
*/
|
||||
private static final long RECONNECT_DELAY_INITIAL_MS = 1_000L;
|
||||
|
||||
/**
|
||||
* 重连退避间隔上限(毫秒),超过此值不再翻倍
|
||||
*/
|
||||
private static final long RECONNECT_DELAY_MAX_MS = 64_000L;
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static volatile MqttManager INSTANCE;
|
||||
|
||||
@@ -77,7 +97,7 @@ public class MqttManager {
|
||||
/**
|
||||
* 重连退避间隔(毫秒),随失败次数指数增长
|
||||
*/
|
||||
private long mReconnectDelayMs = 1_000L;
|
||||
private long mReconnectDelayMs = RECONNECT_DELAY_INITIAL_MS;
|
||||
|
||||
private boolean mHeartbeatRunning;
|
||||
|
||||
@@ -136,19 +156,19 @@ public class MqttManager {
|
||||
mMqttClient = null;
|
||||
}
|
||||
|
||||
URI uri = URI.create(CommonConfig.MQTT_URL);
|
||||
URI uri = URI.create(MQTT_URL);
|
||||
String host = uri.getHost();
|
||||
int port = uri.getPort() > 0 ? uri.getPort() : 1883;
|
||||
|
||||
Mqtt5ClientBuilder builder = Mqtt5Client.builder()
|
||||
.identifier(CommonConfig.MQTT_CLIENT_ID)
|
||||
.identifier(MQTT_CLIENT_ID)
|
||||
.serverHost(host)
|
||||
.serverPort(port)
|
||||
.addDisconnectedListener(context -> onDisconnected());
|
||||
if (!TextUtils.isEmpty(CommonConfig.MQTT_USERNAME)) {
|
||||
if (!TextUtils.isEmpty(MQTT_USERNAME)) {
|
||||
builder.simpleAuth()
|
||||
.username(CommonConfig.MQTT_USERNAME)
|
||||
.password(CommonConfig.MQTT_PASSWORD.getBytes(StandardCharsets.UTF_8))
|
||||
.username(MQTT_USERNAME)
|
||||
.password(MQTT_PASSWORD.getBytes(StandardCharsets.UTF_8))
|
||||
.applySimpleAuth();
|
||||
}
|
||||
|
||||
@@ -167,7 +187,7 @@ public class MqttManager {
|
||||
scheduleReconnect();
|
||||
} else {
|
||||
Logger.e(TAG, "connect: 连接成功");
|
||||
mReconnectDelayMs = 1_000L;
|
||||
mReconnectDelayMs = RECONNECT_DELAY_INITIAL_MS;
|
||||
setConnected(true);
|
||||
// 连接成功后重新订阅之前订阅的主题
|
||||
reSubscribeAll();
|
||||
@@ -205,7 +225,7 @@ public class MqttManager {
|
||||
Logger.e(TAG, "scheduleReconnect: " + (mReconnectDelayMs / 1000) + "s 后重连");
|
||||
mReconnectHandler.postDelayed(mReconnectRunnable, mReconnectDelayMs);
|
||||
// 下次退避间隔翻倍,封顶 30s
|
||||
mReconnectDelayMs = Math.min(mReconnectDelayMs * 2, 30_000L);
|
||||
mReconnectDelayMs = Math.min(mReconnectDelayMs * 2, RECONNECT_DELAY_MAX_MS);
|
||||
}
|
||||
|
||||
private final Runnable mReconnectRunnable = new Runnable() {
|
||||
@@ -321,10 +341,10 @@ public class MqttManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报设备状态,默认发布到 {@link CommonConfig#MQTT_TOPIC_STATUS}
|
||||
* 上报设备状态,默认发布到 {@link #MQTT_TOPIC_STATUS}
|
||||
*/
|
||||
public void publishStatus(String payload) {
|
||||
publish(CommonConfig.MQTT_TOPIC_STATUS, payload);
|
||||
publish(MQTT_TOPIC_STATUS, payload);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -356,7 +376,7 @@ public class MqttManager {
|
||||
Logger.e(TAG, "reportHeartbeat: 设备序列号为空,跳过心跳");
|
||||
return;
|
||||
}
|
||||
publish(CommonConfig.MQTT_TOPIC_STATUS, buildStatusPayload(sn, SystemUtils.isScreenOn(mContext)));
|
||||
publish(MQTT_TOPIC_STATUS, buildStatusPayload(sn, SystemUtils.isScreenOn(mContext)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -371,7 +391,7 @@ public class MqttManager {
|
||||
Logger.e(TAG, "reportScreenState: 设备序列号为空,跳过上报");
|
||||
return;
|
||||
}
|
||||
publish(CommonConfig.MQTT_TOPIC_STATUS, buildStatusPayload(sn, screenOn));
|
||||
publish(MQTT_TOPIC_STATUS, buildStatusPayload(sn, screenOn));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user