扫地机 P2P(新)

更新时间:2025-01-07 09:38:34下载pdf

扫地机 Peer-to-Peer(P2P)能力提供 P2P 下载通道,来获取扫地机上的地图和清扫路线数据。由于数据直接来源于设备点对点的直传,所以无需将实时地图与数据上传至云存储服务器再下载,从而达到数据传输速度更快、更节省云存储成本的目的。

功能入口

val iThingSweeperKitSdk = ThingOptimusSdk.getManager(IThingSweeperKitSdk::class.java)
val mSweeperP2P = iThingSweeperKitSdk.newSweeperP2PKitInstance(devId)

初始化相关

初始化

接口说明

fun initP2PSDKWithUserId(initConfig: ThingInitConfig, callback: SweeperP2PKitCallback?)

参数说明

参数 说明
ThingInitConfig 配置用户 ID
callback 是否初始化成功回调

代码示例

mSweeperP2P?.initP2PSDKWithUserId(ThingInitConfig().apple {
    this.userId = "userId"
}, object : SweeperP2PKitCallback {
    override fun onError(errorCode: Int, errorMsg: String?) {
    }

    override fun onSuccess() {
    }
})

反初始化

接口说明

fun deInit()

代码示例

mSweeperP2P?.deInit(object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }

})

P2P 服务相关

连接 P2P 服务

接口说明

fun connectDevice(devId: String, p2pParams: ThingConnConfig, callback: SweeperP2PKitCallback?)

参数说明

ThingConnConfig

参数 说明
mode 连接模式
  • 0: INTERNET
  • 1: LAN
timeout 超时时长,单位:ms
值为 0 会设置成默认值

代码示例

mSweeperP2P?.connectDevice(devId, ThingConnConfig().apply {
    this.mode = 1
    this.timeout = 15000
}, object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }
})

断开 P2P 服务

接口说明

fun disconnectDevice(devId: String, callback: SweeperP2PKitCallback?)

参数说明

参数 说明
devId 设备 ID
callback 断开 P2P 回调

代码示例

mSweeperP2P?.disconnectDevice("devId", object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }

})

检查 P2P 连接

接口说明

/**
 * @return 是否连接中
 */
fun isActiveSync(devId: String): Boolean

参数说明

参数 说明
devId 设备 ID

代码示例

if (mSweeperP2P?.isActiveSync("devId") == true) {

}

下载相关

下载文件

通过 querySweeperFile 获取地图文件列表后,再调用本接口下载最终地图文件。

文件名后缀为 .stream 表示持续下载设备产生的数据,直到设备自动停止发送数据,或调用 cancelDownload 取消下载。

接口说明

/**
 * @param params 入参
 * @param callback 回调,是否执行成功
 */
fun downloadFile(devId: String, params: ThingDownloadFile, callback: SweeperP2PKitCallback?)

参数说明

ThingDownloadFile

参数 说明
albumName 默认 ipc_sweeper_robot
filePath 下载文件在 App 的本地存储路径
jsonfiles 下载的文件,格式如:{"files":["filesname1", "filesname2", "filesname3" ]}

代码示例

//init file path
val sweeperCacheFile = FileUtil.getSDPath(this) + File.separator + "Sweeper" + File.separator + "Cache" + File.separator + "map" + File.separator
FileUtil.createDir(sweeperCacheFile)
val file = File(sweeperCacheFile)
FileUtil.deleteFile(file)
//Filter Files (result is downloadFile response)
val unCachedList: MutableList<String> = ArrayList()
for (itemBean in result) {
    if (!TextUtils.isEmpty(itemBean?.filename)) {
        val path: String = sweeperCacheFile + itemBean?.filename
        val file = File(path)
        if (file.exists()) {
            file.delete()
        }
        //单次下载
        itemBean?.filename?.let {
            if (!it.endsWith(".stream")) {
                unCachedList.add(itemBean.filename)
            }
        }
        //持续下载设备产生的数据,直到设备自动停止发送数据
        if (itemBean?.filename!!.endsWith(".stream")) {
            unCachedList.add(itemBean.filename)
        }
    }
}
val jsonFiles = JSON.toJSONString(
    DownloadAlbumFiledBean2(
        unCachedList
    )
)
//down file
mSweeperP2P?.downloadFile("devId", ThingDownloadFile().apply {
    this.filePath = sweeperCacheFile
    this.albumName = "ipc_sweeper_robot"
    this.jsonfiles = jsonFiles
}, object : SweeperP2PKitCallback {
    override fun onSuccess() {

    }

    override fun onError(errorCode: Int, errorMsg: String?) {

    }

})

