mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Extracted station position management to a separate class
This commit is contained in:
1 parent
5ef79148bd
commit
1a59b5e3e1
31 files changed
+233
-502
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package com.rtbishop.look4sat.data
|
||||
|
||||
import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
|
||||
import com.rtbishop.look4sat.domain.predict4kotlin.StationPosition
|
||||
|
||||
class LocationRepo(
|
||||
private val locationSource: LocationSource,
|
||||
private val qthConverter: QthConverter
|
||||
) {
|
||||
|
||||
fun getStationPosition(): StationPosition {
|
||||
return locationSource.loadStationPosition()
|
||||
}
|
||||
|
||||
fun getMagDeclination(): Float {
|
||||
return locationSource.getMagDeclination(getStationPosition())
|
||||
}
|
||||
|
||||
fun positionToQTH(lat: Double, lon: Double): String? {
|
||||
return qthConverter.positionToQTH(lat, lon)
|
||||
}
|
||||
|
||||
fun updatePositionFromGPS(): Boolean {
|
||||
val stationPosition = locationSource.getLastKnownLocation() ?: return false
|
||||
locationSource.saveStationPosition(stationPosition)
|
||||
return true
|
||||
}
|
||||
|
||||
fun updatePositionFromQTH(qthString: String): Boolean {
|
||||
val position = qthConverter.qthToPosition(qthString) ?: return false
|
||||
val stationPosition = StationPosition(position.latitude, position.longitude, 0.0)
|
||||
locationSource.saveStationPosition(stationPosition)
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.rtbishop.look4sat.data
|
||||
|
||||
import com.rtbishop.look4sat.domain.predict4kotlin.StationPosition
|
||||
|
||||
interface LocationSource {
|
||||
|
||||
fun getLastKnownLocation(): StationPosition?
|
||||
|
||||
fun getMagDeclination(position: StationPosition): Float
|
||||
|
||||
fun loadStationPosition(): StationPosition
|
||||
|
||||
fun saveStationPosition(position: StationPosition)
|
||||
}
|
||||
+8
-9
@@ -1,6 +1,5 @@
|
||||
package com.rtbishop.look4sat.data
|
||||
|
||||
import com.rtbishop.look4sat.domain.SatelliteRepo
|
||||
import com.rtbishop.look4sat.domain.model.SatEntry
|
||||
import com.rtbishop.look4sat.domain.model.SatItem
|
||||
import com.rtbishop.look4sat.domain.model.SatTrans
|
||||
@@ -13,30 +12,30 @@ import kotlinx.coroutines.withContext
|
||||
import java.io.InputStream
|
||||
import java.util.zip.ZipInputStream
|
||||
|
||||
class DefaultSatelliteRepo(
|
||||
class SatelliteRepo(
|
||||
private val localSource: LocalDataSource,
|
||||
private val remoteSource: RemoteDataSource,
|
||||
private val ioDispatcher: CoroutineDispatcher
|
||||
) : SatelliteRepo {
|
||||
) {
|
||||
|
||||
override fun getSatItems(): Flow<List<SatItem>> {
|
||||
fun getSatItems(): Flow<List<SatItem>> {
|
||||
return localSource.getSatItems()
|
||||
}
|
||||
|
||||
override fun getTransmittersForSat(catNum: Int): Flow<List<SatTrans>> {
|
||||
fun getTransmittersForSat(catNum: Int): Flow<List<SatTrans>> {
|
||||
return localSource.getTransmittersForSat(catNum)
|
||||
}
|
||||
|
||||
override suspend fun getSelectedSatellites(): List<Satellite> {
|
||||
suspend fun getSelectedSatellites(): List<Satellite> {
|
||||
return localSource.getSelectedSatellites()
|
||||
}
|
||||
|
||||
override suspend fun importDataFromStream(stream: InputStream) = withContext(ioDispatcher) {
|
||||
suspend fun importDataFromStream(stream: InputStream) = withContext(ioDispatcher) {
|
||||
val importedEntries = Satellite.importElements(stream).map { tle -> SatEntry(tle) }
|
||||
localSource.updateEntries(importedEntries)
|
||||
}
|
||||
|
||||
override suspend fun importDataFromWeb(sources: List<String>) {
|
||||
suspend fun importDataFromWeb(sources: List<String>) {
|
||||
coroutineScope {
|
||||
launch(ioDispatcher) {
|
||||
val importedEntries = mutableListOf<SatEntry>()
|
||||
@@ -64,7 +63,7 @@ class DefaultSatelliteRepo(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateEntriesSelection(catNums: List<Int>, isSelected: Boolean) {
|
||||
suspend fun updateEntriesSelection(catNums: List<Int>, isSelected: Boolean) {
|
||||
localSource.updateEntriesSelection(catNums, isSelected)
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.rtbishop.look4sat.domain
|
||||
|
||||
import com.rtbishop.look4sat.domain.model.SatItem
|
||||
import com.rtbishop.look4sat.domain.model.SatTrans
|
||||
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.io.InputStream
|
||||
|
||||
interface SatelliteRepo {
|
||||
|
||||
fun getSatItems(): Flow<List<SatItem>>
|
||||
|
||||
fun getTransmittersForSat(catNum: Int): Flow<List<SatTrans>>
|
||||
|
||||
suspend fun getSelectedSatellites(): List<Satellite>
|
||||
|
||||
suspend fun importDataFromStream(stream: InputStream)
|
||||
|
||||
suspend fun importDataFromWeb(sources: List<String>)
|
||||
|
||||
suspend fun updateEntriesSelection(catNums: List<Int>, isSelected: Boolean)
|
||||
}
|
||||
@@ -24,12 +24,12 @@ class QthConverter {
|
||||
fun qthToPosition(qthString: String): Position? {
|
||||
val trimmedQth = qthString.take(6)
|
||||
if (!isValidQTH(trimmedQth)) return null
|
||||
val lonFirst = (trimmedQth[0].toUpperCase().toInt() - 65) * 20
|
||||
val latFirst = (trimmedQth[1].toUpperCase().toInt() - 65) * 10
|
||||
val lonFirst = (trimmedQth[0].uppercaseChar().code - 65) * 20
|
||||
val latFirst = (trimmedQth[1].uppercaseChar().code - 65) * 10
|
||||
val lonSecond = trimmedQth[2].toString().toInt() * 2
|
||||
val latSecond = trimmedQth[3].toString().toInt()
|
||||
val lonThird = (((trimmedQth[4].toLowerCase().toInt() - 97) / 12.0) + (1.0 / 24.0)) - 180
|
||||
val latThird = (((trimmedQth[5].toLowerCase().toInt() - 97) / 24.0) + (1.0 / 48.0)) - 90
|
||||
val lonThird = (((trimmedQth[4].lowercaseChar().code - 97) / 12.0) + (1.0 / 24.0)) - 180
|
||||
val latThird = (((trimmedQth[5].lowercaseChar().code - 97) / 24.0) + (1.0 / 48.0)) - 90
|
||||
val longitude = (lonFirst + lonSecond + lonThird).roundToDecimals(4)
|
||||
val latitude = (latFirst + latSecond + latThird).roundToDecimals(4)
|
||||
return Position(latitude, longitude)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.rtbishop.look4sat.interactors
|
||||
|
||||
import com.rtbishop.look4sat.domain.SatelliteRepo
|
||||
import com.rtbishop.look4sat.data.SatelliteRepo
|
||||
import com.rtbishop.look4sat.domain.model.SatItem
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.rtbishop.look4sat.interactors
|
||||
|
||||
import com.rtbishop.look4sat.domain.SatelliteRepo
|
||||
import com.rtbishop.look4sat.data.SatelliteRepo
|
||||
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
|
||||
|
||||
class GetSelectedSatellites(private val satelliteRepo: SatelliteRepo) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.rtbishop.look4sat.interactors
|
||||
|
||||
import com.rtbishop.look4sat.domain.SatelliteRepo
|
||||
import com.rtbishop.look4sat.data.SatelliteRepo
|
||||
import com.rtbishop.look4sat.domain.model.SatTrans
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.rtbishop.look4sat.interactors
|
||||
|
||||
import com.rtbishop.look4sat.domain.SatelliteRepo
|
||||
import com.rtbishop.look4sat.data.SatelliteRepo
|
||||
import java.io.InputStream
|
||||
|
||||
class ImportDataFromStream(private val satelliteRepo: SatelliteRepo) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.rtbishop.look4sat.interactors
|
||||
|
||||
import com.rtbishop.look4sat.domain.SatelliteRepo
|
||||
import com.rtbishop.look4sat.data.SatelliteRepo
|
||||
|
||||
class ImportDataFromWeb(private val satelliteRepo: SatelliteRepo) {
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.rtbishop.look4sat.interactors
|
||||
|
||||
import com.rtbishop.look4sat.domain.SatelliteRepo
|
||||
import com.rtbishop.look4sat.data.SatelliteRepo
|
||||
|
||||
class UpdateEntriesSelection(private val satelliteRepo: SatelliteRepo) {
|
||||
|
||||
|
||||
@@ -21,40 +21,40 @@ import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
|
||||
import org.junit.Test
|
||||
|
||||
class QthConverterTest {
|
||||
|
||||
private val converter = QthConverter()
|
||||
|
||||
|
||||
private val qthConverter = QthConverter()
|
||||
|
||||
@Test
|
||||
fun `Given valid QTH returns correct location`() {
|
||||
var result = converter.qthToPosition("io91VL39FX")
|
||||
fun `Given valid QTH returns correct POS`() {
|
||||
var result = qthConverter.qthToPosition("io91VL39FX")
|
||||
assert(result?.latitude == 51.4792 && result.longitude == -0.2083)
|
||||
result = converter.qthToPosition("JN58TD")
|
||||
result = qthConverter.qthToPosition("JN58TD")
|
||||
assert(result?.latitude == 48.1458 && result.longitude == 11.6250)
|
||||
result = converter.qthToPosition("gf15vc")
|
||||
result = qthConverter.qthToPosition("gf15vc")
|
||||
assert(result?.latitude == -34.8958 && result.longitude == -56.2083)
|
||||
result = converter.qthToPosition("fm18LW")
|
||||
result = qthConverter.qthToPosition("fm18LW")
|
||||
assert(result?.latitude == 38.9375 && result.longitude == -77.0417)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `Given invalid QTH returns null`() {
|
||||
var result = converter.qthToPosition("ZZ00zz")
|
||||
var result = qthConverter.qthToPosition("ZZ00zz")
|
||||
assert(result == null)
|
||||
result = converter.qthToPosition("JN58tz")
|
||||
result = qthConverter.qthToPosition("JN58tz")
|
||||
assert(result == null)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `Given valid location returns correct QTH`() {
|
||||
assert(converter.positionToQTH(51.4878, -0.2146) == "IO91vl")
|
||||
assert(converter.positionToQTH(48.1466, 11.6083) == "JN58td")
|
||||
assert(converter.positionToQTH(-34.91, -56.2116) == "GF15vc")
|
||||
assert(converter.positionToQTH(38.92, -77.065) == "FM18lw")
|
||||
fun `Given valid POS returns correct QTH`() {
|
||||
assert(qthConverter.positionToQTH(51.4878, -0.2146) == "IO91vl")
|
||||
assert(qthConverter.positionToQTH(48.1466, 11.6083) == "JN58td")
|
||||
assert(qthConverter.positionToQTH(-34.91, -56.2116) == "GF15vc")
|
||||
assert(qthConverter.positionToQTH(38.92, -77.065) == "FM18lw")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given invalid location returns null`() {
|
||||
assert(converter.positionToQTH(91.0542, -170.1142) == null)
|
||||
assert(converter.positionToQTH(89.0542, -240.1142) == null)
|
||||
assert(qthConverter.positionToQTH(91.0542, -170.1142) == null)
|
||||
assert(qthConverter.positionToQTH(89.0542, -240.1142) == null)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user