add GTFS static data
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
package org.pupes.mhdrunpathfinder
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlinx.serialization.Serializable
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import ovh.plrapps.mapcompose.api.addMarker
|
||||
import java.net.URL
|
||||
|
||||
@Serializable
|
||||
data class GTFSRoute(
|
||||
val agency_id: String,
|
||||
val is_night: Boolean,
|
||||
val route_color: String,
|
||||
val route_desc: String? = null,
|
||||
val route_id: String,
|
||||
val route_long_name: String,
|
||||
val route_short_name: String,
|
||||
val route_text_color: String,
|
||||
val route_type: Int,
|
||||
val route_url: String,
|
||||
val is_regional: Boolean,
|
||||
val is_substitute_transport: Boolean
|
||||
)
|
||||
|
||||
// Type alias for array of GTFS routes
|
||||
typealias GTFSRoutes = List<GTFSRoute>
|
||||
|
||||
var gtfsRoutes: GTFSRoutes? = null
|
||||
|
||||
@Serializable
|
||||
data class GTFSStopGeometry(
|
||||
val coordinates: List<Double>,
|
||||
val type: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class GTFSStopProperties(
|
||||
val location_type: Int,
|
||||
val parent_station: String? = null,
|
||||
val platform_code: String? = null,
|
||||
val stop_id: String,
|
||||
val stop_name: String,
|
||||
val wheelchair_boarding: Int,
|
||||
val zone_id: String? = null,
|
||||
val level_id: String? = null
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class GTFSStop(
|
||||
val geometry: GTFSStopGeometry,
|
||||
val properties: GTFSStopProperties,
|
||||
val type: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class GTFSStops(
|
||||
val features: List<GTFSStop>,
|
||||
val type: String
|
||||
)
|
||||
|
||||
var gtfsStops: GTFSStops? = null
|
||||
|
||||
|
||||
// OkHttpClient for making HTTP requests
|
||||
val gtfshttpClient = OkHttpClient.Builder()
|
||||
.build()
|
||||
|
||||
private val GTFSMainHandler = Handler(Looper.getMainLooper())
|
||||
private var GTFSTrackingThread: Thread? = null
|
||||
private var GTFSIsTracking = false
|
||||
|
||||
private fun fetchGTFSPosition(gtfsUrl: URL,api_key: String, callee: Activity) {
|
||||
try {
|
||||
|
||||
val request = Request.Builder()
|
||||
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${"/"}")
|
||||
.header("User-Agent", "${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)")
|
||||
.build()
|
||||
|
||||
gtfshttpClient.newCall(request).execute().use { response ->
|
||||
var message = if (response.isSuccessful) {
|
||||
response.body?.string() ?: "Empty response"
|
||||
} else {
|
||||
"Error: ${response.code} ${response.message}"
|
||||
}
|
||||
|
||||
// TODO: Handle response
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// Post UI update to main thread
|
||||
GTFSMainHandler.post {
|
||||
Toast.makeText(callee, e.message ?: "Unknown error", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start the timer with a URL:
|
||||
fun startGTFSTracking(url: String, api_key: String, callee: Activity) {
|
||||
if (GTFSIsTracking) return
|
||||
|
||||
GTFSIsTracking = true
|
||||
GTFSTrackingThread = Thread {
|
||||
var gtfsUrl = URL(url);
|
||||
try {
|
||||
if(gtfsRoutes == null) {
|
||||
val request = Request.Builder()
|
||||
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.routes_path)}")
|
||||
.header(
|
||||
"User-Agent",
|
||||
"${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)"
|
||||
)
|
||||
.header("X-Access-Token", api_key).build();
|
||||
|
||||
gtfshttpClient.newCall(request).execute().use { response ->
|
||||
var message = if (response.isSuccessful) {
|
||||
response.body?.string() ?: "Empty response"
|
||||
} else {
|
||||
"Error: ${response.code} ${response.message}"
|
||||
}
|
||||
gtfsRoutes = json.decodeFromString<GTFSRoutes>(message)
|
||||
}}
|
||||
if(gtfsStops == null) {
|
||||
val request = Request.Builder()
|
||||
.url("${gtfsUrl.protocol}://${gtfsUrl.host}${callee.getString(R.string.stops_path)}")
|
||||
.header(
|
||||
"User-Agent",
|
||||
"${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} (Android)"
|
||||
)
|
||||
.header("X-Access-Token", api_key).build();
|
||||
|
||||
gtfshttpClient.newCall(request).execute().use { response ->
|
||||
var message = if (response.isSuccessful) {
|
||||
response.body?.string() ?: "Empty response"
|
||||
} else {
|
||||
"Error: ${response.code} ${response.message}"
|
||||
}
|
||||
gtfsStops = json.decodeFromString<GTFSStops>(message)
|
||||
|
||||
// put all stops on the map
|
||||
for (stop in gtfsStops!!.features) {
|
||||
GTFSMainHandler.post {
|
||||
mapState.addMarker(stop.properties.stop_id, x = longitudeToXNormalized(stop.geometry.coordinates[0]), y = latitudeToYNormalized(stop.geometry.coordinates[0])) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.outline_directions_bus_24),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(17.dp),
|
||||
tint = Color(0xFF0000FF)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}}
|
||||
} catch(e: Exception) {
|
||||
Toast.makeText(callee, e.message ?: "Unknown error", Toast.LENGTH_SHORT).show()
|
||||
GTFSMainHandler.post { stopGTFSTracking() }
|
||||
return@Thread
|
||||
}
|
||||
while (GTFSIsTracking) {
|
||||
fetchGTFSPosition(gtfsUrl,api_key, callee)
|
||||
try {
|
||||
Thread.sleep(1000) // Wait 1 second between requests
|
||||
} catch (e: InterruptedException) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}.apply { start() }
|
||||
}
|
||||
|
||||
fun stopGTFSTracking() {
|
||||
GTFSIsTracking = false
|
||||
GTFSTrackingThread?.interrupt()
|
||||
GTFSTrackingThread = null
|
||||
}
|
||||
Reference in New Issue
Block a user