makcar/app/src/main/java/com/aros/apron/manager/PayloadWidgetManager.java

710 lines
35 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.aros.apron.manager;
import androidx.annotation.NonNull;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import com.aros.apron.base.BaseManager;
import com.aros.apron.constant.AMSConfig;
import com.aros.apron.entity.Movement;
import com.aros.apron.entity.PayloadInfo;
import com.aros.apron.entity.PsdkWidgetValuesReport;
import com.aros.apron.tools.LogUtil;
import com.aros.apron.tools.MqttManager;
import com.aros.apron.xclog.XcFileLog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import dji.sdk.keyvalue.key.FlightControllerKey;
import dji.sdk.keyvalue.key.KeyTools;
import dji.v5.common.callback.CommonCallbacks;
import dji.v5.common.error.IDJIError;
import dji.v5.manager.KeyManager;
import dji.v5.manager.aircraft.payload.PayloadCenter;
import dji.v5.manager.aircraft.payload.PayloadIndexType;
import dji.v5.manager.aircraft.payload.data.PayloadBasicInfo;
import dji.v5.manager.aircraft.payload.data.PayloadWidgetInfo;
import dji.v5.manager.aircraft.payload.listener.PayloadBasicInfoListener;
import dji.v5.manager.aircraft.payload.listener.PayloadDataListener;
import dji.v5.manager.aircraft.payload.listener.PayloadWidgetInfoListener;
import dji.v5.manager.interfaces.IPayloadManager;
public class PayloadWidgetManager extends BaseManager {
private String TAG = this.getClass().getSimpleName();
private List<PayloadInfo> payloadInfos = new ArrayList<>();
private PayloadBasicInfo cachedBasicInfo;
private PayloadWidgetInfo cachedWidgetInfo;
private Handler psdkReportHandler;
private Runnable psdkReportRunnable;
private static final long PSDK_REPORT_INTERVAL = 5000; // 5秒
private StringBuffer payloadBuffer = new StringBuffer();
private volatile boolean hasSpeaker = false;
private volatile boolean speakerPortSet = false;
private PayloadWidgetManager() {
}
private static class PayloadWidgetHolder {
private static final PayloadWidgetManager INSTANCE = new PayloadWidgetManager();
}
public static PayloadWidgetManager getInstance() {
return PayloadWidgetHolder.INSTANCE;
}
// 使用GsonBuilder配置Gson实例以允许序列化特殊浮点数值
Gson gson = new GsonBuilder()
.serializeSpecialFloatingPointValues() // 这是关键
.create();
public void initPayloadInfo() {
Boolean isConnect = KeyManager.getInstance().getValue(KeyTools.createKey(FlightControllerKey.KeyConnection));
if (isConnect != null && isConnect) {
//负载信息
Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
if (payloadManager != null) {
IPayloadManager iPayloadManager = payloadManager.get(PayloadIndexType.PORT_2);
if (iPayloadManager != null) {
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_2).pullWidgetInfoFromPayload(new CommonCallbacks.CompletionCallback() {
@Override
public void onSuccess() {
//基础信息
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_2).addPayloadBasicInfoListener(new PayloadBasicInfoListener() {
@Override
public void onPayloadBasicInfoUpdate(PayloadBasicInfo info) {
//LogUtil.log(TAG, "左侧负载基础信息:" + info.toString());
// XcFileLog.getInstace().w(TAG, "左侧负载基础信息:" + info.toString());
//cachedBasicInfo = info;
//startPsdkWidgetReport();
}
});
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_2).addPayloadDataListener(new PayloadDataListener() {
@Override
public void onDataFromPayloadUpdate(byte[] data) {
String cleanData = new String(data, java.nio.charset.StandardCharsets.UTF_8)
.replaceAll("[^\\x20-\\x7E\\u4e00-\\u9fa5]", "");
// LogUtil.log(TAG, "左侧负载数据信息:" + cleanData);
// XcFileLog.getInstace().w(TAG, "左侧负载数据信息:" + cleanData);
if (cleanData.contains("jwD")) {
payloadBuffer.setLength(0);
payloadBuffer.append(cleanData);
// LogUtil.log(TAG, "负载数据开始,清空缓存");
}
if (cleanData.contains("}")) {
if (payloadBuffer.length() > 0) {
payloadBuffer.append(cleanData);
String fullJson = payloadBuffer.toString();
payloadBuffer.setLength(0); // 清空,准备下次
// LogUtil.log(TAG, "负载完整数据: " + fullJson);
sendFloatingWindowText(fullJson);
} else {
//LogUtil.log(TAG, "负载数据: 缓存为空,忽略孤立}");
}
}
}
});
//可以把负载设备控件打印
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_2).addPayloadWidgetInfoListener(new PayloadWidgetInfoListener() {
@Override
public void onPayloadWidgetInfoUpdate(PayloadWidgetInfo info) {
//LogUtil.log(TAG, "左侧负载控件信息:" + info.toString());
//XcFileLog.getInstace().w(TAG, "左侧负载控件信息:" + info.toString());
//cachedWidgetInfo = info;
//如果负载为空
detectSpeaker(info, 2);
if (info.getConfigInterfaceWidgetList() == null || info.getMainInterfaceWidgetList() == null) {
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_2).pullWidgetInfoFromPayload(new CommonCallbacks.CompletionCallback() {
@Override
public void onSuccess() {
LogUtil.log(TAG, "负载重复拉取成功");
}
@Override
public void onFailure(@NonNull IDJIError idjiError) {
LogUtil.log(TAG, "负载重复拉取失败");
}
});
}
}
});
}
@Override
public void onFailure(@NonNull IDJIError idjiError) {
LogUtil.log(TAG, "负载第一次拉取失败");
}
});
} else {
LogUtil.log(TAG, "监听LEFT_OR_MAIN PSDK数据失败:设备未连接");
}
IPayloadManager iPayloadManager3 = payloadManager.get(PayloadIndexType.PORT_3);
if (iPayloadManager3 != null) {
//基础信息
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_3).addPayloadBasicInfoListener(new PayloadBasicInfoListener() {
@Override
public void onPayloadBasicInfoUpdate(PayloadBasicInfo info) {
// LogUtil.log(TAG, "3号负载基础信息:" + info.toString());
}
});
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_3).addPayloadDataListener(new PayloadDataListener() {
@Override
public void onDataFromPayloadUpdate(byte[] data) {
// LogUtil.log(TAG, "3号Data负载基础信息:" + data);
}
});
//可以把负载设备控件打印
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_3).addPayloadWidgetInfoListener(new PayloadWidgetInfoListener() {
@Override
public void onPayloadWidgetInfoUpdate(PayloadWidgetInfo info) {
// LogUtil.log(TAG, "3号负载控件信息:" + info.toString());
detectSpeaker(info, 3);
if (info.getConfigInterfaceWidgetList() == null || info.getMainInterfaceWidgetList() == null) {
PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_3).pullWidgetInfoFromPayload(new CommonCallbacks.CompletionCallback() {
@Override
public void onSuccess() {
// LogUtil.log(TAG, "负载重复拉取成功");
}
@Override
public void onFailure(@NonNull IDJIError idjiError) {
//LogUtil.log(TAG, "负载重复拉取失败");
}
});
}
}
});
} else {
LogUtil.log(TAG, "监听right_OR_MAIN PSDK数据失败:设备未连接");
}
} else {
LogUtil.log(TAG, "监听psdk数据失败:未检测到设备");
}
} else {
LogUtil.log(TAG, "设备未连接");
}
}
// //设置三方负载控件
// public void setWidget(MQMessage message) {
// Boolean isConnect = KeyManager.getInstance().getValue(KeyTools.createKey(FlightControllerKey.KeyConnection));
// if (isConnect != null && isConnect) {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(message.getWidgetValue());
// widgetValue.setIndex(message.getWidgetIndex());
// widgetValue.setType(WidgetType.find(message.getWidgetType()));
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// LogUtil.log(TAG,"设置widget成功");
// sendMsg2Server(message);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError error) {
// LogUtil.log(TAG,"设置widget失败:"+getIDJIErrorMsg(error));
// }
// });
// }
//
// }
//
// //锁定
// public void lock(MQMessage message) {
// Boolean isConnect = KeyManager.getInstance().getValue(KeyTools.createKey(FlightControllerKey.KeyConnection));
// if (isConnect != null && isConnect) {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(0);
// widgetValue.setIndex(0);
// widgetValue.setType(WidgetType.SWITCH);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// sendMsg2Server(message);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError error) {
// LogUtil.log(TAG,"解锁失败:" +new Gson().toJson(error));
// sendMsg2Server(message, "解锁失败:" + getIDJIErrorMsg(error));
// }
// });
// }
//
// }
//
// //解锁
// public void unlock(MQMessage message) {
// Boolean isConnect = KeyManager.getInstance().getValue(KeyTools.createKey(FlightControllerKey.KeyConnection));
// if (isConnect != null && isConnect) {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(1);
// widgetValue.setIndex(0);
// widgetValue.setType(WidgetType.SWITCH);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// sendMsg2Server(message);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"解锁失败:" +new Gson().toJson(idjiError));
// sendMsg2Server(message, "解锁失败:" + getIDJIErrorMsg(idjiError));
//
// }
// });
// }
//
// }
//
// //抛投
// public void throwOne(MQMessage message) {
// Boolean isConnect = KeyManager.getInstance().getValue(KeyTools.createKey(FlightControllerKey.KeyConnection));
// if (isConnect != null && isConnect) {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(0);
// widgetValue.setIndex(0);
// widgetValue.setType(WidgetType.SWITCH);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(1);
// widgetValue.setIndex(0);
// widgetValue.setType(WidgetType.SWITCH);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(1);
// widgetValue.setIndex(1);
// widgetValue.setType(WidgetType.BUTTON);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// sendMsg2Server(message);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"抛投失败:" +new Gson().toJson(idjiError));
// sendMsg2Server(message, "抛投失败:" + getIDJIErrorMsg(idjiError));
//
// }
// });
// }
// }, 500);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"解锁失败:" +new Gson().toJson(idjiError));
// sendMsg2Server(message, "解锁失败:" + getIDJIErrorMsg(idjiError));
//
// }
// });
// }
// }, 500);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"锁定失败:" +new Gson().toJson(idjiError));
// sendMsg2Server(message, "锁定失败:" + getIDJIErrorMsg(idjiError));
// }
// });
//
// }
//
// }
//
// //一件抛投
// public void throwAll(MQMessage message) {
// Boolean isConnect = KeyManager.getInstance().getValue(KeyTools.createKey(FlightControllerKey.KeyConnection));
// if (isConnect != null && isConnect) {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(0);
// widgetValue.setIndex(0);
// widgetValue.setType(WidgetType.SWITCH);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(1);
// widgetValue.setIndex(0);
// widgetValue.setType(WidgetType.SWITCH);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// Map<PayloadIndexType, IPayloadManager> payloadManagerMap = payloadManager;
// WidgetValue widgetValue = new WidgetValue();
// widgetValue.setValue(1);
// widgetValue.setIndex(2);
// widgetValue.setType(WidgetType.BUTTON);
// payloadManagerMap.get(PayloadIndexType.PORT_2).setWidgetValue(widgetValue, new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// sendMsg2Server(message);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"全抛失败:" +new Gson().toJson(idjiError));
// sendMsg2Server(message, "全抛失败:" + getIDJIErrorMsg(idjiError));
// }
// });
// }
// }, 500);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"解锁失败:" +new Gson().toJson(idjiError));
// sendMsg2Server(message, "解锁失败:" + getIDJIErrorMsg(idjiError));
// }
// });
// }
// }, 500);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"锁定失败:" +new Gson().toJson(idjiError));
// sendMsg2Server(message, "锁定失败:" + getIDJIErrorMsg(idjiError));
// }
// });
//
// }
// }
//
// public void sendMsgToPayload(MQMessage message) {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// if (payloadManager != null) {
// IPayloadManager iPayloadManager = payloadManager.get(PayloadIndexType.EXTERNAL);
// if (iPayloadManager != null) {
// if (TextUtils.isEmpty(message.getPayloadData())) {
// sendMsg2Server( message, "发送数据到psdk失败:参数有误");
// return;
// }
// iPayloadManager.sendDataToPayload(Utils.getByte(message.getPayloadData()), new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// LogUtil.log(TAG, "发送数据到psdk:" + Utils.getByte(message.getPayloadData()));
// sendMsg2Server( message);
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"发送数据到psdk失败:" +new Gson().toJson(idjiError));
// sendMsg2Server( message, "发送数据到psdk失败:" + getIDJIErrorMsg(idjiError));
// }
// });
// } else {
// sendMsg2Server( message, "发送数据到psdk失败:设备未连接");
// }
// } else {
// sendMsg2Server( message, "发送数据到psdk失败:未检测到设备");
// }
// }
//
// public void sendMsgToLeftPayload(String txt) {
// Map<PayloadIndexType, IPayloadManager> payloadManager = PayloadCenter.getInstance().getPayloadManager();
// if (payloadManager != null) {
// IPayloadManager iPayloadManager = payloadManager.get(PayloadIndexType.LEFT_OR_MAIN);
// if (iPayloadManager != null) {
//
// iPayloadManager.sendDataToPayload(txt.getBytes(), new CommonCallbacks.CompletionCallback() {
// @Override
// public void onSuccess() {
// LogUtil.log(TAG,"send"+txt+"to psdk success");
// }
//
// @Override
// public void onFailure(@NonNull IDJIError idjiError) {
// LogUtil.log(TAG,"send"+txt+"to psdk fail:"+new Gson().toJson(idjiError));
// }
// });
// } else {
// LogUtil.log(TAG,"发送数据到psdk失败:设备未连接");
// }
// } else {
// LogUtil.log(TAG,"发送数据到psdk失败:未检测到设备");
// }
// }
//
// //推送MSDK收到的PSDK数据
// public void sendMsgFromPSDK2Server(MqttAndroidClient client,byte[] data) {
// try {
// if (client.isConnected()) {
// MqttMessage mqttMessage = null;
// MessageReply message = new MessageReply();
// message.setMsg_type(60119);
// message.setResult(1);
// message.setPayloadData(data);
// mqttMessage = new MqttMessage(new Gson().toJson(message).getBytes("UTF-8"));
// mqttMessage.setQos(2);
// client.publish(AMSConfig.getInstance().getMqttMsdkPushEvent2ServerTopic(), mqttMessage);
//
// } else {
// LogUtil.log(TAG, "psdkData发送失败mqtt 未连接");
// }
// } catch (Exception e) {
// LogUtil.log(TAG, "psdkData发送异常mqtt 未连接");
// e.printStackTrace();
// }
// }
/**
* 检测负载控件信息中是否有喊话器,检测到后自动设置喊话器位置
* @param info 负载控件信息
* @param portIndex 端口编号 2或3
*/
private void detectSpeaker(PayloadWidgetInfo info, int portIndex) {
if (info == null || speakerPortSet) return;
try {
Object speakerWidget = info.getSpeakerWidget();
if (speakerWidget != null) {
String infoStr = info.toString();
// speakerWidget=SpeakerWidget{isTTSEnabled=true, isVoiceEnabled=true} 代表有喊话器
if (infoStr.contains("SpeakerWidget") && infoStr.contains("isTTSEnabled=true")) {
hasSpeaker = true;
LogUtil.log(TAG, "检测到喊话器负载(SpeakerWidget)在 PORT_" + portIndex);
// 自动设置喊话器位置,设置成功后不再重复设置
dji.v5.manager.aircraft.megaphone.MegaphoneIndex targetIndex =
portIndex == 2 ? dji.v5.manager.aircraft.megaphone.MegaphoneIndex.PORT_2
: dji.v5.manager.aircraft.megaphone.MegaphoneIndex.PORT_3;
LogUtil.log(TAG, "自动设置喊话器位置到: " + targetIndex);
dji.v5.manager.aircraft.megaphone.MegaphoneManager.getInstance().setMegaphoneIndex(targetIndex,
new dji.v5.common.callback.CommonCallbacks.CompletionCallback() {
@Override
public void onSuccess() {
speakerPortSet = true;
LogUtil.log(TAG, "喊话器位置设置成功(从负载控件自动设置): " + targetIndex + ",不再重复设置");
// 同步到 SpeakerManager
com.aros.apron.manager.SpeakerManager.psdkindex = portIndex;
com.aros.apron.manager.SpeakerManager.getInstance().getMegaphoneIndex();
}
@Override
public void onFailure(@NonNull dji.v5.common.error.IDJIError error) {
LogUtil.log(TAG, "喊话器位置设置失败(从负载控件自动设置): " + error);
}
});
}
}
} catch (Exception e) {
LogUtil.log(TAG, "检测喊话器异常: " + e.getMessage());
}
}
public boolean hasSpeaker() {
return hasSpeaker;
}
/**
* 检查列表中是否已经存在指定payloadIndexType的PayloadInfo对象。
*/
private boolean isPayloadIndexTypeExists(List<PayloadInfo> list, String payloadIndexType) {
for (PayloadInfo payloadInfo : list) {
if (payloadInfo.getPayloadIndexType().equals(payloadIndexType)) {
return true;
}
}
return false;
}
/**
* 启动5秒定时上报
*/
private void startPsdkWidgetReport() {
if (psdkReportHandler == null) {
psdkReportHandler = new Handler(Looper.getMainLooper());
}
if (psdkReportRunnable != null) {
return; // 已经在运行
}
psdkReportRunnable = new Runnable() {
@Override
public void run() {
psdkReportHandler.postDelayed(this, PSDK_REPORT_INTERVAL);
}
};
psdkReportHandler.post(psdkReportRunnable);
LogUtil.log(TAG, "启动PSDK负载控件5秒定时上报");
}
/**
* 停止定时上报
*/
private void stopPsdkWidgetReport() {
if (psdkReportHandler != null && psdkReportRunnable != null) {
psdkReportHandler.removeCallbacks(psdkReportRunnable);
psdkReportRunnable = null;
LogUtil.log(TAG, "停止PSDK负载控件定时上报");
}
}
/**
* 发送PSDK负载控件值到MQTT
*/
// private void sendPsdkWidgetValues() {
// try {
// if (MqttManager.getInstance().mqttAndroidClient == null || !MqttManager.getInstance().mqttAndroidClient.isConnected()) {
// return;
// }
// if (cachedBasicInfo == null && cachedWidgetInfo == null) {
// return;
// }
//
// PsdkWidgetValuesReport report = new PsdkWidgetValuesReport();
// report.setTid(UUID.randomUUID().toString());
// report.setBid(UUID.randomUUID().toString());
// report.setTimestamp(System.currentTimeMillis());
// report.setMethod("psdk_widget_values");
// report.setGateway(Movement.getInstance().getSerialNumber());
//
// PsdkWidgetValuesReport.Data data = new PsdkWidgetValuesReport.Data();
// List<PsdkWidgetValuesReport.PsdkWidgetValue> widgetList = new ArrayList<>();
//
// // 从cachedBasicInfo和cachedWidgetInfo组装数据
// if (cachedBasicInfo != null) {
// PsdkWidgetValuesReport.PsdkWidgetValue w = new PsdkWidgetValuesReport.PsdkWidgetValue();
// w.setPsdk_index(2);
// w.setPsdk_lib_version(cachedBasicInfo.getVersion() != null ? cachedBasicInfo.getVersion() : "");
// w.setPsdk_name(cachedBasicInfo.getProductName() != null ? cachedBasicInfo.getProductName() : "");
// w.setPsdk_sn(cachedBasicInfo.getDeviceSN() != null ? cachedBasicInfo.getDeviceSN() : "");
// w.setPsdk_type(5); // Speaker type
// w.setPsdk_version(cachedBasicInfo.getVersion() != null ? cachedBasicInfo.getVersion() : "");
//
// // Speaker数据
// PsdkWidgetValuesReport.SpeakerData speaker = new PsdkWidgetValuesReport.SpeakerData();
// speaker.setPlay_file_md5("");
// speaker.setPlay_file_name("");
// speaker.setPlay_mode(1);
// speaker.setPlay_volume(30);
// speaker.setSystem_state(0);
// speaker.setWork_mode(1);
//
// // 从widgetInfo中尝试获取实际的widget值
// if (cachedWidgetInfo != null) {
// if (cachedWidgetInfo.getMainInterfaceWidgetList() != null) {
// for (Object widget : cachedWidgetInfo.getMainInterfaceWidgetList()) {
// // 根据widget类型设置speaker字段
// }
// }
// }
// w.setSpeaker(speaker);
// w.setValues(new ArrayList<>());
// widgetList.add(w);
// }
//
// data.setPsdk_widget_values(widgetList);
// report.setData(data);
//
// String json = gson.toJson(report);
// LogUtil.log(TAG, "上报psdk_widget_values: " + json);
//
// MqttMessage msg = new MqttMessage(json.getBytes("UTF-8"));
// msg.setQos(0);
// MqttManager.getInstance().mqttAndroidClient.publish(AMSConfig.UP_UAV_EVENT, msg);
// } catch (Exception e) {
// LogUtil.log(TAG, "发送psdk_widget_values异常: " + e.getMessage());
// e.printStackTrace();
// }
// }
/**
* 发送负载浮窗文本到MQTT
*/
private void sendFloatingWindowText(String value) {
try {
if (MqttManager.getInstance().mqttAndroidClient == null || !MqttManager.getInstance().mqttAndroidClient.isConnected()) {
LogUtil.log(TAG, "MQTT未连接跳过浮窗文本上报");
return;
}
java.util.Map<String, Object> report = new java.util.LinkedHashMap<>();
report.put("bid", UUID.randomUUID().toString());
report.put("tid", UUID.randomUUID().toString());
report.put("timestamp", System.currentTimeMillis());
report.put("method", "psdk_floating_window_text_jdw");
report.put("gateway", Movement.getInstance().getSerialNumber());
java.util.Map<String, Object> data = new java.util.LinkedHashMap<>();
data.put("psdk_index", 2);
data.put("value", value);
report.put("data", data);
String json = new Gson().toJson(report);
LogUtil.log(TAG, "发送浮窗文本: " + json);
org.eclipse.paho.client.mqttv3.MqttMessage msg = new org.eclipse.paho.client.mqttv3.MqttMessage(json.getBytes("UTF-8"));
msg.setQos(1);
MqttManager.getInstance().mqttAndroidClient.publish(AMSConfig.UP_UAV_EVENT, msg);
} catch (Exception e) {
LogUtil.log(TAG, "发送浮窗文本异常: " + e.getMessage());
e.printStackTrace();
}
}
}