tv-controller-android/app/src/main/java/com/example/tvcontroller/services/DeviceService.kt

120 lines
4.1 KiB
Kotlin

package com.example.tvcontroller.services
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.util.Log
import com.example.tvcontroller.data.Integration
import io.ktor.client.engine.cio.*
import io.ktor.client.*
import io.ktor.client.call.body
import io.ktor.client.request.headers
import io.ktor.client.request.request
import io.ktor.client.request.setBody
import io.ktor.client.statement.HttpResponse
import io.ktor.http.HttpMethod
import org.json.JSONObject
private const val SHARED_PREFERENCES_NAME = "devices";
class DeviceService(private val context: Context) {
private var client = HttpClient(CIO)
private var serverAddress: String = ""
private var token: String = ""
private var deviceId: String = ""
init {
loadPreferences()
}
suspend fun createIntegration(name: String, code: String) {
Log.i("DeviceService", "Creating integration for $name with code $code at $serverAddress")
val requestJson = JSONObject()
requestJson.put("name", name)
requestJson.put("code", code)
try {
val response: HttpResponse = client.request("http://$serverAddress/api/integrations") {
method = HttpMethod.Post
setBody(requestJson.toString())
headers {
append("Content-Type", "application/json")
}
}
val body: String = response.body()
val responseJson = JSONObject(body)
if (responseJson.has("error")) {
val error = responseJson.getString("error")
Log.e("DeviceService", "Error creating integration: $error")
return
}
token = responseJson.getString("token")
deviceId = responseJson.getString("id")
savePreferences()
Log.i("DeviceService", "Response: ${response.status.value} $body")
} catch (e: Exception) {
Log.e("DeviceService", "Error creating integration", e)
}
}
suspend fun getIntegration(): Integration? {
Log.i("DeviceService", "Getting integration $deviceId at $serverAddress")
try {
val response: HttpResponse =
client.request("http://$serverAddress/api/integrations/$deviceId") {
method = HttpMethod.Get
headers {
append("Authorization", "Bearer $token")
}
}
val body: String = response.body()
Log.i("DeviceService", "Response: ${response.status.value} $body")
if (response.status.value != 200) {
return null
}
val responseJson = JSONObject(body)
val integration = Integration(
responseJson.getString("id"),
responseJson.getString("name")
)
Log.i("DeviceService", "Response: ${response.status.value} $body")
return integration
} catch (e: Exception) {
Log.e("DeviceService", "Error getting integration", e)
}
return null
}
private fun loadPreferences() {
val sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
serverAddress = sharedPreferences.getString("server_address", "")!!
token = sharedPreferences.getString("token", "")!!
deviceId = sharedPreferences.getString("device_id", "")!!
Log.i("DeviceService", "Loaded preferences: $serverAddress $token")
}
private fun savePreferences() {
val sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.apply {
putString("server_address", serverAddress)
putString("token", token)
putString("device_id", deviceId)
apply()
}
}
fun setServerAddress(url: String) {
serverAddress = url
}
fun getServerAddress(): String {
return serverAddress
}
fun getToken(): String {
return token
}
}