-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
73 changed files
with
1,363 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
api/src/main/kotlin/nebulosa/api/rotators/RotatorController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package nebulosa.api.rotators | ||
|
||
import jakarta.validation.Valid | ||
import nebulosa.api.connection.ConnectionService | ||
import nebulosa.indi.device.rotator.Rotator | ||
import org.hibernate.validator.constraints.Range | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("rotators") | ||
class RotatorController( | ||
private val connectionService: ConnectionService, | ||
private val rotatorService: RotatorService, | ||
) { | ||
|
||
@GetMapping | ||
fun rotators(): List<Rotator> { | ||
return connectionService.rotators().sorted() | ||
} | ||
|
||
@GetMapping("{rotator}") | ||
fun rotator(rotator: Rotator): Rotator { | ||
return rotator | ||
} | ||
|
||
@PutMapping("{rotator}/connect") | ||
fun connect(rotator: Rotator) { | ||
rotatorService.connect(rotator) | ||
} | ||
|
||
@PutMapping("{rotator}/disconnect") | ||
fun disconnect(rotator: Rotator) { | ||
rotatorService.disconnect(rotator) | ||
} | ||
|
||
@PutMapping("{rotator}/reverse") | ||
fun reverse( | ||
rotator: Rotator, | ||
@RequestParam enabled: Boolean, | ||
) { | ||
rotatorService.reverse(rotator, enabled) | ||
} | ||
|
||
@PutMapping("{rotator}/move") | ||
fun move( | ||
rotator: Rotator, | ||
@RequestParam @Valid @Range(min = 0, max = 360) angle: Double, | ||
) { | ||
rotatorService.move(rotator, angle) | ||
} | ||
|
||
@PutMapping("{rotator}/abort") | ||
fun abort(rotator: Rotator) { | ||
rotatorService.abort(rotator) | ||
} | ||
|
||
@PutMapping("{rotator}/home") | ||
fun home(rotator: Rotator) { | ||
rotatorService.home(rotator) | ||
} | ||
|
||
@PutMapping("{rotator}/sync") | ||
fun sync( | ||
rotator: Rotator, | ||
@RequestParam @Valid @Range(min = 0, max = 360) angle: Double, | ||
) { | ||
rotatorService.sync(rotator, angle) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
api/src/main/kotlin/nebulosa/api/rotators/RotatorDeserializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package nebulosa.api.rotators | ||
|
||
import nebulosa.api.beans.converters.device.DeviceDeserializer | ||
import nebulosa.api.connection.ConnectionService | ||
import nebulosa.indi.device.rotator.Rotator | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.context.annotation.Lazy | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class RotatorDeserializer : DeviceDeserializer<Rotator>(Rotator::class.java) { | ||
|
||
@Autowired @Lazy private lateinit var connectionService: ConnectionService | ||
|
||
override fun deviceFor(name: String) = connectionService.rotator(name) | ||
} |
59 changes: 59 additions & 0 deletions
59
api/src/main/kotlin/nebulosa/api/rotators/RotatorEventHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package nebulosa.api.rotators | ||
|
||
import io.reactivex.rxjava3.subjects.PublishSubject | ||
import nebulosa.api.beans.annotations.Subscriber | ||
import nebulosa.api.messages.MessageService | ||
import nebulosa.indi.device.PropertyChangedEvent | ||
import nebulosa.indi.device.rotator.Rotator | ||
import nebulosa.indi.device.rotator.RotatorAttached | ||
import nebulosa.indi.device.rotator.RotatorDetached | ||
import nebulosa.indi.device.rotator.RotatorEvent | ||
import org.greenrobot.eventbus.Subscribe | ||
import org.greenrobot.eventbus.ThreadMode | ||
import org.springframework.stereotype.Component | ||
import java.io.Closeable | ||
import java.util.concurrent.TimeUnit | ||
|
||
@Component | ||
@Subscriber | ||
class RotatorEventHandler( | ||
private val messageService: MessageService, | ||
) : Closeable { | ||
|
||
private val throttler = PublishSubject.create<RotatorEvent>() | ||
|
||
init { | ||
throttler | ||
.throttleLast(1000, TimeUnit.MILLISECONDS) | ||
.subscribe { sendUpdate(it.device!!) } | ||
} | ||
|
||
@Subscribe(threadMode = ThreadMode.ASYNC) | ||
fun onRotatorEvent(event: RotatorEvent) { | ||
when (event) { | ||
is PropertyChangedEvent -> throttler.onNext(event) | ||
is RotatorAttached -> sendMessage(ROTATOR_ATTACHED, event.device) | ||
is RotatorDetached -> sendMessage(ROTATOR_DETACHED, event.device) | ||
} | ||
} | ||
|
||
@Suppress("NOTHING_TO_INLINE") | ||
private inline fun sendMessage(eventName: String, device: Rotator) { | ||
messageService.sendMessage(RotatorMessageEvent(eventName, device)) | ||
} | ||
|
||
fun sendUpdate(device: Rotator) { | ||
sendMessage(ROTATOR_UPDATED, device) | ||
} | ||
|
||
override fun close() { | ||
throttler.onComplete() | ||
} | ||
|
||
companion object { | ||
|
||
const val ROTATOR_UPDATED = "ROTATOR.UPDATED" | ||
const val ROTATOR_ATTACHED = "ROTATOR.ATTACHED" | ||
const val ROTATOR_DETACHED = "ROTATOR.DETACHED" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
api/src/main/kotlin/nebulosa/api/rotators/RotatorMessageEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package nebulosa.api.rotators | ||
|
||
import nebulosa.api.messages.DeviceMessageEvent | ||
import nebulosa.indi.device.rotator.Rotator | ||
|
||
data class RotatorMessageEvent( | ||
override val eventName: String, | ||
override val device: Rotator, | ||
) : DeviceMessageEvent<Rotator> |
30 changes: 30 additions & 0 deletions
30
api/src/main/kotlin/nebulosa/api/rotators/RotatorSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package nebulosa.api.rotators | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator | ||
import com.fasterxml.jackson.databind.SerializerProvider | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer | ||
import nebulosa.indi.device.rotator.Rotator | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class RotatorSerializer : StdSerializer<Rotator>(Rotator::class.java) { | ||
|
||
override fun serialize(value: Rotator, gen: JsonGenerator, provider: SerializerProvider) { | ||
gen.writeStartObject() | ||
gen.writeStringField("sender", value.sender.id) | ||
gen.writeStringField("id", value.id) | ||
gen.writeStringField("name", value.name) | ||
gen.writeBooleanField("connected", value.connected) | ||
gen.writeBooleanField("moving", value.moving) | ||
gen.writeNumberField("angle", value.angle) | ||
gen.writeBooleanField("canAbort", value.canAbort) | ||
gen.writeBooleanField("canReverse", value.canReverse) | ||
gen.writeBooleanField("reversed", value.reversed) | ||
gen.writeBooleanField("canHome", value.canHome) | ||
gen.writeBooleanField("canSync", value.canSync) | ||
gen.writeBooleanField("hasBacklashCompensation", value.hasBacklashCompensation) | ||
gen.writeNumberField("maxAngle", value.maxAngle) | ||
gen.writeNumberField("minAngle", value.minAngle) | ||
gen.writeEndObject() | ||
} | ||
} |
Oops, something went wrong.