From 43fcb334298dcf4af7b129e6328fa9d8a7055f96 Mon Sep 17 00:00:00 2001 From: zmt Date: Mon, 24 Jan 2022 16:38:41 +0800 Subject: [PATCH] mqtt connect success& offline too much --- app/src/main/assets/app-config.json | 17 + zmt_module/build.gradle | 1 + .../java/io/dcloud/zmt_module/Zmt_AppProxy.java | 164 ++++++++- .../io/dcloud/zmt_module/mqtt/MQTTRequest.java | 49 +++ .../java/io/dcloud/zmt_module/mqtt/MQTTSample.java | 402 +++++++++++++++++++++ .../main/java/io/dcloud/zmt_module/zmtClass.java | 3 + 6 files changed, 634 insertions(+), 2 deletions(-) create mode 100644 app/src/main/assets/app-config.json create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTRequest.java create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTSample.java diff --git a/app/src/main/assets/app-config.json b/app/src/main/assets/app-config.json new file mode 100644 index 0000000..5084530 --- /dev/null +++ b/app/src/main/assets/app-config.json @@ -0,0 +1,17 @@ +{ + "PRODUCT_ID": "BE8N7UZ7OF", + "DEVICE_NAME": "pad20220105", + "DEVICE_PSK": "7udrYcfTVThbzdMlLT9fHQ==", + "SUB_PRODUCT_ID": "", + "SUB_DEVICE_PSK": "", + "SUB_DEV_NAME": "", + "TEST_TOPIC": "", + "SHADOW_TEST_TOPIC": "", + "PRODUCT_KEY": "", + "DOOR_PRODUCT_ID": "", + "DOOR_DEVICE_NAME": "", + "DOOR_DEVICE_PSK": "", + "AIRCONDITIONER_PRODUCT_ID": "", + "AIRCONDITIONER_DEVICE_NAME": "", + "AIRCONDITIONER_DEVICE_PSK": "" +} \ No newline at end of file diff --git a/zmt_module/build.gradle b/zmt_module/build.gradle index 82cbb64..01b40a1 100644 --- a/zmt_module/build.gradle +++ b/zmt_module/build.gradle @@ -57,5 +57,6 @@ dependencies { compileOnly 'com.alibaba:fastjson:1.1.46.android' + implementation 'com.tencent.iot.hub:hub-device-android:3.3.5' } \ No newline at end of file diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java b/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java index 96ba4d1..2fb4654 100644 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/Zmt_AppProxy.java @@ -1,23 +1,97 @@ package io.dcloud.zmt_module; import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; import android.util.Log; +import com.tencent.iot.hub.device.android.core.gateway.TXGatewayConnection; +import com.tencent.iot.hub.device.android.core.util.TXLog; +import com.tencent.iot.hub.device.java.core.common.Status; +import com.tencent.iot.hub.device.java.core.mqtt.TXMqttActionCallBack; + +import org.eclipse.paho.client.mqttv3.IMqttToken; +import org.eclipse.paho.client.mqttv3.MqttMessage; + +import java.util.Arrays; + import io.dcloud.feature.uniapp.UniAppHookProxy; +import io.dcloud.zmt_module.mqtt.MQTTRequest; +import io.dcloud.zmt_module.mqtt.MQTTSample; public class Zmt_AppProxy implements UniAppHookProxy { String TAG = "zmt proxy"; + private MQTTSample mMQTTSample; + + + + // Default testing parameters + private String mBrokerURL = null; //传入null,即使用腾讯云物联网通信默认地址 "${ProductId}.iotcloud.tencentdevices.com:8883" https://cloud.tencent.com/document/product/634/32546 + private String mProductID = ""; + private String mDevName = ""; + private String mDevPSK = ""; + private String mSubProductID = ""; // If you wont test gateway, let this to be null + private String mSubDevName = ""; + private String mSubDevPsk = ""; + private String mTestTopic = ""; // productID/DeviceName/TopicName +// private String mDevCertName = "YOUR_DEVICE_NAME_cert.crt"; +// private String mDevKeyName = "YOUR_DEVICE_NAME_private.key"; +// private String mProductKey = BuildConfig.PRODUCT_KEY; // Used for dynamic register + private String mDevCert = ""; // Cert String + private String mDevPriv = ""; // Priv String + + private final static String BROKER_URL = "broker_url"; + private final static String PRODUCT_ID = "product_id"; + private final static String DEVICE_NAME = "dev_name"; + private final static String DEVICE_PSK = "dev_psk"; + private final static String SUB_PRODUCID = "sub_prodid"; + private final static String SUB_DEVNAME = "sub_devname"; + private final static String TEST_TOPIC = "test_topic"; + + private final static String DEVICE_CERT = "dev_cert"; + private final static String DEVICE_PRIV = "dev_priv"; + private final static String PRODUCT_KEY = "product_key"; + private final static String SUB_DEVICE_PSK = "sub_dev_psk"; + + private volatile boolean mIsConnected; + @Override public void onCreate(Application application) { + + Log.e(TAG, "testAsyncFunc--zmt:onCreate"); //当前uni应用进程回调 仅触发一次 多进程不会触发 //可通过UniSDKEngine注册UniModule或者UniComponent + Context mContext = application.getBaseContext(); + + Object mBrokerURL; +// mMqttConnection = new TXGatewayConnection(mContext, mBrokerURL, mProductID, mDevName, mDevPSK,null,null ,mMqttLogFlag, mMqttLogCallBack, mMqttActionCallBack); +// mMqttConnection.connect(options, mqttRequest); - MyThread mt = new MyThread(); - mt.run(); +// SharedPreferences settings = mParent.getSharedPreferences("config", Context.MODE_PRIVATE); + mBrokerURL =null; + mProductID = "BE8N7UZ7OF"; + mDevName = "pad20220105"; + mDevPSK = "7udrYcfTVThbzdMlLT9fHQ=="; + + mSubProductID = ""; + mSubDevName = ""; + mSubDevPsk =""; + mTestTopic = "BE8N7UZ7OF/pad20220105/data"; + mDevCert = ""; + mDevPriv = ""; + + mMQTTSample = new MQTTSample(mContext, new SelfMqttActionCallBack(), null, mProductID, mDevName, mDevPSK, + mDevCert, mDevPriv, mSubProductID, mSubDevName, mTestTopic, null, null, true, null); + mMQTTSample.setSubDevPsk(mSubDevPsk); + mMQTTSample.connect(); + + + // MyThread mt = new MyThread(); +// mt.run(); } @Override @@ -26,4 +100,90 @@ public class Zmt_AppProxy implements UniAppHookProxy { //其他子进程初始化回调 可用于初始化需要子进程初始化需要的逻辑 } + /** + * 实现TXMqttActionCallBack回调接口 + */ + private class SelfMqttActionCallBack extends TXMqttActionCallBack { + + @Override + public void onConnectCompleted(Status status, boolean reconnect, Object userContext, String msg, Throwable cause) { + String userContextInfo = ""; + if (userContext instanceof MQTTRequest) { + userContextInfo = userContext.toString(); + } + String logInfo = String.format("我在uni里链接成功了!onConnectCompleted, status[%s], reconnect[%b], userContext[%s], msg[%s]", + status.name(), reconnect, userContextInfo, msg); +// mParent.printLogInfo(TAG, logInfo, mLogInfoText, TXLog.LEVEL_INFO); + + System.out.println(TAG+logInfo); + mIsConnected = true; + } + + @Override + public void onConnectionLost(Throwable cause) { + String logInfo = String.format("onConnectionLost, cause[%s]", cause.toString()); +// mParent.printLogInfo(TAG, logInfo, mLogInfoText, TXLog.LEVEL_INFO); + System.out.println(TAG+logInfo); + } + + @Override + public void onDisconnectCompleted(Status status, Object userContext, String msg, Throwable cause) { + String userContextInfo = ""; + if (userContext instanceof MQTTRequest) { + userContextInfo = userContext.toString(); + } + String logInfo = String.format("onDisconnectCompleted, status[%s], userContext[%s], msg[%s]", status.name(), userContextInfo, msg); +// mParent.printLogInfo(TAG, logInfo, mLogInfoText, TXLog.LEVEL_INFO); + System.out.println(TAG+logInfo); + mIsConnected = false; + } + + @Override + public void onPublishCompleted(Status status, IMqttToken token, Object userContext, String errMsg, Throwable cause) { + String userContextInfo = ""; + if (userContext instanceof MQTTRequest) { + userContextInfo = userContext.toString(); + } + String logInfo = String.format("onPublishCompleted, status[%s], topics[%s], userContext[%s], errMsg[%s]", + status.name(), Arrays.toString(token.getTopics()), userContextInfo, errMsg); +// mParent.printLogInfo(TAG, logInfo, mLogInfoText); + System.out.println(TAG+logInfo); + } + + @Override + public void onSubscribeCompleted(Status status, IMqttToken asyncActionToken, Object userContext, String errMsg, Throwable cause) { + String userContextInfo = ""; + if (userContext instanceof MQTTRequest) { + userContextInfo = userContext.toString(); + } + String logInfo = String.format("onSubscribeCompleted, status[%s], topics[%s], userContext[%s], errMsg[%s]", + status.name(), Arrays.toString(asyncActionToken.getTopics()), userContextInfo, errMsg); + if (Status.ERROR == status) { +// mParent.printLogInfo(TAG, logInfo, mLogInfoText, TXLog.LEVEL_ERROR); + System.out.println(TAG+logInfo); + } else { +// mParent.printLogInfo(TAG, logInfo, mLogInfoText); + System.out.println(TAG+logInfo); + } + } + + @Override + public void onUnSubscribeCompleted(Status status, IMqttToken asyncActionToken, Object userContext, String errMsg, Throwable cause) { + String userContextInfo = ""; + if (userContext instanceof MQTTRequest) { + userContextInfo = userContext.toString(); + } + String logInfo = String.format("onUnSubscribeCompleted, status[%s], topics[%s], userContext[%s], errMsg[%s]", + status.name(), Arrays.toString(asyncActionToken.getTopics()), userContextInfo, errMsg); +// mParent.printLogInfo(TAG, logInfo, mLogInfoText); + System.out.println(TAG+logInfo); + } + + @Override + public void onMessageReceived(final String topic, final MqttMessage message) { + String logInfo = String.format("receive command, topic[%s], message[%s]", topic, message.toString()); +// mParent.printLogInfo(TAG, logInfo, mLogInfoText); + System.out.println(TAG+logInfo); + } + } } diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTRequest.java b/zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTRequest.java new file mode 100644 index 0000000..a583cb3 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTRequest.java @@ -0,0 +1,49 @@ +package io.dcloud.zmt_module.mqtt; + + +public class MQTTRequest { + + private static final String TAG = MQTTRequest.class.getSimpleName(); + + /** + * 请求类型 + */ + private String requestType = ""; + + /** + * 请求ID + */ + private int requestId = 0; + + public MQTTRequest() { + } + + public MQTTRequest(String requestType, int requestId) { + this.requestType = requestType; + this.requestId = requestId; + } + + public String getRequestType() { + return requestType; + } + + public void setRequestType(String requestType) { + this.requestType = requestType; + } + + public int getRequestId() { + return requestId; + } + + public void setRequestId(int requestId) { + this.requestId = requestId; + } + + @Override + public String toString() { + return "MQTTRequest{" + + "requestType='" + requestType + '\'' + + ", requestId=" + requestId + + '}'; + } +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTSample.java b/zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTSample.java new file mode 100644 index 0000000..99bc79f --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/mqtt/MQTTSample.java @@ -0,0 +1,402 @@ +package io.dcloud.zmt_module.mqtt; + +import android.content.Context; +import android.os.Environment; +import android.util.Log; + +import com.tencent.iot.hub.device.android.core.gateway.TXGatewayConnection; +import com.tencent.iot.hub.device.android.core.util.AsymcSslUtils; +import com.tencent.iot.hub.device.android.core.util.TXLog; +import com.tencent.iot.hub.device.java.core.log.TXMqttLogCallBack; +import com.tencent.iot.hub.device.java.core.mqtt.TXMqttActionCallBack; +import com.tencent.iot.hub.device.java.core.mqtt.TXMqttConstants; +import com.tencent.iot.hub.device.java.core.mqtt.TXOTACallBack; +import com.tencent.iot.hub.device.java.core.mqtt.TXOTAConstansts; + +import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.ByteArrayInputStream; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +public class MQTTSample { + + private static final String TAG = "TXMQTT"; + // Default Value, should be changed in testing + private String mBrokerURL = null; //传入null,即使用腾讯云物联网通信默认地址 "${ProductId}.iotcloud.tencentdevices.com:8883" https://cloud.tencent.com/document/product/634/32546 + private String mProductID = "PRODUCT-ID"; + private String mDevName = "DEVICE-NAME"; + private String mDevPSK = "DEVICE-SECRET"; + + private String mDevCertName = "DEVICE_CERT-NAME "; + private String mDevKeyName = "DEVICE_KEY-NAME "; + + private String mSubProductID = "SUBDEV_PRODUCT-ID"; + private String mSubDevName = "SUBDEV_DEV-NAME"; + private String mSubDevPsk = "SUBDEV_DEVICE-SECRET"; + private String mTestTopic = "TEST_TOPIC_WITH_SUB_PUB"; + private String mDevCert; + private String mDevPriv; + + private boolean mMqttLogFlag; + private TXMqttLogCallBack mMqttLogCallBack; + + private Context mContext; + private String path2Store = ""; + + private TXMqttActionCallBack mMqttActionCallBack; + + /** + * MQTT连接实例 + */ + private TXGatewayConnection mMqttConnection; + + /** + * 请求ID + */ + private static AtomicInteger requestID = new AtomicInteger(0); + + public MQTTSample(Context context, TXMqttLogCallBack logCallBack, TXMqttActionCallBack callBack) { + mContext = context; + mMqttActionCallBack = callBack; + } + + public MQTTSample(Context context, TXMqttActionCallBack callBack, String brokerURL, String productId, + String devName, String devPSK, String subProductID, String subDevName, String testTopic, String devCertName, String devKeyName, + Boolean mqttLogFlag, TXMqttLogCallBack logCallBack) { + mBrokerURL = brokerURL; + mProductID = productId; + mDevName = devName; + mDevPSK = devPSK; + mSubProductID = subProductID; + mSubDevName = subDevName; + mTestTopic = testTopic; + mDevCertName = devCertName; + mDevKeyName = devKeyName; + + mMqttLogFlag = mqttLogFlag; + mMqttLogCallBack = logCallBack; + + mContext = context; + mMqttActionCallBack = callBack; + } + + public MQTTSample(Context context, TXMqttActionCallBack callBack, String brokerURL, String productId, + String devName, String devPsk, String devCert, String devPriv, String subProductID, String subDevName, String testTopic, String devCertName, String devKeyName, + Boolean mqttLogFlag, TXMqttLogCallBack logCallBack) { + mBrokerURL = brokerURL; + mProductID = productId; + mDevName = devName; + mDevPSK = devPsk; + mDevCert = devCert; + mDevPriv = devPriv; + mSubProductID = subProductID; + mSubDevName = subDevName; + mTestTopic = testTopic; + mDevCertName = devCertName; + mDevKeyName = devKeyName; + + mMqttLogFlag = mqttLogFlag; + mMqttLogCallBack = logCallBack; + + mContext = context; + mMqttActionCallBack = callBack; + path2Store = mContext.getCacheDir().getAbsolutePath(); + } + + public MQTTSample(Context context, TXMqttActionCallBack callBack, String brokerURL, String productId, + String devName, String devPsk, String devCert, String devPriv, String subProductID, String subDevName, String subDevPsk, String testTopic, String devCertName, String devKeyName, + Boolean mqttLogFlag, TXMqttLogCallBack logCallBack) { + this(context, callBack, brokerURL, productId, devName, devPsk, devCert, devPriv, subProductID, subDevName, testTopic, devCertName, devKeyName, mqttLogFlag, logCallBack); + mSubDevPsk = subDevPsk; + } + + public void setSubDevPsk(String val) { + mSubDevPsk = val; + } + + + public MQTTSample(Context context, TXMqttActionCallBack callBack, String brokerURL, String productId, + String devName, String devPSK, String subProductID, String subDevName, String testTopic) { + mBrokerURL = brokerURL; + mProductID = productId; + mDevName = devName; + mDevPSK = devPSK; + mSubProductID = subProductID; + mSubDevName = subDevName; + mTestTopic = testTopic; + + mContext = context; + mMqttActionCallBack = callBack; + } + + private TXOTACallBack oTACallBack = new TXOTACallBack() { + + @Override + public void onReportFirmwareVersion(int resultCode, String version, String resultMsg) { + + } + + @Override + public boolean onLastestFirmwareReady(String url, String md5, String version) { + System.out.println("onLastestFirmwareReady url=" + url + " version " + version); + mMqttConnection.gatewayDownSubdevApp(url, path2Store + "/" + md5, md5, version); + return true; // false 自动触发下载升级文件 true 需要手动触发下载升级文件 + } + + @Override + public void onDownloadProgress(int percent, String version) { + mMqttConnection.gatewaySubdevReportProgress(percent, version); + } + + @Override + public void onDownloadCompleted(String outputFile, String version) { + mMqttConnection.gatewaySubdevReportStart(version); + mMqttConnection.gatewaySubdevReportSuccess(version); + } + + @Override + public void onDownloadFailure(int errCode, String version) { + mMqttConnection.gatewaySubdevReportFail(errCode, "", version); + } + }; + + /** + * 建立MQTT连接 + */ + public void connect() { + mMqttConnection = new TXGatewayConnection(mContext, mBrokerURL, mProductID, mDevName, mDevPSK,null,null ,mMqttLogFlag, mMqttLogCallBack, mMqttActionCallBack); + mMqttConnection.setSubDevName(mSubDevName); + mMqttConnection.setSubDevProductKey(mSubDevPsk); + mMqttConnection.setSubProductID(mSubProductID); + MqttConnectOptions options = new MqttConnectOptions(); + options.setConnectionTimeout(8); + options.setKeepAliveInterval(240); + options.setAutomaticReconnect(true); + + if (mDevPriv != null && mDevCert != null && mDevPriv.length() != 0 && mDevCert.length() != 0) { + TXLog.i(TAG, "Using cert stream " + mDevPriv + " " + mDevCert); + options.setSocketFactory(AsymcSslUtils.getSocketFactoryByStream(new ByteArrayInputStream(mDevCert.getBytes()), new ByteArrayInputStream(mDevPriv.getBytes()))); + } else if (mDevPSK != null && mDevPSK.length() != 0){ + TXLog.i(TAG, "Using PSK"); +// options.setSocketFactory(AsymcSslUtils.getSocketFactory()); 如果您使用的是3.3.0及以下版本的 hub-device-android sdk,由于密钥认证默认配置的ssl://的url,请添加此句setSocketFactory配置。 + } else { + TXLog.i(TAG, "Using cert assets file"); + options.setSocketFactory(AsymcSslUtils.getSocketFactoryByAssetsFile(mContext, mDevCertName, mDevKeyName)); + } + + MQTTRequest mqttRequest = new MQTTRequest("connect", requestID.getAndIncrement()); + mMqttConnection.connect(options, mqttRequest); + + DisconnectedBufferOptions bufferOptions = new DisconnectedBufferOptions(); + bufferOptions.setBufferEnabled(true); + bufferOptions.setBufferSize(1024); + bufferOptions.setDeleteOldestMessages(true); + mMqttConnection.setBufferOpts(bufferOptions); + } + + /** + * 断开MQTT连接 + */ + public void disconnect() { + MQTTRequest mqttRequest = new MQTTRequest("disconnect", requestID.getAndIncrement()); + mMqttConnection.disConnect(mqttRequest); + } + + public void setSubdevOnline() { + // set subdev online + mMqttConnection.gatewaySubdevOnline(mSubProductID, mSubDevName); + } + + public void setSubDevOffline() { + mMqttConnection.gatewaySubdevOffline(mSubProductID, mSubDevName); + } + + public void setSubDevBinded() { + mMqttConnection.gatewayBindSubdev(mSubProductID, mSubDevName, mSubDevPsk); + } + + public void setSubDevUnbinded() { + mMqttConnection.gatewayUnbindSubdev(mSubProductID, mSubDevName); + } + + public void checkSubdevRelation() { + mMqttConnection.getGatewaySubdevRealtion(); + } + + public void getRemoteConfig() { + mMqttConnection.getRemoteConfig(); + } + + public void concernRemoteConfig() { + mMqttConnection.concernConfig(); + } + + public void reportSubDevVersion(String version) { + mMqttConnection.gatewaySubdevReportVer(version); + } + + public void subscribeNTPTopic() { + // QOS等级 + int qos = TXMqttConstants.QOS1; + // 用户上下文(请求实例) + MQTTRequest mqttRequest = new MQTTRequest("subscribeNTPTopic", requestID.getAndIncrement()); + mMqttConnection.subscribeNTPTopic(qos, mqttRequest); + } + + public void getNTPService() { + mMqttConnection.getNTPService(); + } + + public void initOTA() { + TXLog.e(TAG, "path2Store " + path2Store); + mMqttConnection.initOTA(path2Store, oTACallBack); + } + + /** + * 订阅广播主题 + */ + public void subscribeBroadCastTopic() { + // 用户上下文(请求实例) + MQTTRequest mqttRequest = new MQTTRequest("subscribeTopic", requestID.getAndIncrement()); + // 订阅广播主题 + mMqttConnection.subscribeBroadcastTopic(TXMqttConstants.QOS1, mqttRequest); + } + + /** + * 订阅主题 + * + */ + public void subscribeTopic() { + // 主题 + String topic = mTestTopic; + // QOS等级 + int qos = TXMqttConstants.QOS1; + // 用户上下文(请求实例) + MQTTRequest mqttRequest = new MQTTRequest("subscribeTopic", requestID.getAndIncrement()); + + Log.d(TAG, "sub topic is " + topic); + + // 订阅主题 + mMqttConnection.subscribe(topic, qos, mqttRequest); + + } + + /** + * 取消订阅主题 + * + */ + public void unSubscribeTopic() { + // 主题 + String topic = mTestTopic; + // 用户上下文(请求实例) + MQTTRequest mqttRequest = new MQTTRequest("unSubscribeTopic", requestID.getAndIncrement()); + Log.d(TAG, "Start to unSubscribe" + topic); + // 取消订阅主题 + mMqttConnection.unSubscribe(topic, mqttRequest); + } + + /** + * 发布主题 + */ + public void publishTopic(String topicName, Map data) { + // 主题 + String topic = mTestTopic; + // MQTT消息 + MqttMessage message = new MqttMessage(); + + JSONObject jsonObject = new JSONObject(); + try { + for (Map.Entry entrys : data.entrySet()) { + jsonObject.put(entrys.getKey(), entrys.getValue()); + } + } catch (JSONException e) { + TXLog.e(TAG, e, "pack json data failed!"); + } + message.setQos(TXMqttConstants.QOS1); + message.setPayload(jsonObject.toString().getBytes()); + + // 用户上下文(请求实例) + MQTTRequest mqttRequest = new MQTTRequest("publishTopic", requestID.getAndIncrement()); + + Log.d(TAG, "pub topic " + topic + message); + // 发布主题 + mMqttConnection.publish(topic, message, mqttRequest); + + } + + /** + * 订阅RRPC主题 + * + */ + public void subscribeRRPCTopic() { + // 用户上下文(请求实例) + MQTTRequest mqttRequest = new MQTTRequest("subscribeTopic", requestID.getAndIncrement()); + // 订阅主题 + mMqttConnection.subscribeRRPCTopic(TXMqttConstants.QOS0, mqttRequest); + + } + + /** + * 生成一条日志 + * @param logLevel 日志级别: + * 错误:TXMqttLogConstants.LEVEL_ERROR + * 警告:TXMqttLogConstants.LEVEL_WARN + * 通知:TXMqttLogConstants.LEVEL_INFO + * 调试:TXMqttLogConstants.LEVEL_DEBUG + * @param tag + * @param format + * @param obj + */ + public void mLog(int logLevel, final String tag,final String format, final Object... obj) { + if (mMqttLogFlag) + mMqttConnection.mLog(logLevel, tag, format, obj); + } + + /** + * 发起一次日志上传 + */ + public void uploadLog() { + mMqttConnection.uploadLog(); + } + + public void checkFirmware() { + + mMqttConnection.initOTA(Environment.getExternalStorageDirectory().getAbsolutePath(), new TXOTACallBack() { + @Override + public void onReportFirmwareVersion(int resultCode, String version, String resultMsg) { + TXLog.e(TAG, "onReportFirmwareVersion:" + resultCode + ", version:" + version + ", resultMsg:" + resultMsg); + } + + @Override + public boolean onLastestFirmwareReady(String url, String md5, String version) { + TXLog.e(TAG, "MQTTSample onLastestFirmwareReady"); + return false; + } + + @Override + public void onDownloadProgress(int percent, String version) { + TXLog.e(TAG, "onDownloadProgress:" + percent); + } + + @Override + public void onDownloadCompleted(String outputFile, String version) { + TXLog.e(TAG, "onDownloadCompleted:" + outputFile + ", version:" + version); + + mMqttConnection.reportOTAState(TXOTAConstansts.ReportState.DONE, 0, "OK", version); + } + + @Override + public void onDownloadFailure(int errCode, String version) { + TXLog.e(TAG, "onDownloadFailure:" + errCode); + + mMqttConnection.reportOTAState(TXOTAConstansts.ReportState.FAIL, errCode, "FAIL", version); + } + }); + mMqttConnection.reportCurrentFirmwareVersion("0.0.1"); + } +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java index f39162e..039f404 100644 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java @@ -1,6 +1,7 @@ package io.dcloud.zmt_module; import com.alibaba.fastjson.JSONObject; +import com.tencent.iot.hub.device.android.core.gateway.TXGatewayConnection; import io.dcloud.feature.uniapp.annotation.UniJSMethod; import io.dcloud.feature.uniapp.bridge.UniJSCallback; @@ -77,6 +78,8 @@ public class zmtClass extends UniModule { @UniJSMethod(uiThread = false) public JSONObject testSyncFunc(){ Log.e(TAG, "testSyncFunc--zmt2"); + + JSONObject data = new JSONObject(); data.put("code", "success-zmt sync"); return data;