Extracted rotation data reporting to a separate class

This commit is contained in:
Arty Bishop committed 2021-10-27 21:47:02 +01:00
1 parent f3d64d23b2
commit 8c7e2c9bb0
4 files changed
+71 -38

No files matched your search

@@ -0,0 +1,34 @@
package com.rtbishop.look4sat.framework
import com.rtbishop.look4sat.data.DataReporter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import java.io.BufferedWriter
import java.net.Socket
class NetDataReporter(private val reporterDispatcher: CoroutineDispatcher) : DataReporter {
private var rotatorSocket: Socket? = null
private var rotatorWriter: BufferedWriter? = null
override suspend fun setRotatorSocket(server: String, port: Int) {
withContext(reporterDispatcher) {
runCatching {
rotatorSocket = Socket(server, port)
rotatorWriter = rotatorSocket?.getOutputStream()?.bufferedWriter()
}.onFailure { println(it.localizedMessage) }
}
}
override suspend fun reportRotation(azim: Double, elev: Double) {
withContext(reporterDispatcher) {
runCatching {
rotatorWriter?.let { writer ->
writer.write("\\set_pos $azim $elev")
writer.newLine()
writer.flush()
}
}.onFailure { println(it.localizedMessage) }
}
}
}
@@ -21,11 +21,9 @@ import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.location.LocationManager import android.location.LocationManager
import androidx.room.Room import androidx.room.Room
import com.rtbishop.look4sat.data.LocalDataSource import com.rtbishop.look4sat.data.*
import com.rtbishop.look4sat.data.PreferencesSource
import com.rtbishop.look4sat.data.RemoteDataSource
import com.rtbishop.look4sat.data.SatelliteRepo
import com.rtbishop.look4sat.domain.Predictor import com.rtbishop.look4sat.domain.Predictor
import com.rtbishop.look4sat.framework.NetDataReporter
import com.rtbishop.look4sat.framework.PreferencesProvider import com.rtbishop.look4sat.framework.PreferencesProvider
import com.rtbishop.look4sat.framework.api.RemoteSource import com.rtbishop.look4sat.framework.api.RemoteSource
import com.rtbishop.look4sat.framework.api.SatelliteService import com.rtbishop.look4sat.framework.api.SatelliteService
@@ -46,6 +44,7 @@ import javax.inject.Singleton
object SatelliteDataModule { object SatelliteDataModule {
@Provides @Provides
@Singleton
fun provideMoshi(): Moshi { fun provideMoshi(): Moshi {
return Moshi.Builder().build() return Moshi.Builder().build()
} }
@@ -83,11 +82,13 @@ object SatelliteDataModule {
} }
@Provides @Provides
@Singleton
fun provideSatelliteDao(db: SatelliteDb): SatelliteDao { fun provideSatelliteDao(db: SatelliteDb): SatelliteDao {
return db.satelliteDao() return db.satelliteDao()
} }
@Provides @Provides
@Singleton
fun provideSatelliteDb(@ApplicationContext context: Context, moshi: Moshi): SatelliteDb { fun provideSatelliteDb(@ApplicationContext context: Context, moshi: Moshi): SatelliteDb {
Converters.initialize(moshi) Converters.initialize(moshi)
return Room.databaseBuilder(context, SatelliteDb::class.java, "SatelliteDb") return Room.databaseBuilder(context, SatelliteDb::class.java, "SatelliteDb")
@@ -95,15 +96,23 @@ object SatelliteDataModule {
} }
@Provides @Provides
@Singleton
fun provideRemoteDataSource(satelliteService: SatelliteService): RemoteDataSource { fun provideRemoteDataSource(satelliteService: SatelliteService): RemoteDataSource {
return RemoteSource(satelliteService) return RemoteSource(satelliteService)
} }
@Provides @Provides
@Singleton
fun provideSatelliteService(): SatelliteService { fun provideSatelliteService(): SatelliteService {
return Retrofit.Builder() return Retrofit.Builder()
.baseUrl("https://db.satnogs.org/api/") .baseUrl("https://db.satnogs.org/api/")
.addConverterFactory(MoshiConverterFactory.create()) .addConverterFactory(MoshiConverterFactory.create())
.build().create(SatelliteService::class.java) .build().create(SatelliteService::class.java)
} }
@Provides
@Singleton
fun provideDataReporter(@IoDispatcher dispatcher: CoroutineDispatcher): DataReporter {
return NetDataReporter(dispatcher)
}
} }
@@ -18,6 +18,7 @@
package com.rtbishop.look4sat.presentation.radarScreen package com.rtbishop.look4sat.presentation.radarScreen
import androidx.lifecycle.* import androidx.lifecycle.*
import com.rtbishop.look4sat.data.DataReporter
import com.rtbishop.look4sat.data.PreferencesSource import com.rtbishop.look4sat.data.PreferencesSource
import com.rtbishop.look4sat.data.SatelliteRepo import com.rtbishop.look4sat.data.SatelliteRepo
import com.rtbishop.look4sat.domain.Predictor import com.rtbishop.look4sat.domain.Predictor
@@ -25,13 +26,12 @@ import com.rtbishop.look4sat.domain.SatPass
import com.rtbishop.look4sat.domain.SatPos import com.rtbishop.look4sat.domain.SatPos
import com.rtbishop.look4sat.domain.Transmitter import com.rtbishop.look4sat.domain.Transmitter
import com.rtbishop.look4sat.framework.OrientationProvider import com.rtbishop.look4sat.framework.OrientationProvider
import com.rtbishop.look4sat.injection.IoDispatcher
import com.rtbishop.look4sat.utility.round import com.rtbishop.look4sat.utility.round
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.* import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import timber.log.Timber import kotlinx.coroutines.isActive
import java.net.Socket import kotlinx.coroutines.launch
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@@ -41,7 +41,7 @@ class RadarViewModel @Inject constructor(
private val preferences: PreferencesSource, private val preferences: PreferencesSource,
private val predictor: Predictor, private val predictor: Predictor,
private val satelliteRepo: SatelliteRepo, private val satelliteRepo: SatelliteRepo,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher private val dataReporter: DataReporter
) : ViewModel(), OrientationProvider.OrientationListener { ) : ViewModel(), OrientationProvider.OrientationListener {
private val stationPos = preferences.loadStationPosition() private val stationPos = preferences.loadStationPosition()
@@ -59,7 +59,6 @@ class RadarViewModel @Inject constructor(
emit(satPass) emit(satPass)
sendPassData(satPass) sendPassData(satPass)
processTransmitters(satPass) processTransmitters(satPass)
initRotatorControl(satPass)
} }
} }
} }
@@ -78,44 +77,27 @@ class RadarViewModel @Inject constructor(
private fun sendPassData(satPass: SatPass) { private fun sendPassData(satPass: SatPass) {
viewModelScope.launch { viewModelScope.launch {
var track: List<SatPos> = emptyList() var satTrack: List<SatPos> = emptyList()
val rotatorPrefs = preferences.getRotatorServer()
if (rotatorPrefs != null) {
dataReporter.setRotatorSocket(rotatorPrefs.first, rotatorPrefs.second)
}
if (!satPass.isDeepSpace) { if (!satPass.isDeepSpace) {
val startDate = Date(satPass.aosTime) val startDate = Date(satPass.aosTime)
val endDate = Date(satPass.losTime) val endDate = Date(satPass.losTime)
track = predictor.getSatTrack(satPass.satellite, stationPos, startDate, endDate) satTrack = predictor.getSatTrack(satPass.satellite, stationPos, startDate, endDate)
} }
while (isActive) { while (isActive) {
val pos = predictor.getSatPos(satPass.satellite, stationPos, Date()) val satPos = predictor.getSatPos(satPass.satellite, stationPos, Date())
_passData.postValue(RadarData(pos, track)) val azimuth = Math.toDegrees(satPos.azimuth).round(1)
val elevation = Math.toDegrees(satPos.elevation).round(1)
dataReporter.reportRotation(azimuth, elevation)
_passData.postValue(RadarData(satPos, satTrack))
delay(1000) delay(1000)
} }
} }
} }
private fun initRotatorControl(satPass: SatPass) {
viewModelScope.launch {
val rotatorPrefs = preferences.getRotatorServer()
if (rotatorPrefs != null) {
runCatching {
withContext(ioDispatcher) {
val socket = Socket(rotatorPrefs.first, rotatorPrefs.second)
val writer = socket.getOutputStream().bufferedWriter()
while (isActive) {
val satPos = predictor.getSatPos(satPass.satellite, stationPos, Date())
val azimuth = Math.toDegrees(satPos.azimuth).round(1)
val elevation = Math.toDegrees(satPos.elevation).round(1)
writer.write("\\set_pos $azimuth $elevation")
writer.newLine()
writer.flush()
delay(1000)
}
writer.close()
}
}.onFailure { Timber.d(it) }
}
}
}
private fun processTransmitters(satPass: SatPass) { private fun processTransmitters(satPass: SatPass) {
viewModelScope.launch { viewModelScope.launch {
satelliteRepo.getSatTransmitters(satPass.catNum).collect { transList -> satelliteRepo.getSatTransmitters(satPass.catNum).collect { transList ->
@@ -0,0 +1,8 @@
package com.rtbishop.look4sat.data
interface DataReporter {
suspend fun setRotatorSocket(server: String, port: Int)
suspend fun reportRotation(azim: Double, elev: Double)
}