diff --git a/connector/composeApp/src/desktopMain/kotlin/SimulatorStatus.kt b/connector/composeApp/src/desktopMain/kotlin/SimulatorStatus.kt index f9bcc1e..b5a5a2a 100644 --- a/connector/composeApp/src/desktopMain/kotlin/SimulatorStatus.kt +++ b/connector/composeApp/src/desktopMain/kotlin/SimulatorStatus.kt @@ -85,8 +85,8 @@ class SimulatorStatus { /** * Purely for testing */ - private suspend fun loadSimulator(viewModel: InterfaceState) { - delay(1000) - viewModel.simConnection = !viewModel.simConnection + private fun loadSimulator(viewModel: InterfaceState) { + val xpc = XPC() + viewModel.simConnection = xpc.connected() } } diff --git a/connector/shared/src/commonMain/kotlin/XPC.kt b/connector/shared/src/commonMain/kotlin/XPC.kt new file mode 100644 index 0000000..97e0d05 --- /dev/null +++ b/connector/shared/src/commonMain/kotlin/XPC.kt @@ -0,0 +1,27 @@ +import gov.nasa.xpc.XPlaneConnect +import io.ktor.utils.io.errors.* +import java.net.SocketException + +class XPC { + + private lateinit var xpc: XPlaneConnect + + /** + * Connects to the X-Plane and tests the connection + * + * @return `true` if connection successfully established, `false` otherwise + */ + fun connected(): Boolean { + try { + xpc = XPlaneConnect() + + // Ensure connection is established + xpc.getDREF("sim/test/test_float") + return true + } catch (ex: SocketException) { + return false + } catch (ex: IOException) { + return false + } + } +}