package com.aros.apron.manager; import androidx.annotation.NonNull; import com.aros.apron.base.BaseManager; import com.aros.apron.entity.PayloadInfo; import com.aros.apron.tools.LogUtil; 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 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 payloadInfos = new ArrayList<>(); 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 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()); } }); PayloadCenter.getInstance().getPayloadManager().get(PayloadIndexType.PORT_2).addPayloadDataListener(new PayloadDataListener() { @Override public void onDataFromPayloadUpdate(byte[] data) { LogUtil.log(TAG, "左侧负载数据信息:" +new String(data, java.nio.charset.StandardCharsets.UTF_8).replaceAll("[^\\x20-\\x7E\\u4e00-\\u9fa5]", "")); XcFileLog.getInstace().w(TAG, "左侧负载数据信息:" +new String(data, java.nio.charset.StandardCharsets.UTF_8).replaceAll("[^\\x20-\\x7E\\u4e00-\\u9fa5]", "")); } }); //可以把负载设备控件打印 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()); //如果负载为空 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数据失败:设备未连接"); } } 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 payloadManager = PayloadCenter.getInstance().getPayloadManager(); // Map 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 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 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(); // } // } /** * 检查列表中是否已经存在指定payloadIndexType的PayloadInfo对象。 */ private boolean isPayloadIndexTypeExists(List list, String payloadIndexType) { for (PayloadInfo payloadInfo : list) { if (payloadInfo.getPayloadIndexType().equals(payloadIndexType)) { return true; } } return false; } }