Commit 3180219d authored by Lisa (AI Assistant)'s avatar Lisa (AI Assistant)

Fix: Send pairing request on reconnect instead of on closing socket

parent e36c37af
...@@ -63,6 +63,7 @@ class NodeClient() { ...@@ -63,6 +63,7 @@ class NodeClient() {
private var challengeNonce: String = "" private var challengeNonce: String = ""
private var challengeTimestamp: Long = 0 private var challengeTimestamp: Long = 0
private var hasReceivedChallenge = false private var hasReceivedChallenge = false
private var pendingPairingAfterConnect = false // Flag to send pairing request after reconnect
// SharedPreferences for device identity persistence // SharedPreferences for device identity persistence
private var prefs: SharedPreferences? = null private var prefs: SharedPreferences? = null
...@@ -223,8 +224,15 @@ class NodeClient() { ...@@ -223,8 +224,15 @@ class NodeClient() {
hasReceivedChallenge = true hasReceivedChallenge = true
Log.d(TAG, "Received challenge: nonce=$challengeNonce, ts=$challengeTimestamp") Log.d(TAG, "Received challenge: nonce=$challengeNonce, ts=$challengeTimestamp")
// Now send connect request with proper v3 signature // Check if we need to send pairing request instead of connect
sendConnectRequestWithChallenge(nodeId, token, capabilities) if (pendingPairingAfterConnect) {
pendingPairingAfterConnect = false
Log.d(TAG, "Sending pairing request after reconnect")
sendPairingRequest(nodeId, token, capabilities)
} else {
// Now send connect request with proper v3 signature
sendConnectRequestWithChallenge(nodeId, token, capabilities)
}
} else if (event == "node.pair.resolved") { } else if (event == "node.pair.resolved") {
// Pairing was approved, now reconnect with token // Pairing was approved, now reconnect with token
val approved = json.optJSONObject("payload")?.optBoolean("approved", false) val approved = json.optJSONObject("payload")?.optBoolean("approved", false)
...@@ -252,9 +260,9 @@ class NodeClient() { ...@@ -252,9 +260,9 @@ class NodeClient() {
if ((error?.contains("device-required") == true || if ((error?.contains("device-required") == true ||
error?.contains("device signature") == true || error?.contains("device signature") == true ||
error?.contains("invalid") == true) && isPairingMode) { error?.contains("invalid") == true) && isPairingMode) {
// Now send pairing request after connect failed // Set flag to send pairing request after reconnect
Log.d(TAG, "Sending pairing request after connection rejection") pendingPairingAfterConnect = true
sendPairingRequest(currentNodeId, currentToken, capabilities) Log.d(TAG, "Will send pairing request on reconnect after connection rejection")
} else { } else {
_connectionStatus.trySend(ConnectionStatus(false, nodeId)) _connectionStatus.trySend(ConnectionStatus(false, nodeId))
} }
...@@ -276,13 +284,13 @@ class NodeClient() { ...@@ -276,13 +284,13 @@ class NodeClient() {
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
Log.d(TAG, "WebSocket closing: code=$code reason=$reason") Log.d(TAG, "WebSocket closing: code=$code reason=$reason")
// Check if it's a device signature error and we should send pairing request // Check if it's a device signature error - need to reconnect with pairing request
if (isPairingMode && (reason.contains("device signature") || if (isPairingMode && (reason.contains("device signature") ||
reason.contains("invalid") || reason.contains("invalid") ||
reason.contains("device-required"))) { reason.contains("device-required"))) {
Log.d(TAG, "Connection rejected due to device auth, sending pairing request") Log.d(TAG, "Connection rejected due to device auth, will send pairing request on reconnect")
// Send pairing request after connection rejection // Set flag to send pairing request after reconnect
sendPairingRequest(currentNodeId, currentToken, capabilities) pendingPairingAfterConnect = true
} }
webSocket.close(1000, null) webSocket.close(1000, null)
...@@ -296,8 +304,14 @@ class NodeClient() { ...@@ -296,8 +304,14 @@ class NodeClient() {
if (isPairingMode && (reason.contains("device signature") || if (isPairingMode && (reason.contains("device signature") ||
reason.contains("invalid") || reason.contains("invalid") ||
reason.contains("device-required"))) { reason.contains("device-required"))) {
Log.d(TAG, "Connection rejected due to device auth, sending pairing request") Log.d(TAG, "Connection rejected due to device auth, will send pairing request on reconnect")
sendPairingRequest(currentNodeId, currentToken, capabilities) // Set flag to send pairing request after reconnect
pendingPairingAfterConnect = true
// Reconnect
scope.launch {
delay(500) // Brief delay before reconnecting
connect(currentNodeId, currentToken, capabilities)
}
return return
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment