master
Zhengyu Peng 3 years ago
parent 6d25b649ab
commit a1b80ad5f9

@ -3,17 +3,15 @@ package com.rookiedev.hexapod
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog import android.app.AlertDialog
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.view.* import android.view.*
import android.widget.ImageView import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.rookiedev.hexapod.network.TCPClient import com.rookiedev.hexapod.network.TCPClient
import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished import com.rookiedev.hexapod.network.TCPClient.*
import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived
import com.rookiedev.hexapod.network.TCPClient.OnDisconnected
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlin.math.PI import kotlin.math.PI
import kotlin.math.atan2 import kotlin.math.atan2
@ -75,12 +73,13 @@ class ControlActivity : AppCompatActivity() {
private var radius = 0f private var radius = 0f
private var tcpClient: TCPClient? = null private var tcpClient: TCPClient? = null
private var ip:String = "" private var ip: String = ""
private var port = 0 private var port = 0
private val scope = CoroutineScope(Job() + Dispatchers.IO) private val scope = CoroutineScope(Job() + Dispatchers.IO)
private var currentState: String = "standby" private var currentState: String = "standby"
private lateinit var progressBar: ConstraintLayout
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@ -96,6 +95,7 @@ class ControlActivity : AppCompatActivity() {
controlWindowInsets(true) controlWindowInsets(true)
val controlCircle = findViewById<ImageView>(R.id.control_image) val controlCircle = findViewById<ImageView>(R.id.control_image)
progressBar = findViewById<ConstraintLayout>(R.id.progressBar)
val vto: ViewTreeObserver = controlCircle.viewTreeObserver val vto: ViewTreeObserver = controlCircle.viewTreeObserver
vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener { vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
@ -190,7 +190,40 @@ class ControlActivity : AppCompatActivity() {
} }
} }
) )
this.tcpClient = TCPClient(this, ip, port, object : OnMessageReceived { // this.tcpClient = TCPClient(this, ip, port, object : OnMessageReceived {
// override fun messageReceived(message: String?) {
// if (message == null) {
//// alertDialog(DISCONNECTED)
// println("no message")
// }
// }
// }, object : OnConnectEstablished {
// override fun onConnected() {
//// udpClient.start()
// println("connected")
// Handler(Looper.getMainLooper()).post {
// progressBar.visibility = View.GONE
// }
// }
// }, object : OnDisconnected{
// override fun onDisconnected() {
// Handler(Looper.getMainLooper()).post {
// progressBar.visibility = View.GONE
// alertDialog(0)
// }
// }
// }
// )
// this.tcpClient!!.start()
// alertDialog(0)
}
override fun onResume() {
super.onResume()
progressBar.visibility = View.VISIBLE
this.tcpClient = TCPClient(ip, port, object : OnMessageReceived {
override fun messageReceived(message: String?) { override fun messageReceived(message: String?) {
if (message == null) { if (message == null) {
// alertDialog(DISCONNECTED) // alertDialog(DISCONNECTED)
@ -201,10 +234,14 @@ class ControlActivity : AppCompatActivity() {
override fun onConnected() { override fun onConnected() {
// udpClient.start() // udpClient.start()
println("connected") println("connected")
Handler(Looper.getMainLooper()).post {
progressBar.visibility = View.GONE
} }
}, object : OnDisconnected{ }
}, object : OnDisconnected {
override fun onDisconnected() { override fun onDisconnected() {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
progressBar.visibility = View.GONE
alertDialog(0) alertDialog(0)
} }
} }
@ -212,7 +249,16 @@ class ControlActivity : AppCompatActivity() {
) )
this.tcpClient!!.start() this.tcpClient!!.start()
// alertDialog(0) }
override fun onPause() {
super.onPause()
println("on Pause")
// saveSharedPref()
tcpClient!!.stopClient()
tcpClient!!.interrupt()
} }
@ -246,7 +292,8 @@ class ControlActivity : AppCompatActivity() {
when (type) { when (type) {
0 -> { 0 -> {
alert.setTitle("Failed to connect") alert.setTitle("Failed to connect")
alert.setMessage("Failed to connect to the Hexapod" alert.setMessage(
"Failed to connect to the Hexapod"
) )
alert.setOnCancelListener(DialogInterface.OnCancelListener { finish() }) alert.setOnCancelListener(DialogInterface.OnCancelListener { finish() })
alert.setButton(AlertDialog.BUTTON_POSITIVE, alert.setButton(AlertDialog.BUTTON_POSITIVE,
@ -256,7 +303,6 @@ class ControlActivity : AppCompatActivity() {
} }
alert.show() alert.show()
} }
} }

@ -1,17 +1,11 @@
package com.rookiedev.hexapod.network package com.rookiedev.hexapod.network
import com.rookiedev.hexapod.ControlActivity import com.rookiedev.hexapod.ControlActivity
import android.util.Log
import java.io.* import java.io.*
import java.net.InetAddress import java.net.*
import java.net.InetSocketAddress
import java.net.Socket
import java.net.UnknownHostException
import java.util.concurrent.locks.ReentrantLock
class TCPClient( class TCPClient(
c: ControlActivity,
ip: String?, ip: String?,
port: Int, port: Int,
messagelistener: OnMessageReceived?, messagelistener: OnMessageReceived?,
@ -19,21 +13,17 @@ class TCPClient(
ondisconnect: OnDisconnected? ondisconnect: OnDisconnected?
) : ) :
Thread() { Thread() {
private val controller: ControlActivity = c private var TCPSocket: Socket? = null
private var TCPSocket:Socket? = null
private var SERVERIP: InetAddress? = null private var SERVERIP: InetAddress? = null
private val SERVERPORT: Int private val SERVERPORT: Int
private var serverAddr: InetSocketAddress? = null private var serverAddr: InetSocketAddress? = null
private var TCPOut: PrintWriter? = null private var TCPOut: PrintWriter? = null
private var TCPIn: BufferedReader? = null private var TCPIn: BufferedReader? = null
private var TCPMessage: String? = null
private var mMessageListener: OnMessageReceived? = null private var mMessageListener: OnMessageReceived? = null
private var onConnected: OnConnectEstablished? = null private var onConnected: OnConnectEstablished? = null
private var onDisconnected: OnDisconnected?=null private var onDisconnected: OnDisconnected? = null
private var isConnected = false private var isConnected = false
private var pause = false // if the thread is paused by system private var pause = false // if the thread is paused by system
private var isNewData = false
private var newMessage:String? = null
override fun run() { override fun run() {
try { try {
this.TCPSocket = Socket() this.TCPSocket = Socket()
@ -44,40 +34,23 @@ class TCPClient(
BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())), BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())),
true true
) )
TCPIn = BufferedReader(InputStreamReader(this.TCPSocket!!.getInputStream()))
// sendMessage("test") // sendMessage("test")
isConnected = true isConnected = true
while(isConnected) while (isConnected) {
{
// if (isNewData)
// {
// if (TCPOut != null && !TCPOut!!.checkError()) {
// TCPOut!!.println(newMessage)
// TCPOut!!.flush()
// }
// isNewData = false
// }else{
// sleep(100)
// }
sleep(1000) sleep(1000)
} }
} catch (e: Exception) { } catch (e: InterruptedException) {
// controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT) // controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT)
println("unable to connect") println(e)
// controller.alertDialog(0) // controller.alertDialog(0)
// onDisconnected!!.onDisconnected()
} catch (e: SocketTimeoutException) {
println(e)
onDisconnected!!.onDisconnected() onDisconnected!!.onDisconnected()
} }
} }
// 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 * Sends the message entered by client to the server
* *
@ -86,17 +59,20 @@ class TCPClient(
fun sendMessage(message: String?) { fun sendMessage(message: String?) {
// newMessage = message // newMessage = message
// isNewData = true // isNewData = true
if (TCPOut != null && !TCPOut!!.checkError()) {
TCPOut!!.println(message) if (this.TCPOut != null && !this.TCPOut!!.checkError()) {
TCPOut!!.flush() println("send message")
this.TCPOut!!.println(message)
this.TCPOut!!.flush()
} }
} }
// fun stopClient() { fun stopClient() {
// sendMessage(Constants.requestMessage(Constants.REQUEST_DISCONNECT)) // sendMessage(Constants.requestMessage(Constants.REQUEST_DISCONNECT))
// pause = true pause = true
// isConnected = false isConnected = false
// } this.TCPSocket!!.close()
}
interface OnMessageReceived { interface OnMessageReceived {
fun messageReceived(message: String?) fun messageReceived(message: String?)
@ -124,4 +100,5 @@ class TCPClient(
} }
pause = false pause = false
} }
} }

@ -1,25 +1,43 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ControlActivity"> tools:context=".ControlActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1"> android:layout_weight="1">
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="32dp"
android:layout_marginLeft="32dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button <Button
android:id="@+id/button_rotatex" android:id="@+id/button_rotatex"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="32dp" android:layout_weight="1"
android:layout_marginBottom="24dp"
android:paddingTop="24dp" android:paddingTop="24dp"
android:paddingBottom="24dp" android:paddingBottom="24dp"
android:layout_margin="16dp"
android:text="Rotate X" android:text="Rotate X"
app:layout_constraintBottom_toTopOf="@+id/button_rotatey" app:layout_constraintBottom_toTopOf="@+id/button_rotatey"
app:layout_constraintEnd_toStartOf="@+id/button_climb" app:layout_constraintEnd_toStartOf="@+id/button_climb"
@ -30,27 +48,32 @@
<Button <Button
android:id="@+id/button_climb" android:id="@+id/button_climb"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="24dp" android:layout_weight="1"
android:layout_marginBottom="24dp"
android:paddingTop="24dp" android:paddingTop="24dp"
android:paddingBottom="24dp" android:paddingBottom="24dp"
android:layout_margin="16dp"
android:text="Climb" android:text="Climb"
app:layout_constraintBottom_toTopOf="@+id/button_twist" app:layout_constraintBottom_toTopOf="@+id/button_twist"
app:layout_constraintStart_toEndOf="@+id/button_rotatex" app:layout_constraintStart_toEndOf="@+id/button_rotatex"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" /> app:layout_constraintVertical_chainStyle="packed" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button <Button
android:id="@+id/button_rotatey" android:id="@+id/button_rotatey"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="32dp" android:layout_weight="1"
android:layout_marginBottom="24dp"
android:paddingTop="24dp" android:paddingTop="24dp"
android:paddingBottom="24dp" android:paddingBottom="24dp"
android:layout_margin="16dp"
android:text="Rotate Y" android:text="Rotate Y"
app:layout_constraintBottom_toTopOf="@+id/button_rotatez" app:layout_constraintBottom_toTopOf="@+id/button_rotatez"
app:layout_constraintEnd_toStartOf="@+id/button_twist" app:layout_constraintEnd_toStartOf="@+id/button_twist"
@ -59,24 +82,31 @@
<Button <Button
android:id="@+id/button_twist" android:id="@+id/button_twist"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="24dp" android:layout_weight="1"
android:layout_marginBottom="24dp"
android:paddingTop="24dp" android:paddingTop="24dp"
android:paddingBottom="24dp" android:paddingBottom="24dp"
android:layout_margin="16dp"
android:text="Twist" android:text="Twist"
app:layout_constraintBottom_toTopOf="@+id/button6" app:layout_constraintBottom_toTopOf="@+id/button6"
app:layout_constraintStart_toEndOf="@+id/button_rotatey" app:layout_constraintStart_toEndOf="@+id/button_rotatey"
app:layout_constraintTop_toBottomOf="@+id/button_climb" /> app:layout_constraintTop_toBottomOf="@+id/button_climb" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button <Button
android:id="@+id/button_rotatez" android:id="@+id/button_rotatez"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="32dp" android:layout_weight="1"
android:paddingTop="24dp" android:paddingTop="24dp"
android:paddingBottom="24dp" android:paddingBottom="24dp"
android:layout_margin="16dp"
android:text="Rotate Z" android:text="Rotate Z"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button6" app:layout_constraintEnd_toStartOf="@+id/button6"
@ -85,17 +115,21 @@
<Button <Button
android:id="@+id/button6" android:id="@+id/button6"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="24dp" android:layout_weight="1"
android:paddingTop="24dp" android:paddingTop="24dp"
android:paddingBottom="24dp" android:paddingBottom="24dp"
android:layout_margin="16dp"
android:text="Button" android:text="Button"
android:visibility="invisible" android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/button_rotatez" app:layout_constraintStart_toEndOf="@+id/button_rotatez"
app:layout_constraintTop_toBottomOf="@+id/button_twist" /> app:layout_constraintTop_toBottomOf="@+id/button_twist" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
@ -118,4 +152,26 @@
app:srcCompat="@drawable/ic_control_circle" /> app:srcCompat="@drawable/ic_control_circle" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout> </LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:visibility="gone">
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Loading…
Cancel
Save