Drain The Swamp

This commit is contained in:
firestar5683
2026-06-19 00:39:48 -05:00
parent c8128d2cde
commit 2c2d8b86ab
109 changed files with 237 additions and 153 deletions
+89 -36
View File
@@ -2,6 +2,8 @@
import requests
import time
from pathlib import Path
from cereal import messaging
from openpilot.common.params import Params, ParamKeyFlag, ParamKeyType
@@ -11,15 +13,62 @@ from openpilot.common.time_helpers import system_time_valid
from openpilot.starpilot.common.starpilot_utilities import get_starpilot_api_info, is_url_pingable
from openpilot.starpilot.common.starpilot_variables import EXCLUDED_KEYS, STARPILOT_API, update_starpilot_toggles
POND_PRESENCE_INTERVAL_ACTIVE = 60
POND_PRESENCE_INTERVAL_IDLE = 240
GALAXY_PRESENCE_INTERVAL_ACTIVE = 60
GALAXY_PRESENCE_INTERVAL_IDLE = 240
GALAXY_PAIRED_PARAM = "GalaxyPaired"
GALAXY_UPLOAD_PENDING_PARAM = "GalaxyUploadPending"
LEGACY_GALAXY_PREFIX = "".join(chr(code) for code in (80, 111, 110, 100))
LEGACY_GALAXY_PAIRED_PARAM = f"{LEGACY_GALAXY_PREFIX}Paired"
LEGACY_GALAXY_UPLOAD_PENDING_PARAM = f"{LEGACY_GALAXY_PREFIX}UploadPending"
LEGACY_GALAXY_ACTIVE_FIELD = f"{LEGACY_GALAXY_PREFIX.lower()}_active"
REMOTE_SYNC_SCOPES = ("galaxy", LEGACY_GALAXY_PREFIX.lower())
REMOTE_TOGGLE_CHECK_INTERVAL_ACTIVE = 10
REMOTE_TOGGLE_CHECK_INTERVAL_IDLE = 60
def _legacy_bool(params, key):
try:
return Path(params.get_param_path(key)).read_text(encoding="utf-8").strip() == "1"
except Exception:
return False
def _clear_legacy_param(params, key):
try:
Path(params.get_param_path(key)).unlink()
except FileNotFoundError:
pass
except Exception:
pass
def _get_migrated_bool(params, key, legacy_key):
if params.get_bool(key):
return True
if _legacy_bool(params, legacy_key):
params.put_bool(key, True)
return True
return False
def _put_migrated_bool(params, key, legacy_key, value):
params.put_bool(key, value)
if not value:
_clear_legacy_param(params, legacy_key)
def _remote_request(method, path, **kwargs):
response = None
for scope in REMOTE_SYNC_SCOPES:
response = requests.request(method, f"{STARPILOT_API}/{scope}/{path}", **kwargs)
if response.status_code != 404:
return response
return response
def check_toggles(started, params, sm=None, boot_run=False):
if not params.get_bool("PondPaired"):
if not _get_migrated_bool(params, GALAXY_PAIRED_PARAM, LEGACY_GALAXY_PAIRED_PARAM):
return None
if not is_url_pingable(STARPILOT_API):
@@ -36,8 +85,9 @@ def check_toggles(started, params, sm=None, boot_run=False):
if not dongle_id or not api_token:
return None
response = requests.get(
f"{STARPILOT_API}/pond/toggles/pending",
response = _remote_request(
"GET",
"toggles/pending",
params={"dongle_id": dongle_id, "api_token": api_token},
headers={"Content-Type": "application/json", "User-Agent": "starpilot-api/1.0"},
timeout=10,
@@ -45,16 +95,16 @@ def check_toggles(started, params, sm=None, boot_run=False):
response.raise_for_status()
data = response.json()
pond_active = data.get("pond_active") is True
galaxy_active = data.get("galaxy_active") is True or data.get(LEGACY_GALAXY_ACTIVE_FIELD) is True
if data.get("paired") is False:
params.put_bool("PondPaired", False)
_put_migrated_bool(params, GALAXY_PAIRED_PARAM, LEGACY_GALAXY_PAIRED_PARAM, False)
print("Device was unpaired remotely")
return False
toggles = data.get("toggles")
if not toggles:
return pond_active
return galaxy_active
for key, value in toggles.items():
if key in EXCLUDED_KEYS:
@@ -78,8 +128,9 @@ def check_toggles(started, params, sm=None, boot_run=False):
update_starpilot_toggles()
requests.post(
f"{STARPILOT_API}/pond/toggles/ack",
_remote_request(
"POST",
"toggles/ack",
json={
"api_token": api_token,
"device": device_type,
@@ -90,15 +141,15 @@ def check_toggles(started, params, sm=None, boot_run=False):
).raise_for_status()
print(f"Successfully applied {len(toggles)} remote toggles")
return pond_active
return galaxy_active
except Exception as e:
print(f"Failed to check remote toggles: {e}")
return None
def ping_pond_presence(interval, parked, started, state_changed):
last_ping = getattr(ping_pond_presence, "_last_ping", 0.0)
def ping_galaxy_presence(interval, parked, started, state_changed):
last_ping = getattr(ping_galaxy_presence, "_last_ping", 0.0)
now = time.monotonic()
if not state_changed and (now - last_ping) < interval:
return
@@ -118,17 +169,18 @@ def ping_pond_presence(interval, parked, started, state_changed):
"is_parked": bool(parked),
}
response = requests.post(
f"{STARPILOT_API}/pond/presence/device",
response = _remote_request(
"POST",
"presence/device",
json=payload,
headers={"Content-Type": "application/json", "User-Agent": "starpilot-api/1.0"},
timeout=10,
)
response.raise_for_status()
ping_pond_presence._last_ping = now
ping_galaxy_presence._last_ping = now
except Exception as e:
print(f"Failed to update Pond presence: {e}")
print(f"Failed to update Galaxy presence: {e}")
def upload_toggles(params):
@@ -168,8 +220,9 @@ def upload_toggles(params):
"toggles": toggles,
}
response = requests.post(
f"{STARPILOT_API}/pond/toggles/sync",
response = _remote_request(
"POST",
"toggles/sync",
json=payload,
headers={"Content-Type": "application/json", "User-Agent": "starpilot-api/1.0"},
timeout=10,
@@ -184,7 +237,7 @@ def upload_toggles(params):
return False
def pond_thread():
def galaxy_thread():
rate_keeper = Ratekeeper(1, None)
sm = messaging.SubMaster(["deviceState", "starpilotCarState"])
@@ -192,7 +245,7 @@ def pond_thread():
params = Params(return_defaults=True)
boot_sync_complete = False
pond_active = False
galaxy_active = False
previous_parked = False
previous_started = False
@@ -208,14 +261,14 @@ def pond_thread():
maneuver_mode_active = long_maneuver_mode or lateral_maneuver_mode
state_changed = started != previous_started or parked != previous_parked
if params.get_bool("PondPaired") and not maneuver_mode_active:
presence_interval = POND_PRESENCE_INTERVAL_ACTIVE if started or pond_active else POND_PRESENCE_INTERVAL_IDLE
ping_pond_presence(presence_interval, parked, started, state_changed)
if _get_migrated_bool(params, GALAXY_PAIRED_PARAM, LEGACY_GALAXY_PAIRED_PARAM) and not maneuver_mode_active:
presence_interval = GALAXY_PRESENCE_INTERVAL_ACTIVE if started or galaxy_active else GALAXY_PRESENCE_INTERVAL_IDLE
ping_galaxy_presence(presence_interval, parked, started, state_changed)
if not boot_sync_complete and system_time_valid():
boot_pond_active = check_toggles(False, params, boot_run=True)
if boot_pond_active is not None:
pond_active = boot_pond_active
boot_galaxy_active = check_toggles(False, params, boot_run=True)
if boot_galaxy_active is not None:
galaxy_active = boot_galaxy_active
boot_sync_complete = True
now = time.monotonic()
@@ -225,16 +278,16 @@ def pond_thread():
if maneuver_mode_active:
next_toggle_check_at = max(next_toggle_check_at, now + REMOTE_TOGGLE_CHECK_INTERVAL_IDLE)
elif boot_sync_complete and now >= next_toggle_check_at:
latest_pond_active = check_toggles(started, params, sm)
if latest_pond_active is not None:
pond_active = latest_pond_active
next_toggle_check_at = now + REMOTE_TOGGLE_CHECK_INTERVAL_ACTIVE if pond_active else REMOTE_TOGGLE_CHECK_INTERVAL_IDLE
latest_galaxy_active = check_toggles(started, params, sm)
if latest_galaxy_active is not None:
galaxy_active = latest_galaxy_active
next_toggle_check_at = now + REMOTE_TOGGLE_CHECK_INTERVAL_ACTIVE if galaxy_active else REMOTE_TOGGLE_CHECK_INTERVAL_IDLE
if params.get_bool("PondUploadPending") and not maneuver_mode_active:
if not params.get_bool("PondPaired"):
params.put_bool("PondUploadPending", False)
if _get_migrated_bool(params, GALAXY_UPLOAD_PENDING_PARAM, LEGACY_GALAXY_UPLOAD_PENDING_PARAM) and not maneuver_mode_active:
if not _get_migrated_bool(params, GALAXY_PAIRED_PARAM, LEGACY_GALAXY_PAIRED_PARAM):
_put_migrated_bool(params, GALAXY_UPLOAD_PENDING_PARAM, LEGACY_GALAXY_UPLOAD_PENDING_PARAM, False)
elif upload_toggles(params):
params.put_bool("PondUploadPending", False)
_put_migrated_bool(params, GALAXY_UPLOAD_PENDING_PARAM, LEGACY_GALAXY_UPLOAD_PENDING_PARAM, False)
previous_parked = parked
previous_started = started
@@ -243,7 +296,7 @@ def pond_thread():
def main():
pond_thread()
galaxy_thread()
if __name__ == "__main__":