Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This library supports parsing any valid JSON file using the following schema ver

- v0.13
- v14
- v15

Note: Endpoints using versions 0.12 and older are not supported, they will
result in a `ParseError` being thrown.
Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/io/spaceapi/types/Area.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* spaceapi-kt
* https://github.com/spaceapi-community/spaceapi-kt/
* Copyright (C) 2020-2024 Danilo Bargen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.spaceapi.types

import kotlinx.serialization.Serializable

@Serializable
data class Area(
@JvmField
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var square_meters: Float,
)
29 changes: 29 additions & 0 deletions src/main/kotlin/io/spaceapi/types/LinkedSpace.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* spaceapi-kt
* https://github.com/spaceapi-community/spaceapi-kt/
* Copyright (C) 2020-2024 Danilo Bargen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.spaceapi.types

import kotlinx.serialization.Serializable

@Serializable
data class LinkedSpace(
@JvmField
var website: String? = null,
@JvmField
var endpoint: String? = null,
)
10 changes: 8 additions & 2 deletions src/main/kotlin/io/spaceapi/types/Location.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ data class Location(
@JvmField
var address: String? = null,
@JvmField
var lat: Float,
var lat: Float? = null,
@JvmField
var lon: Float,
var lon: Float? = null,
@JvmField
var timezone: String? = null,
@JvmField
var country_code: String? = null,
@JvmField
var areas: Array<Area> = emptyArray(),
@JvmField
var hint: String? = null,
)
62 changes: 62 additions & 0 deletions src/main/kotlin/io/spaceapi/types/Sensors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ data class Sensors(
var people_now_present: Array<PeoplePresent> = emptyArray(),
@JvmField
var network_traffic: Array<NetworkTraffic> = emptyArray(),
@JvmField
var power_generation: Array<PowerGeneration> = emptyArray(),
@JvmField
var carbondioxide: Array<CarbonDioxide> = emptyArray(),
)

