From 449a07e13e070c2821b72ead56277e53a750d2ad Mon Sep 17 00:00:00 2001 From: Reyva Babtista Date: Mon, 2 Dec 2024 11:00:46 -0600 Subject: [PATCH 1/3] feat: add UTC to current timezone timestamp converter --- app/src/main/java/org/beiwe/app/utils.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/src/main/java/org/beiwe/app/utils.kt b/app/src/main/java/org/beiwe/app/utils.kt index 15fde57f..43411e8f 100644 --- a/app/src/main/java/org/beiwe/app/utils.kt +++ b/app/src/main/java/org/beiwe/app/utils.kt @@ -1,8 +1,12 @@ package org.beiwe.app import android.app.PendingIntent +import android.icu.text.SimpleDateFormat +import android.icu.util.TimeZone import android.os.Build import android.util.Log +import java.util.Date +import java.util.Locale // This file is a location for new static functions, further factoring into files will occur when length of file becomes a problem. @@ -53,4 +57,14 @@ fun pending_intent_flag_fix(flag: Int): Int { return (PendingIntent.FLAG_IMMUTABLE or flag) else return flag +} + +/** + * Converts a UTC timestamp to a human-readable date and time string, based on current device's timezone. + */ +fun convertTimestamp(utcTimestamp: Long): String { + val date = Date(utcTimestamp) + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) + dateFormat.timeZone = TimeZone.getDefault() + return dateFormat.format(date) } \ No newline at end of file From f7558960db780f41a0223b8d711f6e985628f86c Mon Sep 17 00:00:00 2001 From: Reyva Babtista Date: Mon, 2 Dec 2024 11:01:00 -0600 Subject: [PATCH 2/3] fix: make on-off logging clearer --- .../main/java/org/beiwe/app/MainService.kt | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/beiwe/app/MainService.kt b/app/src/main/java/org/beiwe/app/MainService.kt index b49bbca3..02d94c10 100644 --- a/app/src/main/java/org/beiwe/app/MainService.kt +++ b/app/src/main/java/org/beiwe/app/MainService.kt @@ -463,14 +463,14 @@ class MainService : Service() { * check. (we pad with an extra second to ensure that check hits an inflection point where * action is required.) */ fun do_an_on_off_session_check( - now: Long, - is_running: Boolean, - should_turn_off_at: Long, - should_turn_on_again_at: Long, - identifier_string: String, - intent_off_string: String, - on_action: () -> Unit, - off_action: () -> Unit, + now: Long, + is_running: Boolean, + should_turn_off_at: Long, + should_turn_on_at: Long, + intent_on_string: String, + intent_off_string: String, + on_action: () -> Unit, + off_action: () -> Unit, ) { // val t1 = System.currentTimeMillis() if (is_running && now <= should_turn_off_at) { @@ -483,28 +483,28 @@ class MainService : Service() { if (is_running && should_turn_off_at < now) { // printi("'$identifier_string' time to turn off") off_action() - val should_turn_on_again_at_safe = should_turn_on_again_at + 1000 // add a second to ensure we pass the timer - print("setting ON TIMER for $identifier_string to $should_turn_on_again_at_safe") - timer!!.setupSingleAlarmAt(should_turn_on_again_at_safe, Timer.intent_map[identifier_string]!!) + val should_turn_on_at_safe = should_turn_on_at + 1000 // add a second to ensure we pass the timer + print("Sensor listener service turned off. Next $intent_on_string is scheduled to ${convertTimestamp(should_turn_on_at_safe)}") + timer!!.setupSingleAlarmAt(should_turn_on_at_safe, Timer.intent_map[intent_on_string]!!) // printv("'$identifier_string - turn off - ${System.currentTimeMillis() - t1}") } // not_running, should turn on is still in the future, do nothing - if (!is_running && should_turn_on_again_at >= now) { + if (!is_running && should_turn_on_at >= now) { // printw("'$identifier_string' correctly off") // printv("'$identifier_string - correctly off - ${System.currentTimeMillis() - t1}") return } // not running, should be on, (on is in the past) - if (!is_running && should_turn_on_again_at < now) { + if (!is_running && should_turn_on_at < now) { // always get the current time, the now value could be stale - unlikely but possible we // care that we get data, not that data be rigidly accurate to a clock. - PersistentData.setMostRecentAlarmTime(identifier_string, System.currentTimeMillis()) + PersistentData.setMostRecentAlarmTime(intent_on_string, System.currentTimeMillis()) // printe("'$identifier_string' turn it on!") on_action() val should_turn_off_at_safe = should_turn_off_at + 1000 // add a second to ensure we pass the timer - print("setting OFF TIMER for $identifier_string to $should_turn_off_at_safe") + print("Sensor listener service turned on. Next $intent_off_string is scheduled to ${convertTimestamp(should_turn_off_at_safe)}") timer!!.setupSingleAlarmAt(should_turn_off_at_safe, Timer.intent_map[intent_off_string]!!) // printv("'$identifier_string - on action - ${System.currentTimeMillis() - t1}") } @@ -776,7 +776,7 @@ class MainService : Service() { startForeground(1, notification) foregroundServiceLastStarted = now } - + PersistentData.serviceStartCommand = Date(System.currentTimeMillis()).toLocaleString() // onStartCommand is called every 30 seconds due to repeating high-priority-or-whatever From eac92bf1c83764048967c192e615fc38b4f59e53 Mon Sep 17 00:00:00 2001 From: Reyva Babtista Date: Thu, 5 Dec 2024 17:03:19 -0600 Subject: [PATCH 3/3] refactor: logging messages --- app/src/main/java/org/beiwe/app/MainService.kt | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/beiwe/app/MainService.kt b/app/src/main/java/org/beiwe/app/MainService.kt index 02d94c10..260685f3 100644 --- a/app/src/main/java/org/beiwe/app/MainService.kt +++ b/app/src/main/java/org/beiwe/app/MainService.kt @@ -474,25 +474,21 @@ class MainService : Service() { ) { // val t1 = System.currentTimeMillis() if (is_running && now <= should_turn_off_at) { - // printw("'$identifier_string' is running, not time to turn of") - // printv("'$identifier_string - is running - ${System.currentTimeMillis() - t1}") + print("Sensor listener service is running. Next $intent_off_string is scheduled to ${convertTimestamp(should_turn_off_at)}") return } // running, should be off, off is in the past if (is_running && should_turn_off_at < now) { - // printi("'$identifier_string' time to turn off") off_action() val should_turn_on_at_safe = should_turn_on_at + 1000 // add a second to ensure we pass the timer - print("Sensor listener service turned off. Next $intent_on_string is scheduled to ${convertTimestamp(should_turn_on_at_safe)}") timer!!.setupSingleAlarmAt(should_turn_on_at_safe, Timer.intent_map[intent_on_string]!!) - // printv("'$identifier_string - turn off - ${System.currentTimeMillis() - t1}") + print("Sensor listener service turned off. Next $intent_on_string is scheduled to ${convertTimestamp(should_turn_on_at_safe)}") } // not_running, should turn on is still in the future, do nothing if (!is_running && should_turn_on_at >= now) { - // printw("'$identifier_string' correctly off") - // printv("'$identifier_string - correctly off - ${System.currentTimeMillis() - t1}") + print("Sensor listener service is off. Next $intent_on_string is scheduled to ${convertTimestamp(should_turn_on_at)}") return } @@ -501,12 +497,10 @@ class MainService : Service() { // always get the current time, the now value could be stale - unlikely but possible we // care that we get data, not that data be rigidly accurate to a clock. PersistentData.setMostRecentAlarmTime(intent_on_string, System.currentTimeMillis()) - // printe("'$identifier_string' turn it on!") on_action() val should_turn_off_at_safe = should_turn_off_at + 1000 // add a second to ensure we pass the timer - print("Sensor listener service turned on. Next $intent_off_string is scheduled to ${convertTimestamp(should_turn_off_at_safe)}") timer!!.setupSingleAlarmAt(should_turn_off_at_safe, Timer.intent_map[intent_off_string]!!) - // printv("'$identifier_string - on action - ${System.currentTimeMillis() - t1}") + print("Sensor listener service turned on. Next $intent_off_string is scheduled to ${convertTimestamp(should_turn_off_at_safe)}") } }