Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82095e614d | ||
|
|
d72abb5f40 |
@@ -11,8 +11,8 @@ android {
|
|||||||
applicationId = "com.expapp.trigger"
|
applicationId = "com.expapp.trigger"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 34
|
targetSdk = 34
|
||||||
versionCode = 19
|
versionCode = 21
|
||||||
versionName = "1.3.0"
|
versionName = "1.3.2"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables {
|
vectorDrawables {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
|
android:usesCleartextTraffic="true"
|
||||||
android:theme="@style/Theme.ExpApp">
|
android:theme="@style/Theme.ExpApp">
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
|
|||||||
@@ -117,8 +117,11 @@ class MainActivity : FragmentActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auto-upgrade existing default device with SysPulse defaults if missing
|
// Auto-upgrade existing default device with SysPulse defaults if missing
|
||||||
|
try {
|
||||||
repository.getDevices().forEach { dev ->
|
repository.getDevices().forEach { dev ->
|
||||||
if (dev.node.equals("debian", ignoreCase = true) && dev.syspulseUrl.isBlank()) {
|
val sysUrl = dev.syspulseUrl
|
||||||
|
val nodeName = dev.node
|
||||||
|
if (nodeName.equals("debian", ignoreCase = true) && sysUrl.isBlank()) {
|
||||||
val upgraded = dev.copy(
|
val upgraded = dev.copy(
|
||||||
syspulseUrl = "http://100.85.63.44:8088",
|
syspulseUrl = "http://100.85.63.44:8088",
|
||||||
syspulseSecretKey = if (dev.syspulseSecretKey.isBlank()) "default_pulse_secret_key_change_me" else dev.syspulseSecretKey
|
syspulseSecretKey = if (dev.syspulseSecretKey.isBlank()) "default_pulse_secret_key_change_me" else dev.syspulseSecretKey
|
||||||
@@ -126,6 +129,9 @@ class MainActivity : FragmentActivity() {
|
|||||||
repository.updateDevice(upgraded)
|
repository.updateDevice(upgraded)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (_: Exception) {
|
||||||
|
// Guard against any startup migration issues
|
||||||
|
}
|
||||||
triggerClient = TriggerClient()
|
triggerClient = TriggerClient()
|
||||||
appSettings = AppSettings(applicationContext)
|
appSettings = AppSettings(applicationContext)
|
||||||
historyRepository = TriggerHistoryRepository(applicationContext)
|
historyRepository = TriggerHistoryRepository(applicationContext)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ data class DeviceConfig(
|
|||||||
val name: String,
|
val name: String,
|
||||||
val node: String,
|
val node: String,
|
||||||
val secretKey: String,
|
val secretKey: String,
|
||||||
val r2Url: String,
|
val r2Url: String = "",
|
||||||
val powerOnUrl: String,
|
val powerOnUrl: String,
|
||||||
val powerOffUrl: String,
|
val powerOffUrl: String,
|
||||||
val rebootUrl: String,
|
val rebootUrl: String,
|
||||||
|
|||||||
@@ -4,7 +4,14 @@ import android.content.Context
|
|||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import com.expapp.trigger.data.model.DeviceConfig
|
import com.expapp.trigger.data.model.DeviceConfig
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import com.google.gson.JsonDeserializationContext
|
||||||
|
import com.google.gson.JsonDeserializer
|
||||||
|
import com.google.gson.JsonElement
|
||||||
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import java.lang.reflect.Type
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
interface KeyValueStorage {
|
interface KeyValueStorage {
|
||||||
fun getString(key: String, defaultValue: String?): String?
|
fun getString(key: String, defaultValue: String?): String?
|
||||||
@@ -44,8 +51,29 @@ class SharedPrefsStorage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DeviceConfigDeserializer : JsonDeserializer<DeviceConfig> {
|
||||||
|
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): DeviceConfig {
|
||||||
|
val obj = if (json.isJsonObject) json.asJsonObject else JsonObject()
|
||||||
|
return DeviceConfig(
|
||||||
|
id = obj.get("id")?.asString?.ifBlank { UUID.randomUUID().toString() } ?: UUID.randomUUID().toString(),
|
||||||
|
name = obj.get("name")?.asString ?: "Server",
|
||||||
|
node = obj.get("node")?.asString ?: "debian",
|
||||||
|
secretKey = obj.get("secretKey")?.asString ?: "",
|
||||||
|
r2Url = obj.get("r2Url")?.asString ?: "",
|
||||||
|
powerOnUrl = obj.get("powerOnUrl")?.asString ?: "",
|
||||||
|
powerOffUrl = obj.get("powerOffUrl")?.asString ?: "",
|
||||||
|
rebootUrl = obj.get("rebootUrl")?.asString ?: "",
|
||||||
|
statusUrl = obj.get("statusUrl")?.asString ?: "",
|
||||||
|
syspulseUrl = obj.get("syspulseUrl")?.asString ?: "",
|
||||||
|
syspulseSecretKey = obj.get("syspulseSecretKey")?.asString ?: ""
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class DeviceRepository(private val storage: KeyValueStorage) {
|
class DeviceRepository(private val storage: KeyValueStorage) {
|
||||||
private val gson = Gson()
|
private val gson: Gson = GsonBuilder()
|
||||||
|
.registerTypeAdapter(DeviceConfig::class.java, DeviceConfigDeserializer())
|
||||||
|
.create()
|
||||||
private val storageKey = "saved_devices_json"
|
private val storageKey = "saved_devices_json"
|
||||||
|
|
||||||
fun getDevices(): List<DeviceConfig> {
|
fun getDevices(): List<DeviceConfig> {
|
||||||
|
|||||||
@@ -2,14 +2,23 @@ package com.expapp.trigger.network
|
|||||||
|
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import com.expapp.trigger.data.model.CpuMetrics
|
||||||
import com.expapp.trigger.data.model.DeviceConfig
|
import com.expapp.trigger.data.model.DeviceConfig
|
||||||
|
import com.expapp.trigger.data.model.DiskMetrics
|
||||||
|
import com.expapp.trigger.data.model.RamMetrics
|
||||||
import com.expapp.trigger.data.model.ScheduledTask
|
import com.expapp.trigger.data.model.ScheduledTask
|
||||||
import com.expapp.trigger.data.model.ScheduledTasksResponse
|
import com.expapp.trigger.data.model.ScheduledTasksResponse
|
||||||
import com.expapp.trigger.data.model.ServiceOperation
|
import com.expapp.trigger.data.model.ServiceOperation
|
||||||
import com.expapp.trigger.data.model.SysPulseStatus
|
import com.expapp.trigger.data.model.SysPulseStatus
|
||||||
|
import com.expapp.trigger.data.model.SysServiceInfo
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import com.google.gson.JsonDeserializationContext
|
||||||
|
import com.google.gson.JsonDeserializer
|
||||||
|
import com.google.gson.JsonElement
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
import okhttp3.Call
|
import okhttp3.Call
|
||||||
import okhttp3.Callback
|
import okhttp3.Callback
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
@@ -18,8 +27,41 @@ import okhttp3.Request
|
|||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.lang.reflect.Type
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
class SysPulseStatusDeserializer : JsonDeserializer<SysPulseStatus> {
|
||||||
|
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): SysPulseStatus {
|
||||||
|
if (!json.isJsonObject) return SysPulseStatus()
|
||||||
|
val obj = json.asJsonObject
|
||||||
|
val cpu = if (obj.has("cpu") && obj.get("cpu").isJsonObject) {
|
||||||
|
context.deserialize<CpuMetrics>(obj.get("cpu"), CpuMetrics::class.java) ?: CpuMetrics()
|
||||||
|
} else CpuMetrics()
|
||||||
|
val ram = if (obj.has("ram") && obj.get("ram").isJsonObject) {
|
||||||
|
context.deserialize<RamMetrics>(obj.get("ram"), RamMetrics::class.java) ?: RamMetrics()
|
||||||
|
} else RamMetrics()
|
||||||
|
val disk = if (obj.has("disk") && obj.get("disk").isJsonArray) {
|
||||||
|
val listType = object : TypeToken<List<DiskMetrics>>() {}.type
|
||||||
|
context.deserialize<List<DiskMetrics>>(obj.get("disk"), listType) ?: emptyList()
|
||||||
|
} else emptyList()
|
||||||
|
val services = if (obj.has("services") && obj.get("services").isJsonArray) {
|
||||||
|
val listType = object : TypeToken<List<SysServiceInfo>>() {}.type
|
||||||
|
context.deserialize<List<SysServiceInfo>>(obj.get("services"), listType) ?: emptyList()
|
||||||
|
} else emptyList()
|
||||||
|
val uptime = obj.get("uptime")?.asString ?: ""
|
||||||
|
val success = obj.get("success")?.asBoolean ?: true
|
||||||
|
|
||||||
|
return SysPulseStatus(
|
||||||
|
success = success,
|
||||||
|
uptime = uptime,
|
||||||
|
cpu = cpu,
|
||||||
|
ram = ram,
|
||||||
|
disk = disk,
|
||||||
|
services = services
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class SysPulseClient(
|
class SysPulseClient(
|
||||||
private val okHttpClient: OkHttpClient = OkHttpClient.Builder()
|
private val okHttpClient: OkHttpClient = OkHttpClient.Builder()
|
||||||
.followRedirects(true)
|
.followRedirects(true)
|
||||||
@@ -27,7 +69,9 @@ class SysPulseClient(
|
|||||||
.connectTimeout(8, TimeUnit.SECONDS)
|
.connectTimeout(8, TimeUnit.SECONDS)
|
||||||
.readTimeout(12, TimeUnit.SECONDS)
|
.readTimeout(12, TimeUnit.SECONDS)
|
||||||
.build(),
|
.build(),
|
||||||
private val gson: Gson = Gson()
|
private val gson: Gson = GsonBuilder()
|
||||||
|
.registerTypeAdapter(SysPulseStatus::class.java, SysPulseStatusDeserializer())
|
||||||
|
.create()
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
const val DEFAULT_FALLBACK_SECRET = "default_pulse_secret_key_change_me"
|
const val DEFAULT_FALLBACK_SECRET = "default_pulse_secret_key_change_me"
|
||||||
@@ -49,7 +93,11 @@ class SysPulseClient(
|
|||||||
|
|
||||||
fun resolveBaseUrl(device: DeviceConfig, nodeStatus: NodeStatus? = null): String {
|
fun resolveBaseUrl(device: DeviceConfig, nodeStatus: NodeStatus? = null): String {
|
||||||
if (device.syspulseUrl.isNotBlank()) {
|
if (device.syspulseUrl.isNotBlank()) {
|
||||||
return device.syspulseUrl.trim().removeSuffix("/")
|
return device.syspulseUrl.trim()
|
||||||
|
.removeSuffix("/")
|
||||||
|
.removeSuffix("/api/status")
|
||||||
|
.removeSuffix("/api")
|
||||||
|
.removeSuffix("/")
|
||||||
}
|
}
|
||||||
val ip = nodeStatus?.ip?.trim()
|
val ip = nodeStatus?.ip?.trim()
|
||||||
if (!ip.isNullOrBlank()) {
|
if (!ip.isNullOrBlank()) {
|
||||||
|
|||||||
@@ -120,4 +120,16 @@ class DeviceRepositoryTest {
|
|||||||
|
|
||||||
org.junit.Assert.assertFalse(updated)
|
org.junit.Assert.assertFalse(updated)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getDevices_whenOldJsonWithoutSyspulse_doesNotCrash() {
|
||||||
|
val oldJson = """[{"id":"2dc98ae9-9f71-4f29-95a6-a59ac94d7bf6","name":"Server","node":"debian","secretKey":"Carbon@12","r2Url":"https://bucket.linuxguy.space/home-server-app/endpoints.json","powerOnUrl":"https://trigger.macrodroid.com/87f2f62c-09ee-43cb-8796-fd1a6465754b/wakeserver","powerOffUrl":"https://cosmos.linuxguy.space/poweroff?node=debian","rebootUrl":"https://cosmos.linuxguy.space/reboot?node=debian","statusUrl":"https://cosmos.linuxguy.space/status?node=debian"}]"""
|
||||||
|
storage.putString("saved_devices_json", oldJson)
|
||||||
|
|
||||||
|
val devices = repository.getDevices()
|
||||||
|
assertEquals(1, devices.size)
|
||||||
|
val dev = devices[0]
|
||||||
|
assertTrue(dev.syspulseUrl.isBlank())
|
||||||
|
assertTrue(dev.syspulseSecretKey.isBlank())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user