Added zipped TLE import from Mike McCants' web site

This commit is contained in:
Arty Bishop committed 2021-03-06 18:10:15 +00:00
1 parent 0dfd7e6250
commit 44910389e3
2 files changed
+34 -14

No files matched your search

@@ -121,7 +121,9 @@ class PrefsRepo @Inject constructor(val preferences: SharedPreferences, val mosh
private fun loadDefaultSources(): List<TleSource> { private fun loadDefaultSources(): List<TleSource> {
return listOf( return listOf(
TleSource("https://celestrak.com/NORAD/elements/active.txt"), TleSource("https://celestrak.com/NORAD/elements/active.txt"),
TleSource("https://amsat.org/tle/current/nasabare.txt") TleSource("https://amsat.org/tle/current/nasabare.txt"),
TleSource("https://www.prismnet.com/~mmccants/tles/classfd.zip"),
TleSource("https://www.prismnet.com/~mmccants/tles/inttles.zip")
) )
} }
} }
@@ -34,6 +34,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.InputStream import java.io.InputStream
import java.util.zip.ZipInputStream
import javax.inject.Inject import javax.inject.Inject
class SatelliteRepo @Inject constructor( class SatelliteRepo @Inject constructor(
@@ -59,8 +60,7 @@ class SatelliteRepo @Inject constructor(
withContext(ioDispatcher) { withContext(ioDispatcher) {
runCatching { runCatching {
resolver.openInputStream(uri)?.use { stream -> resolver.openInputStream(uri)?.use { stream ->
val importedEntries = TLE.importSat(stream).map { tle -> SatEntry(tle) } updateAndRestoreSelection(importEntriesFromStreams(listOf(stream)))
updateAndRestoreSelection(importedEntries)
} }
} }
} }
@@ -68,17 +68,9 @@ class SatelliteRepo @Inject constructor(
suspend fun updateEntriesFromWeb(sources: List<TleSource>) { suspend fun updateEntriesFromWeb(sources: List<TleSource>) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
val streams = mutableListOf<InputStream>() val streams = getStreamsForSources(sources)
sources.forEach { source -> val entries = importEntriesFromStreams(streams)
val stream = satelliteService.fetchFile(source.url).body()?.byteStream() updateAndRestoreSelection(entries)
stream?.let { inputStream -> streams.add(inputStream) }
}
val importedEntries = mutableListOf<SatEntry>()
streams.forEach { stream ->
val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) }
importedEntries.addAll(entries)
}
updateAndRestoreSelection(importedEntries)
} }
} }
@@ -90,6 +82,32 @@ class SatelliteRepo @Inject constructor(
satelliteDao.insertTransmitters(satelliteService.fetchTransmitters()) satelliteDao.insertTransmitters(satelliteService.fetchTransmitters())
} }
private suspend fun getStreamsForSources(sources: List<TleSource>): List<InputStream> {
val streams = mutableListOf<InputStream>()
sources.forEach { tleSource ->
satelliteService.fetchFile(tleSource.url).body()?.byteStream()?.let { inputStream ->
if (tleSource.url.contains(".zip")) {
// Handle zip stream
val zipInputStream = ZipInputStream(inputStream)
val zipEntry = zipInputStream.nextEntry
if (zipEntry != null && !zipEntry.isDirectory) streams.add(zipInputStream)
} else {
streams.add(inputStream)
}
}
}
return streams
}
private fun importEntriesFromStreams(streams: List<InputStream>): List<SatEntry> {
val importedEntries = mutableListOf<SatEntry>()
streams.forEach { stream ->
val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) }
importedEntries.addAll(entries)
}
return importedEntries
}
private suspend fun updateAndRestoreSelection(entries: List<SatEntry>) { private suspend fun updateAndRestoreSelection(entries: List<SatEntry>) {
val selectedCatNums = satelliteDao.getSelectedCatNums() val selectedCatNums = satelliteDao.getSelectedCatNums()
satelliteDao.insertEntries(entries) satelliteDao.insertEntries(entries)