Real-time communication with frontend

- Frontend shows heatmap of most visit places
- Maximum accuracy can now be set
- Fix bug where battery chart filtered values wrongly
This commit is contained in:
2020-10-26 23:38:34 +01:00
parent fa60f58d3c
commit e12ed7775b
20 changed files with 770 additions and 161 deletions

View File

@@ -38,6 +38,7 @@ class MainActivity : AppCompatActivity() {
companion object {
@JvmStatic val API_URL = "http://192.168.178.26:8040"
var TOKEN = ""
var USER: User? = null
}
private var broadcastReceiver: BroadcastReceiver? = null
@@ -50,6 +51,7 @@ class MainActivity : AppCompatActivity() {
val client = OkHttpClient()
val req = Request.Builder()
.url("$API_URL/phone/$androidId")
.header("token", TOKEN)
.get()
.build()
val response = client.newCall(req).execute()
@@ -65,7 +67,7 @@ class MainActivity : AppCompatActivity() {
androidId,
Build.MODEL,
Build.PRODUCT,
Build.VERSION.RELEASE,
Build.VERSION.BASE_OS + " " + Build.VERSION.RELEASE + " " + Build.VERSION.CODENAME,
System.getProperty("os.arch")
)
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
@@ -91,7 +93,6 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
startService(Intent(this, TrackerService::class.java))
// Check authorization
val backendChecks = Thread(Runnable {
@@ -119,14 +120,34 @@ class MainActivity : AppCompatActivity() {
this.runOnUiThread(Runnable {
findViewById<TextView>(R.id.httpStatus).setTextColor(Color.GREEN)
findViewById<TextView>(R.id.httpStatus).text = "CONNECTED"
findViewById<Button>(R.id.signin).isEnabled = false
findViewById<Button>(R.id.signin).text = "Logged in"
})
Snackbar.make(findViewById<FloatingActionButton>(R.id.fab), "Login succeeded", Snackbar.LENGTH_SHORT)
.setBackgroundTint(Color.GREEN)
.setActionTextColor(Color.WHITE)
.show()
// Get all user information
val userinfo = Request.Builder()
.url("$API_URL/user")
.get()
.header("Content-Type", "application/json")
.header("token", TOKEN)
.build()
val userinfoResponse = client.newCall(userinfo).execute()
checkIfPhoneIsRegistered()
if (userinfoResponse.code == 200) {
val jsonToUser = moshi.adapter(User::class.java)
val userInfoResponseBody = userinfoResponse.body!!.string()
USER = jsonToUser.fromJson(userInfoResponseBody)
// Only start service if authentication went good.
startService(Intent(this, TrackerService::class.java))
Snackbar.make(findViewById<FloatingActionButton>(R.id.fab), "Login succeeded", Snackbar.LENGTH_SHORT)
.setBackgroundTint(Color.GREEN)
.setActionTextColor(Color.WHITE)
.show()
checkIfPhoneIsRegistered()
}
} else {
// Since we are in another thread, we have to get back to the UI thread.
this.runOnUiThread(Runnable {
@@ -158,7 +179,7 @@ class MainActivity : AppCompatActivity() {
findViewById<TextView>(R.id.rabbitStatus).setTextColor(Color.RED)
}
if (statusHttp == 200) {
/*if (statusHttp == 200) {
findViewById<TextView>(R.id.httpStatus).text = "ONLINE (no login)"
findViewById<TextView>(R.id.httpStatus).setTextColor(Color.CYAN)
findViewById<Button>(R.id.signin).isEnabled = false
@@ -168,7 +189,7 @@ class MainActivity : AppCompatActivity() {
findViewById<TextView>(R.id.httpStatus).setTextColor(Color.RED)
findViewById<Button>(R.id.signin).isEnabled = true
findViewById<Button>(R.id.signin).text = "Sign in"
}
}*/
}
}
registerReceiver(broadcastReceiver, IntentFilter("de.nicolasklier.livebeat"))
@@ -192,6 +213,12 @@ class MainActivity : AppCompatActivity() {
}
}
override fun onStart() {
super.onStart()
registerReceiver(broadcastReceiver, IntentFilter("de.nicolasklier.livebeat"))
}
override fun onStop() {
super.onStop()

View File

@@ -2,25 +2,35 @@ package de.nicolasklier.livebeat
import com.squareup.moshi.JsonClass
import com.squareup.moshi.ToJson
import java.util.*
/**
* This class represents one singe beat that will be send to the database.
*/
class Beat(
final val token: String, // Token of this phone
final val gpsLocation: Array<Double>,
final val battery: Int?,
final val timestamp: Long
val token: String, // Token of this phone
val gpsLocation: Array<Double>,
val battery: Int?,
val timestamp: Long
) {}
class Phone(
final val androidId: String,
final val modelName: String,
final val displayName: String,
final val operatingSystem: String,
final val architecture: String
val androidId: String,
val modelName: String,
val displayName: String,
val operatingSystem: String,
val architecture: String
) {}
class User(
val name: String,
val type: String,
val lastLogin: String,
val twoFASecret: String?,
val brokerToken: String,
val createdAt: String
)
class Login(
final val token: String
val token: String
) {}

View File

@@ -40,16 +40,12 @@ class TrackerService : Service() {
// This thread only connects to RabbitMQ
val connectionThread = Thread(Runnable {
val client = OkHttpClient()
val req = Request.Builder()
.url(MainActivity.API_URL)
.get()
.build()
val factory = ConnectionFactory()
factory.username = "lineage"
factory.password = "ZSo\$X97GQ547JXL7nGq"
factory.username = MainActivity.USER!!.name
factory.password = MainActivity.USER!!.brokerToken
factory.virtualHost = "/"
factory.host = "nk-home.ddns.net"
factory.host = "192.168.178.26"
factory.port = 5672
factory.isAutomaticRecoveryEnabled = true
try {
@@ -59,11 +55,10 @@ class TrackerService : Service() {
val intent = Intent("de.nicolasklier.livebeat")
val bundle = Bundle()
bundle.putBoolean("statusRabbit", true)
bundle.putInt("statusHttp", client.newCall(req).execute().code)
intent.putExtras(bundle)
this.sendBroadcast(intent)
channel[0]?.queueDeclare("tracker", true, false, false, null)
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) {
@@ -83,7 +78,7 @@ class TrackerService : Service() {
val androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates("gps", 5000, 0f
locationManager.requestLocationUpdates("gps", 10000, 0f
) { location ->
Log.i("Location", "Location is: " + location.latitude + " | " + location.longitude)