Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
Current flight latitude/longitude functional
Browse files Browse the repository at this point in the history
  • Loading branch information
bachhuberdesign committed May 28, 2018
1 parent e3fddb9 commit 33b99f5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 49 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bachhuberdesign.xplaneopenmap.data

import io.milkcan.effortlessandroid.d
import io.milkcan.effortlessandroid.e
import java.net.DatagramPacket
import java.net.DatagramSocket
Expand All @@ -13,18 +14,19 @@ open class UDPListener(private val callback: UDPCallback) : Thread() {
private var isKeepRunning = true

override fun run() {
val packet = DatagramPacket(ByteArray(MAX_UDP_DATAGRAM_LENGTH), MAX_UDP_DATAGRAM_LENGTH)
var packet = DatagramPacket(ByteArray(64), MAX_UDP_DATAGRAM_LENGTH)

var socket: DatagramSocket? = null

try {
socket = DatagramSocket(48001)

d("Listening for packets on port: 48001")
while (isKeepRunning) {
socket.receive(packet)
callback.onNextPacket(packet)
}
} catch (ex: Exception) {
} catch (ex: Throwable) {
e("Error handling datagram: ${ex.message}", ex)
} finally {
socket?.close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.bachhuberdesign.xplaneopenmap.data.model

data class FlightPathWrapper(val latitude: Float, val longitude: Float)
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.bachhuberdesign.xplaneopenmap.data.viewmodel

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import com.bachhuberdesign.xplaneopenmap.data.UDPCallback
import com.bachhuberdesign.xplaneopenmap.data.UDPListener
import com.bachhuberdesign.xplaneopenmap.data.model.FlightPathWrapper
import io.milkcan.effortlessandroid.d
import java.net.DatagramPacket
import java.util.*
import java.nio.ByteOrder.LITTLE_ENDIAN
import java.nio.ByteBuffer

class FlightMapViewModel : ViewModel(), UDPCallback {

private val flightPathLiveData: MutableLiveData<FlightPathWrapper> = MutableLiveData()
private val messageLiveData: MutableLiveData<String> = MutableLiveData()
private var udpListener: UDPListener? = null

override fun onCleared() {
udpListener?.kill()

super.onCleared()
}

override fun onNextPacket(packet: DatagramPacket) {
val data = packet.data

d("Packet received: ${Arrays.toString(data)}")

val dataSetIndex: Int = data[5].toInt()
val floatArray = getFloatsFromDataPacket(data)

when (dataSetIndex) {
20 -> handleLatitudeLongitudeAltitude(floatArray)
21 -> handleLocationVelocityDistanceTravelled(floatArray)
22 -> handleAllPlanesLatitude(floatArray)
23 -> handleAllPlanesLongitude(floatArray)
}
}

fun startUDPClient() {
udpListener = UDPListener(this)
udpListener!!.start()
}

fun getFlightPathStream(): LiveData<FlightPathWrapper> = flightPathLiveData

fun getMessageStream(): LiveData<String> = messageLiveData

private fun handleLatitudeLongitudeAltitude(floats: FloatArray) {
floats.forEachIndexed { i, float -> d("handleLatitudeLongitudeAltitude $i: $float") }

val latitude = floats[0]
val longitude = floats[1]

flightPathLiveData.postValue(FlightPathWrapper(latitude, longitude))
}

private fun handleLocationVelocityDistanceTravelled(floats: FloatArray) {

}

private fun handleAllPlanesLatitude(floats: FloatArray) {

}

private fun handleAllPlanesLongitude(floats: FloatArray) {

}

private fun getFloatsFromDataPacket(data: ByteArray): FloatArray {
return arrayOf(
byteArrayOf(data[9], data[10], data[11], data[12]),
byteArrayOf(data[13], data[14], data[15], data[16]),
byteArrayOf(data[17], data[18], data[19], data[20]),
byteArrayOf(data[21], data[22], data[23], data[24]),
byteArrayOf(data[25], data[26], data[27], data[28]),
byteArrayOf(data[29], data[30], data[31], data[32]),
byteArrayOf(data[33], data[34], data[35], data[36]),
byteArrayOf(data[37], data[38], data[39], data[40])
)
.map { ByteBuffer.wrap(it).order(LITTLE_ENDIAN).float }
.toFloatArray()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@ package com.bachhuberdesign.xplaneopenmap.ui

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.DialogInterface
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.bachhuberdesign.xplaneopenmap.data.FlightMapViewModel
import com.bachhuberdesign.xplaneopenmap.data.UDPCallback
import com.bachhuberdesign.xplaneopenmap.data.UDPListener
import com.bachhuberdesign.xplaneopenmap.data.viewmodel.FlightMapViewModel

import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import io.milkcan.effortlessandroid.d
import io.milkcan.effortlessandroid.toastLong
import xplaneopenmap.bachhuberdesign.com.openmap.R
import java.net.DatagramPacket

class FlightMapActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var map: GoogleMap
private lateinit var viewModel: FlightMapViewModel

private var cameraMoved: Boolean = false
private var dialog: AlertDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -49,18 +48,14 @@ class FlightMapActivity : AppCompatActivity(), OnMapReadyCallback {

override fun onMapReady(googleMap: GoogleMap) {
map = googleMap

val sydney = LatLng(-34.0, 151.0)
map.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
map.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}

private fun showIpAddressDialog() {
if (dialog != null && dialog!!.isShowing) {
dialog?.dismiss()
}

val ipAddress = AndroidHelper.getLocalIpAddress()
val ipAddress = AndroidHelper.getDeviceLocalIpAddress()

val message = if (!ipAddress.isNullOrBlank()) {
"Please set your X-Plane data export to your device's IP address: $ipAddress"
Expand All @@ -83,7 +78,18 @@ class FlightMapActivity : AppCompatActivity(), OnMapReadyCallback {
viewModel.getMessageStream().observe(this, Observer { toastLong("$it") })

viewModel.getFlightPathStream().observe(this, Observer {
// TODO: Handle display of flight location data
val position = LatLng(it!!.latitude.toDouble(), it.longitude.toDouble())
d("Flight path position: $position")
val cameraUpdate = CameraUpdateFactory.newLatLngZoom(position, 12.0f)

map.clear()

if (!cameraMoved){
map.moveCamera(cameraUpdate)
cameraMoved = true
}

map.addMarker(MarkerOptions().position(position).title("Current Flight"))
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.net.NetworkInterface
object AndroidHelper {

@JvmStatic
fun getLocalIpAddress(): String? {
fun getDeviceLocalIpAddress(): String? {
try {
val interfaces = NetworkInterface.getNetworkInterfaces()

Expand Down

0 comments on commit 33b99f5

Please sign in to comment.