|
@@ -8,6 +8,7 @@ import com.example.modifier.model.InstallApkAction
|
|
|
import com.example.modifier.model.SocketCallback
|
|
import com.example.modifier.model.SocketCallback
|
|
|
import com.example.modifier.model.TaskAction
|
|
import com.example.modifier.model.TaskAction
|
|
|
import com.example.modifier.model.TaskConfig
|
|
import com.example.modifier.model.TaskConfig
|
|
|
|
|
+import com.example.modifier.model.TaskExecutionResult
|
|
|
import com.example.modifier.serializer.Json
|
|
import com.example.modifier.serializer.Json
|
|
|
import com.example.modifier.utils.uniqueId
|
|
import com.example.modifier.utils.uniqueId
|
|
|
import io.socket.client.IO
|
|
import io.socket.client.IO
|
|
@@ -38,9 +39,13 @@ class SocketClient(
|
|
|
mSocket = IO.socket(server, mSocketOpts)
|
|
mSocket = IO.socket(server, mSocketOpts)
|
|
|
|
|
|
|
|
mSocket.on("message", this)
|
|
mSocket.on("message", this)
|
|
|
- mSocket.on(Socket.EVENT_CONNECT) { this.onConnected() }
|
|
|
|
|
- mSocket.on(Socket.EVENT_DISCONNECT) { this.onDisconnected() }
|
|
|
|
|
- mSocket.on(Socket.EVENT_CONNECT_ERROR) { this.onConnectError() }
|
|
|
|
|
|
|
+ mSocket.on(Socket.EVENT_CONNECT) { this.onConnected(it) }
|
|
|
|
|
+ mSocket.on(Socket.EVENT_DISCONNECT) { this.onDisconnected(it) }
|
|
|
|
|
+ mSocket.on(Socket.EVENT_CONNECT_ERROR) {
|
|
|
|
|
+ it.forEach {
|
|
|
|
|
+ Log.e(TAG, "Connection error", it as Throwable)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
try {
|
|
try {
|
|
|
mSocket.connect()
|
|
mSocket.connect()
|
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|
|
@@ -119,22 +124,15 @@ class SocketClient(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fun onConnected() {
|
|
|
|
|
|
|
+ fun onConnected(vararg args: Any) {
|
|
|
Log.i(tag, "Connected to server")
|
|
Log.i(tag, "Connected to server")
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
delay(500)
|
|
delay(500)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fun onDisconnected() {}
|
|
|
|
|
|
|
+ fun onDisconnected(vararg args: Any) {}
|
|
|
|
|
|
|
|
- fun onConnectError(vararg args: Any) {
|
|
|
|
|
- Log.i(TAG, "Connection error: " + args[0])
|
|
|
|
|
- if (args[0] is Exception) {
|
|
|
|
|
- val e = args[0] as Exception
|
|
|
|
|
- e.printStackTrace()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
fun reportDeviceStatues(canSend: Boolean, busy: Boolean, currentCountry: String) {
|
|
fun reportDeviceStatues(canSend: Boolean, busy: Boolean, currentCountry: String) {
|
|
|
val data = JSONObject()
|
|
val data = JSONObject()
|
|
@@ -151,7 +149,15 @@ class SocketClient(
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fun emitEvent(event: String, data: Any) {
|
|
|
|
|
|
|
+ fun emitEvent(event: String, data: TaskExecutionResult) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ mSocket.emit(event, JSONObject(Json.encodeToString(data)))
|
|
|
|
|
+ } catch (e: Exception) {
|
|
|
|
|
+ Log.e(tag, "emitEvent error", e)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fun <T> emitEvent(event: String, data: SocketCallback<T>) {
|
|
|
try {
|
|
try {
|
|
|
mSocket.emit(event, JSONObject(Json.encodeToString(data)))
|
|
mSocket.emit(event, JSONObject(Json.encodeToString(data)))
|
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|