Skip to content

Commit

Permalink
[api][desktop]: Fixes & improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Oct 9, 2024
1 parent 20bfed7 commit 18c114d
Show file tree
Hide file tree
Showing 65 changed files with 562 additions and 671 deletions.
1 change: 0 additions & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ dependencies {
implementation(libs.okhttp)
implementation(libs.oshi)
implementation(libs.javalin)
implementation(libs.validator)
implementation(libs.koin)
implementation(libs.airline)

Expand Down
22 changes: 0 additions & 22 deletions api/src/main/kotlin/nebulosa/api/Nebulosa.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import nebulosa.api.inject.*
import nebulosa.json.PathModule
import nebulosa.log.loggerFor
import org.koin.core.context.startKoin
import java.nio.file.Path
import java.time.LocalDate
import java.time.LocalTime
import java.util.concurrent.ConcurrentHashMap

@Command(name = "nebulosa")
Expand All @@ -36,11 +33,6 @@ class Nebulosa : Runnable, AutoCloseable {
config.showJavalinBanner = false
// JACKSON
config.jsonMapper(JavalinJackson(OBJECT_MAPPER))
// VALIDATOR
config.validation.register(Path::class.java, Path::of)
config.validation.register(Location::class.java, LocationConverter)
config.validation.register(LocalTime::class.java, LocalTimeConverter)
config.validation.register(LocalDate::class.java, LocalDateConverter)
// CORS
config.bundledPlugins.enableCors { cors ->
cors.addRule {
Expand Down Expand Up @@ -73,20 +65,6 @@ class Nebulosa : Runnable, AutoCloseable {
}
}

private data object LocalTimeConverter : (String) -> LocalTime? {

override fun invoke(value: String): LocalTime? {
return if (value.isBlank()) null else LocalTime.parse(value)
}
}

private data object LocalDateConverter : (String) -> LocalDate? {

override fun invoke(value: String): LocalDate? {
return if (value.isBlank()) null else LocalDate.parse(value)
}
}

companion object {

@JvmStatic private val LOG = loggerFor<Nebulosa>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package nebulosa.api.alignment.polar

import io.javalin.Javalin
import io.javalin.http.Context
import io.javalin.http.bodyValidator
import io.javalin.http.bodyAsClass
import nebulosa.api.alignment.polar.darv.DARVStartRequest
import nebulosa.api.alignment.polar.tppa.TPPAStartRequest
import nebulosa.api.connection.ConnectionService
import nebulosa.api.javalin.validate
import nebulosa.api.javalin.notNull
import nebulosa.api.javalin.valid

class PolarAlignmentController(
app: Javalin,
Expand All @@ -26,9 +27,9 @@ class PolarAlignmentController(
}

private fun darvStart(ctx: Context) {
val camera = connectionService.camera(ctx.pathParam("camera"))!!
val guideOutput = connectionService.guideOutput(ctx.pathParam("guideOutput"))!!
val body = ctx.bodyValidator<DARVStartRequest>().validate().get()
val camera = connectionService.camera(ctx.pathParam("camera")).notNull()
val guideOutput = connectionService.guideOutput(ctx.pathParam("guideOutput")).notNull()
val body = ctx.bodyAsClass<DARVStartRequest>().valid()
polarAlignmentService.darvStart(camera, guideOutput, body)
}

Expand All @@ -38,14 +39,14 @@ class PolarAlignmentController(
}

private fun darvStatus(ctx: Context) {
val camera = connectionService.camera(ctx.pathParam("camera"))!!
val camera = connectionService.camera(ctx.pathParam("camera")).notNull()
polarAlignmentService.darvStatus(camera)?.also(ctx::json)
}

private fun tppaStart(ctx: Context) {
val camera = connectionService.camera(ctx.pathParam("camera"))!!
val mount = connectionService.mount(ctx.pathParam("mount"))!!
val body = ctx.bodyValidator<TPPAStartRequest>().validate().get()
val camera = connectionService.camera(ctx.pathParam("camera")).notNull()
val mount = connectionService.mount(ctx.pathParam("mount")).notNull()
val body = ctx.bodyAsClass<TPPAStartRequest>().valid()
polarAlignmentService.tppaStart(camera, mount, body)
}

Expand All @@ -65,7 +66,7 @@ class PolarAlignmentController(
}

private fun tppaStatus(ctx: Context) {
val camera = connectionService.camera(ctx.pathParam("camera"))!!
val camera = connectionService.camera(ctx.pathParam("camera")).notNull()
polarAlignmentService.tppaStatus(camera)?.also(ctx::json)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import nebulosa.api.message.MessageService
import nebulosa.indi.device.camera.Camera
import nebulosa.indi.device.camera.CameraEvent
import nebulosa.indi.device.guider.GuideOutput
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -19,10 +20,15 @@ import java.util.function.Consumer
class DARVExecutor(
private val messageService: MessageService,
private val executorService: ExecutorService,
eventBus: EventBus,
) : Consumer<MessageEvent>, CameraEventAware, Executor by executorService {

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

init {
eventBus.register(this)
}

override fun accept(event: MessageEvent) {
messageService.sendMessage(event)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ data class DARVJob(
status.state = if (event.task === delayTask) DARVState.INITIAL_PAUSE
else if (event.task === forwardGuidePulseTask.delayTask) DARVState.FORWARD
else DARVState.BACKWARD

status.capture.handleCameraDelayEvent(event, CameraCaptureState.EXPOSURING)
}
is CameraExposureEvent -> {
status.capture.handleCameraExposureEvent(event)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package nebulosa.api.alignment.polar.darv

import jakarta.validation.Valid
import nebulosa.api.cameras.CameraStartCaptureRequest
import nebulosa.api.javalin.Validatable
import nebulosa.guiding.GuideDirection

data class DARVStartRequest(
@JvmField @field:Valid val capture: CameraStartCaptureRequest = CameraStartCaptureRequest.EMPTY,
@JvmField val capture: CameraStartCaptureRequest = CameraStartCaptureRequest.EMPTY,
@JvmField val direction: GuideDirection = GuideDirection.NORTH,
@JvmField val reversed: Boolean = false,
)
) : Validatable {

override fun validate() {
capture.validate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class TPPAEvent(
@JvmField @field:JsonSerialize(using = DeclinationSerializer::class) var declination: Angle = 0.0,
@JvmField @field:JsonSerialize(using = DeclinationSerializer::class) var azimuthError: Angle = 0.0,
@JvmField @field:JsonSerialize(using = DeclinationSerializer::class) var altitudeError: Angle = 0.0,
@JvmField @JsonSerialize(using = DeclinationSerializer::class) var totalError: Angle = 0.0,
@JvmField @field:JsonSerialize(using = DeclinationSerializer::class) var totalError: Angle = 0.0,
@JvmField var azimuthErrorDirection: String = "",
@JvmField var altitudeErrorDirection: String = "",
@JvmField @field:JsonIgnoreProperties("camera") val capture: CameraCaptureEvent = CameraCaptureEvent(camera),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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 okhttp3.OkHttpClient
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -17,12 +17,16 @@ import java.util.function.Consumer

class TPPAExecutor(
private val messageService: MessageService,
private val httpClient: OkHttpClient,
private val executorService: ExecutorService,
eventBus: EventBus,
) : Consumer<MessageEvent>, CameraEventAware, MountEventAware {

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

init {
eventBus.register(this)
}

override fun accept(event: MessageEvent) {
messageService.sendMessage(event)
}
Expand All @@ -44,7 +48,7 @@ class TPPAExecutor(
check(jobs.none { it.camera === camera }) { "${camera.name} TPPA Job is already in progress" }
check(jobs.none { it.mount === mount }) { "${camera.name} TPPA Job is already in progress" }

val solver = request.plateSolver.get(httpClient)
val solver = request.plateSolver.get()

with(TPPAJob(this, camera, solver, request, mount)) {
val completable = runAsync(executorService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ data class TPPAJob(
mount.tracking(false)
}

status.capture.handleCameraCaptureFinished()
status.state = TPPAState.FINISHED
status.send()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package nebulosa.api.alignment.polar.tppa

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import jakarta.validation.Valid
import jakarta.validation.constraints.NotNull
import nebulosa.api.beans.converters.time.DurationUnit
import nebulosa.api.cameras.CameraStartCaptureRequest
import nebulosa.api.javalin.Validatable
import nebulosa.api.javalin.min
import nebulosa.api.platesolver.PlateSolverRequest
import nebulosa.guiding.GuideDirection
import org.hibernate.validator.constraints.time.DurationMin
import java.time.Duration
import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit

data class TPPAStartRequest(
@JsonIgnoreProperties("camera", "focuser", "wheel") @JvmField val capture: CameraStartCaptureRequest = CameraStartCaptureRequest.EMPTY,
@field:NotNull @Valid @JvmField val plateSolver: PlateSolverRequest = PlateSolverRequest.EMPTY,
@JvmField val plateSolver: PlateSolverRequest = PlateSolverRequest.EMPTY,
@JvmField val startFromCurrentPosition: Boolean = true,
@JvmField val compensateRefraction: Boolean = false,
@JvmField val stopTrackingWhenDone: Boolean = true,
@field:DurationMin(seconds = 1L) @JvmField val stepDirection: GuideDirection = GuideDirection.EAST,
@field:DurationUnit(ChronoUnit.SECONDS) @field:DurationMin(seconds = 1L) @JvmField val stepDuration: Duration = Duration.ZERO,
@JvmField val stepDirection: GuideDirection = GuideDirection.EAST,
@field:DurationUnit(ChronoUnit.SECONDS) @JvmField val stepDuration: Duration = Duration.ZERO,
@JvmField val stepSpeed: String? = null,
)
) : Validatable {

override fun validate() {
plateSolver.validate()
stepDuration.min(1L, TimeUnit.SECONDS)
}
}
Loading

0 comments on commit 18c114d

Please sign in to comment.