connection to FlightGear and design
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.example.flightgearcontrollerapp.model
|
||||
|
||||
|
||||
class FGModel {
|
||||
private val telnetClient = TelnetClient()
|
||||
private var isExceptionOccurred = false
|
||||
|
||||
//private val joystickView = JoystickView()
|
||||
private var isTouchingJoystick = false
|
||||
|
||||
/*
|
||||
private val jsCenterX = 0
|
||||
private val jsCenterY = 0
|
||||
private val jsOuterRadius = 0
|
||||
private val jsInnerRadius = 0
|
||||
*/
|
||||
|
||||
|
||||
fun connect(ipAddress: String, portAddress: String): Boolean {
|
||||
var res = false
|
||||
val t = Thread {
|
||||
try {
|
||||
res = telnetClient.connect(ipAddress, portAddress.toInt())
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
}
|
||||
val h = Thread.UncaughtExceptionHandler { _, _ -> isExceptionOccurred = true;res= false }
|
||||
t.uncaughtExceptionHandler = h
|
||||
t.start()
|
||||
try {
|
||||
t.join()
|
||||
if (!isExceptionOccurred) {
|
||||
this.isTouchingJoystick = false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return false
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
telnetClient.disconnect()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.example.flightgearcontrollerapp.model
|
||||
|
||||
class FG_model {
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.flightgearcontrollerapp.model
|
||||
|
||||
import java.io.PrintWriter
|
||||
import java.net.Socket
|
||||
|
||||
class TelnetClient{
|
||||
|
||||
private lateinit var client:Socket
|
||||
private lateinit var output:PrintWriter
|
||||
fun connect(ipAddress: String, portAddress: Int): Boolean {
|
||||
return try {
|
||||
client = Socket(ipAddress, portAddress)
|
||||
output = PrintWriter(client.getOutputStream(), true)
|
||||
true
|
||||
}catch (e: Exception){
|
||||
e.printStackTrace()
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
try{
|
||||
client.close()
|
||||
}catch (e: Exception){
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.example.flightgearcontrollerapp.view_model
|
||||
|
||||
class VM_connection {
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.flightgearcontrollerapp.view_model
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.example.flightgearcontrollerapp.model.FGModel
|
||||
|
||||
class FGViewModel : ViewModel() {
|
||||
private val model = FGModel()
|
||||
fun connectToFG(ipAddress: String, portAddress: String): Boolean{
|
||||
if(model.connect(ipAddress,portAddress)){
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun disconnectFromFG() {
|
||||
model.disconnect()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.flightgearcontrollerapp.views
|
||||
|
||||
class JoystickView{
|
||||
}
|
||||
@@ -1,12 +1,64 @@
|
||||
package com.example.flightgearcontrollerapp.views
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.example.flightgearcontrollerapp.R
|
||||
import com.example.flightgearcontrollerapp.R.id.*
|
||||
import com.example.flightgearcontrollerapp.view_model.FGViewModel
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
//private var vmConnection: FGViewModel = FGViewModel()
|
||||
private var isConnected = false
|
||||
private val vmConnection = FGViewModel()
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
}
|
||||
}
|
||||
|
||||
fun onClickConnect(view: View) {
|
||||
if (view.id == btn_connect) {
|
||||
val btnConnection = findViewById<Button>(btn_connect)
|
||||
if (!isConnected) {
|
||||
val editTextIP = findViewById<EditText>(ip_address)
|
||||
val editTextPort = findViewById<EditText>(port_address)
|
||||
val builder = AlertDialog.Builder(this)
|
||||
builder.setTitle(getString(R.string.alert_dialog_flightgear_connection))
|
||||
builder.setMessage(getString(R.string.alert_instruction))
|
||||
builder.setPositiveButton(getString(R.string.ok_btn)){ dialog, _ ->
|
||||
if ((editTextIP.text.toString() !="")&&(editTextPort.text.toString()!="")&&(vmConnection.connectToFG(editTextIP.text.toString(),editTextPort.text.toString()))) {
|
||||
btnConnection.setText(R.string.disconnect)
|
||||
isConnected = true
|
||||
} else {
|
||||
val conFailed = AlertDialog.Builder(this)
|
||||
conFailed.setTitle(getString(R.string.alert_title))
|
||||
conFailed.setMessage(getString(R.string.connection_failed))
|
||||
val alertFail = conFailed.create()
|
||||
alertFail.show()
|
||||
}
|
||||
dialog.cancel()
|
||||
}
|
||||
builder.setNegativeButton(getString(android.R.string.cancel)){ dialog, _ ->
|
||||
dialog.cancel()
|
||||
}
|
||||
val alert = builder.create()
|
||||
alert.show()
|
||||
|
||||
} else {
|
||||
vmConnection.disconnectFromFG()
|
||||
btnConnection.setText(R.string.btn_connect)
|
||||
isConnected = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user