feat(data): add DeviceConfig model, flexible R2ConfigParser, and DeviceRepository with unit tests
This commit is contained in:
@@ -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
|
||||
)
|
||||
@@ -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<ParsedEndpoints> {
|
||||
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<String, String>()
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<DeviceConfig> {
|
||||
val json = storage.getString(storageKey, null) ?: return emptyList()
|
||||
val type = object : TypeToken<List<DeviceConfig>>() {}.type
|
||||
return try {
|
||||
gson.fromJson<List<DeviceConfig>>(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<DeviceConfig>) {
|
||||
val json = gson.toJson(devices)
|
||||
storage.putString(storageKey, json)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user