Switch from RabbitMQ to server-sent events

(not fully working yet)
This commit is contained in:
2021-05-03 20:58:40 +02:00
parent 533749c7c8
commit daa7209742
38 changed files with 22158 additions and 672 deletions

View File

@@ -10,7 +10,6 @@ import android.content.pm.PackageManager
import android.graphics.Color
import android.location.LocationManager
import android.os.BatteryManager
import android.os.Bundle
import android.os.IBinder
import android.provider.Settings
import android.util.Log
@@ -18,14 +17,9 @@ import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import com.rabbitmq.client.Channel
import com.rabbitmq.client.Connection
import com.rabbitmq.client.ConnectionFactory
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import java.util.concurrent.TimeoutException
class TrackerService : Service() {
@@ -36,38 +30,9 @@ class TrackerService : Service() {
return null
}
private fun connectWithBroker() {
// This thread only connects to RabbitMQ
val connectionThread = Thread(Runnable {
val client = OkHttpClient()
val factory = ConnectionFactory()
factory.username = MainActivity.USER!!.name
factory.password = MainActivity.USER!!.brokerToken
factory.virtualHost = "/"
factory.host = "192.168.178.26"
factory.port = 5672
factory.isAutomaticRecoveryEnabled = true
try {
conn[0] = factory.newConnection()
channel[0] = conn[0]?.createChannel()
val intent = Intent("de.nicolasklier.livebeat")
val bundle = Bundle()
bundle.putBoolean("statusRabbit", true)
intent.putExtras(bundle)
this.sendBroadcast(intent)
channel[0]?.queueDeclare("tracker-" + factory.username, true, false, false, null)
//channel[0]?.basicPublish("", "Tracker", null, "Test message".toByteArray())
Log.i("RabbitMQ", "run: Published test message")
} catch (e: IOException) {
e.printStackTrace()
} catch (e: TimeoutException) {
e.printStackTrace()
}
})
connectionThread.start()
@SuppressLint("CheckResult")
private fun subscribeToEvents() {
HttpRequests.sse("http://192.168.178.26/user/events?token=${MainActivity.USER?.eventToken}")
}
@SuppressLint("HardwareIds")
@@ -105,7 +70,7 @@ class TrackerService : Service() {
}
}
connectWithBroker()
subscribeToEvents()
startForeground()
return super.onStartCommand(intent, flags, startId)
}