mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Added rotator control function to PolarViewModel.kt
This commit is contained in:
1 parent
703dc17794
commit
a205ebfa2b
2 files changed
+43
-9
No files matched your search
@@ -22,13 +22,13 @@ import com.rtbishop.look4sat.data.model.SatPass
|
||||
import com.rtbishop.look4sat.data.model.SatTrans
|
||||
import com.rtbishop.look4sat.data.repository.PassesRepo
|
||||
import com.rtbishop.look4sat.data.repository.SatelliteRepo
|
||||
import com.rtbishop.look4sat.di.IoDispatcher
|
||||
import com.rtbishop.look4sat.utility.PrefsManager
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import java.net.Socket
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -37,7 +37,8 @@ class PolarViewModel @Inject constructor(
|
||||
private val orientation: Orientation,
|
||||
private val prefsManager: PrefsManager,
|
||||
private val passesRepo: PassesRepo,
|
||||
private val satelliteRepo: SatelliteRepo
|
||||
private val satelliteRepo: SatelliteRepo,
|
||||
@IoDispatcher private val ioDispatcher: CoroutineDispatcher
|
||||
) : ViewModel(), Orientation.OrientationListener {
|
||||
|
||||
private val magDeclination = prefsManager.getMagDeclination()
|
||||
@@ -51,6 +52,7 @@ class PolarViewModel @Inject constructor(
|
||||
val pass = passes.find { it.catNum == catNum && it.aosDate.time == aosTime }
|
||||
pass?.let { satPass ->
|
||||
processTransmitters(satPass)
|
||||
initRotatorControl(satPass)
|
||||
emit(satPass)
|
||||
}
|
||||
}
|
||||
@@ -68,6 +70,27 @@ class PolarViewModel @Inject constructor(
|
||||
_azimuth.value = azimuth + magDeclination
|
||||
}
|
||||
|
||||
private fun initRotatorControl(satPass: SatPass) {
|
||||
viewModelScope.launch {
|
||||
val rotatorPrefs = prefsManager.getRotatorServer()
|
||||
if (rotatorPrefs != null) {
|
||||
runCatching {
|
||||
withContext(ioDispatcher) {
|
||||
val client = Socket(rotatorPrefs.first, rotatorPrefs.second)
|
||||
val writer = client.getOutputStream()
|
||||
while (isActive) {
|
||||
val satPos = satPass.predictor.getSatPos(Date())
|
||||
val message = "\\set_pos ${satPos.azimuth} ${satPos.elevation}"
|
||||
writer?.write(message.toByteArray())
|
||||
delay(1000)
|
||||
}
|
||||
writer.close()
|
||||
}
|
||||
}.onFailure { Timber.d(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun processTransmitters(satPass: SatPass) {
|
||||
viewModelScope.launch {
|
||||
satelliteRepo.getSatTransmitters(satPass.catNum).collect { transList ->
|
||||
|
||||
@@ -47,6 +47,9 @@ class PrefsManager @Inject constructor(val preferences: SharedPreferences, moshi
|
||||
const val keyPositionGPS = "setPositionGPS"
|
||||
const val keyPositionQTH = "setPositionQTH"
|
||||
const val keyIsFirstLaunch = "shouldShowSplash"
|
||||
const val keyRotator = "isRotatorEnabled"
|
||||
const val keyRotatorAddress = "rotatorAddress"
|
||||
const val keyRotatorPort = "rotatorPort"
|
||||
}
|
||||
|
||||
fun getHoursAhead(): Int {
|
||||
@@ -59,10 +62,10 @@ class PrefsManager @Inject constructor(val preferences: SharedPreferences, moshi
|
||||
|
||||
fun getStationPosition(): GroundStationPosition {
|
||||
val defaultGSP = "0.0"
|
||||
val lat = preferences.getString(keyLatitude, defaultGSP)!!.toDouble()
|
||||
val lon = preferences.getString(keyLongitude, defaultGSP)!!.toDouble()
|
||||
val alt = preferences.getString(keyAltitude, defaultGSP)!!.toDouble()
|
||||
return GroundStationPosition(lat, lon, alt)
|
||||
val latitude = preferences.getString(keyLatitude, null) ?: defaultGSP
|
||||
val longitude = preferences.getString(keyLongitude, null) ?: defaultGSP
|
||||
val altitude = preferences.getString(keyAltitude, null) ?: defaultGSP
|
||||
return GroundStationPosition(latitude.toDouble(), longitude.toDouble(), altitude.toDouble())
|
||||
}
|
||||
|
||||
fun setStationPosition(latitude: Double, longitude: Double, altitude: Double) {
|
||||
@@ -141,4 +144,12 @@ class PrefsManager @Inject constructor(val preferences: SharedPreferences, moshi
|
||||
}
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
fun getRotatorServer(): Pair<String, Int>? {
|
||||
return if (preferences.getBoolean(keyRotator, false)) {
|
||||
val address = preferences.getString(keyRotatorAddress, null) ?: "127.0.0.1"
|
||||
val port = preferences.getString(keyRotatorPort, null) ?: "4096"
|
||||
Pair(address, port.toInt())
|
||||
} else null
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user