|
|
@@ -5,6 +5,8 @@ import android.util.Log
|
|
|
import com.example.modifier.BuildConfig
|
|
|
import com.example.modifier.TAG
|
|
|
import com.example.modifier.model.InstallApkAction
|
|
|
+import com.example.modifier.model.RunScriptAction
|
|
|
+import com.example.modifier.model.RunScriptResult
|
|
|
import com.example.modifier.model.SocketCallback
|
|
|
import com.example.modifier.model.TaskAction
|
|
|
import com.example.modifier.model.TaskConfig
|
|
|
@@ -64,6 +66,7 @@ class SocketClient(
|
|
|
if (args[0] is JSONObject) {
|
|
|
val json = args[0] as JSONObject
|
|
|
val action = json.optString("action")
|
|
|
+ val id = json.optString("id")
|
|
|
if ("send" == action) {
|
|
|
val data = json.optJSONObject("data")
|
|
|
if (data != null) {
|
|
|
@@ -84,23 +87,60 @@ class SocketClient(
|
|
|
}
|
|
|
}
|
|
|
} else if ("task" == action) {
|
|
|
- val taskAction = Json.decodeFromString<TaskAction>(json.toString())
|
|
|
- CoroutineScope(Dispatchers.IO).launch {
|
|
|
- taskRunner.runTask(taskAction, onSuccess = { res ->
|
|
|
- socketCallback(taskAction.id, res)
|
|
|
- }, onError = { error ->
|
|
|
- socketCallback(taskAction.id, -1, error.message)
|
|
|
- })
|
|
|
+ try {
|
|
|
+ val taskAction = Json.decodeFromString<TaskAction>(json.toString())
|
|
|
+ CoroutineScope(Dispatchers.IO).launch {
|
|
|
+ taskRunner.runTask(taskAction, onSuccess = { res ->
|
|
|
+ socketCallback(taskAction.id, res)
|
|
|
+ }, onError = { error ->
|
|
|
+ socketCallback(
|
|
|
+ id = taskAction.id,
|
|
|
+ status = -1,
|
|
|
+ error = error.message
|
|
|
+ )
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e(tag, "taskAction error", e)
|
|
|
+ socketCallback(id = id, status = -1, error = e.message)
|
|
|
}
|
|
|
} else if ("installApk" == action) {
|
|
|
- val installApkAction = Json.decodeFromString<InstallApkAction>(json.toString())
|
|
|
-
|
|
|
- CoroutineScope(Dispatchers.IO).launch {
|
|
|
- taskRunner.installApk(installApkAction, onSuccess = {
|
|
|
- socketCallback(installApkAction.id, 0)
|
|
|
- }, onError = { error ->
|
|
|
- socketCallback(installApkAction.id, -1, error.message)
|
|
|
- })
|
|
|
+ try {
|
|
|
+ val installApkAction =
|
|
|
+ Json.decodeFromString<InstallApkAction>(json.toString())
|
|
|
+ CoroutineScope(Dispatchers.IO).launch {
|
|
|
+ taskRunner.installApk(installApkAction, onSuccess = {
|
|
|
+ socketCallback(id = installApkAction.id, status = 0, data = "OK")
|
|
|
+ }, onError = { error ->
|
|
|
+ socketCallback(
|
|
|
+ id = installApkAction.id,
|
|
|
+ status = -1,
|
|
|
+ error = error.message
|
|
|
+ )
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e(tag, "installApk error", e)
|
|
|
+ socketCallback(id = id, status = -1, error = e.message)
|
|
|
+ }
|
|
|
+ } else if ("runScript" == action) {
|
|
|
+ try {
|
|
|
+ val runScriptAction =
|
|
|
+ Json.decodeFromString<RunScriptAction>(json.toString())
|
|
|
+ CoroutineScope(Dispatchers.IO).launch {
|
|
|
+ taskRunner.runScript(runScriptAction, onSuccess = { out, err ->
|
|
|
+ socketCallback(runScriptAction.id, RunScriptResult(out, err))
|
|
|
+ }, onError = { error ->
|
|
|
+ socketCallback(
|
|
|
+ id = runScriptAction.id,
|
|
|
+ status = -1,
|
|
|
+ error = error.message
|
|
|
+ )
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e(tag, "runScript error", e)
|
|
|
+ socketCallback(id = id, status = -1, error = e.message)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -150,14 +190,38 @@ class SocketClient(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun socketCallback(id: String, status: Int, error: String? = null) {
|
|
|
+ private fun socketCallback(id: String, data: RunScriptResult) {
|
|
|
try {
|
|
|
mSocket.emit(
|
|
|
"callback", JSONObject(
|
|
|
Json.encodeToString(
|
|
|
- SocketCallback<String>(
|
|
|
+ SocketCallback(
|
|
|
+ id = id,
|
|
|
+ status = 0,
|
|
|
+ data = data
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e(tag, "emitEvent error", e)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun socketCallback(
|
|
|
+ id: String,
|
|
|
+ status: Int,
|
|
|
+ data: String? = null,
|
|
|
+ error: String? = null
|
|
|
+ ) {
|
|
|
+ try {
|
|
|
+ mSocket.emit(
|
|
|
+ "callback", JSONObject(
|
|
|
+ Json.encodeToString(
|
|
|
+ SocketCallback(
|
|
|
id = id,
|
|
|
status = status,
|
|
|
+ data = data,
|
|
|
error = error
|
|
|
)
|
|
|
)
|