testsAndMisc/phone_focus_mode/deploy_gps.sh
Krzysztof kuhy Rudnicki b11b93457f
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
deploy: split 875 -> 241 + 6, finishing phone_focus_mode
Held until last on purpose: all five enforcer splits this session grew
deploy.sh's two hardcoded file lists, so cutting it earlier would have left
every "add to BOTH lists" step pointing at a stale location. Those lists are
final as of the focus_ctl commit, so this is now safe.

do_deploy was 421 lines -- over the cap on its own, so moving it whole would
have achieved nothing. It is cut at the phase boundaries it already prints
("[4/7] Uploading scripts...", "[5/7] Setting permissions...") plus two more
inside the push phase, giving seven named functions instead of one wall.

The two hardcoded lists now live in SEPARATE files, which is the real risk
this commit introduces. deploy_phases.sh holds the push list and
deploy_install.sh the cp list; each header names the other and says what
happens when they drift -- the file is staged, never lands, and whatever
sources it fails to start with no obvious cause. Checked in both directions
after the move: the only asymmetry is config_secrets.sh, which a conditional
block handles so the phone's real coordinates survive a deploy. That was
asymmetric at HEAD too.

check_coords now RETURNS whether the coordinates are placeholders instead of
assigning $NEEDS_GPS_FETCH. The flag is written in deploy.sh and read in
deploy_phases.sh, so assigning it from deploy_gps.sh as well left that file
writing a global it never reads. Behaviour proven identical to HEAD: real
coords -> 0, placeholders -> 1.

NO TESTS, and that is a deliberate limit rather than an omission. Every
phase here is a sequence of adb calls against a real device; a stub layer
deep enough to mean anything would be modelling the phone, not testing the
script. Verification is that deploy.sh parses, sources all six libraries and
reaches its usage message -- which catches a broken split, not a wrong one.

515 assertions across ten phone suites still pass; run_phone.sh --help
exits 0.

Over-cap 16 -> 15, and NO phone_focus_mode file is over the cap any more.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 17:53:52 +02:00

84 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# deploy_gps.sh — the home-coordinate capture: reading a fix off the phone
# when config_secrets.sh still holds placeholders, and the --capture-coords
# action that writes one deliberately.
#
# Sourced by deploy.sh, which owns adb_cmd and $DEPLOY_DIR. $NEEDS_GPS_FETCH
# is written by check_coords here and read by the deploy run, so it stays
# declared in deploy.sh with the other run-scoped state.
check_coords() {
local lat lon
lat="$(grep '^.*HOME_LAT=' "$DEPLOY_DIR/config.sh" "$DEPLOY_DIR/config_secrets.sh" 2>/dev/null | tail -1 | cut -d'"' -f2)"
lon="$(grep '^.*HOME_LON=' "$DEPLOY_DIR/config.sh" "$DEPLOY_DIR/config_secrets.sh" 2>/dev/null | tail -1 | cut -d'"' -f2)"
if [ "$lat" = "0.000000" ] && [ "$lon" = "0.000000" ]; then
echo "ERROR: Home coordinates not set (all zeros)."
exit 1
fi
# If both look like valid floats, use them; otherwise auto-capture from
# phone GPS. The answer is the EXIT STATUS rather than an assignment to
# $NEEDS_GPS_FETCH: this file would otherwise write a global it never
# reads (SC2034), and the linter runs without -x so each file has to
# stand alone. deploy.sh sets the flag from what this returns.
if [[ -n "$lat" && "$lat" =~ ^[+-]?[0-9]+\.[0-9]+$ && -n "$lon" && "$lon" =~ ^[+-]?[0-9]+\.[0-9]+$ ]]; then
echo " Home location: $lat, $lon"
return 1
fi
echo " Home location: placeholder — will be captured from phone GPS at deploy time."
return 0
}
fetch_home_coords_from_phone() {
echo " Enabling location services on phone..." >&2
adb_cmd shell settings put secure location_mode 3 2>/dev/null || true
echo " Waiting for network/fused location fix (up to ${GPS_MAX_WAIT_SECS}s)..." >&2
local waited=0 coords=""
while [[ -z "$coords" && $waited -lt $GPS_MAX_WAIT_SECS ]]; do
sleep 3
waited=$((waited + 3))
# Format on Android 10+: " last location=Location[fused LAT,LON ...]"
local raw
raw="$(adb_cmd shell dumpsys location 2>/dev/null |
grep 'last location=Location\[' |
grep -oE '[+-]?[0-9]+\.[0-9]+,[+-]?[0-9]+\.[0-9]+' |
head -1 || true)"
[[ -n "$raw" ]] && coords="$raw"
printf '.' >&2
done
printf '\n' >&2
if [[ -z "$coords" ]]; then
echo "ERROR: No location fix after ${GPS_MAX_WAIT_SECS}s." >&2
echo " Make sure the phone has cellular or WiFi data, then retry." >&2
echo " Or set HOME_LAT/HOME_LON manually in config_secrets.sh." >&2
return 1
fi
local lat="${coords%,*}"
local lon="${coords#*,}"
echo " GPS fix acquired: ${lat}, ${lon}" >&2
printf '%s %s' "$lat" "$lon"
}
do_capture_coords() {
# Standalone GPS capture for post-WiFi-setup use.
# Overwrites config_secrets.sh on the phone with the current location.
connect_adb
if ! adb_root "id" 2>/dev/null | grep -q "uid=0"; then
echo "ERROR: Root not available."
exit 1
fi
echo "Capturing home coordinates from phone GPS..."
local gps_result gps_lat gps_lon
gps_result="$(fetch_home_coords_from_phone)"
gps_lat="${gps_result% *}"
gps_lon="${gps_result#* }"
adb_root "printf '#!/system/bin/sh\n# Home coordinates auto-captured from GPS\nexport HOME_LAT=\"${gps_lat}\"\nexport HOME_LON=\"${gps_lon}\"\n' \
> $REMOTE_DIR/config_secrets.sh"
echo "Home coordinates updated on phone: ${gps_lat}, ${gps_lon}"
echo "Restarting focus daemon to apply new coordinates..."
adb_root "sh $REMOTE_DIR/focus_ctl.sh restart" 2>/dev/null || true
echo "Done."
}