fix(notifications): instant state transition polling, 2-stage verification to prevent false positives (v1.2.1)
This commit is contained in:
@@ -11,8 +11,8 @@ android {
|
|||||||
applicationId = "com.expapp.trigger"
|
applicationId = "com.expapp.trigger"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 34
|
targetSdk = 34
|
||||||
versionCode = 14
|
versionCode = 15
|
||||||
versionName = "1.2.0"
|
versionName = "1.2.1"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables {
|
vectorDrawables {
|
||||||
|
|||||||
@@ -248,8 +248,16 @@ class TriggerClient(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If targetNode not matched or not provided, fallback to root or first entry
|
if (matchedObj != null) {
|
||||||
matchedObj ?: if (nodesObj.size() > 0) nodesObj.entrySet().first().value.asJsonObject else rootJson
|
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 {
|
} else {
|
||||||
rootJson
|
rootJson
|
||||||
}
|
}
|
||||||
@@ -258,7 +266,7 @@ class TriggerClient(
|
|||||||
json.has("online") && json.get("online").isJsonPrimitive && json.get("online").asJsonPrimitive.isBoolean ->
|
json.has("online") && json.get("online").isJsonPrimitive && json.get("online").asJsonPrimitive.isBoolean ->
|
||||||
json.get("online").asBoolean
|
json.get("online").asBoolean
|
||||||
json.has("status") && json.get("status").isJsonPrimitive ->
|
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
|
else -> false
|
||||||
}
|
}
|
||||||
val ip = if (json.has("ip") && !json.get("ip").isJsonNull) json.get("ip").asString else null
|
val ip = if (json.has("ip") && !json.get("ip").isJsonNull) json.get("ip").asString else null
|
||||||
@@ -288,10 +296,10 @@ class TriggerClient(
|
|||||||
isReachable = true
|
isReachable = true
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
NodeStatus(isOnline = false, statusText = "ERROR", isReachable = true)
|
NodeStatus(isOnline = false, statusText = "ERROR", isReachable = isHttpSuccess)
|
||||||
}
|
}
|
||||||
} else if (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)
|
return NodeStatus(isOnline = isOnline, statusText = if (isOnline) "ONLINE" else "OFFLINE", isReachable = true)
|
||||||
} else {
|
} else {
|
||||||
return NodeStatus(isOnline = false, statusText = "OFFLINE", isReachable = false)
|
return NodeStatus(isOnline = false, statusText = "OFFLINE", isReachable = false)
|
||||||
|
|||||||
@@ -136,11 +136,12 @@ class ActionTriggerWorker(
|
|||||||
triggerClient: TriggerClient,
|
triggerClient: TriggerClient,
|
||||||
appSettings: AppSettings
|
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 attempts = 0
|
||||||
|
var consecutiveTargetMatches = 0
|
||||||
|
|
||||||
// Brief delay for hardware/OS to begin reaction
|
// Brief delay before first check to allow server to receive trigger
|
||||||
delay(4000L)
|
delay(1500L)
|
||||||
|
|
||||||
while (attempts < maxAttempts) {
|
while (attempts < maxAttempts) {
|
||||||
attempts++
|
attempts++
|
||||||
@@ -148,13 +149,16 @@ class ActionTriggerWorker(
|
|||||||
|
|
||||||
// If phone network is unavailable, do not misinterpret as server going offline
|
// If phone network is unavailable, do not misinterpret as server going offline
|
||||||
if (!status.isReachable) {
|
if (!status.isReachable) {
|
||||||
delay(4000L)
|
delay(2000L)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
when (action) {
|
when (action) {
|
||||||
TriggerAction.POWER_ON -> {
|
TriggerAction.POWER_ON -> {
|
||||||
if (status.isOnline) {
|
if (status.isOnline) {
|
||||||
|
consecutiveTargetMatches++
|
||||||
|
// Require 2 consecutive online confirmations (or 1 if status explicitly verified)
|
||||||
|
if (consecutiveTargetMatches >= 2 || status.statusText == "ONLINE") {
|
||||||
appSettings.setLastStatus(device.id, true)
|
appSettings.setLastStatus(device.id, true)
|
||||||
NotificationHelper.sendNotification(
|
NotificationHelper.sendNotification(
|
||||||
context = appContext,
|
context = appContext,
|
||||||
@@ -166,9 +170,15 @@ class ActionTriggerWorker(
|
|||||||
ServerWidgetProvider.updateAllWidgets(appContext)
|
ServerWidgetProvider.updateAllWidgets(appContext)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
consecutiveTargetMatches = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
TriggerAction.POWER_OFF -> {
|
TriggerAction.POWER_OFF -> {
|
||||||
if (!status.isOnline) {
|
if (!status.isOnline) {
|
||||||
|
consecutiveTargetMatches++
|
||||||
|
// Require 2 consecutive offline confirmations to completely eliminate false positives
|
||||||
|
if (consecutiveTargetMatches >= 2) {
|
||||||
appSettings.setLastStatus(device.id, false)
|
appSettings.setLastStatus(device.id, false)
|
||||||
NotificationHelper.sendNotification(
|
NotificationHelper.sendNotification(
|
||||||
context = appContext,
|
context = appContext,
|
||||||
@@ -180,10 +190,13 @@ class ActionTriggerWorker(
|
|||||||
ServerWidgetProvider.updateAllWidgets(appContext)
|
ServerWidgetProvider.updateAllWidgets(appContext)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
consecutiveTargetMatches = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
TriggerAction.REBOOT -> {
|
TriggerAction.REBOOT -> {
|
||||||
// No notification for reboot per user instructions
|
// No notification for reboot per user instructions
|
||||||
if (attempts >= 2 && status.isOnline) {
|
if (attempts >= 4 && status.isOnline) {
|
||||||
appSettings.setLastStatus(device.id, true)
|
appSettings.setLastStatus(device.id, true)
|
||||||
ServerWidgetProvider.updateAllWidgets(appContext)
|
ServerWidgetProvider.updateAllWidgets(appContext)
|
||||||
return
|
return
|
||||||
@@ -191,7 +204,7 @@ class ActionTriggerWorker(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(4000L)
|
delay(2000L)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ class ServerMonitorWorker(
|
|||||||
for (device in devices) {
|
for (device in devices) {
|
||||||
if (device.statusUrl.isBlank()) continue
|
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),
|
// 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.
|
// 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)
|
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)
|
appSettings.setLastStatus(device.id, status.isOnline)
|
||||||
|
|
||||||
// Determine if notification is needed only when server actually transitions
|
// Determine if notification is needed only when server actually transitions
|
||||||
|
|||||||
Reference in New Issue
Block a user