添加MQTT接收

This commit is contained in:
jjLv 2025-09-01 15:23:09 +08:00
parent 6df7e26e28
commit 964760e69d
3 changed files with 64 additions and 9 deletions

View File

@ -74,7 +74,7 @@ public class UpdateBasicData {
int personNum = 0;
try {
JSONObject json = new JSONObject();
json.put(UPDATE_TIME, "");
// json.put(UPDATE_TIME, "");
json.put("type", type);
json.put("userId", userId);
String jsonString = json.toString();
@ -124,7 +124,7 @@ public class UpdateBasicData {
Log.i(TAG, "getFacePhoto: " + time);
personFaceNum = 0;
JSONObject json = new JSONObject();
json.put(UPDATE_TIME, "");
// json.put(UPDATE_TIME, "");
json.put("userId", userId);
String jsonString = json.toString();
Log.i(TAG, "getFacePhoto jsonString: " + jsonString);

View File

@ -1,10 +1,21 @@
package com.bonus.canteen.utils.rabbitmq;
import static com.xuexiang.xutil.XUtil.runOnUiThread;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bonus.canteen.db.AppDatabase;
import com.bonus.canteen.db.beans.base.CustPhotoFulInfo;
import com.bonus.canteen.db.entity.base.UserInfo;
import com.bonus.canteen.face.util.FaceServer;
import com.bonus.canteen.service.data.GetBasicDataService;
import com.bonus.canteen.service.data.impl.GetBasicDataServiceImp;
import com.bonus.canteen.utils.ThreadPoolManager;
import com.bonus.canteen.utils.WorkConfig;
import org.eclipse.paho.android.service.MqttAndroidClient;
@ -68,10 +79,7 @@ public class RabbitMqMqttHelper {
public void messageArrived(String topic, MqttMessage message) {
String payload = new String(message.getPayload());
Log.d(TAG, "Received message: " + payload);
new Handler(Looper.getMainLooper()).postDelayed(() ->
// TODO 在主线程中处理消息
Log.d(TAG, "Processing message on main thread: " + payload)
, 5000);
new Handler(Looper.getMainLooper()).postDelayed(() -> handleIncomingMessage(topic, payload), 5000);
}
@Override
@ -80,7 +88,54 @@ public class RabbitMqMqttHelper {
}
};
}
private void handleIncomingMessage(String topic, String payload) {
JSONObject jsonObject = JSON.parseObject(payload);
if (topic.contains("morning-inspection-device-update-person-config-v4")) {
handlePersonConfigUpdate(jsonObject);
}
}
/**
* 处理人员配置更新包含子操作
*/
private void handlePersonConfigUpdate(JSONObject json) {
checkAndLogUpdate(json, UPDATE_PERSON_PHOTO, "人员信息");
checkAndLogUpdate(json, UPDATE_PERSON_PHOTO, "人员照片");
}
/**
* 通用更新检查方法
*/
private void checkAndLogUpdate(JSONObject json, String field, String desc) {
try {
if (json.getIntValue(field) > 0 && desc.equals("人员照片")) {
ThreadPoolManager.getExecutor().execute(() -> {
if (json.getString("type").equals("del")) {
int userId = json.getIntValue(field);
Log.d(TAG, "userId: " + userId);
AppDatabase.getDatabase(context).userDao().delete(new UserInfo(Long.parseLong(String.valueOf(userId))));
AppDatabase.getDatabase(context).custPhotoFulDao().delete(new CustPhotoFulInfo(Long.parseLong(String.valueOf(userId))));
} else {
GetBasicDataService service = new GetBasicDataServiceImp();
service.getFacePhoto(json.getString("currentTime"), json.getIntValue(UPDATE_PERSON));
}
runOnUiThread(() -> FaceServer.getInstance().loadFace(context));
});
} else if (json.getIntValue(field) > 0 && desc.equals("人员信息")) {
ThreadPoolManager.getExecutor().execute(() -> {
if (json.getString("type").equals("del")) {
int userId = json.getIntValue(field);
Log.d(TAG, "userId: " + userId);
AppDatabase.getDatabase(context).userDao().delete(new UserInfo(Long.parseLong(String.valueOf(userId))));
AppDatabase.getDatabase(context).custPhotoFulDao().delete(new CustPhotoFulInfo(Long.parseLong(String.valueOf(userId))));
} else {
GetBasicDataService service = new GetBasicDataServiceImp();
service.getPersonInfo(json.getString("currentTime"), "user", json.getIntValue(UPDATE_PERSON));
}
});
}
} catch (Exception e) {
Log.d(TAG, "处理人员配置更新异常");
}
}
private MqttTraceHandler createMqttTraceHandler() {
return new MqttTraceHandler() {
@Override
@ -130,7 +185,7 @@ public class RabbitMqMqttHelper {
return;
}
subscriptionTopics = new String[]{
"device-update-person-config-v4." + tenantId,
"morning-inspection-device-update-person-config-v4." + tenantId,
"device-update-info-v4." + tenantId + "." + sn,
"time-calibration-v4." + tenantId + "." + sn };
mqttAndroidClient.subscribe(subscriptionTopics, new int[]{1, 1, 1}, null, new IMqttActionListener() {

View File

@ -73,7 +73,7 @@
android:drawableStart="@drawable/ic_password"
android:paddingStart="0dp"
android:hint="请输入密码"
android:text="Bonus$2031"
android:text="Bonus$2027"
android:inputType="textPassword"
tools:ignore="RtlSymmetry" />
</LinearLayout>