feat(connector): add getAircraftState function for VDMJTransaction

This commit is contained in:
Anthony Berg 2024-05-07 17:53:39 +01:00
parent 5a50114f04
commit d551c1b5c5

View File

@ -9,9 +9,11 @@ import io.ktor.utils.io.errors.*
* VDMJ transactions used to control the aircraft in the simulator * VDMJ transactions used to control the aircraft in the simulator
* and run steps * and run steps
*/ */
class VDMJTransaction(val actions: List<Action>, xpc: XPC) { class VDMJTransaction(val actions: List<Action>, private val xpc: XPC) {
private val vdmj = VDMJ() private val vdmj = VDMJ()
private val drefs: Array<String> = actions.map { it.type }.toTypedArray()
private var aircraft: Aircraft private var aircraft: Aircraft
init { init {
@ -21,36 +23,43 @@ class VDMJTransaction(val actions: List<Action>, xpc: XPC) {
} }
// Create Aircraft // Create Aircraft
val items: MutableMap<String, ItemObject> = mutableMapOf() val items = getAircraftState()
val procedure: MutableList<ProcedureItem> = mutableListOf()
for (item: Action in actions) {
val dref = item.type
val type = ItemType.SWITCH
val initDref = xpc.getState(dref)[0].toInt()
val goal = item.goal.toInt()
val middlePosition: Boolean = goal > 1
items[dref] = ItemObject( val procedures: MutableList<ProcedureItem> = actions.map {
type = type, ProcedureItem(
item = Switch( dref = it.type,
position = initDref, type = ItemType.SWITCH,
middlePosition = middlePosition goal = it.goal.toInt(),
) complete = false
) )
procedure.addLast( }.toMutableList()
ProcedureItem(
dref = dref,
type = type,
goal = goal,
complete = false
)
)
}
aircraft = Aircraft(items = items, procedure = procedure) aircraft = Aircraft(items = items, procedure = procedures)
} }
fun nextStep() { fun nextStep() {
} }
/**
* Gets the state of all the DREFs in X-Plane for [Aircraft.items]
*
* @return Map with key that is a DREF and value [ItemObject]
*/
private fun getAircraftState(): Map<String, ItemObject> {
val initDrefs = xpc.getStates(drefs = drefs)
val items: Map<String, ItemObject> = actions.associateBy(
{ it.type },
{ ItemObject(
type = ItemType.SWITCH,
item = Switch(
position = initDrefs[it.step][0].toInt(),
middlePosition = it.goal.toInt() > 1
)
)}
)
return items
}
} }