Add the setup wizard's log program, rotator and AirScout answers

Written-by: Gemini 4 Hydrogen (Antigravity)
Written-by: Claude Fable 5.2
This commit is contained in:
Rsclub2_2 committed 2026-11-18 21:32:13 +01:00
1 parent 0ab9ffc241
commit facba2b010
2 files changed
+501

No files matched your search

@@ -0,0 +1,221 @@
package kst4contest.view.compose.wizard
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import kst4contest.model.ChatPreferences
import kst4contest.view.compose.tabs.AirscoutTabState
import kst4contest.view.compose.tabs.StationTabState
/** Where the worked stations come from. The wizard knows one source at a time. */
enum class WizardLogProgram {
NONE,
/** The Win-Test network listener. */
WINTEST,
/** UCXLog, DXLog.net, N1MM+ and QARTEST: QSO messages on the shared UDP port. */
UDP,
/** A log file polled for callsigns. */
FILE,
}
/** Something that was on in the profile and will be off after Save, for the summary. */
enum class WizardSwitch {
SECOND_CHAT,
LOG_WINTEST,
LOG_UDP,
LOG_FILE,
QRG_FOLLOW,
CLUSTER_SERVER,
ROTATOR,
AIRSCOUT,
}
/**
* The setup wizard's answers about log program, rotator and AirScout.
*
* Two pages share this class because they share a rule: a station set up anew gets only what
* the operator said yes to. [loadFresh] therefore switches everything off -- the bundled
* template has several of these on, which a newcomer without a log program does not know --
* while ports, names, host and identifiers stay as stored and are proposals.
*
* The wizard switches things on and off and takes numbers over. It invents none: no port,
* no identifier and no default here differs from what the settings tabs accept.
*
* The settings window allows several log sources at once and the wizard's question does
* not. [severalStoredLogSources] says when the stored profile is such a case, so the caller
* can keep the log page shut rather than collapse the sources into one.
*
* @param prefs the profile's preferences, already read from its file
*/
class WizardIntegrations(prefs: ChatPreferences) {
private val storedWintest = prefs.isLogsynch_wintestNetworkListenerEnabled()
private val storedUdp = prefs.isLogsynch_ucxUDPWkdCallListenerEnabled()
private val storedFile = prefs.isLogsynch_fileBasedWkdCallInterpreterEnabled()
private val storedUdpQrg = prefs.isTrxSynch_ucxLogUDPListenerEnabled()
private val storedWintestQrg = prefs.isLogsynch_wintestQrgSyncEnabled()
private val storedCluster = prefs.isNotify_dxClusterServerEnabled()
private val storedRotator = prefs.isStn_pstRotatorEnabled()
private val storedAirScout = prefs.isAirScout_asUDPListenerEnabled()
/** Rotator 2 only counts while SO2R uses it, as on the Station tab. */
private val rotatorTwoInUse = prefs.isTrxSynch_so2rEnabled() && prefs.isStn_pstRotator2Enabled()
private val rotatorTwoPort = prefs.getStn_pstRotator2Port()
/** More than one log source is on in the profile; the log page cannot show that. */
val severalStoredLogSources: Boolean = listOf(storedWintest, storedUdp, storedFile).count { it } > 1
var logProgram: WizardLogProgram by mutableStateOf(WizardLogProgram.NONE)
private set
var udpPort: Int by mutableStateOf(prefs.getLogsynch_ucxUDPWkdCallListenerPort())
var wintestPort: Int by mutableStateOf(prefs.getLogsynch_wintestNetworkPort())
var wintestStationName: String by mutableStateOf(prefs.getLogsynch_wintestNetworkStationNameOfKST().orEmpty())
var filePath: String by mutableStateOf(prefs.getLogsynch_fileBasedWkdCallInterpreterFileNameReadOnly().orEmpty())
/** Take the own frequency from the chosen network program. */
var qrgFollow: Boolean by mutableStateOf(false)
/** Pass spots to the log program through the local DX cluster server. */
var clusterServer: Boolean by mutableStateOf(false)
var rotator: Boolean by mutableStateOf(false)
var rotatorHost: String by mutableStateOf(prefs.getStn_pstRotatorHost().orEmpty())
var rotatorPort: Int by mutableStateOf(prefs.getStn_pstRotatorPort())
var airScout: Boolean by mutableStateOf(false)
var airScoutPort: Int by mutableStateOf(prefs.getAirScout_asCommunicationPort())
var airScoutServerName: String by mutableStateOf(prefs.getAirScout_asServerNameString().orEmpty())
var airScoutClientName: String by mutableStateOf(prefs.getAirScout_asClientNameString().orEmpty())
init {
loadStored()
}
/** The switches as the profile has them. Several sources show as one, the network ones first. */
fun loadStored() {
logProgram = when {
storedWintest -> WizardLogProgram.WINTEST
storedUdp -> WizardLogProgram.UDP
storedFile -> WizardLogProgram.FILE
else -> WizardLogProgram.NONE
}
qrgFollow = when (logProgram) {
WizardLogProgram.WINTEST -> storedWintestQrg
WizardLogProgram.UDP -> storedUdpQrg
else -> false
}
clusterServer = networked && storedCluster
rotator = storedRotator
airScout = storedAirScout
}
/** A station set up anew: every switch off, every number left as a proposal. */
fun loadFresh() {
logProgram = WizardLogProgram.NONE
qrgFollow = false
clusterServer = false
rotator = false
airScout = false
}
/** Only a network program can deliver a frequency or take spots. */
val networked: Boolean
get() = logProgram == WizardLogProgram.WINTEST || logProgram == WizardLogProgram.UDP
fun chooseLogProgram(program: WizardLogProgram) {
logProgram = program
if (!networked) {
qrgFollow = false
clusterServer = false
}
}
/** Whether what the chosen program needs is there. A program not chosen needs nothing. */
val logComplete: Boolean
get() = when (logProgram) {
WizardLogProgram.NONE -> true
WizardLogProgram.WINTEST -> AirscoutTabState.isValidPort(wintestPort) && wintestStationName.isNotBlank()
WizardLogProgram.UDP -> AirscoutTabState.isValidPort(udpPort)
WizardLogProgram.FILE -> filePath.isNotBlank()
}
/** The same or the next port as rotator 2, which answers on its port + 1. */
val rotatorPortClashes: Boolean
get() = rotator && rotatorTwoInUse && StationTabState.portsClash(rotatorPort, rotatorTwoPort)
/** Whether what the switched-on equipment needs is there. */
val gearComplete: Boolean
get() {
val rotatorComplete = !rotator ||
(rotatorHost.isNotBlank() && AirscoutTabState.isValidPort(rotatorPort) && !rotatorPortClashes)
val airScoutComplete = !airScout ||
(AirscoutTabState.isValidPort(airScoutPort) &&
AirscoutTabState.isValidIdentifier(airScoutServerName) &&
AirscoutTabState.isValidIdentifier(airScoutClientName))
return rotatorComplete && airScoutComplete
}
/**
* Puts the log page's answers into [prefs]: exactly the chosen source on, its details
* stored, and the frequency source and the DX cluster server only with a network program.
* Details of a program that is not chosen are left as they are. Call only while
* [logComplete].
*/
fun writeLogTo(prefs: ChatPreferences) {
prefs.setLogsynch_wintestNetworkListenerEnabled(logProgram == WizardLogProgram.WINTEST)
prefs.setLogsynch_ucxUDPWkdCallListenerEnabled(logProgram == WizardLogProgram.UDP)
prefs.setLogsynch_fileBasedWkdCallInterpreterEnabled(logProgram == WizardLogProgram.FILE)
when (logProgram) {
WizardLogProgram.WINTEST -> {
prefs.setLogsynch_wintestNetworkPort(wintestPort)
prefs.setLogsynch_wintestNetworkStationNameOfKST(wintestStationName.trim())
}
WizardLogProgram.UDP -> prefs.setLogsynch_ucxUDPWkdCallListenerPort(udpPort)
WizardLogProgram.FILE -> prefs.setLogsynch_fileBasedWkdCallInterpreterFileNameReadOnly(filePath)
WizardLogProgram.NONE -> Unit
}
prefs.setLogsynch_wintestQrgSyncEnabled(logProgram == WizardLogProgram.WINTEST && qrgFollow)
prefs.setTrxSynch_ucxLogUDPListenerEnabled(logProgram == WizardLogProgram.UDP && qrgFollow)
prefs.setNotify_dxClusterServerEnabled(networked && clusterServer)
}
/**
* Puts the equipment page's answers into [prefs]. Host, port and identifiers are stored
* only for what is switched on. Call only while [gearComplete].
*/
fun writeGearTo(prefs: ChatPreferences) {
prefs.setStn_pstRotatorEnabled(rotator)
if (rotator) {
prefs.setStn_pstRotatorHost(rotatorHost.trim())
prefs.setStn_pstRotatorPort(rotatorPort)
}
prefs.setAirScout_asUDPListenerEnabled(airScout)
if (airScout) {
prefs.setAirScout_asCommunicationPort(airScoutPort)
prefs.setAirScout_asServerNameString(airScoutServerName.trim())
prefs.setAirScout_asClientNameString(airScoutClientName.trim())
}
}
/** What the log page's answers switch off that the profile had on, in a fixed order. */
fun switchedOffByLog(): List<WizardSwitch> = buildList {
if (storedWintest && logProgram != WizardLogProgram.WINTEST) add(WizardSwitch.LOG_WINTEST)
if (storedUdp && logProgram != WizardLogProgram.UDP) add(WizardSwitch.LOG_UDP)
if (storedFile && logProgram != WizardLogProgram.FILE) add(WizardSwitch.LOG_FILE)
if ((storedUdpQrg || storedWintestQrg) && !qrgFollow) add(WizardSwitch.QRG_FOLLOW)
if (storedCluster && !(networked && clusterServer)) add(WizardSwitch.CLUSTER_SERVER)
}
/** What the equipment page's answers switch off that the profile had on. */
fun switchedOffByGear(): List<WizardSwitch> = buildList {
if (storedRotator && !rotator) add(WizardSwitch.ROTATOR)
if (storedAirScout && !airScout) add(WizardSwitch.AIRSCOUT)
}
}
@@ -0,0 +1,280 @@
package kst4contest.view.compose.wizard
import kst4contest.model.ChatPreferences
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.nio.file.Path
/**
* The answers about log program, rotator and AirScout: what a new station starts with, what
* a kept one shows, when a page may be left, and what is written.
*/
class WizardIntegrationsTest {
private fun prefs(configure: ChatPreferences.() -> Unit = {}): ChatPreferences =
ChatPreferences(Path.of("wizard-integrations-test-unused.xml")).apply(configure)
/** Everything off, so each test states what it switches on. */
private fun allOff(configure: ChatPreferences.() -> Unit = {}) = prefs {
setLogsynch_wintestNetworkListenerEnabled(false)
setLogsynch_ucxUDPWkdCallListenerEnabled(false)
setLogsynch_fileBasedWkdCallInterpreterEnabled(false)
setTrxSynch_ucxLogUDPListenerEnabled(false)
setLogsynch_wintestQrgSyncEnabled(false)
setNotify_dxClusterServerEnabled(false)
setStn_pstRotatorEnabled(false)
setAirScout_asUDPListenerEnabled(false)
setTrxSynch_so2rEnabled(false)
setStn_pstRotator2Enabled(false)
setLogsynch_ucxUDPWkdCallListenerPort(12060)
setLogsynch_wintestNetworkPort(9871)
setLogsynch_wintestNetworkStationNameOfKST("KST")
setStn_pstRotatorHost("127.0.0.1")
setStn_pstRotatorPort(12000)
setStn_pstRotator2Port(12010)
setAirScout_asCommunicationPort(9872)
setAirScout_asServerNameString("AS")
setAirScout_asClientNameString("KST")
configure()
}
/** What the bundled template switches on. */
private fun likeTheTemplate() = allOff {
setLogsynch_ucxUDPWkdCallListenerEnabled(true)
setLogsynch_fileBasedWkdCallInterpreterEnabled(true)
setTrxSynch_ucxLogUDPListenerEnabled(true)
setNotify_dxClusterServerEnabled(true)
setAirScout_asUDPListenerEnabled(true)
}
private fun winTestStation() = allOff {
setLogsynch_wintestNetworkListenerEnabled(true)
setLogsynch_wintestNetworkPort(9999)
setLogsynch_wintestQrgSyncEnabled(true)
setNotify_dxClusterServerEnabled(true)
setStn_pstRotatorEnabled(true)
}
@Test
fun aKeptStationShowsWhatIsStored() {
val answers = WizardIntegrations(winTestStation())
assertEquals(WizardLogProgram.WINTEST, answers.logProgram)
assertEquals(9999, answers.wintestPort)
assertTrue(answers.qrgFollow)
assertTrue(answers.clusterServer)
assertTrue(answers.rotator)
assertFalse(answers.airScout)
assertFalse(answers.severalStoredLogSources)
}
@Test
fun aNewStationStartsWithEverythingOffButKeepsTheNumbers() {
val answers = WizardIntegrations(winTestStation())
answers.loadFresh()
assertEquals(WizardLogProgram.NONE, answers.logProgram)
assertFalse(answers.qrgFollow)
assertFalse(answers.clusterServer)
assertFalse(answers.rotator)
assertFalse(answers.airScout)
assertEquals(9999, answers.wintestPort, "a port the operator once set is a proposal, not lost")
assertTrue(answers.logComplete)
assertTrue(answers.gearComplete)
}
@Test
fun loadingTheStoredValuesAgainUndoesAFreshStart() {
val answers = WizardIntegrations(winTestStation())
answers.loadFresh()
answers.loadStored()
assertEquals(WizardLogProgram.WINTEST, answers.logProgram)
assertTrue(answers.rotator)
}
@Test
fun severalStoredSourcesMakeTheLogPageUneditable() {
val answers = WizardIntegrations(likeTheTemplate())
assertTrue(answers.severalStoredLogSources, "the template has the network listener and the file on")
// Shown as one of them, the network one first; the state decides not to offer the page.
assertEquals(WizardLogProgram.UDP, answers.logProgram)
}
@Test
fun leavingANetworkProgramDropsWhatOnlyANetworkProgramCanDo() {
val answers = WizardIntegrations(allOff())
answers.chooseLogProgram(WizardLogProgram.UDP)
answers.qrgFollow = true
answers.clusterServer = true
assertTrue(answers.networked)
answers.chooseLogProgram(WizardLogProgram.FILE)
assertFalse(answers.networked)
assertFalse(answers.qrgFollow)
assertFalse(answers.clusterServer)
}
@Test
fun aLogFileNeedsAFile() {
val answers = WizardIntegrations(allOff())
// The compiled-in default names a file, which is a proposal like every stored value.
answers.filePath = ""
answers.chooseLogProgram(WizardLogProgram.FILE)
assertFalse(answers.logComplete)
answers.filePath = "/home/op/log.txt"
assertTrue(answers.logComplete)
}
@Test
fun anInvalidPortMakesItsPageIncomplete() {
val answers = WizardIntegrations(allOff())
answers.chooseLogProgram(WizardLogProgram.UDP)
answers.udpPort = 0
assertFalse(answers.logComplete)
answers.udpPort = 70000
assertFalse(answers.logComplete)
answers.udpPort = 12060
assertTrue(answers.logComplete)
answers.chooseLogProgram(WizardLogProgram.WINTEST)
answers.wintestStationName = " "
assertFalse(answers.logComplete, "Win-Test needs a station name to send from")
answers.wintestStationName = "KST"
answers.wintestPort = -1
assertFalse(answers.logComplete)
// A port of a program that is not chosen is nobody's problem.
answers.chooseLogProgram(WizardLogProgram.NONE)
assertTrue(answers.logComplete)
answers.airScout = true
answers.airScoutPort = 0
assertFalse(answers.gearComplete)
answers.airScoutPort = 9872
answers.airScoutServerName = "A\"S"
assertFalse(answers.gearComplete, "a quotation mark breaks the AirScout message")
answers.airScoutServerName = "AS"
assertTrue(answers.gearComplete)
answers.rotator = true
answers.rotatorHost = " "
assertFalse(answers.gearComplete)
}
@Test
fun aRotatorPortNextToRotatorTwoIsRefusedOnlyWithSo2r() {
val withSo2r = WizardIntegrations(allOff {
setTrxSynch_so2rEnabled(true)
setStn_pstRotator2Enabled(true)
})
withSo2r.rotator = true
withSo2r.rotatorPort = 12011
assertTrue(withSo2r.rotatorPortClashes, "PSTRotator answers on port + 1")
assertFalse(withSo2r.gearComplete)
withSo2r.rotatorPort = 12000
assertFalse(withSo2r.rotatorPortClashes)
assertTrue(withSo2r.gearComplete)
val withoutSo2r = WizardIntegrations(allOff())
withoutSo2r.rotator = true
withoutSo2r.rotatorPort = 12011
assertFalse(withoutSo2r.rotatorPortClashes, "rotator 2 is not in use without SO2R")
assertTrue(withoutSo2r.gearComplete)
}
@Test
fun writingTheLogPageSwitchesExactlyTheChosenSourceOn() {
val target = likeTheTemplate()
val answers = WizardIntegrations(target)
answers.loadFresh()
answers.chooseLogProgram(WizardLogProgram.WINTEST)
answers.wintestPort = 9871
answers.wintestStationName = " KST "
answers.qrgFollow = true
answers.writeLogTo(target)
assertTrue(target.isLogsynch_wintestNetworkListenerEnabled())
assertFalse(target.isLogsynch_ucxUDPWkdCallListenerEnabled())
assertFalse(target.isLogsynch_fileBasedWkdCallInterpreterEnabled())
assertEquals(9871, target.getLogsynch_wintestNetworkPort())
assertEquals("KST", target.getLogsynch_wintestNetworkStationNameOfKST(), "trimmed, as the tab stores it")
assertTrue(target.isLogsynch_wintestQrgSyncEnabled())
assertFalse(target.isTrxSynch_ucxLogUDPListenerEnabled(), "the other program's frequency source goes off")
assertFalse(target.isNotify_dxClusterServerEnabled())
assertTrue(target.isAirScout_asUDPListenerEnabled(), "the log page does not touch AirScout")
}
@Test
fun writingNoLogProgramSwitchesEverySynchronisationOff() {
val target = likeTheTemplate()
val answers = WizardIntegrations(target)
answers.loadFresh()
answers.writeLogTo(target)
assertFalse(target.isLogsynch_wintestNetworkListenerEnabled())
assertFalse(target.isLogsynch_ucxUDPWkdCallListenerEnabled())
assertFalse(target.isLogsynch_fileBasedWkdCallInterpreterEnabled())
assertFalse(target.isTrxSynch_ucxLogUDPListenerEnabled())
assertFalse(target.isLogsynch_wintestQrgSyncEnabled())
assertFalse(target.isNotify_dxClusterServerEnabled())
assertEquals(12060, target.getLogsynch_ucxUDPWkdCallListenerPort(), "an unused port is left alone")
}
@Test
fun writingTheGearPageSetsRotatorAndAirScoutOnly() {
val target = likeTheTemplate()
val answers = WizardIntegrations(target)
answers.loadFresh()
answers.rotator = true
answers.rotatorHost = " 192.168.1.20 "
answers.rotatorPort = 12040
answers.writeGearTo(target)
assertTrue(target.isStn_pstRotatorEnabled())
assertEquals("192.168.1.20", target.getStn_pstRotatorHost())
assertEquals(12040, target.getStn_pstRotatorPort())
assertFalse(target.isAirScout_asUDPListenerEnabled())
assertEquals(9872, target.getAirScout_asCommunicationPort(), "an unused port is left alone")
assertTrue(target.isLogsynch_ucxUDPWkdCallListenerEnabled(), "the gear page does not touch the log sources")
}
@Test
fun theSwitchedOffListNamesWhatWasOnAndWillBeOff() {
val answers = WizardIntegrations(likeTheTemplate())
answers.loadFresh()
assertEquals(
listOf(WizardSwitch.LOG_UDP, WizardSwitch.LOG_FILE, WizardSwitch.QRG_FOLLOW, WizardSwitch.CLUSTER_SERVER),
answers.switchedOffByLog(),
)
assertEquals(listOf(WizardSwitch.AIRSCOUT), answers.switchedOffByGear())
answers.chooseLogProgram(WizardLogProgram.UDP)
answers.qrgFollow = true
answers.airScout = true
assertEquals(listOf(WizardSwitch.LOG_FILE, WizardSwitch.CLUSTER_SERVER), answers.switchedOffByLog())
assertEquals(emptyList<WizardSwitch>(), answers.switchedOffByGear())
}
@Test
fun nothingIsSwitchedOffOnAStationThatHadNothingOn() {
val answers = WizardIntegrations(allOff())
answers.loadFresh()
assertEquals(emptyList<WizardSwitch>(), answers.switchedOffByLog())
assertEquals(emptyList<WizardSwitch>(), answers.switchedOffByGear())
}
}