All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.hassemble.core.boot.StateChangeEventSubscriber.kt Maven / Gradle / Ivy

The newest version!
package hassemble.core.boot

import co.touchlab.kermit.Kermit
import hassemble.WebSocketSession
import hassemble.communicating.CALLER_ID
import hassemble.core.ResultResponse

internal class StateChangeEventSubscriber(
    private val session: WebSocketSession
) {

    private val logger = Kermit()
    private val id
        get() = CALLER_ID.incrementAndGet()

    suspend fun subscribe() {
        sendEventListenerRequest()
        consumeResultResponse().let { resultResponse ->
            when (resultResponse.success) {
                false -> logger.e { "Could not subscribe to state change events" }
                true -> logger.i { "Successfully started listening to state changes" }
            }
        }
    }

    private val eventListenerRequest =
        EventListeningRequest(id = id, eventType = "state_changed")

    private suspend fun sendEventListenerRequest() =
        session.callWebSocketApi(eventListenerRequest)

    private suspend fun consumeResultResponse() =
        session.consumeSingleMessage()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy