102 lines
3.4 KiB
Kotlin
102 lines
3.4 KiB
Kotlin
package com.example.tvcontroller.services
|
|
|
|
import android.content.Context
|
|
import android.util.Log
|
|
import com.example.tvcontroller.R
|
|
import com.example.tvcontroller.data.RemoteCommand
|
|
import com.opencsv.CSVReader
|
|
import org.json.JSONObject
|
|
|
|
|
|
class ControllerService(
|
|
private val context: Context,
|
|
private val bluetoothService: BluetoothService
|
|
) {
|
|
private val samsungCommands = mutableMapOf<String, RemoteCommand>()
|
|
|
|
init {
|
|
loadCommands()
|
|
}
|
|
|
|
fun sendCommand(command: String) {
|
|
if (samsungCommands[command] == null) return
|
|
Log.i("ControllerService", "Sending command: $command")
|
|
val jsonString = remoteCommandToJsonString(samsungCommands[command]!!)
|
|
sendData(jsonString)
|
|
}
|
|
|
|
fun remoteCommandToJsonString(command: RemoteCommand): String {
|
|
var commandObject = JSONObject()
|
|
commandObject.put("protocol", command.protocol)
|
|
commandObject.put("address", command.device)
|
|
commandObject.put("command", command.function)
|
|
return commandObject.toString()
|
|
}
|
|
|
|
fun sendData(data: String) {
|
|
bluetoothService.sendData(data)
|
|
}
|
|
|
|
fun loadCommands() {
|
|
Log.i("ControllerService", "Loading commands");
|
|
var inputStream = context.resources.openRawResource(R.raw.samsung)
|
|
var csvReader = CSVReader(inputStream.reader())
|
|
csvReader.forEach { nextLine ->
|
|
if (nextLine.size < 5) return@forEach
|
|
if (nextLine[0] == "functionname") return@forEach
|
|
var remoteCommand = RemoteCommand()
|
|
remoteCommand.functionName = nextLine[0]
|
|
remoteCommand.protocol = "samsung"
|
|
remoteCommand.device = nextLine[2]
|
|
remoteCommand.subdevice = nextLine[3]
|
|
remoteCommand.function = nextLine[4]
|
|
samsungCommands[remoteCommand.functionName!!] = remoteCommand
|
|
}
|
|
Log.i("ControllerService", "Commands loaded: ${samsungCommands.size}")
|
|
}
|
|
|
|
companion object {
|
|
const val POWER = "POWER"
|
|
const val CURSOR_UP = "CURSOR UP"
|
|
const val CURSOR_DOWN = "CURSOR DOWN"
|
|
const val CURSOR_LEFT = "CURSOR LEFT"
|
|
const val CURSOR_RIGHT = "CURSOR RIGHT"
|
|
const val ENTER = "ENTER"
|
|
const val ONE = "1"
|
|
const val TWO = "2"
|
|
const val THREE = "3"
|
|
const val FOUR = "4"
|
|
const val FIVE = "5"
|
|
const val SIX = "6"
|
|
const val SEVEN = "7"
|
|
const val EIGHT = "8"
|
|
const val NINE = "9"
|
|
const val ZERO = "0"
|
|
const val CHANNEL_UP = "CHANNEL +"
|
|
const val CHANNEL_DOWN = "CHANNEL -"
|
|
const val VOLUME_UP = "VOLUME +"
|
|
const val VOLUME_DOWN = "VOLUME -"
|
|
const val MUTE = "MUTE"
|
|
const val GUIDE = "GUIDE"
|
|
const val INFO = "INFO"
|
|
const val AD_SUBTITLE = "AD/SUBT"
|
|
const val E_MANUAL = "E-MANUAL"
|
|
const val TOOLS = "TOOLS"
|
|
const val RETURN = "RETURN"
|
|
const val MENU = "MENU"
|
|
const val SMART_HUB = "SMART HUB"
|
|
const val EXIT = "EXIT"
|
|
const val CHANNEL_LIST = "CH LIST"
|
|
const val HOME = "HOME"
|
|
const val SETTINGS = "SETTINGS"
|
|
const val RED = "RED"
|
|
const val GREEN = "GREEN"
|
|
const val YELLOW = "YELLOW"
|
|
const val BLUE = "BLUE"
|
|
const val INPUT_SOURCE = "INPUT SOURCE"
|
|
const val REWIND = "REWIND"
|
|
const val PLAY = "PLAY"
|
|
const val PAUSE = "PAUSE"
|
|
const val FORWARD = "FORWARD"
|
|
}
|
|
} |