From 9b3edbbe00a196c292a0242adafc9df75ca4b2fe Mon Sep 17 00:00:00 2001 From: Acho Arnold Date: Mon, 25 Nov 2024 10:16:26 +0200 Subject: [PATCH] Add ability to store multiple heartbeats in 1 request --- .../com/httpsms/FirebaseMessagingService.kt | 9 ++++- .../java/com/httpsms/HttpSmsApiService.kt | 8 ++-- .../src/main/java/com/httpsms/MainActivity.kt | 19 ++++------ .../com/httpsms/worker/HeartbeatWorker.kt | 38 +++++++------------ api/pkg/validators/validator.go | 2 +- 5 files changed, 33 insertions(+), 43 deletions(-) diff --git a/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt b/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt index 969efea5..33afe54c 100644 --- a/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt +++ b/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt @@ -1,6 +1,5 @@ package com.httpsms -import android.app.Application import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -56,7 +55,13 @@ class MyFirebaseMessagingService : FirebaseMessagingService() { } Thread { try { - HttpSmsApiService.create(applicationContext).storeHeartbeat(Settings.getSIM1PhoneNumber(applicationContext), Settings.isCharging(applicationContext)) + val phoneNumbers = mutableListOf() + 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) diff --git a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt index 3a48a40b..46c3c1e0 100644 --- a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt +++ b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt @@ -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, charging: Boolean) { val body = """ { "charging": $charging, - "owner": "$phoneNumber" + "phone_numbers": ${phoneNumbers.joinToString(prefix = "[", postfix = "]") { "\"$it\"" }} } """.trimIndent() @@ -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()}]" ) } diff --git a/android/app/src/main/java/com/httpsms/MainActivity.kt b/android/app/src/main/java/com/httpsms/MainActivity.kt index 451914d2..277fe674 100644 --- a/android/app/src/main/java/com/httpsms/MainActivity.kt +++ b/android/app/src/main/java/com/httpsms/MainActivity.kt @@ -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 @@ -309,7 +310,6 @@ class MainActivity : AppCompatActivity() { val progressBar = findViewById(R.id.mainProgressIndicator) progressBar.visibility = View.VISIBLE - val liveData = MutableLiveData() liveData.observe(this) { exception -> run { @@ -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() + 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() diff --git a/android/app/src/main/java/com/httpsms/worker/HeartbeatWorker.kt b/android/app/src/main/java/com/httpsms/worker/HeartbeatWorker.kt index 1ac9cb4c..ab83a3ec 100644 --- a/android/app/src/main/java/com/httpsms/worker/HeartbeatWorker.kt +++ b/android/app/src/main/java/com/httpsms/worker/HeartbeatWorker.kt @@ -16,37 +16,25 @@ class HeartbeatWorker(appContext: Context, workerParams: WorkerParameters) : Wor return Result.failure() } - sendSIM1Heartbeat() - if (Settings.isDualSIM(applicationContext)) { - sendSIM2Heartbeat() + val phoneNumbers = mutableListOf() + 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() } } diff --git a/api/pkg/validators/validator.go b/api/pkg/validators/validator.go index 0436a480..4200f592 100644 --- a/api/pkg/validators/validator.go +++ b/api/pkg/validators/validator.go @@ -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) } }