Skip to content

Commit

Permalink
[api][desktop]: Refactor tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed May 2, 2024
1 parent e82606a commit ba5e62b
Show file tree
Hide file tree
Showing 27 changed files with 130 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ class PolarAlignmentController(
@RequestBody body: TPPAStartRequest,
) = polarAlignmentService.tppaStart(camera, mount, body)

@PutMapping("tppa/{camera}/{mount}/stop")
fun tppaStop(camera: Camera, mount: Mount) {
polarAlignmentService.tppaStop(camera, mount)
@PutMapping("tppa/{camera}/stop")
fun tppaStop(camera: Camera) {
polarAlignmentService.tppaStop(camera)
}

@PutMapping("tppa/{camera}/{mount}/pause")
fun tppaPause(camera: Camera, mount: Mount) {
polarAlignmentService.tppaPause(camera, mount)
@PutMapping("tppa/{camera}/pause")
fun tppaPause(camera: Camera) {
polarAlignmentService.tppaPause(camera)
}

@PutMapping("tppa/{camera}/{mount}/unpause")
fun tppaUnpause(camera: Camera, mount: Mount) {
polarAlignmentService.tppaUnpause(camera, mount)
@PutMapping("tppa/{camera}/unpause")
fun tppaUnpause(camera: Camera) {
polarAlignmentService.tppaUnpause(camera)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class PolarAlignmentService(
tppaExecutor.execute(camera, mount, tppaStartRequest)
}

fun tppaStop(camera: Camera, mount: Mount) {
tppaExecutor.stop(camera, mount)
fun tppaStop(camera: Camera) {
tppaExecutor.stop(camera)
}

fun tppaPause(camera: Camera, mount: Mount) {
tppaExecutor.pause(camera, mount)
fun tppaPause(camera: Camera) {
tppaExecutor.pause(camera)
}

fun tppaUnpause(camera: Camera, mount: Mount) {
tppaExecutor.unpause(camera, mount)
fun tppaUnpause(camera: Camera) {
tppaExecutor.unpause(camera)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package nebulosa.api.alignment.polar.darv

import nebulosa.api.messages.MessageEvent
import nebulosa.guiding.GuideDirection
import nebulosa.indi.device.camera.Camera
import java.time.Duration

data class DARVEvent(
@JvmField val id: String,
@JvmField val camera: Camera,
@JvmField val remainingTime: Duration = Duration.ZERO,
@JvmField val direction: GuideDirection = GuideDirection.NORTH,
@JvmField val progress: Double = 0.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DARVExecutor(
private val messageService: MessageService,
) : Consumer<DARVEvent> {

private val jobs = ConcurrentHashMap.newKeySet<DARVJob>(2)
private val jobs = ConcurrentHashMap.newKeySet<DARVJob>(1)

override fun accept(event: DARVEvent) {
messageService.sendMessage(event)
Expand All @@ -35,7 +35,8 @@ class DARVExecutor(
fun execute(camera: Camera, guideOutput: GuideOutput, request: DARVStartRequest) {
check(camera.connected) { "${camera.name} Camera is not connected" }
check(guideOutput.connected) { "${guideOutput.name} Guide Output is not connected" }
check(jobs.any { it.task.camera === camera }) { "${camera.name} DARV Job in progress" }
check(jobs.any { it.task.camera === camera }) { "${camera.name} DARV Job is already in progress" }
check(jobs.any { it.task.guideOutput === guideOutput }) { "${camera.name} DARV Job is already in progress" }

val task = DARVTask(camera, guideOutput, request)
task.subscribe(this)
Expand All @@ -48,8 +49,6 @@ class DARVExecutor(
}

fun stop(camera: Camera) {
jobs.find { it.task.camera === camera }
?.also(jobs::remove)
?.stop()
jobs.find { it.task.camera === camera }?.stop()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import nebulosa.indi.device.camera.CameraEvent

data class DARVJob(override val task: DARVTask) : Job() {

override val name = "DARV Job"
override val name = "${task.camera.name} DARV Job"

fun handleCameraEvent(event: CameraEvent) {
task.handleCameraEvent(event)
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/kotlin/nebulosa/api/alignment/polar/darv/DARVTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import nebulosa.indi.device.camera.Camera
import nebulosa.indi.device.camera.CameraEvent
import nebulosa.indi.device.camera.FrameType
import nebulosa.indi.device.guide.GuideOutput
import nebulosa.log.loggerFor
import java.nio.file.Files
import java.time.Duration
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -55,6 +56,8 @@ data class DARVTask(
}

override fun execute(cancellationToken: CancellationToken) {
LOG.info("DARV started. camera={}, guideOutput={}, request={}", camera, guideOutput, request)

val a = CompletableFuture.runAsync {
// CAPTURE.
cameraExposureTask.execute(cancellationToken)
Expand All @@ -72,6 +75,8 @@ data class DARVTask(
}

CompletableFuture.allOf(a, b).join()

LOG.info("DARV finished. camera={}, guideOutput={}, request={}", camera, guideOutput, request)
}

override fun accept(event: Any) {
Expand All @@ -89,4 +94,9 @@ data class DARVTask(
backwardGuidePulseTask.close()
super.close()
}

companion object {

@JvmStatic private val LOG = loggerFor<DARVTask>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import nebulosa.indi.device.camera.Camera
import nebulosa.indi.device.camera.CameraEvent
import nebulosa.indi.device.mount.Mount
import nebulosa.indi.device.mount.MountEvent
import nebulosa.log.info
import nebulosa.log.loggerFor
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.springframework.stereotype.Component
Expand All @@ -22,7 +20,7 @@ class TPPAExecutor(
private val plateSolverService: PlateSolverService,
) : Consumer<TPPAEvent> {

private val jobs = ConcurrentHashMap.newKeySet<TPPAJob>()
private val jobs = ConcurrentHashMap.newKeySet<TPPAJob>(1)

override fun accept(event: TPPAEvent) {
messageService.sendMessage(event)
Expand All @@ -41,10 +39,9 @@ class TPPAExecutor(
@Synchronized
fun execute(camera: Camera, mount: Mount, request: TPPAStartRequest) {
check(camera.connected) { "${camera.name} Camera is not connected" }
check(mount.connected) { "${mount.name} Guide Output is not connected" }
check(jobs.any { it.task.camera === camera || it.task.mount === mount }) { "${camera.name}/${mount.name} TPPA Job in progress" }

LOG.info { "starting TPPA. camera=$camera, mount=$mount, request=$request" }
check(mount.connected) { "${mount.name} Mount is not connected" }
check(jobs.any { it.task.camera === camera }) { "${camera.name} TPPA Job is already in progress" }
check(jobs.any { it.task.mount === mount }) { "${camera.name} TPPA Job is already in progress" }

val solver = plateSolverService.solverFor(request.plateSolver)
val task = TPPATask(camera, solver, request, mount)
Expand All @@ -57,22 +54,15 @@ class TPPAExecutor(
}
}

fun stop(camera: Camera, mount: Mount) {
jobs.find { it.task.camera === camera && it.task.mount === mount }
?.also(jobs::remove)
?.stop()
}

fun pause(camera: Camera, mount: Mount) {
jobs.find { it.task.camera === camera && it.task.mount === mount }?.pause()
fun stop(camera: Camera) {
jobs.find { it.task.camera === camera }?.stop()
}

fun unpause(camera: Camera, mount: Mount) {
jobs.find { it.task.camera === camera && it.task.mount === mount }?.unpause()
fun pause(camera: Camera) {
jobs.find { it.task.camera === camera }?.pause()
}

companion object {

@JvmStatic private val LOG = loggerFor<TPPAExecutor>()
fun unpause(camera: Camera) {
jobs.find { it.task.camera === camera }?.unpause()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import nebulosa.indi.device.mount.MountEvent

data class TPPAJob(override val task: TPPATask) : Job() {

override val name = "Three-Point Polar Alignment Job"
override val name = "${task.camera.name} TPPA Job"

fun handleCameraEvent(event: CameraEvent) {
task.handleCameraEvent(event)
Expand Down
16 changes: 12 additions & 4 deletions api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPATask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import nebulosa.indi.device.camera.CameraEvent
import nebulosa.indi.device.camera.FrameType
import nebulosa.indi.device.mount.Mount
import nebulosa.indi.device.mount.MountEvent
import nebulosa.log.debug
import nebulosa.log.loggerFor
import nebulosa.math.Angle
import nebulosa.math.deg
import nebulosa.math.formatHMS
import nebulosa.math.formatSignedDMS
import nebulosa.plate.solving.PlateSolver
import java.nio.file.Files
import java.nio.file.Path
Expand Down Expand Up @@ -81,9 +82,9 @@ data class TPPATask(
}

override fun execute(cancellationToken: CancellationToken) {
while (!cancellationToken.isDone) {
LOG.debug { "executing TPPA. camera=$camera, mount=$mount, request=$request" }
LOG.info("TPPA started. camera={}, mount={}, request={}", camera, mount, request)

while (!cancellationToken.isDone) {
mount?.tracking(true)

if (cancellationToken.isPaused) {
Expand Down Expand Up @@ -124,7 +125,7 @@ data class TPPATask(
request.compensateRefraction, cancellationToken
)

LOG.info("alignment completed. result=$result")
LOG.info("TPPA alignment completed. result=$result")

if (cancellationToken.isDone) return

Expand Down Expand Up @@ -167,6 +168,11 @@ data class TPPATask(
else -> ""
}

LOG.info(
"TPPA alignment computed. rightAscension={}, declination={}, azimuthError={}, altitudeError={}",
rightAscension.formatHMS(), declination.formatSignedDMS(), azimuthError.formatSignedDMS(), altitudeError.formatSignedDMS(),
)

sendEvent(TPPAState.COMPUTED)

continue
Expand All @@ -179,6 +185,8 @@ data class TPPATask(
}

sendEvent(TPPAState.FINISHED)

LOG.info("TPPA finished. camera={}, mount={}, request={}", camera, mount, request)
}

private fun sendEvent(state: TPPAState) {
Expand Down
5 changes: 2 additions & 3 deletions api/src/main/kotlin/nebulosa/api/cameras/CameraCaptureTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore
import io.reactivex.rxjava3.functions.Consumer
import nebulosa.api.guiding.DitherAfterExposureEvent
import nebulosa.api.guiding.DitherAfterExposureTask
import nebulosa.api.guiding.WaitForSettleEvent
import nebulosa.api.guiding.WaitForSettleTask
import nebulosa.api.tasks.Task
import nebulosa.api.tasks.delay.DelayEvent
Expand Down Expand Up @@ -93,6 +92,8 @@ data class CameraCaptureTask(
ditherAfterExposureTask.execute(cancellationToken)
}
}

LOG.info("camera capture finished. camera={}, request={}, exposureCount={}", camera, request, exposureCount)
}

@Synchronized
Expand All @@ -105,8 +106,6 @@ data class CameraCaptureTask(
stepRemainingTime = event.remainingTime
stepProgress = event.progress
}
is WaitForSettleEvent -> {
}
is CameraExposureEvent -> {
when (event.state) {
CameraExposureState.STARTED -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data class DitherAfterExposureTask(
&& guider.state == GuideState.GUIDING
&& !cancellationToken.isDone
) {
LOG.info("dithering. request={}", request)
LOG.info("Dither started. request={}", request)

try {
cancellationToken.listen(this)
Expand All @@ -49,6 +49,8 @@ data class DitherAfterExposureTask(

guider.unregisterGuiderListener(this)
cancellationToken.unlisten(this)

LOG.info("Dither finished. request={}", request)
}
}
}
Expand All @@ -58,6 +60,8 @@ data class DitherAfterExposureTask(
this.dy = dy
state = DitherAfterExposureState.DITHERED

LOG.info("dithered. dx={}, dy={}", dx, dy)

ditherLatch.reset()
}

Expand Down
4 changes: 3 additions & 1 deletion api/src/main/kotlin/nebulosa/api/guiding/GuidePulseTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ data class GuidePulseTask(

override fun execute(cancellationToken: CancellationToken) {
if (guideOutput.pulseGuide(request.duration, request.direction)) {
LOG.info("guide pulsing for {} ms at {} direction", request.duration.toMillis(), request.direction)
LOG.info("Guide Pulse started. duration={}, direction={}", request.duration.toMillis(), request.direction)

try {
cancellationToken.listen(this)
delayTask.execute(cancellationToken)
} finally {
cancellationToken.unlisten(this)
}

LOG.info("Guide Pulse finished. duration={}, direction={}", request.duration.toMillis(), request.direction)
}
}

Expand Down

This file was deleted.

12 changes: 4 additions & 8 deletions api/src/main/kotlin/nebulosa/api/guiding/WaitForSettleTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import nebulosa.api.tasks.Task
import nebulosa.common.concurrency.cancel.CancellationToken
import nebulosa.guiding.Guider
import nebulosa.log.loggerFor
import java.time.Duration
import kotlin.system.measureTimeMillis

data class WaitForSettleTask(
@JvmField val guider: Guider?,
) : Task<WaitForSettleEvent>() {
) : Task<Unit>() {

override fun execute(cancellationToken: CancellationToken) {
if (guider != null && guider.isSettling && !cancellationToken.isDone) {
LOG.info("waiting for guiding to settle")

onNext(WaitForSettleEvent(this))
val elapsedTime = measureTimeMillis { guider.waitForSettle(cancellationToken) }
onNext(WaitForSettleEvent(this, Duration.ofMillis(elapsedTime)))
LOG.info("Wait For Settle started")
guider.waitForSettle(cancellationToken)
LOG.info("Wait For Settle finished")
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/src/main/kotlin/nebulosa/api/image/ImageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class ImageService(
}
}

LOG.info("Found {} minor planets", count)
LOG.info("found {} minor planets", count)
}.whenComplete { _, e -> e?.printStackTrace() }
.also(tasks::add)
}
Expand Down Expand Up @@ -256,7 +256,7 @@ class ImageService(
count++
}

LOG.info("Found {} stars/DSOs", count)
LOG.info("found {} stars/DSOs", count)
}.whenComplete { _, e -> e?.printStackTrace() }
.also(tasks::add)
}
Expand Down
Loading

0 comments on commit ba5e62b

Please sign in to comment.