diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 711bc5c..e233fae 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -11,8 +11,8 @@ android { applicationId = "com.expapp.trigger" minSdk = 24 targetSdk = 34 - versionCode = 14 - versionName = "1.2.0" + versionCode = 15 + versionName = "1.2.1" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { diff --git a/app/src/main/java/com/expapp/trigger/network/TriggerClient.kt b/app/src/main/java/com/expapp/trigger/network/TriggerClient.kt index e1b8e98..7f196ce 100644 --- a/app/src/main/java/com/expapp/trigger/network/TriggerClient.kt +++ b/app/src/main/java/com/expapp/trigger/network/TriggerClient.kt @@ -248,8 +248,16 @@ class TriggerClient( } } } - // If targetNode not matched or not provided, fallback to root or first entry - matchedObj ?: if (nodesObj.size() > 0) nodesObj.entrySet().first().value.asJsonObject else rootJson + if (matchedObj != null) { + matchedObj + } else if (targetNode.isNotBlank()) { + // Node explicitly configured but missing from sentinel nodes map + return NodeStatus(isOnline = false, statusText = "UNKNOWN NODE", isReachable = true) + } else if (nodesObj.size() > 0) { + nodesObj.entrySet().first().value.asJsonObject + } else { + rootJson + } } else { rootJson } @@ -258,7 +266,7 @@ class TriggerClient( json.has("online") && json.get("online").isJsonPrimitive && json.get("online").asJsonPrimitive.isBoolean -> json.get("online").asBoolean json.has("status") && json.get("status").isJsonPrimitive -> - json.get("status").asString.equals("online", ignoreCase = true) + json.get("status").asString.trim().equals("online", ignoreCase = true) else -> false } val ip = if (json.has("ip") && !json.get("ip").isJsonNull) json.get("ip").asString else null @@ -288,10 +296,10 @@ class TriggerClient( isReachable = true ) } catch (e: Exception) { - NodeStatus(isOnline = false, statusText = "ERROR", isReachable = true) + NodeStatus(isOnline = false, statusText = "ERROR", isReachable = isHttpSuccess) } } else if (isHttpSuccess) { - val isOnline = body.equals("ONLINE", ignoreCase = true) + val isOnline = body.trim().equals("ONLINE", ignoreCase = true) return NodeStatus(isOnline = isOnline, statusText = if (isOnline) "ONLINE" else "OFFLINE", isReachable = true) } else { return NodeStatus(isOnline = false, statusText = "OFFLINE", isReachable = false) diff --git a/app/src/main/java/com/expapp/trigger/worker/ActionTriggerWorker.kt b/app/src/main/java/com/expapp/trigger/worker/ActionTriggerWorker.kt index a8985ce..e3ed602 100644 --- a/app/src/main/java/com/expapp/trigger/worker/ActionTriggerWorker.kt +++ b/app/src/main/java/com/expapp/trigger/worker/ActionTriggerWorker.kt @@ -136,11 +136,12 @@ class ActionTriggerWorker( triggerClient: TriggerClient, appSettings: AppSettings ) { - val maxAttempts = 15 // Up to 60 seconds (15 attempts * 4s) + val maxAttempts = 30 // Up to 60 seconds (30 attempts * 2s) for instant notification var attempts = 0 + var consecutiveTargetMatches = 0 - // Brief delay for hardware/OS to begin reaction - delay(4000L) + // Brief delay before first check to allow server to receive trigger + delay(1500L) while (attempts < maxAttempts) { attempts++ @@ -148,42 +149,54 @@ class ActionTriggerWorker( // If phone network is unavailable, do not misinterpret as server going offline if (!status.isReachable) { - delay(4000L) + delay(2000L) continue } when (action) { TriggerAction.POWER_ON -> { if (status.isOnline) { - appSettings.setLastStatus(device.id, true) - NotificationHelper.sendNotification( - context = appContext, - id = device.id.hashCode(), - title = "✅ Server Online", - message = "${device.name} is now online and reachable.", - isSuccess = true - ) - ServerWidgetProvider.updateAllWidgets(appContext) - return + consecutiveTargetMatches++ + // Require 2 consecutive online confirmations (or 1 if status explicitly verified) + if (consecutiveTargetMatches >= 2 || status.statusText == "ONLINE") { + appSettings.setLastStatus(device.id, true) + NotificationHelper.sendNotification( + context = appContext, + id = device.id.hashCode(), + title = "✅ Server Online", + message = "${device.name} is now online and reachable.", + isSuccess = true + ) + ServerWidgetProvider.updateAllWidgets(appContext) + return + } + } else { + consecutiveTargetMatches = 0 } } TriggerAction.POWER_OFF -> { if (!status.isOnline) { - appSettings.setLastStatus(device.id, false) - NotificationHelper.sendNotification( - context = appContext, - id = device.id.hashCode(), - title = "⚠️ Server Offline", - message = "${device.name} has powered off or is offline.", - isSuccess = false - ) - ServerWidgetProvider.updateAllWidgets(appContext) - return + consecutiveTargetMatches++ + // Require 2 consecutive offline confirmations to completely eliminate false positives + if (consecutiveTargetMatches >= 2) { + appSettings.setLastStatus(device.id, false) + NotificationHelper.sendNotification( + context = appContext, + id = device.id.hashCode(), + title = "⚠️ Server Offline", + message = "${device.name} has powered off or is offline.", + isSuccess = false + ) + ServerWidgetProvider.updateAllWidgets(appContext) + return + } + } else { + consecutiveTargetMatches = 0 } } TriggerAction.REBOOT -> { // No notification for reboot per user instructions - if (attempts >= 2 && status.isOnline) { + if (attempts >= 4 && status.isOnline) { appSettings.setLastStatus(device.id, true) ServerWidgetProvider.updateAllWidgets(appContext) return @@ -191,7 +204,7 @@ class ActionTriggerWorker( } } - delay(4000L) + delay(2000L) } } } diff --git a/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt b/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt index 655fc20..1381748 100644 --- a/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt +++ b/app/src/main/java/com/expapp/trigger/worker/ServerMonitorWorker.kt @@ -91,7 +91,7 @@ class ServerMonitorWorker( for (device in devices) { if (device.statusUrl.isBlank()) continue - val status = triggerClient.checkStatusSync(device) + var status = triggerClient.checkStatusSync(device) // If the endpoint could not be reached (e.g. phone has no internet / network failure), // do NOT treat this as the server going offline. Maintain the last known state. @@ -101,7 +101,20 @@ class ServerMonitorWorker( val previousStatus = appSettings.getLastStatus(device.id) - // Save new status + // If a transition is detected (especially online -> offline or offline -> online), + // double-check with an immediate secondary request after 2 seconds to eliminate false positives + if (previousStatus != null && previousStatus != status.isOnline) { + kotlinx.coroutines.delay(2000L) + val verificationStatus = triggerClient.checkStatusSync(device) + if (verificationStatus.isReachable && verificationStatus.isOnline == status.isOnline) { + status = verificationStatus + } else { + // False positive or transient state; do not change status or trigger false alert + continue + } + } + + // Save new verified status appSettings.setLastStatus(device.id, status.isOnline) // Determine if notification is needed only when server actually transitions