From 42e1af594e58f096936c407c868fe70f035885ce Mon Sep 17 00:00:00 2001 From: Lee Ballard Date: Thu, 2 May 2024 11:24:49 -0500 Subject: [PATCH] ballle98/AqualinkD#90: Support sharing same PDA ID between control panel and physical PDA --- pda.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/pda.c b/pda.c index f2718531..d9ca9fa4 100644 --- a/pda.c +++ b/pda.c @@ -56,6 +56,9 @@ bool pda_shouldSleep() { //LOG(PDA_LOG,LOG_DEBUG, "PDA loop count %d, will sleep at %d\n",_pda_loop_cnt,PDA_LOOP_COUNT); struct timespec now; struct timespec elapsed; + static bool probeRecvdAfterActive = false; + static bool activate = false; + // If aqualinkd was restarted and a probe has not been received force a sleep if (! _pda_first_probe_recvd) { return true; @@ -63,26 +66,47 @@ bool pda_shouldSleep() { return false; } + if (activate) { + LOG(PDA_LOG,LOG_DEBUG, "PDA received probe after activate\n"); + probeRecvdAfterActive = true; + } + // NSF NEED TO CHECK ACTIVE THREADS. if (_aqualink_data->active_thread.thread_id != 0) { LOG(PDA_LOG,LOG_DEBUG, "PDA can't sleep as thread %s(%d),%p is active\n", ptypeName(_aqualink_data->active_thread.ptype), _aqualink_data->active_thread.ptype, _aqualink_data->active_thread.thread_id); - return false; - } + activate = true; // Last see if there are any open websockets. (don't sleep if the web UI is open) - if ((! _config_parameters->pda_sleep_with_websock) && + } else if ((! _config_parameters->pda_sleep_with_websock) && ( _aqualink_data->open_websockets > 0 )) { LOG(PDA_LOG,LOG_DEBUG, "PDA can't sleep as websocket is active\n"); - return false; + activate = true; + } else { + clock_gettime(CLOCK_REALTIME, &now); + timespec_subtract(&elapsed, &now, &(_aqualink_data->last_active_time)); + if (elapsed.tv_sec >= PDA_SLEEP_FOR) { + if (!activate) { + LOG(PDA_LOG,LOG_DEBUG, "PDA Slept for %ld sec\n", elapsed.tv_sec); + } + activate = true; + } else { + activate = false; + probeRecvdAfterActive = false; + } } - clock_gettime(CLOCK_REALTIME, &now); - timespec_subtract(&elapsed, &now, &(_aqualink_data->last_active_time)); - if (elapsed.tv_sec > PDA_SLEEP_FOR) { - return false; + if (activate) { + if (probeRecvdAfterActive) { + return false; + } else { + LOG(PDA_LOG,LOG_DEBUG, "Wait for probe to activate\n"); + probeRecvdAfterActive = false; + _pda_first_probe_recvd = false; + return true; + } } return true;