|
|
@@ -1,7 +1,22 @@
|
|
|
package com.grkj.iscs_mars.websocket
|
|
|
|
|
|
+import com.google.gson.Gson
|
|
|
+import com.google.gson.reflect.TypeToken
|
|
|
+import com.grkj.iscs_mars.R
|
|
|
+import com.grkj.iscs_mars.util.AppUtils
|
|
|
+import com.grkj.iscs_mars.util.CommonUtils
|
|
|
+import com.grkj.iscs_mars.util.LzTekUtils
|
|
|
+import com.grkj.iscs_mars.util.SPUtils
|
|
|
import com.grkj.iscs_mars.util.log.LogUtil
|
|
|
+import com.grkj.iscs_mars.view.dialog.LoadingProgressDialog
|
|
|
+import com.grkj.iscs_mars.view.fragment.DockTestFragment
|
|
|
+import com.sik.sikandroid.activity.ActivityTracker
|
|
|
+import com.sik.sikcore.SIKCore
|
|
|
import com.sik.sikcore.shell.ShellUtils
|
|
|
+import com.sik.sikcore.thread.ThreadUtils
|
|
|
+import com.sik.siknet.http.httpDownloadFile
|
|
|
+import com.sik.siknet.http.interceptor.ProgressListener
|
|
|
+import kotlinx.coroutines.delay
|
|
|
|
|
|
/**
|
|
|
* 消息业务处理
|
|
|
@@ -12,6 +27,41 @@ object MessageBusinessManager {
|
|
|
*/
|
|
|
const val MESSAGE_TYPE_COMMAND_TO_LOG = "commandToLog"
|
|
|
|
|
|
+ /**
|
|
|
+ * 重启硬件
|
|
|
+ */
|
|
|
+ const val MESSAGE_TYPE_REBOOT_MACHINE = "rebootMachine"
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重启软件
|
|
|
+ */
|
|
|
+ const val MESSAGE_TYPE_REBOOT_APP = "rebootApp"
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新app
|
|
|
+ */
|
|
|
+ const val MESSAGE_TYPE_UPDATE_APP = "updateApp"
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 基座配置
|
|
|
+ */
|
|
|
+ const val MESSAGE_TYPE_DOCK_CONFIG = "dockConfig"
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 端口配置
|
|
|
+ */
|
|
|
+ const val MESSAGE_TYPE_DOCK_PORT = "dockPort"
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加载弹窗
|
|
|
+ */
|
|
|
+ private var loadingProgressDialog: LoadingProgressDialog? = null
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 最后更新时间
|
|
|
+ */
|
|
|
+ private var lastUpdateTime: Long = System.currentTimeMillis()
|
|
|
+
|
|
|
/**
|
|
|
* 处理消息
|
|
|
*/
|
|
|
@@ -24,6 +74,87 @@ object MessageBusinessManager {
|
|
|
val result = ShellUtils.execCmd(messageEntity.content!!)
|
|
|
LogUtil.i("执行结果:${result.successMsg}")
|
|
|
}
|
|
|
+
|
|
|
+ MESSAGE_TYPE_REBOOT_MACHINE -> {
|
|
|
+ LzTekUtils.rebootMachine()
|
|
|
+ }
|
|
|
+
|
|
|
+ MESSAGE_TYPE_REBOOT_APP -> {
|
|
|
+ CommonUtils.restartApp(SIKCore.getApplication())
|
|
|
+ }
|
|
|
+
|
|
|
+ MESSAGE_TYPE_UPDATE_APP -> {
|
|
|
+ val apkDownloadPath = messageEntity.content ?: return
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
+ val downloadApk = apkDownloadPath.httpDownloadFile(progressListener = object :
|
|
|
+ ProgressListener {
|
|
|
+ override fun update(
|
|
|
+ bytesRead: Long,
|
|
|
+ contentLength: Long,
|
|
|
+ done: Boolean
|
|
|
+ ) {
|
|
|
+ if (System.currentTimeMillis() - lastUpdateTime < 1000 && !done) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ lastUpdateTime = System.currentTimeMillis()
|
|
|
+ ThreadUtils.runOnMain {
|
|
|
+ ActivityTracker.getCurrentActivity()?.let {
|
|
|
+ if (loadingProgressDialog == null) {
|
|
|
+ loadingProgressDialog = LoadingProgressDialog(it)
|
|
|
+ }
|
|
|
+ if (loadingProgressDialog?.isShowing == false && !done) {
|
|
|
+ loadingProgressDialog?.show()
|
|
|
+ }
|
|
|
+ val percent = if (contentLength > 0) {
|
|
|
+ LogUtil.i(
|
|
|
+ "下载进度:${
|
|
|
+ String.format(
|
|
|
+ "%.2f",
|
|
|
+ bytesRead * 100f / contentLength
|
|
|
+ )
|
|
|
+ },${bytesRead},${contentLength}"
|
|
|
+ )
|
|
|
+ String.format("%.2f", bytesRead * 100f / contentLength)
|
|
|
+ } else {
|
|
|
+ "--" // 或者直接显示 "…" 表示未知
|
|
|
+ }
|
|
|
+ if (!done) {
|
|
|
+ loadingProgressDialog?.setProgress(
|
|
|
+ "${CommonUtils.getStr(R.string.apk_downloading)},${percent}%"
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ loadingProgressDialog?.setProgress("${CommonUtils.getStr(R.string.restart_after_install)}")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ downloadApk?.let {
|
|
|
+ LogUtil.i("调用静默安装API进行安装:${it.absolutePath}")
|
|
|
+ LzTekUtils.installApk(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ MESSAGE_TYPE_DOCK_CONFIG -> {
|
|
|
+ val dockConfig = messageEntity.content ?: return
|
|
|
+ try {
|
|
|
+ Gson().fromJson<List<DockTestFragment.DockTestBean>>(
|
|
|
+ dockConfig,
|
|
|
+ object : TypeToken<List<DockTestFragment.DockTestBean>>() {}.type
|
|
|
+ )
|
|
|
+ SPUtils.saveDockConfig(SIKCore.getApplication(), dockConfig)
|
|
|
+ CommonUtils.restartApp(SIKCore.getApplication())
|
|
|
+ } catch (e: Exception) {
|
|
|
+ LogUtil.i("解析dock配置失败:${e.message}")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ MESSAGE_TYPE_DOCK_PORT -> {
|
|
|
+ val dockPort = messageEntity.content ?: return
|
|
|
+ SPUtils.savePortConfig(SIKCore.getApplication(), dockPort)
|
|
|
+ CommonUtils.restartApp(SIKCore.getApplication())
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|