Skip to content

Commit

Permalink
[api]: Implement DustCap endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Aug 25, 2024
1 parent 398744d commit 015eb4f
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import nebulosa.api.calibration.CalibrationFrameRepository
import nebulosa.api.connection.ConnectionService
import nebulosa.indi.device.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 @@ -41,6 +42,7 @@ class DeviceOrEntityParamMethodArgumentResolver(
Rotator::class.java to { connectionService.rotator(it) },
GPS::class.java to { connectionService.gps(it) },
GuideOutput::class.java to { connectionService.guideOutput(it) },
DustCap::class.java to { connectionService.dustCap(it) },
Thermometer::class.java to { connectionService.thermometer(it) },
)

Expand Down
18 changes: 18 additions & 0 deletions api/src/main/kotlin/nebulosa/api/connection/ConnectionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import nebulosa.indi.device.Device
import nebulosa.indi.device.DeviceEventHandler
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 @@ -134,6 +135,10 @@ class ConnectionService(
return providers[id]?.guideOutputs() ?: emptyList()
}

fun dustCaps(id: String): Collection<DustCap> {
return providers[id]?.dustCaps() ?: emptyList()
}

fun thermometers(id: String): Collection<Thermometer> {
return providers[id]?.thermometers() ?: emptyList()
}
Expand Down Expand Up @@ -166,6 +171,10 @@ class ConnectionService(
return providers.values.flatMap { it.guideOutputs() }
}

fun dustCaps(): List<DustCap> {
return providers.values.flatMap { it.dustCaps() }
}

fun thermometers(): List<Thermometer> {
return providers.values.flatMap { it.thermometers() }
}
Expand Down Expand Up @@ -198,6 +207,10 @@ class ConnectionService(
return providers[id]?.guideOutput(name)
}

fun dustCap(id: String, name: String): DustCap? {
return providers[id]?.dustCap(name)
}

fun thermometer(id: String, name: String): Thermometer? {
return providers[id]?.thermometer(name)
}
Expand Down Expand Up @@ -230,6 +243,10 @@ class ConnectionService(
return providers.firstNotNullOfOrNull { it.value.guideOutput(name) }
}

fun dustCap(name: String): DustCap? {
return providers.firstNotNullOfOrNull { it.value.dustCap(name) }
}

fun thermometer(name: String): Thermometer? {
return providers.firstNotNullOfOrNull { it.value.thermometer(name) }
}
Expand All @@ -241,6 +258,7 @@ class ConnectionService(
?: wheel(name)
?: rotator(name)
?: guideOutput(name)
?: dustCap(name)
?: gps(name)
?: thermometer(name)
}
Expand Down
46 changes: 46 additions & 0 deletions api/src/main/kotlin/nebulosa/api/dustcap/DustCapController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package nebulosa.api.dustcap

import nebulosa.api.connection.ConnectionService
import nebulosa.indi.device.dustcap.DustCap
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("dust-cap")
class DustCapController(
private val connectionService: ConnectionService,
private val dustCapService: DustCapService,
) {

@GetMapping
fun dustCaps(): List<DustCap> {
return connectionService.dustCaps().sorted()
}

@GetMapping("{dustCap}")
fun dustCap(dustCap: DustCap): DustCap {
return dustCap
}

@PutMapping("{dustCap}/connect")
fun connect(dustCap: DustCap) {
dustCapService.connect(dustCap)
}

@PutMapping("{dustCap}/disconnect")
fun disconnect(dustCap: DustCap) {
dustCapService.disconnect(dustCap)
}

@PutMapping("{dustCap}/park")
fun park(dustCap: DustCap) {
dustCapService.park(dustCap)
}

@PutMapping("{dustCap}/unpark")
fun unpark(dustCap: DustCap) {
dustCapService.unpark(dustCap)
}
}
16 changes: 16 additions & 0 deletions api/src/main/kotlin/nebulosa/api/dustcap/DustCapDeserializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nebulosa.api.dustcap

import nebulosa.api.beans.converters.device.DeviceDeserializer
import nebulosa.api.connection.ConnectionService
import nebulosa.indi.device.dustcap.DustCap
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.stereotype.Component

@Component
class DustCapDeserializer : DeviceDeserializer<DustCap>(DustCap::class.java) {

@Autowired @Lazy private lateinit var connectionService: ConnectionService

override fun deviceFor(name: String) = connectionService.dustCap(name)
}
8 changes: 8 additions & 0 deletions api/src/main/kotlin/nebulosa/api/dustcap/DustCapEventAware.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package nebulosa.api.dustcap

import nebulosa.indi.device.dustcap.DustCapEvent

