update
This commit is contained in:
parent
6d25b649ab
commit
a1b80ad5f9
@ -3,17 +3,15 @@ package com.rookiedev.hexapod
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.AlertDialog
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.*
|
||||
import android.widget.ImageView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import com.rookiedev.hexapod.network.TCPClient
|
||||
import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished
|
||||
import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived
|
||||
import com.rookiedev.hexapod.network.TCPClient.OnDisconnected
|
||||
import com.rookiedev.hexapod.network.TCPClient.*
|
||||
import kotlinx.coroutines.*
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.atan2
|
||||
@ -75,12 +73,13 @@ class ControlActivity : AppCompatActivity() {
|
||||
private var radius = 0f
|
||||
|
||||
private var tcpClient: TCPClient? = null
|
||||
private var ip:String = ""
|
||||
private var ip: String = ""
|
||||
private var port = 0
|
||||
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.IO)
|
||||
|
||||
private var currentState: String = "standby"
|
||||
private lateinit var progressBar: ConstraintLayout
|
||||
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@ -96,6 +95,7 @@ class ControlActivity : AppCompatActivity() {
|
||||
controlWindowInsets(true)
|
||||
|
||||
val controlCircle = findViewById<ImageView>(R.id.control_image)
|
||||
progressBar = findViewById<ConstraintLayout>(R.id.progressBar)
|
||||
|
||||
val vto: ViewTreeObserver = controlCircle.viewTreeObserver
|
||||
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?) {
|
||||
if (message == null) {
|
||||
// alertDialog(DISCONNECTED)
|
||||
@ -201,10 +234,14 @@ class ControlActivity : AppCompatActivity() {
|
||||
override fun onConnected() {
|
||||
// udpClient.start()
|
||||
println("connected")
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
progressBar.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}, object : OnDisconnected{
|
||||
}, object : OnDisconnected {
|
||||
override fun onDisconnected() {
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
progressBar.visibility = View.GONE
|
||||
alertDialog(0)
|
||||
}
|
||||
}
|
||||
@ -212,7 +249,16 @@ class ControlActivity : AppCompatActivity() {
|
||||
)
|
||||
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) {
|
||||
0 -> {
|
||||
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.setButton(AlertDialog.BUTTON_POSITIVE,
|
||||
@ -256,7 +303,6 @@ class ControlActivity : AppCompatActivity() {
|
||||
}
|
||||
alert.show()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,17 +1,11 @@
|
||||
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
|
||||
import java.net.*
|
||||
|
||||
|
||||
class TCPClient(
|
||||
c: ControlActivity,
|
||||
ip: String?,
|
||||
port: Int,
|
||||
messagelistener: OnMessageReceived?,
|
||||
@ -19,21 +13,17 @@ class TCPClient(
|
||||
ondisconnect: OnDisconnected?
|
||||
) :
|
||||
Thread() {
|
||||
private val controller: ControlActivity = c
|
||||
private var TCPSocket:Socket? = null
|
||||
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 onDisconnected: OnDisconnected?=null
|
||||
private var onDisconnected: OnDisconnected? = 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()
|
||||
@ -41,43 +31,26 @@ class TCPClient(
|
||||
this.TCPSocket!!.connect(serverAddr, 3000) // connecting socket and set timeout in 3s
|
||||
onConnected!!.onConnected()
|
||||
TCPOut = PrintWriter(
|
||||
BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())),
|
||||
true
|
||||
)
|
||||
BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())),
|
||||
true
|
||||
)
|
||||
TCPIn = BufferedReader(InputStreamReader(this.TCPSocket!!.getInputStream()))
|
||||
// sendMessage("test")
|
||||
isConnected = true
|
||||
while(isConnected)
|
||||
{
|
||||
// if (isNewData)
|
||||
// {
|
||||
// if (TCPOut != null && !TCPOut!!.checkError()) {
|
||||
// TCPOut!!.println(newMessage)
|
||||
// TCPOut!!.flush()
|
||||
// }
|
||||
// isNewData = false
|
||||
// }else{
|
||||
// sleep(100)
|
||||
// }
|
||||
while (isConnected) {
|
||||
sleep(1000)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
} catch (e: InterruptedException) {
|
||||
// controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT)
|
||||
println("unable to connect")
|
||||
println(e)
|
||||
// controller.alertDialog(0)
|
||||
// onDisconnected!!.onDisconnected()
|
||||
} catch (e: SocketTimeoutException) {
|
||||
println(e)
|
||||
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
|
||||
*
|
||||
@ -86,17 +59,20 @@ class TCPClient(
|
||||
fun sendMessage(message: String?) {
|
||||
// newMessage = message
|
||||
// isNewData = true
|
||||
if (TCPOut != null && !TCPOut!!.checkError()) {
|
||||
TCPOut!!.println(message)
|
||||
TCPOut!!.flush()
|
||||
|
||||
if (this.TCPOut != null && !this.TCPOut!!.checkError()) {
|
||||
println("send message")
|
||||
this.TCPOut!!.println(message)
|
||||
this.TCPOut!!.flush()
|
||||
}
|
||||
}
|
||||
|
||||
// fun stopClient() {
|
||||
fun stopClient() {
|
||||
// sendMessage(Constants.requestMessage(Constants.REQUEST_DISCONNECT))
|
||||
// pause = true
|
||||
// isConnected = false
|
||||
// }
|
||||
pause = true
|
||||
isConnected = false
|
||||
this.TCPSocket!!.close()
|
||||
}
|
||||
|
||||
interface OnMessageReceived {
|
||||
fun messageReceived(message: String?)
|
||||
@ -124,4 +100,5 @@ class TCPClient(
|
||||
}
|
||||
pause = false
|
||||
}
|
||||
|
||||
}
|
@ -1,121 +1,177 @@
|
||||
<?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:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ControlActivity">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_rotatex"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:text="Rotate X"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button_rotatey"
|
||||
app:layout_constraintEnd_toStartOf="@+id/button_climb"
|
||||
app:layout_constraintHorizontal_chainStyle="packed"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_chainStyle="packed" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_climb"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:text="Climb"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button_twist"
|
||||
app:layout_constraintStart_toEndOf="@+id/button_rotatex"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_chainStyle="packed" />
|
||||
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_rotatey"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:text="Rotate Y"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button_rotatez"
|
||||
app:layout_constraintEnd_toStartOf="@+id/button_twist"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_rotatex" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_twist"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:text="Twist"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button6"
|
||||
app:layout_constraintStart_toEndOf="@+id/button_rotatey"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_climb" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_rotatez"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="32dp"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:text="Rotate Z"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/button6"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_rotatey" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button6"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="24dp"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:text="Button"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/button_rotatez"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_twist" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/control_image"
|
||||
android:layout_width="match_parent"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginTop="32dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
android:layout_marginBottom="32dp"
|
||||
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
|
||||
android:id="@+id/button_rotatex"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:layout_margin="16dp"
|
||||
android:text="Rotate X"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button_rotatey"
|
||||
app:layout_constraintEnd_toStartOf="@+id/button_climb"
|
||||
app:layout_constraintHorizontal_chainStyle="packed"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_chainStyle="packed" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_climb"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:layout_margin="16dp"
|
||||
android:text="Climb"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button_twist"
|
||||
app:layout_constraintStart_toEndOf="@+id/button_rotatex"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_chainStyle="packed" />
|
||||
|
||||
</TableRow>
|
||||
|
||||
<TableRow
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_rotatey"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:layout_margin="16dp"
|
||||
android:text="Rotate Y"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button_rotatez"
|
||||
app:layout_constraintEnd_toStartOf="@+id/button_twist"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_rotatex" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_twist"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:layout_margin="16dp"
|
||||
android:text="Twist"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button6"
|
||||
app:layout_constraintStart_toEndOf="@+id/button_rotatey"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_climb" />
|
||||
|
||||
</TableRow>
|
||||
|
||||
<TableRow
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button_rotatez"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:layout_margin="16dp"
|
||||
android:text="Rotate Z"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/button6"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_rotatey" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button6"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp"
|
||||
android:layout_margin="16dp"
|
||||
android:text="Button"
|
||||
android:visibility="invisible"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/button_rotatez"
|
||||
app:layout_constraintTop_toBottomOf="@+id/button_twist" />
|
||||
|
||||
</TableRow>
|
||||
|
||||
</TableLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/control_image"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginTop="32dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
android:layout_marginBottom="32dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:srcCompat="@drawable/ic_control_circle" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</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"
|
||||
app:srcCompat="@drawable/ic_control_circle" />
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
Loading…
x
Reference in New Issue
Block a user