diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt b/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt index 323eda69..837aea65 100644 --- a/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt +++ b/app/src/main/java/com/rtbishop/look4sat/framework/PreferencesSource.kt @@ -83,10 +83,6 @@ class PreferencesSource @Inject constructor( const val keyPositionQTH = "setPositionQTH" } - fun positionToQTH(lat: Double, lon: Double): String? { - return QthConverter.positionToQTH(lat, lon) - } - fun loadStationPosition(): GeoPos { val defaultSP = "0.0" val latitude = preferences.getString(keyLatitude, null) ?: defaultSP @@ -185,11 +181,9 @@ class PreferencesSource @Inject constructor( return preferences.getBoolean(keyRotator, false) } - fun getRotatorServer(): Pair? { - return if (isRotatorEnabled()) { - val address = preferences.getString(keyRotatorAddress, null) ?: "127.0.0.1" - val port = preferences.getString(keyRotatorPort, null) ?: "4533" - Pair(address, port.toInt()) - } else null + fun getRotatorServer(): Pair { + val address = preferences.getString(keyRotatorAddress, null) ?: "127.0.0.1" + val port = preferences.getString(keyRotatorPort, null) ?: "4533" + return Pair(address, port.toInt()) } } diff --git a/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt b/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt index 38939e51..a832d913 100644 --- a/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt +++ b/app/src/main/java/com/rtbishop/look4sat/presentation/radarScreen/RadarViewModel.kt @@ -78,10 +78,6 @@ class RadarViewModel @Inject constructor( private fun sendPassData(satPass: SatPass) { viewModelScope.launch { var satTrack: List = emptyList() - val rotatorPrefs = preferences.getRotatorServer() - if (rotatorPrefs != null) { - dataReporter.setupRotatorSocket(rotatorPrefs.first, rotatorPrefs.second) - } if (!satPass.isDeepSpace) { val startDate = Date(satPass.aosTime) val endDate = Date(satPass.losTime) @@ -89,9 +85,13 @@ class RadarViewModel @Inject constructor( } 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) - dataReporter.reportRotation(azimuth, elevation) + if (preferences.isRotatorEnabled()) { + val server = preferences.getRotatorServer().first + val port = preferences.getRotatorServer().second + val azimuth = Math.toDegrees(satPos.azimuth).round(1) + val elevation = Math.toDegrees(satPos.elevation).round(1) + dataReporter.reportRotation(server, port, azimuth, elevation) + } _passData.postValue(RadarData(satPos, satTrack)) delay(1000) } diff --git a/core/src/main/java/com/rtbishop/look4sat/domain/DataReporter.kt b/core/src/main/java/com/rtbishop/look4sat/domain/DataReporter.kt index 3dcd7cbd..f1b943a2 100644 --- a/core/src/main/java/com/rtbishop/look4sat/domain/DataReporter.kt +++ b/core/src/main/java/com/rtbishop/look4sat/domain/DataReporter.kt @@ -17,57 +17,50 @@ */ package com.rtbishop.look4sat.domain -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.withContext -import java.io.BufferedWriter -import java.net.Socket +import kotlinx.coroutines.* +import java.net.InetSocketAddress +import java.nio.ByteBuffer +import java.nio.channels.SocketChannel -class DataReporter(private val reporterDispatcher: CoroutineDispatcher) { +class DataReporter(reporterDispatcher: CoroutineDispatcher) { - private var radioSocket: Socket? = null - private var radioWriter: BufferedWriter? = null - private var rotatorSocket: Socket? = null - private var rotatorWriter: BufferedWriter? = null + private val reporterScope = CoroutineScope(reporterDispatcher) + private var frequencySocketChannel: SocketChannel? = null + private var rotationSocketChannel: SocketChannel? = null + private var frequencyReporting: Job? = null + private var rotationReporting: Job? = null - suspend fun setupRadioSocket(server: String, port: Int) { - withContext(reporterDispatcher) { + fun reportFrequency(server: String, port: Int, frequency: Long) { + frequencyReporting = reporterScope.launch { runCatching { - radioSocket = Socket(server, port) - radioWriter = rotatorSocket?.getOutputStream()?.bufferedWriter() - }.onFailure { println(it.localizedMessage) } - } - } - - suspend fun reportFrequency(freq: Long) { - withContext(reporterDispatcher) { - runCatching { - rotatorWriter?.let { writer -> - writer.write("\\set_freq $freq") - writer.newLine() - writer.flush() + if (frequencySocketChannel == null) { + frequencySocketChannel = SocketChannel.open(InetSocketAddress(server, port)) + } else { + val buffer = ByteBuffer.wrap("\\set_freq $frequency\n".toByteArray()) + frequencySocketChannel?.write(buffer) } - }.onFailure { println(it.localizedMessage) } + }.onFailure { error -> + println(error.localizedMessage) + frequencySocketChannel = null + frequencyReporting?.cancelAndJoin() + } } } - suspend fun setupRotatorSocket(server: String, port: Int) { - withContext(reporterDispatcher) { + fun reportRotation(server: String, port: Int, azimuth: Double, elevation: Double) { + rotationReporting = reporterScope.launch { runCatching { - rotatorSocket = Socket(server, port) - rotatorWriter = rotatorSocket?.getOutputStream()?.bufferedWriter() - }.onFailure { println(it.localizedMessage) } - } - } - - suspend fun reportRotation(azim: Double, elev: Double) { - withContext(reporterDispatcher) { - runCatching { - rotatorWriter?.let { writer -> - writer.write("\\set_pos $azim $elev") - writer.newLine() - writer.flush() + if (rotationSocketChannel == null) { + rotationSocketChannel = SocketChannel.open(InetSocketAddress(server, port)) + } else { + val buffer = ByteBuffer.wrap("\\set_pos $azimuth $elevation\n".toByteArray()) + rotationSocketChannel?.write(buffer) } - }.onFailure { println(it.localizedMessage) } + }.onFailure { error -> + println(error.localizedMessage) + rotationSocketChannel = null + rotationReporting?.cancelAndJoin() + } } } }