@Serializable
Expand All @@ -66,6 +70,8 @@ data class Temperature(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -78,6 +84,8 @@ data class DoorLocked(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -92,6 +100,8 @@ data class Barometer(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand Down Expand Up @@ -122,6 +132,8 @@ data class RadiationSensor(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -136,6 +148,8 @@ data class Humidity(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -150,6 +164,8 @@ data class BeverageSupply(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -164,6 +180,8 @@ data class PowerConsumption(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -176,6 +194,8 @@ data class Wind(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand Down Expand Up @@ -228,6 +248,8 @@ data class NetworkConnection(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -250,6 +272,8 @@ data class AccountBalance(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -262,6 +286,8 @@ data class MemberCount(
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -276,6 +302,8 @@ data class PeoplePresent(
var names: Array<String> = emptyArray(),
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -288,6 +316,8 @@ data class NetworkTraffic(
var location: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
Expand All @@ -311,3 +341,35 @@ data class PacketsPerSecond(
@JvmField
var value: Long,
)

@Serializable
data class PowerGeneration(
@JvmField
var value: Float,
@JvmField
var unit: String,
@JvmField
var location: String,
@JvmField
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)

@Serializable
data class CarbonDioxide(
@JvmField
var value: Float,
@JvmField
var unit: String,
@JvmField
var location: String,
@JvmField
var name: String? = null,
@JvmField
var description: String? = null,
@JvmField
var lastchange: Float? = null,
)
4 changes: 3 additions & 1 deletion src/main/kotlin/io/spaceapi/types/Status.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ data class Status(
@JvmField
var url: String,
@JvmField
var location: Location,
var location: Location? = null,
@JvmField
var spacefed: SpaceFed? = null,
@JvmField
Expand Down Expand Up @@ -71,4 +71,6 @@ data class Status(
@Deprecated("The 'radio_show' field was removed in API v14")
@JvmField
var radio_show: Array<RadioShow> = emptyArray(),
@JvmField
var linked_spaces: Array<LinkedSpace> = emptyArray(),
)
101 changes: 92 additions & 9 deletions src/test/kotlin/io/spaceapi/ParserTestKotlin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class ParserTestKotlin {
assertEquals("https://www.coredump.ch/wp-content/uploads/2016/11/logo.png", parsed.logo)
assertEquals("https://www.coredump.ch/", parsed.url.toString())

assertEquals("Neue Jonastrasse 107, 8640 Rapperswil, Switzerland", parsed.location.address)
assertEquals(47.2251f, parsed.location.lon)
assertEquals(8.8339f, parsed.location.lat)
assertEquals("Neue Jonastrasse 107, 8640 Rapperswil, Switzerland", parsed.location!!.address)
assertEquals(47.2251f, parsed.location!!.lon)
assertEquals(8.8339f, parsed.location!!.lat)

assertEquals(false, parsed.state!!.open)
assertEquals(null, parsed.state!!.lastchange)
Expand Down Expand Up @@ -96,9 +96,9 @@ class ParserTestKotlin {
assertEquals("https://www.coredump.ch/wp-content/uploads/2016/11/logo.png", parsed.logo)
assertEquals("https://www.coredump.ch/", parsed.url.toString())

assertEquals("Neue Jonastrasse 107, 8640 Rapperswil, Switzerland", parsed.location.address)
assertEquals(47.2251f, parsed.location.lon)
assertEquals(8.8339f, parsed.location.lat)
assertEquals("Neue Jonastrasse 107, 8640 Rapperswil, Switzerland", parsed.location!!.address)
assertEquals(47.2251f, parsed.location!!.lon)
assertEquals(8.8339f, parsed.location!!.lat)

assertEquals(false, parsed.state!!.open)
assertEquals(null, parsed.state!!.lastchange)
Expand Down Expand Up @@ -130,9 +130,9 @@ class ParserTestKotlin {
assertEquals("Coredump", parsed.space)
assertEquals("https://www.coredump.ch/wp-content/uploads/2016/11/logo.png", parsed.logo)
assertEquals("https://www.coredump.ch/", parsed.url)
assertEquals(null, parsed.location.address)
assertEquals(47.2251f, parsed.location.lon)
assertEquals(8.8339f, parsed.location.lat)
assertEquals(null, parsed.location!!.address)
assertEquals(47.2251f, parsed.location!!.lon)
assertEquals(8.8339f, parsed.location!!.lat)

@Suppress("DEPRECATION")
assertEquals(emptySet(), parsed.issue_report_channels)
Expand All @@ -150,6 +150,89 @@ class ParserTestKotlin {
assertEquals(setOf("14"), parsed.api_compatibility)
}

@Test
fun testParseV15FromString() {
val parsed = parseString("""
{
"api": "0.13",
"api_compatibility": ["14", "15"],
"space": "Coredump",
"logo": "https://www.coredump.ch/wp-content/uploads/2016/11/logo.png",
"url": "https://www.coredump.ch/",
"location": {
"address": "Neue Jonastrasse 107, 8640 Rapperswil, Switzerland",
"lon": 47.2251,
"lat": 8.8339,
"country_code": "CH",
"hint": "Ring bell twice"
},
"contact": {
"email": "vorstand@lists.coredump.ch",
"irc": "irc://freenode.net/#coredump",
"twitter": "@coredump_ch"
},
"issue_report_channels": ["email", "twitter"],
"state": {
"open": false,
"message": "Open Mondays from 20:00"
},
"linked_spaces": [
{
"endpoint": "https://spaceapi.kabelsalat.ch/",
"website": "https://ccc-basel.ch/"
}
]
}
""")

assertEquals("Coredump", parsed.space)
assertEquals("0.13", parsed.api)
assertEquals(setOf("14", "15"), parsed.api_compatibility)
assertEquals("https://www.coredump.ch/wp-content/uploads/2016/11/logo.png", parsed.logo)
assertEquals("https://www.coredump.ch/", parsed.url.toString())

assertEquals("Neue Jonastrasse 107, 8640 Rapperswil, Switzerland", parsed.location!!.address)
assertEquals(47.2251f, parsed.location!!.lon)
assertEquals(8.8339f, parsed.location!!.lat)
assertEquals("CH", parsed.location!!.country_code)
assertEquals("Ring bell twice", parsed.location!!.hint)

assertEquals(false, parsed.state!!.open)
assertEquals(null, parsed.state!!.lastchange)
assertEquals("Open Mondays from 20:00", parsed.state!!.message)

assertEquals("vorstand@lists.coredump.ch", parsed.contact.email)
assertEquals("irc://freenode.net/#coredump", parsed.contact.irc)
assertEquals("@coredump_ch", parsed.contact.twitter)
assertEquals(null, parsed.contact.facebook)

assertEquals(1, parsed.linked_spaces.size)
assertEquals("https://spaceapi.kabelsalat.ch/", parsed.linked_spaces[0].endpoint)
assertEquals("https://ccc-basel.ch/", parsed.linked_spaces[0].website)

@Suppress("DEPRECATION")
assertEquals(setOf("email", "twitter"), parsed.issue_report_channels)
}

private val minimalV15Data = """{
"api_compatibility": ["15"],
"space": "Coredump",
"logo": "https://www.coredump.ch/wp-content/uploads/2016/11/logo.png",
"url": "https://www.coredump.ch/",
"contact": {}
}"""

@Test
fun testParseV15Minimal() {
val parsed = parseString(minimalV15Data)

assertEquals(setOf("15"), parsed.api_compatibility)
assertEquals("Coredump", parsed.space)
assertEquals("https://www.coredump.ch/wp-content/uploads/2016/11/logo.png", parsed.logo)
assertEquals("https://www.coredump.ch/", parsed.url)
assertEquals(null, parsed.location)
}

/**
* Regression test for #2.
*/
Expand Down
Loading