From d1b45a4d5ab088c1487f45faddf86b7a69ff2a6f Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Thu, 9 May 2024 09:00:55 +0100 Subject: [PATCH] feat(connector): add parser for String to Aircraft --- .../connector/shared/vdmj/VDMJTransaction.kt | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/vdmj/VDMJTransaction.kt b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/vdmj/VDMJTransaction.kt index 4795cc4..c45f9b4 100644 --- a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/vdmj/VDMJTransaction.kt +++ b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/vdmj/VDMJTransaction.kt @@ -38,14 +38,14 @@ class VDMJTransaction(val actions: List, private val xpc: XPC) { aircraft = Aircraft(items = items, procedure = procedures) } - suspend fun expectedEndState(): String { + suspend fun expectedEndState(): Aircraft { val command = "p complete_procedure(${aircraft.toVDMString()})" println(command) val result = vdmj.run(command = command) - return result.output + return parseAircraftString(result.output) } // fun nextStep() { @@ -73,4 +73,39 @@ class VDMJTransaction(val actions: List, private val xpc: XPC) { return items } + + private fun parseAircraftString(aircraftString: String): Aircraft { + val itemsString = aircraftString.substringAfter("mk_Aircraft({").substringBefore("}, [") + val proceduresString = aircraftString.substringAfter("}, [").substringBefore("])") + + val items = itemsString.split("), ").associate { itemString -> + val key = itemString.substringBefore(" |-> ") + val switch = itemString.substringAfter("mk_Switch(").substringBefore(")") + val valueString = switch.substringAfter("<").substringBefore(">, ") + val middlePosition = switch.takeLast(4) == "true" + val position = when (valueString) { + "OFF" -> 0 + "MIDDLE" -> 1 + "ON" -> if (middlePosition) 2 else 1 + else -> throw IOException("Switch has incorrect switch type object") + } + key to ItemObject(ItemType.SWITCH, Switch(position, middlePosition)) + } + + val procedures = proceduresString.split("), ").map { procedureString -> + val parts = procedureString.removePrefix("mk_ChecklistItem(").removeSuffix(")").split(", ") + val dref = parts[0].removeSurrounding("\"") + val type = ItemType.SWITCH + val goal = when (parts[2]) { + "" -> 0 + "" -> 1 + "" -> if (aircraft.items[dref]?.item?.middlePosition == true) 2 else 1 + else -> throw IOException("Procedure has incorrect switch type object") + } + val complete = parts[3] == "true" + ProcedureItem(dref, type, goal, complete) + }.toMutableList() + + return Aircraft(items, procedures) + } }