Skip to content

Commit

Permalink
Add ability to store multiple heartbeats in 1 request
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Nov 25, 2024
1 parent 98fd2d7 commit 9b3edbb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.httpsms

import android.app.Application
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -56,7 +55,13 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
}
Thread {
try {
HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
val phoneNumbers = mutableListOf<String>()
phoneNumbers.add(Settings.getSIM1PhoneNumber(applicationContext))
if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
}

HttpSmsApiService.create(applicationContext).storeHeartbeat(phoneNumbers.toTypedArray(), Settings.isCharging(applicationContext))
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
} catch (exception: Exception) {
Timber.e(exception)
Expand Down
8 changes: 4 additions & 4 deletions android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
return true
}

fun storeHeartbeat(phoneNumber: String, charging: Boolean) {
fun storeHeartbeat(phoneNumbers: Array<String>, charging: Boolean) {
val body = """
{
"charging": $charging,
"owner": "$phoneNumber"
"phone_numbers": ${phoneNumbers.joinToString(prefix = "[", postfix = "]") { "\"$it\"" }}
}
""".trimIndent()

Expand All @@ -146,13 +146,13 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {

val response = client.newCall(request).execute()
if (!response.isSuccessful) {
Timber.e("error response [${response.body?.string()}] with code [${response.code}] while sending heartbeat [$body] for owner [$phoneNumber]")
Timber.e("error response [${response.body?.string()}] with code [${response.code}] while sending heartbeat [$body] for phone numbers [${phoneNumbers.joinToString()}]")
response.close()
return
}

response.close()
Timber.i( "heartbeat stored successfully for owner [$phoneNumber]" )
Timber.i( "heartbeat stored successfully for phone numbers [${phoneNumbers.joinToString()}]" )
}


Expand Down
19 changes: 8 additions & 11 deletions android/app/src/main/java/com/httpsms/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ListenableWorker.Result
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
Expand Down Expand Up @@ -309,7 +310,6 @@ class MainActivity : AppCompatActivity() {
val progressBar = findViewById<LinearProgressIndicator>(R.id.mainProgressIndicator)
progressBar.visibility = View.VISIBLE


val liveData = MutableLiveData<String?>()
liveData.observe(this) { exception ->
run {
Expand All @@ -331,21 +331,18 @@ class MainActivity : AppCompatActivity() {
val charging = Settings.isCharging(applicationContext)
var error: String? = null
try {
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM1PhoneNumber(context), charging)
val phoneNumbers = mutableListOf<String>()
phoneNumbers.add(Settings.getSIM1PhoneNumber(applicationContext))
if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
}
Timber.w("numbers = [${phoneNumbers.joinToString()}]")
HttpSmsApiService.create(context).storeHeartbeat(phoneNumbers.toTypedArray(), charging)
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
} catch (exception: Exception) {
Timber.e(exception)
error = exception.javaClass.simpleName
}
if (Settings.isDualSIM(context)) {
try {
HttpSmsApiService.create(context).storeHeartbeat(Settings.getSIM2PhoneNumber(context), charging)
Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
} catch (exception: Exception) {
Timber.e(exception)
error = exception.javaClass.simpleName
}
}
liveData.postValue(error)
Timber.d("finished sending pulse")
}.start()
Expand Down
38 changes: 13 additions & 25 deletions android/app/src/main/java/com/httpsms/worker/HeartbeatWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,25 @@ class HeartbeatWorker(appContext: Context, workerParams: WorkerParameters) : Wor
return Result.failure()
}

sendSIM1Heartbeat()
if (Settings.isDualSIM(applicationContext)) {
sendSIM2Heartbeat()
val phoneNumbers = mutableListOf<String>()
if (Settings.getActiveStatus(applicationContext, Constants.SIM1)) {
phoneNumbers.add(Settings.getSIM1PhoneNumber(applicationContext))
}

return Result.success()
}

private fun sendSIM1Heartbeat() {
if (!Settings.getActiveStatus(applicationContext, Constants.SIM1)) {
Timber.w("[SIM1] user is not active, stopping processing")
return
if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
}

HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
Timber.d("[SIM1] finished sending heartbeat to server")

Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
Timber.d("[SIM1] set the heartbeat timestamp")
}

private fun sendSIM2Heartbeat() {
if (!Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
Timber.w("[SIM2] user is not active, stopping processing")
return
if (phoneNumbers.isEmpty()) {
Timber.w("both [SIM1] and [SIM2] are inactive stopping processing.")
return Result.success()
}

HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM2PhoneNumber(applicationContext), Settings.isCharging(applicationContext))
Timber.d("[SIM2] finished sending heartbeat to server")
HttpSmsApiService.create(applicationContext).storeHeartbeat(phoneNumbers.toTypedArray(), Settings.isCharging(applicationContext))
Timber.d("finished sending heartbeats to server")

Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
Timber.d("[SIM2] set the heartbeat timestamp")
Timber.d("Set the heartbeat timestamp")

return Result.success()
}
}
2 changes: 1 addition & 1 deletion api/pkg/validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {
for index, number := range phoneNumbers {
_, err := phonenumbers.Parse(number, phonenumbers.UNKNOWN_REGION)
if err != nil {
return fmt.Errorf("The %s field in index [%s] must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164", field, index)
return fmt.Errorf("The %s field in index [%d] must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164", field, index)
}
}

Expand Down

0 comments on commit 9b3edbb

Please sign in to comment.