Skip to content

Commit da25d4b

Browse files
authored
Replace the original API for retrieving the ISS's position (#506)
1 parent 64434e6 commit da25d4b

6 files changed

Lines changed: 19 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
The people data comes from [The Space Devs API](https://thespacedevs.com/llapi) (names, bios and images of the people
1717
currently in space) and the position of the International Space Station from the
18-
[Open Notify ISS-Now API](http://open-notify.org/Open-Notify-API/ISS-Location-Now/), both served through this
18+
[Where the ISS at? API](https://wheretheiss.at/w/developer), both served through this
1919
project's own small Ktor backend (see `backend` module below).
2020

2121
The project is included as sample in the official [Kotlin Multiplatform docs](https://kotlinlang.org/docs/multiplatform-samples.html) and also the [Google Dev Library](https://devlibrary.withgoogle.com/products/android)

backend/src/jvmMain/kotlin/Server.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dev.johnoreilly.common.di.initKoin
22
import dev.johnoreilly.common.remote.Assignment
33
import dev.johnoreilly.common.remote.AstroResult
4-
import dev.johnoreilly.common.remote.IssResponse
4+
import dev.johnoreilly.common.remote.IssPosition
55
import dev.johnoreilly.common.remote.PeopleInSpaceApi
66
import io.ktor.http.*
77
import io.ktor.openapi.OpenApiInfo
@@ -68,7 +68,7 @@ fun main() {
6868
summary = "Get the position of the ISS"
6969
responses {
7070
HttpStatusCode.OK {
71-
schema = jsonSchema<IssResponse>()
71+
schema = jsonSchema<IssPosition>()
7272
}
7373
}
7474
}

common/src/commonMain/kotlin/dev/johnoreilly/common/remote/PeopleInSpaceApi.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ data class Assignment(
2020
)
2121

2222
@Serializable
23-
data class IssPosition(val latitude: Double, val longitude: Double, val timestamp: Long = -1)
24-
25-
@Serializable
26-
data class IssResponse(val message: String, val iss_position: IssPosition, val timestamp: Long)
23+
data class IssPosition(val latitude: Double, val longitude: Double)
2724

2825
@Single
2926
class PeopleInSpaceApi(private val client: HttpClient) : KoinComponent {
3027
var baseUrl = "https://people-in-space-proxy.ew.r.appspot.com"
28+
var baseIssPositionUrl = "https://api.wheretheiss.at"
3129

3230
suspend fun fetchPeople() = client.get("$baseUrl/astros.json").body<AstroResult>()
33-
suspend fun fetchISSPosition() = client.get("$baseUrl/iss-now.json").body<IssResponse>()
31+
suspend fun fetchISSPosition() = client.get("$baseIssPositionUrl/v1/satellites/25544").body<IssPosition>()
3432
}

common/src/commonMain/kotlin/dev/johnoreilly/common/repository/PeopleInSpaceRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class PeopleInSpaceRepository(
9999
return flow {
100100
while (true) {
101101
try {
102-
val position = peopleInSpaceApi.fetchISSPosition().iss_position
102+
val position = peopleInSpaceApi.fetchISSPosition()
103103
if (currentCoroutineContext().isActive) {
104104
emit(position)
105105
}

common/src/commonMain/kotlin/dev/johnoreilly/common/ui/ISSPositionContent.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.compose.ui.text.font.FontWeight
2424
import androidx.compose.ui.unit.dp
2525
import androidx.lifecycle.compose.collectAsStateWithLifecycle
2626
import dev.johnoreilly.common.viewmodel.ISSPositionViewModel
27+
import dev.johnoreilly.common.util.round
2728

2829
@Composable
2930
fun ISSPositionContent(viewModel: ISSPositionViewModel) {
@@ -54,15 +55,15 @@ fun ISSPositionContent(viewModel: ISSPositionViewModel) {
5455
) {
5556
CoordinateDisplay(
5657
label = "Latitude",
57-
value = position.latitude.toString(),
58+
value = position.latitude.round(4).toString(),
5859
modifier = Modifier.weight(1f)
5960
)
6061

6162
Spacer(Modifier.width(16.dp))
6263

6364
CoordinateDisplay(
6465
label = "Longitude",
65-
value = position.longitude.toString(),
66+
value = position.longitude.round(4).toString(),
6667
modifier = Modifier.weight(1f)
6768
)
6869
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.johnoreilly.common.util
2+
3+
import kotlin.math.round
4+
5+
fun Double.round(decimals: Int): Double {
6+
var multiplier = 1.0
7+
repeat(decimals) { multiplier *= 10 }
8+
return round(this * multiplier) / multiplier
9+
}

0 commit comments

Comments
 (0)