BE: DEEP_SLEEP #1555

Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
jokob-sk
2026-04-22 21:46:37 +10:00
parent f96bdcce6e
commit f5046ff862
27 changed files with 120 additions and 18 deletions

View File

@@ -27,7 +27,7 @@ from logger import mylog
from helper import filePermissions
from utils.datetime_utils import timeNowUTC
from app_state import updateState
from api import update_api
from api import update_api, check_activity
from scan.session_events import process_scan
from initialise import importConfigs, renameSettings
from database import DB
@@ -262,7 +262,25 @@ def main():
update_api(db, all_plugins, True, ["devices"], userUpdatedDevices)
# loop
time.sleep(5) # wait for N seconds
# ------------------------------------------------------------------
# Dynamic sleep (energy saving)
# ------------------------------------------------------------------
if conf.DEEP_SLEEP:
is_active = check_activity()
if is_active:
mylog("debug", ["[DEEP_SLEEP] Active Cycle"])
time.sleep(5)
else:
mylog("debug", ["[DEEP_SLEEP] Passive Cycle"])
for _ in range(3):
if check_activity():
break
time.sleep(20)
else:
time.sleep(5)
# ===============================================================================

View File

@@ -251,3 +251,31 @@ def stop_periodic_write():
periodic_write_thread.join()
periodic_write_running = False
mylog("trace", ["[API] periodic_write thread stopped."])
def check_activity():
"""
Check for active TCP connections on the host.
Reads `/proc/net/tcp` and looks for entries in the ESTABLISHED state
(state code `01`). If any are found, the system is considered "active",
typically indicating interaction via the web UI or API.
Returns:
bool: True if at least one established TCP connection exists,
False otherwise or if the check fails.
Notes:
- Linux-only: relies on `/proc/net/tcp`.
- Lightweight heuristic; does not distinguish connection origin
(e.g., UI vs other services).
- Fail-safe: returns False on any read/parse error.
"""
try:
with open("/proc/net/tcp", "r") as f:
for line in f:
if " 01 " in line: # ESTABLISHED
return True
except:
pass
return False

View File

@@ -246,6 +246,15 @@ def importConfigs(pm, db, all_plugins):
"[]",
"General",
)
conf.DEEP_SLEEP = ccd(
"DEEP_SLEEP",
False,
c_d,
"Deep Sleep",
"""{"dataType": "boolean","elements": [{"elementType": "input","elementOptions": [{ "type": "checkbox" }],"transformers": []}]}""",
"[]",
"General",
)
conf.DISCOVER_PLUGINS = ccd(
"DISCOVER_PLUGINS",
True,