fun interface DustCapEventAware {

fun handleDustCapEvent(event: DustCapEvent)
}
36 changes: 36 additions & 0 deletions api/src/main/kotlin/nebulosa/api/dustcap/DustCapEventHub.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nebulosa.api.dustcap

import nebulosa.api.beans.annotations.Subscriber
import nebulosa.api.devices.DeviceEventHub
import nebulosa.api.message.MessageService
import nebulosa.indi.device.DeviceType
import nebulosa.indi.device.PropertyChangedEvent
import nebulosa.indi.device.dustcap.DustCap
import nebulosa.indi.device.dustcap.DustCapAttached
import nebulosa.indi.device.dustcap.DustCapDetached
import nebulosa.indi.device.dustcap.DustCapEvent
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.springframework.stereotype.Component

@Component
@Subscriber
class DustCapEventHub(
private val messageService: MessageService,
) : DeviceEventHub<DustCap, DustCapEvent>(DeviceType.DUST_CAP), DustCapEventAware {

@Subscribe(threadMode = ThreadMode.ASYNC)
override fun handleDustCapEvent(event: DustCapEvent) {
if (event.device.type == DeviceType.DUST_CAP) {
when (event) {
is PropertyChangedEvent -> onNext(event)
is DustCapAttached -> onAttached(event.device)
is DustCapDetached -> onDetached(event.device)
}
}
}

override fun sendMessage(eventName: String, device: DustCap) {
messageService.sendMessage(DustCapMessageEvent(eventName, device))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nebulosa.api.dustcap

import nebulosa.api.devices.DeviceMessageEvent
import nebulosa.indi.device.dustcap.DustCap

data class DustCapMessageEvent(override val eventName: String, override val device: DustCap) : DeviceMessageEvent<DustCap>
24 changes: 24 additions & 0 deletions api/src/main/kotlin/nebulosa/api/dustcap/DustCapSerializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nebulosa.api.dustcap

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import nebulosa.indi.device.dustcap.DustCap
import org.springframework.stereotype.Component

@Component
class DustCapSerializer : StdSerializer<DustCap>(DustCap::class.java) {

override fun serialize(value: DustCap, gen: JsonGenerator, provider: SerializerProvider) {
gen.writeStartObject()
gen.writeStringField("type", value.type.name)
gen.writeStringField("sender", value.sender.id)
gen.writeStringField("id", value.id)
gen.writeStringField("name", value.name)
gen.writeBooleanField("connected", value.connected)
gen.writeBooleanField("canPark", value.canPark)
gen.writeBooleanField("parking", value.parking)
gen.writeBooleanField("parked", value.parked)
gen.writeEndObject()
}
}
28 changes: 28 additions & 0 deletions api/src/main/kotlin/nebulosa/api/dustcap/DustCapService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package nebulosa.api.dustcap

import nebulosa.indi.device.dustcap.DustCap
import org.springframework.stereotype.Service

@Service
class DustCapService(private val dustCapEventHub: DustCapEventHub) {

fun connect(dustCap: DustCap) {
dustCap.connect()
}

fun disconnect(dustCap: DustCap) {
dustCap.disconnect()
}

fun park(dustCap: DustCap) {
dustCap.park()
}

fun unpark(dustCap: DustCap) {
dustCap.unpark()
}

fun listen(dustCap: DustCap) {
dustCapEventHub.listen(dustCap)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package nebulosa.api.focusers
import nebulosa.api.devices.DeviceMessageEvent
import nebulosa.indi.device.focuser.Focuser

data class FocuserMessageEvent(override val eventName: String, override val device: Focuser) :
DeviceMessageEvent<Focuser>
data class FocuserMessageEvent(override val eventName: String, override val device: Focuser) : DeviceMessageEvent<Focuser>
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package nebulosa.api.guiding
import nebulosa.api.devices.DeviceMessageEvent
import nebulosa.indi.device.guider.GuideOutput

data class GuideOutputMessageEvent(override val eventName: String, override val device: GuideOutput) :
DeviceMessageEvent<GuideOutput>
data class GuideOutputMessageEvent(override val eventName: String, override val device: GuideOutput) : DeviceMessageEvent<GuideOutput>
3 changes: 1 addition & 2 deletions api/src/main/kotlin/nebulosa/api/wheels/WheelMessageEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package nebulosa.api.wheels
import nebulosa.api.devices.DeviceMessageEvent
import nebulosa.indi.device.filterwheel.FilterWheel

data class WheelMessageEvent(override val eventName: String, override val device: FilterWheel) :
DeviceMessageEvent<FilterWheel>
data class WheelMessageEvent(override val eventName: String, override val device: FilterWheel) : DeviceMessageEvent<FilterWheel>

0 comments on commit 015eb4f

Please sign in to comment.