Skip to content

Commit

Permalink
[api]: Implement INDI DustCap
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Aug 25, 2024
1 parent 1c3f847 commit 398744d
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import nebulosa.indi.client.device.camera.AsiCamera
import nebulosa.indi.client.device.camera.INDICamera
import nebulosa.indi.client.device.camera.SVBonyCamera
import nebulosa.indi.client.device.camera.SimCamera
import nebulosa.indi.client.device.dustcap.INDIDustCap
import nebulosa.indi.client.device.focuser.INDIFocuser
import nebulosa.indi.client.device.mount.INDIMount
import nebulosa.indi.client.device.rotator.INDIRotator
import nebulosa.indi.client.device.wheel.INDIFilterWheel
import nebulosa.indi.device.Device
import nebulosa.indi.device.INDIDeviceProvider
import nebulosa.indi.device.camera.Camera
import nebulosa.indi.device.dustcap.DustCap
import nebulosa.indi.device.filterwheel.FilterWheel
import nebulosa.indi.device.focuser.Focuser
import nebulosa.indi.device.gps.GPS
Expand Down Expand Up @@ -76,6 +78,10 @@ data class INDIClient(val connection: INDIConnection) : INDIDeviceProtocolHandle
return INDIGuideOutput(this, name)
}

override fun newDustCap(name: String, executable: String): DustCap {
return INDIDustCap(this, name)
}

override fun start() {
super.start()
sendMessageToServer(GetProperties())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import nebulosa.indi.device.Device
import nebulosa.indi.device.DeviceMessageReceived
import nebulosa.indi.device.MessageSender
import nebulosa.indi.device.camera.Camera
import nebulosa.indi.device.dustcap.DustCap
import nebulosa.indi.device.filterwheel.FilterWheel
import nebulosa.indi.device.focuser.Focuser
import nebulosa.indi.device.gps.GPS
Expand Down Expand Up @@ -46,6 +47,8 @@ abstract class INDIDeviceProtocolHandler : AbstractINDIDeviceProvider(), Message

protected abstract fun newGuideOutput(name: String, executable: String): GuideOutput

protected abstract fun newDustCap(name: String, executable: String): DustCap

private fun registerCamera(message: TextVector<*>): Camera? {
val executable = message["DRIVER_EXEC"]?.value

Expand Down Expand Up @@ -116,6 +119,16 @@ abstract class INDIDeviceProtocolHandler : AbstractINDIDeviceProvider(), Message
}
}

private fun registerDustCap(message: TextVector<*>): DustCap? {
val executable = message["DRIVER_EXEC"]?.value

return if (!executable.isNullOrEmpty() && message.device.isNotEmpty() && dustCap(message.device) == null) {
newDustCap(message.device, executable).also(::registerDustCap)
} else {
null
}
}

open fun start() {
if (protocolReader == null) {
protocolReader = INDIProtocolReader(this, Thread.MIN_PRIORITY)
Expand Down Expand Up @@ -220,6 +233,14 @@ abstract class INDIDeviceProtocolHandler : AbstractINDIDeviceProvider(), Message
}
}

if (DeviceInterfaceType.isDustCap(interfaceType)) {
registerDustCap(message)?.also {
registered = true
it.handleMessage(message)
takeMessageFromReorderingQueue(it)
}
}

