From 42b4798a009ccea5d59d6f2cbf62e0883352ed7b Mon Sep 17 00:00:00 2001 From: Zhengyu Peng Date: Mon, 14 Feb 2022 14:56:18 -0500 Subject: [PATCH] update --- software/android/app/build.gradle | 2 +- .../android/app/src/main/AndroidManifest.xml | 1 + .../com/rookiedev/hexapod/ControlActivity.kt | 118 ++++++++++++++++-- .../rookiedev/hexapod/network/TCPClient.kt | 118 ++++++++++++++++++ 4 files changed, 227 insertions(+), 12 deletions(-) create mode 100644 software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt diff --git a/software/android/app/build.gradle b/software/android/app/build.gradle index d838980..5cbc0a3 100644 --- a/software/android/app/build.gradle +++ b/software/android/app/build.gradle @@ -32,7 +32,7 @@ android { } dependencies { - + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0' implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' diff --git a/software/android/app/src/main/AndroidManifest.xml b/software/android/app/src/main/AndroidManifest.xml index 0d29843..4d3cdfc 100644 --- a/software/android/app/src/main/AndroidManifest.xml +++ b/software/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + = radius / 3 && length < 2 * radius / 3) { var angle = atan2(coorY, coorX) - if (angle>-PI/4 && angle<=PI/4) - { + if (angle > -PI / 4 && angle <= PI / 4) { println("Move right") - } else if ( angle>PI/4 && angle<=3*PI/4){ + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Move right") + } + } + } else if (angle > PI / 4 && angle <= 3 * PI / 4) { println("Move back") - } else if ( angle>-3*PI/4 && angle<-PI/4){ + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Move back") + } + } + } else if (angle > -3 * PI / 4 && angle < -PI / 4) { println("Move forward") + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Move forward") + } + } } else { println("Move left") + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Move left") + } + } } } else if (length >= 2 * radius / 3 && length < radius) { var angle = atan2(coorY, coorX) - if (angle>-PI/4 && angle<=PI/4) - { + if (angle > -PI / 4 && angle <= PI / 4) { println("Turn right") - } else if ( angle>PI/4 && angle<=3*PI/4){ + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Turn right") + } + } + } else if (angle > PI / 4 && angle <= 3 * PI / 4) { println("Fast back") - } else if ( angle>-3*PI/4 && angle<-PI/4){ + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Fast back") + } + } + } else if (angle > -3 * PI / 4 && angle < -PI / 4) { println("Fast forward") + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Fast forward") + } + } } else { println("Turn left") + + runBlocking { // this: CoroutineScope + launch { // launch a new coroutine and continue + tcpClient?.sendMessage("Turn left") + } + } } } // val width = view.width @@ -149,6 +209,21 @@ class ControlActivity : AppCompatActivity() { } } ) + this.tcpClient = TCPClient(this, "192.168.1.202", 1234, object : OnMessageReceived { + override fun messageReceived(message: String?) { + if (message == null) { +// alertDialog(DISCONNECTED) + println("no message") + } + } + }, object : OnConnectEstablished { + override fun onConnected() { +// udpClient.start() + println("connected") + } + } + ) + this.tcpClient!!.start() } @@ -166,5 +241,26 @@ class ControlActivity : AppCompatActivity() { insetsController.show(type) } } + + + + fun sendMessageAsync(message: String) { + // Starts a new coroutine within the scope + scope.launch { + // New coroutine that can call suspend functions +// suspend fun sendMessage(message: String) = // Dispatchers.Main + withContext(Dispatchers.IO) { // Dispatchers.IO (main-safety block) + tcpClient?.sendMessage(message) + /* perform network IO here */ // Dispatchers.IO (main-safety block) + } + } + } + +// suspend fun sendMessage(message: String) = // Dispatchers.Main +// withContext(Dispatchers.IO) { // Dispatchers.IO (main-safety block) +// tcpClient?.sendMessage(message) +// /* perform network IO here */ // Dispatchers.IO (main-safety block) +// } // Dispatchers.Main } + diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt new file mode 100644 index 0000000..6c40723 --- /dev/null +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/network/TCPClient.kt @@ -0,0 +1,118 @@ +package com.rookiedev.hexapod.network + +import com.rookiedev.hexapod.ControlActivity +import android.util.Log +import java.io.* +import java.net.InetAddress +import java.net.InetSocketAddress +import java.net.Socket +import java.net.UnknownHostException +import java.util.concurrent.locks.ReentrantLock + + +class TCPClient( + c: ControlActivity, + ip: String?, + port: Int, + messagelistener: OnMessageReceived?, + onconnected: OnConnectEstablished? +) : + Thread() { + private val controller: ControlActivity = c + private var TCPSocket:Socket? = null + private var SERVERIP: InetAddress? = null + private val SERVERPORT: Int + private var serverAddr: InetSocketAddress? = null + private var TCPOut: PrintWriter? = null + private var TCPIn: BufferedReader? = null + private var TCPMessage: String? = null + private var mMessageListener: OnMessageReceived? = null + private var onConnected: OnConnectEstablished? = null + private var isConnected = false + private var pause = false // if the thread is paused by system + private var isNewData = false + private var newMessage:String? = null + override fun run() { + try { + this.TCPSocket = Socket() + this.TCPSocket!!.soTimeout = 3000 + this.TCPSocket!!.connect(serverAddr, 3000) // connecting socket and set timeout in 3s + onConnected!!.onConnected() + TCPOut = PrintWriter( + BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())), + true + ) +// sendMessage("test") + isConnected = true + while(isConnected) + { +// if (isNewData) +// { +// if (TCPOut != null && !TCPOut!!.checkError()) { +// TCPOut!!.println(newMessage) +// TCPOut!!.flush() +// } +// isNewData = false +// }else{ +// sleep(100) +// } + sleep(1000) + } + } catch (e: Exception) { +// controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT) + println("unable to connect") + } + } + +// private fun keepAlive() { +// sendMessage(Constants.requestMessage(Constants.REQUEST_ISALIVE)) +// try { +// TCPMessage = TCPIn!!.readLine() +// mMessageListener!!.messageReceived(TCPMessage) +// } catch (e: IOException) { +// controller.alertDialog(java.lang.ModuleLayer.Controller.DISCONNECTED) +// } +// } + + /** + * Sends the message entered by client to the server + * + * @param message text entered by client + */ + fun sendMessage(message: String?) { +// newMessage = message +// isNewData = true + if (TCPOut != null && !TCPOut!!.checkError()) { + TCPOut!!.println(message) + TCPOut!!.flush() + } + } + +// fun stopClient() { +// sendMessage(Constants.requestMessage(Constants.REQUEST_DISCONNECT)) +// pause = true +// isConnected = false +// } + + interface OnMessageReceived { + fun messageReceived(message: String?) + } + + interface OnConnectEstablished { + fun onConnected() + } + + init { + SERVERPORT = port + mMessageListener = messagelistener + onConnected = onconnected + try { + SERVERIP = InetAddress.getByName(ip) + serverAddr = InetSocketAddress(SERVERIP, SERVERPORT) + } catch (e: UnknownHostException) { + // TODO Auto-generated catch block + e.printStackTrace() + } + pause = false + } +} \ No newline at end of file