czuckie
06/21/2023, 6:55 PMczuckie
06/21/2023, 6:59 PMclass CommonHaService {
val json = Json {}
private val client = HttpClient {
install(WebSockets)
}
var commandId = 1
private val commandChannel = Channel<HAServiceCall>()
private val commandFlow: Flow<HAServiceCall> = flow {
while (true) {
// Forward received commands to command flow collector
emit(commandChannel.receive())
}
}
val eventFlow = flow<HaServiceEvent> {
var authed = false
client.webSocket("...") {
async {
commandFlow.collect {
// Handle sending data
}
}
incoming.receiveAsFlow().catch {
emit(HaServiceEvent.Disconnected)
}.collect {
// Handle receiving data
}
}
}
}
Adel Ayman
06/21/2023, 7:00 PMczuckie
06/21/2023, 7:01 PMczuckie
06/21/2023, 7:02 PMczuckie
06/21/2023, 7:02 PMAdel Ayman
06/21/2023, 7:03 PMczuckie
06/21/2023, 7:04 PMczuckie
06/21/2023, 7:05 PMAdel Ayman
06/21/2023, 7:06 PM