diff --git a/app/src/main/java/com/expapp/trigger/data/model/DeviceConfig.kt b/app/src/main/java/com/expapp/trigger/data/model/DeviceConfig.kt new file mode 100644 index 0000000..6ec668d --- /dev/null +++ b/app/src/main/java/com/expapp/trigger/data/model/DeviceConfig.kt @@ -0,0 +1,14 @@ +package com.expapp.trigger.data.model + +import java.util.UUID + +data class DeviceConfig( + val id: String = UUID.randomUUID().toString(), + val name: String, + val node: String, + val secretKey: String, + val r2Url: String, + val powerOnUrl: String, + val powerOffUrl: String, + val rebootUrl: String +) diff --git a/app/src/main/java/com/expapp/trigger/data/parser/R2ConfigParser.kt b/app/src/main/java/com/expapp/trigger/data/parser/R2ConfigParser.kt new file mode 100644 index 0000000..49f7b15 --- /dev/null +++ b/app/src/main/java/com/expapp/trigger/data/parser/R2ConfigParser.kt @@ -0,0 +1,51 @@ +package com.expapp.trigger.data.parser + +import com.google.gson.JsonParser + +data class ParsedEndpoints( + val powerOn: String, + val powerOff: String, + val reboot: String +) + +object R2ConfigParser { + fun parse(jsonString: String): Result { + return runCatching { + val jsonElement = JsonParser.parseString(jsonString) + if (!jsonElement.isJsonObject) { + throw IllegalArgumentException("Expected JSON object") + } + val jsonObject = jsonElement.asJsonObject + + // Case-insensitive & variant normalization + val normalizedMap = mutableMapOf() + for ((key, value) in jsonObject.entrySet()) { + val cleanKey = key.lowercase().replace("_", "").replace("-", "") + if (value.isJsonPrimitive && value.asJsonPrimitive.isString) { + normalizedMap[cleanKey] = value.asString + } + } + + val powerOn = normalizedMap["poweron"] + ?: normalizedMap["on"] + ?: normalizedMap["start"] + ?: throw IllegalArgumentException("Missing required endpoint key: 'poweron'") + + val powerOff = normalizedMap["poweroff"] + ?: normalizedMap["off"] + ?: normalizedMap["shutdown"] + ?: throw IllegalArgumentException("Missing required endpoint key: 'poweroff'") + + val reboot = normalizedMap["reboot"] + ?: normalizedMap["restart"] + ?: normalizedMap["reset"] + ?: throw IllegalArgumentException("Missing required endpoint key: 'reboot'") + + ParsedEndpoints( + powerOn = powerOn, + powerOff = powerOff, + reboot = reboot + ) + } + } +} diff --git a/app/src/main/java/com/expapp/trigger/data/repository/DeviceRepository.kt b/app/src/main/java/com/expapp/trigger/data/repository/DeviceRepository.kt new file mode 100644 index 0000000..d59e2f8 --- /dev/null +++ b/app/src/main/java/com/expapp/trigger/data/repository/DeviceRepository.kt @@ -0,0 +1,61 @@ +package com.expapp.trigger.data.repository + +import android.content.Context +import android.content.SharedPreferences +import com.expapp.trigger.data.model.DeviceConfig +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken + +interface KeyValueStorage { + fun getString(key: String, defaultValue: String?): String? + fun putString(key: String, value: String?) +} + +class SharedPrefsStorage(context: Context) : KeyValueStorage { + private val prefs: SharedPreferences = + context.getSharedPreferences("device_trigger_prefs", Context.MODE_PRIVATE) + + override fun getString(key: String, defaultValue: String?): String? = + prefs.getString(key, defaultValue) + + override fun putString(key: String, value: String?) { + prefs.edit().putString(key, value).apply() + } +} + +class DeviceRepository(private val storage: KeyValueStorage) { + private val gson = Gson() + private val storageKey = "saved_devices_json" + + fun getDevices(): List { + val json = storage.getString(storageKey, null) ?: return emptyList() + val type = object : TypeToken>() {}.type + return try { + gson.fromJson>(json, type) ?: emptyList() + } catch (e: Exception) { + emptyList() + } + } + + fun addDevice(device: DeviceConfig) { + val current = getDevices().toMutableList() + current.removeAll { it.id == device.id } + current.add(device) + saveDevices(current) + } + + fun deleteDevice(id: String) { + val current = getDevices().toMutableList() + current.removeAll { it.id == id } + saveDevices(current) + } + + fun updateDevice(device: DeviceConfig) { + addDevice(device) + } + + private fun saveDevices(devices: List) { + val json = gson.toJson(devices) + storage.putString(storageKey, json) + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..bf48846 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Device Trigger + diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..02ac49e --- /dev/null +++ b/app/src/main/res/values/themes.xml @@ -0,0 +1,6 @@ + + + diff --git a/app/src/test/java/com/expapp/trigger/data/parser/R2ConfigParserTest.kt b/app/src/test/java/com/expapp/trigger/data/parser/R2ConfigParserTest.kt new file mode 100644 index 0000000..ef0954f --- /dev/null +++ b/app/src/test/java/com/expapp/trigger/data/parser/R2ConfigParserTest.kt @@ -0,0 +1,65 @@ +package com.expapp.trigger.data.parser + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +class R2ConfigParserTest { + + @Test + fun parse_standardLowercaseKeys_returnsEndpoints() { + val json = """ + { + "poweron": "https://cosmos.linuxguy.space/poweron", + "poweroff": "https://cosmos.linuxguy.space/poweroff", + "reboot": "https://cosmos.linuxguy.space/reboot" + } + """.trimIndent() + + val result = R2ConfigParser.parse(json) + assertTrue(result.isSuccess) + val endpoints = result.getOrThrow() + assertEquals("https://cosmos.linuxguy.space/poweron", endpoints.powerOn) + assertEquals("https://cosmos.linuxguy.space/poweroff", endpoints.powerOff) + assertEquals("https://cosmos.linuxguy.space/reboot", endpoints.reboot) + } + + @Test + fun parse_camelCaseAndUnderscoreKeys_returnsEndpoints() { + val json = """ + { + "power_on": "https://cosmos.linuxguy.space/poweron", + "powerOff": "https://cosmos.linuxguy.space/poweroff", + "restart": "https://cosmos.linuxguy.space/reboot" + } + """.trimIndent() + + val result = R2ConfigParser.parse(json) + assertTrue(result.isSuccess) + val endpoints = result.getOrThrow() + assertEquals("https://cosmos.linuxguy.space/poweron", endpoints.powerOn) + assertEquals("https://cosmos.linuxguy.space/poweroff", endpoints.powerOff) + assertEquals("https://cosmos.linuxguy.space/reboot", endpoints.reboot) + } + + @Test + fun parse_missingRequiredKey_returnsFailure() { + val json = """ + { + "poweron": "https://cosmos.linuxguy.space/poweron", + "reboot": "https://cosmos.linuxguy.space/reboot" + } + """.trimIndent() + + val result = R2ConfigParser.parse(json) + assertTrue(result.isFailure) + assertTrue(result.exceptionOrNull()?.message?.contains("poweroff", ignoreCase = true) == true) + } + + @Test + fun parse_invalidJson_returnsFailure() { + val invalid = "{ invalid json" + val result = R2ConfigParser.parse(invalid) + assertTrue(result.isFailure) + } +} diff --git a/app/src/test/java/com/expapp/trigger/data/repository/DeviceRepositoryTest.kt b/app/src/test/java/com/expapp/trigger/data/repository/DeviceRepositoryTest.kt new file mode 100644 index 0000000..43dc02b --- /dev/null +++ b/app/src/test/java/com/expapp/trigger/data/repository/DeviceRepositoryTest.kt @@ -0,0 +1,67 @@ +package com.expapp.trigger.data.repository + +import com.expapp.trigger.data.model.DeviceConfig +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test + +class DeviceRepositoryTest { + + private class FakeKeyValueStorage : KeyValueStorage { + private val data = mutableMapOf() + override fun getString(key: String, defaultValue: String?): String? = data[key] ?: defaultValue + override fun putString(key: String, value: String?) { + data[key] = value + } + } + + private lateinit var storage: FakeKeyValueStorage + private lateinit var repository: DeviceRepository + + @Before + fun setup() { + storage = FakeKeyValueStorage() + repository = DeviceRepository(storage) + } + + @Test + fun getDevices_whenEmpty_returnsEmptyList() { + val devices = repository.getDevices() + assertTrue(devices.isEmpty()) + } + + @Test + fun addDevice_savesAndReturnsDevice() { + val device = DeviceConfig( + id = "dev-1", + name = "Debian Server", + node = "debian", + secretKey = "Carbon@12", + r2Url = "https://r2.example.com/config.json", + powerOnUrl = "https://cosmos.linuxguy.space/poweron", + powerOffUrl = "https://cosmos.linuxguy.space/poweroff", + rebootUrl = "https://cosmos.linuxguy.space/reboot" + ) + + repository.addDevice(device) + val devices = repository.getDevices() + assertEquals(1, devices.size) + assertEquals(device, devices[0]) + } + + @Test + fun deleteDevice_removesDeviceById() { + val d1 = DeviceConfig(id = "1", name = "D1", node = "n1", secretKey = "k1", r2Url = "", powerOnUrl = "", powerOffUrl = "", rebootUrl = "") + val d2 = DeviceConfig(id = "2", name = "D2", node = "n2", secretKey = "k2", r2Url = "", powerOnUrl = "", powerOffUrl = "", rebootUrl = "") + + repository.addDevice(d1) + repository.addDevice(d2) + assertEquals(2, repository.getDevices().size) + + repository.deleteDevice("1") + val remaining = repository.getDevices() + assertEquals(1, remaining.size) + assertEquals("2", remaining[0].id) + } +}