Add support for Breezy Weather gzip format

This commit is contained in:
MM20
2025-10-10 12:46:10 +02:00
parent aaf75ce81e
commit 5272ab5cac

View File

@@ -12,9 +12,11 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.io.IOException
import kotlinx.serialization.SerializationException import kotlinx.serialization.SerializationException
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
import java.util.zip.GZIPInputStream
class BreezyWeatherReceiver : BroadcastReceiver(), KoinComponent { class BreezyWeatherReceiver : BroadcastReceiver(), KoinComponent {
@@ -28,19 +30,40 @@ class BreezyWeatherReceiver: BroadcastReceiver(), KoinComponent {
if (provider != BreezyWeatherProvider.Id) { if (provider != BreezyWeatherProvider.Id) {
return@launch return@launch
} }
val weatherJson = intent.getStringExtra("WeatherJson") val weatherData = if (intent.hasExtra("WeatherGz")) {
val gz = intent.getByteArrayExtra("WeatherGz") ?: return@launch
if (weatherJson == null) {
Log.e("BreezyWeatherPlugin", "Broadcast was received but WeatherJson was null") val json = try {
val inputStream = GZIPInputStream(gz.inputStream())
inputStream.bufferedReader().use { it.readText() }
} catch (e: IOException) {
CrashReporter.logException(e)
return@launch return@launch
} }
val weatherData = try { try {
Json.Lenient.decodeFromString<BreezyWeatherData>(weatherJson) Json.Lenient.decodeFromString<List<BreezyWeatherData>>(json).firstOrNull()
} catch (e: SerializationException) { } catch (e: SerializationException) {
CrashReporter.logException(e) CrashReporter.logException(e)
return@launch return@launch
} }
} else if (intent.hasExtra("WeatherJson")) {
val json = intent.getStringExtra("WeatherJson") ?: return@launch
try {
Json.Lenient.decodeFromString<BreezyWeatherData>(json)
} catch (e: SerializationException) {
CrashReporter.logException(e)
return@launch
}
} else {
null
}
if (weatherData == null) {
Log.e("BreezyWeatherReceiver", "Broadcast was received but it did not contain weather data")
return@launch
}
BreezyWeatherProvider(context).pushWeatherData(weatherData) BreezyWeatherProvider(context).pushWeatherData(weatherData)