if (!registered) {
LOG.warn("device is not registered. name={}, interface={}", message.device, interfaceType)
notRegisteredDevices.add(message.device)
Expand Down Expand Up @@ -258,6 +279,7 @@ abstract class INDIDeviceProtocolHandler : AbstractINDIDeviceProvider(), Message
is Rotator -> unregisterRotator(device)
is GPS -> unregisterGPS(device)
is GuideOutput -> unregisterGuideOutput(device)
is DustCap -> unregisterDustCap(device)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package nebulosa.indi.client.device.dustcap

import nebulosa.indi.client.INDIClient
import nebulosa.indi.client.device.INDIDevice
import nebulosa.indi.device.dustcap.DustCap
import nebulosa.indi.device.dustcap.DustCapCanParkChanged
import nebulosa.indi.device.dustcap.DustCapParkChanged
import nebulosa.indi.device.firstOnSwitchOrNull
import nebulosa.indi.protocol.DefSwitchVector
import nebulosa.indi.protocol.DefVector.Companion.isNotReadOnly
import nebulosa.indi.protocol.INDIProtocol
import nebulosa.indi.protocol.SwitchVector
import nebulosa.indi.protocol.Vector.Companion.isBusy

internal open class INDIDustCap(
override val sender: INDIClient,
override val name: String,
) : INDIDevice(), DustCap {

@Volatile final override var canPark = false
@Volatile final override var parking = false
@Volatile final override var parked = false

override fun handleMessage(message: INDIProtocol) {
when (message) {
is SwitchVector<*> -> {
when (message.name) {
"CAP_PARK" -> {
if (message is DefSwitchVector) {
canPark = message.isNotReadOnly

sender.fireOnEventReceived(DustCapCanParkChanged(this))
}

parking = message.isBusy
parked = message.firstOnSwitchOrNull()?.name == "PARK"

sender.fireOnEventReceived(DustCapParkChanged(this))
}
}
}
else -> Unit
}

super.handleMessage(message)
}

override fun park() {
if (canPark) {
sendNewSwitch("CAP_PARK", "PARK" to true)
}
}

override fun unpark() {
if (canPark) {
sendNewSwitch("CAP_PARK", "UNPARK" to true)
}
}

override fun close() = Unit

override fun toString() = "DustCap(name=$name, connected=$connected, parking=$parking, parked=$parked)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import nebulosa.indi.device.camera.Camera
import nebulosa.indi.device.camera.CameraAttached
import nebulosa.indi.device.camera.CameraDetached
import nebulosa.indi.device.camera.GuideHead
import nebulosa.indi.device.dustcap.DustCap
import nebulosa.indi.device.dustcap.DustCapAttached
import nebulosa.indi.device.dustcap.DustCapDetached
import nebulosa.indi.device.filterwheel.FilterWheel
import nebulosa.indi.device.filterwheel.FilterWheelAttached
import nebulosa.indi.device.filterwheel.FilterWheelDetached
Expand Down Expand Up @@ -38,6 +41,7 @@ abstract class AbstractINDIDeviceProvider : INDIDeviceProvider {
private val rotators = HashMap<String, Rotator>(2)
private val gps = HashMap<String, GPS>(2)
private val guideOutputs = HashMap<String, GuideOutput>(2)
private val dustCaps = HashMap<String, DustCap>(2)
private val thermometers = HashMap<String, Thermometer>(2)

override fun registerDeviceEventHandler(handler: DeviceEventHandler) = handlers.add(handler)
Expand All @@ -57,6 +61,7 @@ abstract class AbstractINDIDeviceProvider : INDIDeviceProvider {
rotator(id)?.also(devices::add)
gps(id)?.also(devices::add)
guideOutput(id)?.also(devices::add)
dustCap(id)?.also(devices::add)
thermometer(id)?.also(devices::add)
return devices
}
Expand Down Expand Up @@ -89,6 +94,10 @@ abstract class AbstractINDIDeviceProvider : INDIDeviceProvider {

override fun guideOutput(id: String) = guideOutputs[id]

override fun dustCaps() = dustCaps.values.toSet()

override fun dustCap(id: String) = dustCaps[id]

override fun thermometers() = thermometers.values.toSet()

override fun thermometer(id: String) = thermometers[id]
Expand Down Expand Up @@ -213,6 +222,21 @@ abstract class AbstractINDIDeviceProvider : INDIDeviceProvider {
LOG.info("guide output detached: {} ({})", device.name, device.id)
}

fun registerDustCap(device: DustCap): Boolean {
if (device.id in dustCaps) return false
dustCaps[device.id] = device
dustCaps[device.name] = device
fireOnEventReceived(DustCapAttached(device))
LOG.info("dust cap attached: {} ({})", device.name, device.id)
return true
}

fun unregisterDustCap(device: DustCap) {
dustCaps.remove(device.name)
fireOnEventReceived(DustCapDetached(dustCaps.remove(device.id) ?: return))
LOG.info("dust cap detached: {} ({})", device.name, device.id)
}

fun registerThermometer(device: Thermometer): Boolean {
if (device.id in thermometers) return false
thermometers[device.id] = device
Expand All @@ -236,6 +260,7 @@ abstract class AbstractINDIDeviceProvider : INDIDeviceProvider {
rotators().onEach(Device::close).onEach(::unregisterRotator)
gps().onEach(Device::close).onEach(::unregisterGPS)
guideOutputs().onEach(Device::close).onEach(::unregisterGuideOutput)
dustCaps().onEach(Device::close).onEach(::unregisterDustCap)

cameras.clear()
mounts.clear()
Expand All @@ -244,6 +269,7 @@ abstract class AbstractINDIDeviceProvider : INDIDeviceProvider {
rotators.clear()
gps.clear()
guideOutputs.clear()
dustCaps.clear()
thermometers.clear()

handlers.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ enum class DeviceType(@JvmField val code: String) {
DOME("DOM"),
SWITCH("SWT"),
GUIDE_OUTPUT("GDT"),
DUST_CAP("DCP"),
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nebulosa.indi.device

import nebulosa.indi.device.camera.Camera
import nebulosa.indi.device.dustcap.DustCap
import nebulosa.indi.device.filterwheel.FilterWheel
import nebulosa.indi.device.focuser.Focuser
import nebulosa.indi.device.gps.GPS
Expand Down Expand Up @@ -49,6 +50,10 @@ interface INDIDeviceProvider : MessageSender, AutoCloseable {

fun guideOutput(id: String): GuideOutput?

fun dustCaps(): Collection<DustCap>

fun dustCap(id: String): DustCap?

fun thermometers(): Collection<Thermometer>

fun thermometer(id: String): Thermometer?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package nebulosa.indi.device.dustcap

import nebulosa.indi.device.Device
import nebulosa.indi.device.DeviceType
import nebulosa.indi.device.Parkable

interface DustCap : Device, Parkable
interface DustCap : Device, Parkable {

override val type
get() = DeviceType.DUST_CAP
}

0 comments on commit 398744d

Please sign in to comment.