Update connection logic

This commit is contained in:
Alexander Nozik 2022-11-21 13:28:39 +03:00
parent 279b848039
commit 4ceffef67a
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
3 changed files with 102 additions and 81 deletions

View File

@ -4,10 +4,10 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"tags": [],
"pycharm": { "pycharm": {
"is_executing": true "is_executing": true
}, }
"tags": []
}, },
"outputs": [], "outputs": [],
"source": [ "source": [

View File

@ -148,9 +148,12 @@ private fun CoroutineScope.collectChange(
/** /**
* Generate a flow of changes of this vision and its children * Generate a flow of changes of this vision and its children
*
* @param sendInitial if true, send the initial vision state as first change
*/ */
public fun Vision.flowChanges( public fun Vision.flowChanges(
collectionDuration: Duration, collectionDuration: Duration,
sendInitial: Boolean = false
): Flow<VisionChange> = flow { ): Flow<VisionChange> = flow {
val manager = manager ?: error("Orphan vision could not collect changes") val manager = manager ?: error("Orphan vision could not collect changes")
coroutineScope { coroutineScope {
@ -158,9 +161,11 @@ public fun Vision.flowChanges(
val mutex = Mutex() val mutex = Mutex()
collectChange(Name.EMPTY, this@flowChanges, mutex, collector) collectChange(Name.EMPTY, this@flowChanges, mutex, collector)
if(sendInitial) {
//Send initial vision state //Send initial vision state
val initialChange = VisionChange(vision = deepCopy(manager)) val initialChange = VisionChange(vision = deepCopy(manager))
emit(initialChange) emit(initialChange)
}
while (true) { while (true) {
//Wait for changes to accumulate //Wait for changes to accumulate

View File

@ -45,7 +45,7 @@ public class VisionClient : AbstractPlugin() {
return attribute?.value return attribute?.value
} }
private val renderers by lazy { context.gather<ElementVisionRenderer>(ElementVisionRenderer.TYPE).values } internal val renderers by lazy { context.gather<ElementVisionRenderer>(ElementVisionRenderer.TYPE).values }
private fun findRendererFor(vision: Vision): ElementVisionRenderer? = renderers.mapNotNull { private fun findRendererFor(vision: Vision): ElementVisionRenderer? = renderers.mapNotNull {
val rating = it.rateVision(vision) val rating = it.rateVision(vision)
@ -71,13 +71,13 @@ public class VisionClient : AbstractPlugin() {
changeCollector.setChild(name, child) changeCollector.setChild(name, child)
} }
private fun renderVision(name: String, element: Element, vision: Vision?, outputMeta: Meta) { private fun renderVision(element: Element, vision: Vision, outputMeta: Meta) {
if (vision != null) {
vision.setAsRoot(visionManager) vision.setAsRoot(visionManager)
val renderer = findRendererFor(vision) val renderer = findRendererFor(vision) ?: error("Could not find renderer for ${vision::class}")
?: error("Could not find renderer for ${vision::class}")
renderer.render(element, vision, outputMeta) renderer.render(element, vision, outputMeta)
}
private fun updateVision(name: String, element: Element, vision: Vision?, outputMeta: Meta) {
element.attributes[OUTPUT_CONNECT_ATTRIBUTE]?.let { attr -> element.attributes[OUTPUT_CONNECT_ATTRIBUTE]?.let { attr ->
val wsUrl = if (attr.value.isBlank() || attr.value == VisionTagConsumer.AUTO_DATA_ATTRIBUTE) { val wsUrl = if (attr.value.isBlank() || attr.value == VisionTagConsumer.AUTO_DATA_ATTRIBUTE) {
val endpoint = resolveEndpoint(element) val endpoint = resolveEndpoint(element)
@ -104,14 +104,16 @@ public class VisionClient : AbstractPlugin() {
stringData stringData
) )
if (change.vision != null) { // If change contains root vision replacement, do it
renderer.render(element, vision, outputMeta) change.vision?.let { vision ->
renderVision(element, vision, outputMeta)
} }
logger.debug { "Got update $change for output with name $name" } logger.debug { "Got update $change for output with name $name" }
if (vision == null) error("Can't update vision because it is not loaded.")
vision.update(change) vision.update(change)
} else { } else {
console.error("WebSocket message data is not a string") logger.error { "WebSocket message data is not a string" }
} }
} }
@ -130,17 +132,16 @@ public class VisionClient : AbstractPlugin() {
changeCollector.reset() changeCollector.reset()
} }
} }
console.info("WebSocket update channel established for output '$name'") logger.info { "WebSocket update channel established for output '$name'" }
} }
onclose = { onclose = {
feedbackJob?.cancel() feedbackJob?.cancel()
console.info("WebSocket update channel closed for output '$name'") logger.info { "WebSocket update channel closed for output '$name'" }
} }
onerror = { onerror = {
feedbackJob?.cancel() feedbackJob?.cancel()
console.error("WebSocket update channel error for output '$name'") logger.error { "WebSocket update channel error for output '$name'" }
}
} }
} }
} }
@ -164,17 +165,8 @@ public class VisionClient : AbstractPlugin() {
VisionManager.defaultJson.decodeFromString(MetaSerializer, it) VisionManager.defaultJson.decodeFromString(MetaSerializer, it)
} ?: Meta.EMPTY } ?: Meta.EMPTY
//Trying to render embedded vision
val embeddedVision = element.getEmbeddedData(VisionTagConsumer.OUTPUT_DATA_CLASS)?.let {
visionManager.decodeFromString(it)
}
when { when {
embeddedVision != null -> { // fetch data if path is provided
logger.info { "Found embedded vision for output with name $name" }
renderVision(name, element, embeddedVision, outputMeta)
}
element.attributes[OUTPUT_FETCH_ATTRIBUTE] != null -> { element.attributes[OUTPUT_FETCH_ATTRIBUTE] != null -> {
val attr = element.attributes[OUTPUT_FETCH_ATTRIBUTE]!! val attr = element.attributes[OUTPUT_FETCH_ATTRIBUTE]!!
@ -195,7 +187,8 @@ public class VisionClient : AbstractPlugin() {
if (response.ok) { if (response.ok) {
response.text().then { text -> response.text().then { text ->
val vision = visionManager.decodeFromString(text) val vision = visionManager.decodeFromString(text)
renderVision(name, element, vision, outputMeta) renderVision(element, vision, outputMeta)
updateVision(name, element, vision, outputMeta)
} }
} else { } else {
logger.error { "Failed to fetch initial vision state from $fetchUrl" } logger.error { "Failed to fetch initial vision state from $fetchUrl" }
@ -203,6 +196,22 @@ public class VisionClient : AbstractPlugin() {
} }
} }
// use embedded data if it is available
element.getElementsByClassName(VisionTagConsumer.OUTPUT_DATA_CLASS).length > 0 -> {
//Getting embedded vision data
val embeddedVision = element.getEmbeddedData(VisionTagConsumer.OUTPUT_DATA_CLASS)!!.let {
visionManager.decodeFromString(it)
}
logger.info { "Found embedded vision for output with name $name" }
renderVision(element, embeddedVision, outputMeta)
updateVision(name, element, embeddedVision, outputMeta)
}
//Try to load vision via websocket
element.attributes[OUTPUT_CONNECT_ATTRIBUTE] != null -> {
updateVision(name, element, null, outputMeta)
}
else -> error("No embedded vision data / fetch url for $name") else -> error("No embedded vision data / fetch url for $name")
} }
element.setAttribute(OUTPUT_RENDERED, "true") element.setAttribute(OUTPUT_RENDERED, "true")
@ -237,7 +246,7 @@ private fun whenDocumentLoaded(block: Document.() -> Unit): Unit {
*/ */
public fun VisionClient.renderAllVisionsIn(element: Element) { public fun VisionClient.renderAllVisionsIn(element: Element) {
val elements = element.getElementsByClassName(VisionTagConsumer.OUTPUT_CLASS) val elements = element.getElementsByClassName(VisionTagConsumer.OUTPUT_CLASS)
console.info("Finished search for outputs. Found ${elements.length} items") logger.info { "Finished search for outputs. Found ${elements.length} items" }
elements.asList().forEach { child -> elements.asList().forEach { child ->
renderVisionIn(child) renderVisionIn(child)
} }
@ -251,7 +260,7 @@ public fun VisionClient.renderAllVisionsById(id: String): Unit = whenDocumentLoa
if (element != null) { if (element != null) {
renderAllVisionsIn(element) renderAllVisionsIn(element)
} else { } else {
console.warn("Element with id $id not found") logger.warn { "Element with id $id not found" }
} }
} }
@ -268,7 +277,14 @@ public class VisionClientApplication(public val context: Context) : Application
private val client = context.fetch(VisionClient) private val client = context.fetch(VisionClient)
override fun start(document: Document, state: Map<String, Any>) { override fun start(document: Document, state: Map<String, Any>) {
console.info("Starting Vision Client") context.logger.info {
"Starting VisionClient with renderers: ${
client.renderers.joinToString(
prefix = "\n\t",
separator = "\n\t"
) { it.name.toString() }
}"
}
val element = document.body ?: error("Document does not have a body") val element = document.body ?: error("Document does not have a body")
client.renderAllVisionsIn(element) client.renderAllVisionsIn(element)
} }
@ -279,7 +295,7 @@ public class VisionClientApplication(public val context: Context) : Application
* Create a vision client context and render all visions on the page. * Create a vision client context and render all visions on the page.
*/ */
public fun runVisionClient(contextBuilder: ContextBuilder.() -> Unit) { public fun runVisionClient(contextBuilder: ContextBuilder.() -> Unit) {
console.info("Starting VisionForge context") Global.logger.info { "Starting VisionForge context" }
val context = Context("VisionForge") { val context = Context("VisionForge") {
plugin(VisionClient) plugin(VisionClient)