diff --git a/api/src/main/kotlin/nebulosa/api/connection/ConnectionEventHub.kt b/api/src/main/kotlin/nebulosa/api/connection/ConnectionEventHub.kt index 45eb9f3cd..d492782bd 100644 --- a/api/src/main/kotlin/nebulosa/api/connection/ConnectionEventHub.kt +++ b/api/src/main/kotlin/nebulosa/api/connection/ConnectionEventHub.kt @@ -7,7 +7,6 @@ import nebulosa.api.mounts.MountEventHub import nebulosa.api.rotators.RotatorEventHub import nebulosa.api.wheels.WheelEventHub import nebulosa.indi.device.DeviceConnectionEvent -import nebulosa.indi.device.DeviceDetached import nebulosa.indi.device.DeviceEvent import nebulosa.indi.device.DeviceEventHandler import nebulosa.indi.device.camera.Camera @@ -38,13 +37,6 @@ class ConnectionEventHub( if (device is FilterWheel) wheelEventHub.onConnectionChanged(device) if (device is Rotator) rotatorEventHub.onConnectionChanged(device) if (device is GuideOutput) guideOutputEventHub.onConnectionChanged(device) - } else if (event is DeviceDetached<*>) { - if (device is Camera) cameraEventHub.onDeviceDetached(device) - if (device is Mount) mountEventHub.onDeviceDetached(device) - if (device is Focuser) focuserEventHub.onDeviceDetached(device) - if (device is FilterWheel) wheelEventHub.onDeviceDetached(device) - if (device is Rotator) rotatorEventHub.onDeviceDetached(device) - if (device is GuideOutput) guideOutputEventHub.onDeviceDetached(device) } } } diff --git a/api/src/main/kotlin/nebulosa/api/devices/DeviceEventHub.kt b/api/src/main/kotlin/nebulosa/api/devices/DeviceEventHub.kt index 4d18a170b..3b27e0958 100644 --- a/api/src/main/kotlin/nebulosa/api/devices/DeviceEventHub.kt +++ b/api/src/main/kotlin/nebulosa/api/devices/DeviceEventHub.kt @@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit abstract class DeviceEventHub>(deviceType: DeviceType) : Consumer, AutoCloseable { - private val throttlers = HashMap(4) + private val throttlers = ConcurrentHashMap(4) private val listenable = ConcurrentHashMap(2) private val updateEventName = "$deviceType.UPDATED" @@ -25,29 +25,22 @@ abstract class DeviceEventHub>(deviceType: Device } open fun onAttached(device: D) { + throttlers.computeIfAbsent(device) { Throttler() } sendMessage(attachedEventName, device) } open fun onDetached(device: D) { sendMessage(detachedEventName, device) + throttlers.remove(device)?.onComplete() } open fun onConnectionChanged(device: D) { sendUpdate(device) } - open fun onDeviceDetached(device: D) { - synchronized(throttlers) { - throttlers.remove(device)?.onComplete() - } - } - protected open fun onNext(event: E) { val device = event.device ?: return - - synchronized(throttlers) { - throttlers.getOrPut(device, ::Throttler).onNext(event) - } + throttlers[device]?.onNext(event) } fun listen(device: D): Boolean {