Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix on off logging #102

Open
wants to merge 3 commits into
base: staged-updates
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions app/src/main/java/org/beiwe/app/MainService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -463,50 +463,44 @@ 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) {
// 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_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]!!)
// printv("'$identifier_string - turn off - ${System.currentTimeMillis() - t1}")
val should_turn_on_at_safe = should_turn_on_at + 1000 // add a second to ensure we pass the timer
timer!!.setupSingleAlarmAt(should_turn_on_at_safe, Timer.intent_map[intent_on_string]!!)
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_again_at >= now) {
// printw("'$identifier_string' correctly off")
// printv("'$identifier_string - correctly off - ${System.currentTimeMillis() - t1}")
if (!is_running && should_turn_on_at >= now) {
print("Sensor listener service is off. Next $intent_on_string is scheduled to ${convertTimestamp(should_turn_on_at)}")
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())
// printe("'$identifier_string' turn it on!")
PersistentData.setMostRecentAlarmTime(intent_on_string, System.currentTimeMillis())
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")
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)}")
}
}

Expand Down Expand Up @@ -776,7 +770,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
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/beiwe/app/utils.kt
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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)
}