fun resolveWatches()

in code/platform/src/main/kotlin/org/fbme/ide/platform/debugger/WatcherFacade.kt [208:253]


        fun resolveWatches(device: DeviceDeclaration, watch: String): Map<WatchableData, String> {
            return try {
                val doc = JDOMUtil.loadDocument(ByteArrayInputStream(watch.toByteArray()))
                val watches: MutableMap<WatchableData, String> = HashMap()
                val watchesElement = doc.rootElement.getChild("Watches")
                for (resourceElement in watchesElement.getChildren("Resource")) {
                    val resourceName = resourceElement.getAttributeValue("name")
                    val resource = device.resources.firstOrNull { it.name == resourceName }
                        ?: error("resource not found")
                    for (fbElement in resourceElement.getChildren("FB")) {
                        val fbPath = fbElement.getAttributeValue("name").split(".")

                        val firstFB = resource.allFunctionBlocks().firstOrNull { it.name == fbPath.first() }
                            ?: error("fb not found")

                        val fbDeclarationsPath = mutableListOf(firstFB)

                        var currentFB = firstFB

                        for (fbName in fbPath.drop(1)) {
                            val currentFBDeclaration = currentFB.type.declaration as FBTypeDeclaration

                            if (currentFBDeclaration is DeclarationWithNetwork) {
                                currentFB = currentFBDeclaration.network.functionBlocks
                                    .firstOrNull { it.name == fbName }
                                    ?: error("fb not found")
                            }

                            fbDeclarationsPath.add(currentFB)
                        }

                        for (portElement in fbElement.getChildren("Port")) {
                            val portName = portElement.getAttributeValue("name")
                            val dataElement = portElement.getChild("Data")
                            if (dataElement != null) {
                                watches[Watchable(WatchablePath(resource, fbDeclarationsPath), portName).serialize()] =
                                    dataElement.getAttributeValue("value")
                            }
                        }
                    }
                }
                watches
            } catch (e: Throwable) {
                error(e)
            }
        }