Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Aug 20, 2024
2 parents 1160042 + ffc035b commit 29f4e50
Show file tree
Hide file tree
Showing 27 changed files with 1,421 additions and 1,141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import nebulosa.api.mounts.MountEventHub
import nebulosa.api.rotators.RotatorEventHub
import nebulosa.api.wheels.WheelEventHub
import nebulosa.indi.device.DeviceConnectionEvent
import nebulosa.indi.device.DeviceDetached
import nebulosa.indi.device.DeviceEvent
import nebulosa.indi.device.DeviceEventHandler
import nebulosa.indi.device.camera.Camera
Expand Down Expand Up @@ -38,13 +37,6 @@ class ConnectionEventHub(
if (device is FilterWheel) wheelEventHub.onConnectionChanged(device)
if (device is Rotator) rotatorEventHub.onConnectionChanged(device)
if (device is GuideOutput) guideOutputEventHub.onConnectionChanged(device)
} else if (event is DeviceDetached<*>) {
if (device is Camera) cameraEventHub.onDeviceDetached(device)
if (device is Mount) mountEventHub.onDeviceDetached(device)
if (device is Focuser) focuserEventHub.onDeviceDetached(device)
if (device is FilterWheel) wheelEventHub.onDeviceDetached(device)
if (device is Rotator) rotatorEventHub.onDeviceDetached(device)
if (device is GuideOutput) guideOutputEventHub.onDeviceDetached(device)
}
}
}
15 changes: 4 additions & 11 deletions api/src/main/kotlin/nebulosa/api/devices/DeviceEventHub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit

abstract class DeviceEventHub<D : Device, E : DeviceEvent<D>>(deviceType: DeviceType) : Consumer<E>, AutoCloseable {

private val throttlers = HashMap<D, Throttler>(4)
private val throttlers = ConcurrentHashMap<D, Throttler>(4)
private val listenable = ConcurrentHashMap<D, Long>(2)

private val updateEventName = "$deviceType.UPDATED"
Expand All @@ -25,29 +25,22 @@ abstract class DeviceEventHub<D : Device, E : DeviceEvent<D>>(deviceType: Device
}

open fun onAttached(device: D) {
throttlers.computeIfAbsent(device) { Throttler() }
sendMessage(attachedEventName, device)
}

open fun onDetached(device: D) {
sendMessage(detachedEventName, device)
throttlers.remove(device)?.onComplete()
}

open fun onConnectionChanged(device: D) {
sendUpdate(device)
}

open fun onDeviceDetached(device: D) {
synchronized(throttlers) {
throttlers.remove(device)?.onComplete()
}
}

protected open fun onNext(event: E) {
val device = event.device ?: return

synchronized(throttlers) {
throttlers.getOrPut(device, ::Throttler).onNext(event)
}
throttlers[device]?.onNext(event)
}

fun listen(device: D): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class PreferenceRepository(@Qualifier("preferenceBox") override val box: Box<Pre
.build().use { it.findUnique() }
}

@Synchronized
fun deleteByKey(key: String) {
return box.query(PreferenceEntity_.key equal key)
.build().use { it.remove() }
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/kotlin/nebulosa/api/system/GitHubLatestRelease.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package nebulosa.api.system

import com.fasterxml.jackson.annotation.JsonProperty

data class GitHubLatestRelease(
@JvmField @field:JsonProperty("tag_name") val version: String = "",
@JvmField @field:JsonProperty("name") val name: String = "",
@JvmField @field:JsonProperty("draft") val draft: Boolean = false,
@JvmField @field:JsonProperty("prerelease") val preRelease: Boolean = false,
)
13 changes: 13 additions & 0 deletions api/src/main/kotlin/nebulosa/api/system/SystemController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package nebulosa.api.system

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("system")
class SystemController(private val systemService: SystemService) {

@GetMapping("latest-release")
fun latestRelease() = systemService.latestRelease()
}
35 changes: 35 additions & 0 deletions api/src/main/kotlin/nebulosa/api/system/SystemService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package nebulosa.api.system

import com.fasterxml.jackson.databind.ObjectMapper
import okhttp3.OkHttpClient
import okhttp3.Request
import org.springframework.http.HttpHeaders
import org.springframework.stereotype.Service

@Service
class SystemService(
private val httpClient: OkHttpClient,
private val objectMapper: ObjectMapper,
) {

fun latestRelease(): GitHubLatestRelease? {
val request = Request.Builder()
.get()
.url(GITHUB_LATEST_RELEASE_URL)
.header(HttpHeaders.ACCEPT, "application/vnd.github+json")
.header(GITHUB_API_VERSION_HEADER_KEY, GITHUB_API_VERSION_HEADER_VALUE)
.build()

val response = httpClient.newCall(request).execute()

return response.body?.byteStream()
?.use { objectMapper.readValue(it, GitHubLatestRelease::class.java) }
}

companion object {

const val GITHUB_LATEST_RELEASE_URL = "https://api.github.com/repos/tiagohm/nebulosa/releases/latest"
const val GITHUB_API_VERSION_HEADER_KEY = "X-GitHub-Api-Version"
const val GITHUB_API_VERSION_HEADER_VALUE = "2022-11-28"
}
}
Loading

0 comments on commit 29f4e50

Please sign in to comment.