Simplified data output code, aligned with HamLib defaults

This commit is contained in:
Arty Bishop committed 2026-04-06 12:48:06 +01:00
1 parent 3f981c09d5
commit 868155c532
5 files changed
+66 -36

No files matched your search

@@ -55,8 +55,8 @@ class BluetoothReporter(
if (!rotatorConnected) return@launch if (!rotatorConnected) return@launch
val el = if (elevation > 0.0) elevation else 0.0 val el = if (elevation > 0.0) elevation else 0.0
val command = format val command = format
.replace($$"$AZ", "%03d".format(azimuth.toInt())) .replace($$"$AZ", azimuth.toString())
.replace($$"$EL", "%03d".format(el.toInt())) .replace($$"$EL", el.toString())
.unescapeControlChars() .unescapeControlChars()
write(rotatorStream, command) { rotatorConnected = false } write(rotatorStream, command) { rotatorConnected = false }
} }
@@ -52,6 +52,7 @@ class NetworkReporter(
val command = format val command = format
.replace($$"$AZ", azimuth.toString()) .replace($$"$AZ", azimuth.toString())
.replace($$"$EL", el.toString()) .replace($$"$EL", el.toString())
.unescapeControlChars()
write(rotatorSocket, command) { rotatorConnected = false } write(rotatorSocket, command) { rotatorConnected = false }
} }
} }
@@ -60,7 +61,9 @@ class NetworkReporter(
reporterScope.launch { reporterScope.launch {
ensureFrequencyConnected() ensureFrequencyConnected()
if (!frequencyConnected) return@launch if (!frequencyConnected) return@launch
val command = format.replace($$"$FREQ", frequency.toString()) val command = format
.replace($$"$FREQ", frequency.toString())
.unescapeControlChars()
write(frequencySocket, command) { frequencyConnected = false } write(frequencySocket, command) { frequencyConnected = false }
} }
} }
@@ -110,4 +113,7 @@ class NetworkReporter(
onError() onError()
} }
} }
private fun String.unescapeControlChars(): String =
replace("\\r", "\r").replace("\\n", "\n").replace("\\t", "\t")
} }
@@ -246,6 +246,29 @@ class SettingsRepo(
//endregion //endregion
//region # RC settings //region # RC settings
init {
migrateRCFormats()
}
// TODO: Remove after a few releases (added in v4.2.0)
private val keyRCFormatsMigrated = "rcFormatsMigrated"
private fun migrateRCFormats() {
if (preferences.getBoolean(keyRCFormatsMigrated, false)) return
val formatKeys = listOf(
keyRotatorFormat, keyFrequencyFormat, keyBluetoothRotatorFormat, keyBluetoothFrequencyFormat
)
preferences.edit {
for (key in formatKeys) {
val value = preferences.getString(key, null) ?: continue
if (value.contains("_") && !value.startsWith("\\")) {
putString(key, "\\$value")
}
}
putBoolean(keyRCFormatsMigrated, true)
}
}
private val _rcSettings = MutableStateFlow(getRCSettings()) private val _rcSettings = MutableStateFlow(getRCSettings())
override val rcSettings: StateFlow<RCSettings> = _rcSettings override val rcSettings: StateFlow<RCSettings> = _rcSettings
@@ -280,12 +303,12 @@ class SettingsRepo(
frequencyPort = preferences.getString(keyFrequencyPort, null) ?: "4532", frequencyPort = preferences.getString(keyFrequencyPort, null) ?: "4532",
frequencyFormat = preferences.getString(keyFrequencyFormat, null) ?: $$"F $FREQ", frequencyFormat = preferences.getString(keyFrequencyFormat, null) ?: $$"F $FREQ",
bluetoothRotatorState = preferences.getBoolean(keyBluetoothRotatorState, false), bluetoothRotatorState = preferences.getBoolean(keyBluetoothRotatorState, false),
bluetoothRotatorFormat = preferences.getString(keyBluetoothRotatorFormat, null) ?: $$"W$AZ $EL", bluetoothRotatorFormat = preferences.getString(keyBluetoothRotatorFormat, null) ?: $$"P $AZ $EL",
bluetoothRotatorName = preferences.getString(keyBluetoothRotatorName, null) ?: "Default", bluetoothRotatorName = preferences.getString(keyBluetoothRotatorName, null) ?: "Default",
bluetoothRotatorAddress = preferences.getString(keyBluetoothRotatorAddress, null) ?: "00:0C:BF:13:80:5D", bluetoothRotatorAddress = preferences.getString(keyBluetoothRotatorAddress, null) ?: "00:0C:BF:13:80:5D",
bluetoothFrequencyState = preferences.getBoolean(keyBluetoothFrequencyState, false), bluetoothFrequencyState = preferences.getBoolean(keyBluetoothFrequencyState, false),
bluetoothFrequencyAddress = preferences.getString(keyBluetoothFrequencyAddress, null) ?: "00:0C:BF:13:80:5D", bluetoothFrequencyAddress = preferences.getString(keyBluetoothFrequencyAddress, null) ?: "00:0C:BF:13:80:5D",
bluetoothFrequencyFormat = preferences.getString(keyBluetoothFrequencyFormat, null) ?: $$"FA$FREQ" bluetoothFrequencyFormat = preferences.getString(keyBluetoothFrequencyFormat, null) ?: $$"F $FREQ"
) )
//endregion //endregion
@@ -129,7 +129,6 @@ class RadarViewModel(
} }
processRadios(transmitters, satPass.orbitalObject, timeNow) processRadios(transmitters, satPass.orbitalObject, timeNow)
sendPassData(pos) sendPassData(pos)
sendPassDataBT(pos)
delay(1000) delay(1000)
} }
} }
@@ -149,33 +148,35 @@ class RadarViewModel(
} }
private fun sendPassData(orbitalPos: OrbitalPos) { private fun sendPassData(orbitalPos: OrbitalPos) {
viewModelScope.launch { val rc = settingsRepo.rcSettings.value
val rc = settingsRepo.rcSettings.value sendReporterData(
if (rc.rotatorState) { networkReporter, orbitalPos,
val azimuth = orbitalPos.azimuth.toDegrees().round(2) rc.rotatorState, rc.rotatorFormat,
val elevation = orbitalPos.elevation.toDegrees().round(2) rc.frequencyState, rc.frequencyFormat
networkReporter.reportRotation(rc.rotatorFormat, azimuth, elevation) )
} sendReporterData(
if (rc.frequencyState) { bluetoothReporter, orbitalPos,
_uiState.value.selectedFrequency?.let { freq -> rc.bluetoothRotatorState, rc.bluetoothRotatorFormat,
networkReporter.reportFrequency(rc.frequencyFormat, freq) rc.bluetoothFrequencyState, rc.bluetoothFrequencyFormat
} )
}
}
} }
private fun sendPassDataBT(orbitalPos: OrbitalPos) { private fun sendReporterData(
viewModelScope.launch { reporter: IReporter,
val rc = settingsRepo.rcSettings.value orbitalPos: OrbitalPos,
if (rc.bluetoothRotatorState) { rotatorEnabled: Boolean,
val azimuth = orbitalPos.azimuth.toDegrees().round(0) rotatorFormat: String,
val elevation = orbitalPos.elevation.toDegrees().round(0) frequencyEnabled: Boolean,
bluetoothReporter.reportRotation(rc.bluetoothRotatorFormat, azimuth, elevation) frequencyFormat: String
} ) {
if (rc.bluetoothFrequencyState) { if (rotatorEnabled) {
_uiState.value.selectedFrequency?.let { freq -> val azimuth = orbitalPos.azimuth.toDegrees().round(2)
bluetoothReporter.reportFrequency(rc.bluetoothFrequencyFormat, freq) val elevation = orbitalPos.elevation.toDegrees().round(2)
} reporter.reportRotation(rotatorFormat, azimuth, elevation)
}
if (frequencyEnabled) {
_uiState.value.selectedFrequency?.let { freq ->
reporter.reportFrequency(frequencyFormat, freq)
} }
} }
} }
@@ -216,12 +216,12 @@ fun PreviewNetworkOutputDialog() {
frequencyPort = "4532", frequencyPort = "4532",
frequencyFormat = $$"F $FREQ", frequencyFormat = $$"F $FREQ",
bluetoothRotatorState = false, bluetoothRotatorState = false,
bluetoothRotatorFormat = $$"W$AZ $EL", bluetoothRotatorFormat = $$"P $AZ $EL",
bluetoothRotatorName = "Default", bluetoothRotatorName = "Default",
bluetoothRotatorAddress = "00:0C:BF:13:80:5D", bluetoothRotatorAddress = "00:0C:BF:13:80:5D",
bluetoothFrequencyState = false, bluetoothFrequencyState = false,
bluetoothFrequencyAddress = "00:0C:BF:13:80:5D", bluetoothFrequencyAddress = "00:0C:BF:13:80:5D",
bluetoothFrequencyFormat = $$"FA$FREQ" bluetoothFrequencyFormat = $$"F $FREQ"
), ),
onDismiss = {}, onDismiss = {},
onSave = { _, _, _, _, _, _, _, _ -> } onSave = { _, _, _, _, _, _, _, _ -> }
@@ -361,14 +361,14 @@ fun PreviewBluetoothOutputDialog() {
frequencyState = false, frequencyState = false,
frequencyAddress = "127.0.0.1", frequencyAddress = "127.0.0.1",
frequencyPort = "4532", frequencyPort = "4532",
frequencyFormat = $$"set_freq $FREQ", frequencyFormat = $$"F $FREQ",
bluetoothRotatorState = false, bluetoothRotatorState = false,
bluetoothRotatorFormat = $$"W$AZ $EL", bluetoothRotatorFormat = $$"P $AZ $EL",
bluetoothRotatorName = "Default", bluetoothRotatorName = "Default",
bluetoothRotatorAddress = "00:0C:BF:13:80:5D", bluetoothRotatorAddress = "00:0C:BF:13:80:5D",
bluetoothFrequencyState = false, bluetoothFrequencyState = false,
bluetoothFrequencyAddress = "00:0C:BF:13:80:5D", bluetoothFrequencyAddress = "00:0C:BF:13:80:5D",
bluetoothFrequencyFormat = $$"FA$FREQ" bluetoothFrequencyFormat = $$"F $FREQ"
), ),
onDismiss = {}, onDismiss = {},
onSave = { _, _, _, _, _, _ -> } onSave = { _, _, _, _, _, _ -> }