取消下载任务

接口说明

/**
 * @param callback 回调
 */
fun cancelDownload(devId: String, callback: SweeperP2PKitCallback?)

代码示例


mSweeperP2P?.cancelDownload("devId", object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }

})

监听相关

注册监听

接口说明

/**
 * 注册监听
 * @param listener 主要监听:连接状态、单个文件下载成功、单个文件下载进度
 */
fun addFileTransListener(listener: SweeperFileTransListener)

回调类说明

interface SweeperFileTransListener {

    /**
     * 链接状态
     */
    fun onSessionStatusChanged(status: ThingSessionStatus)

    /**
     * 单个文件下载成功
     */
    fun onFileFinished(complete: ThingDownloadComplete)

    /**
     * 单个文件下载进度
     */
    fun onFileProgress(progress: ThingDownloadProgress)

    /**
     * 整体下载进度
     */
    fun onFileTotalProgress(totalProgress: ThingDownloadTotalProgress)

}

参数说明

ThingSessionStatus

参数 说明
deviceId 设备 ID
status 状态值, 小于 0 为断开连接,其他见 P2P SDK 错误码

ThingDownloadComplete

参数 说明
deviceId 设备 ID
fileName 文件名
index 索引

ThingDownloadProgress

参数 说明
deviceId 设备 ID
fileName 文件名
progress 进度

ThingDownloadTotalProgress

参数 说明
deviceId 设备 ID
progress 进度

代码示例

mSweeperP2P?.addFileTransListener(object : SweeperFileTransListener{
    override fun onFileFinished(complete: ThingDownloadComplete) {
        TODO("Not yet implemented")
    }

    override fun onFileProgress(progress: ThingDownloadProgress) {
        TODO("Not yet implemented")
    }

    override fun onFileTotalProgress(totalProgress: ThingDownloadTotalProgress) {
        TODO("Not yet implemented")
    }

    override fun onSessionStatusChanged(status: ThingSessionStatus) {
        TODO("Not yet implemented")
    }

})

移除监听

接口说明

fun removeFileTransListener(listener: SweeperFileTransListener)

查询文件索引列表

接口说明

/**
 * @param callback 回调
 */
fun querySweeperFile(devId: String, albumName: String, callback: SweeperP2PKitDataCallback<List<ThingAlbumFileIndex?>?>?)

参数说明

参数 说明
devId 设备 ID
albumName 默认 ipc_sweeper_robot

代码示例

mSweeperP2P?.querySweeperFile(
    "devId",
    "ipc_sweeper_robot",
    object : SweeperP2PKitDataCallback<List<ThingAlbumFileIndex?>?> {
        override fun onSuccess(result: List<ThingAlbumFileIndex?>?) {
            //TODO downFile
        }

        override fun onError(errorCode: Int, errorMsg: String?) {
        }

    })

错误码

Code 说明
-1025 获取 P2P 能力失败(没加载 P2P 的能力)
-1030 P2P 未连接(P2P 未连接的情况下调用和连接相关的方法)
-1031 P2P 正在连接
-1040 正在查询文件目录
-1041 文件目录查询失败
-1060 文件下载失败
-1061 没有正在下载的任务,追加下载失败

更多内容,参考 P2P SDK 错误码