TCP/IP reconnect

This commit is contained in:
2025-10-27 23:56:16 -04:00
parent ea2f98cfc3
commit 7d16f0fd20
2 changed files with 155 additions and 6 deletions

48
main.py
View File

@@ -6,6 +6,7 @@ import time
import logging
from BetterADBSync import sync_with_options
from tcpip import reconnect_as_tcpip
# debug vs standard logging
DEBUG = False
@@ -79,11 +80,24 @@ def traverse_tree(tree: dict, level: int = 0):
def main():
parser = argparse.ArgumentParser(description="Daemon for syncing files between a computer and an Android device.")
parser.add_argument("--dry-run", action="store_true", help="Perform a dry run without actually "
"syncing files. (Only runs once)")
parser.add_argument(
"--dry-run",
action="store_true",
help="Perform a dry run without actually syncing files. (Only runs once)"
)
parser.add_argument(
"--tcpip",
action="store_true",
help="Reconnect to device over TCP/IP before syncing. Linux only."
)
args = parser.parse_args()
if args.tcpip:
if os.name != 'posix':
logging.error("TCP/IP mode is only supported on Linux systems.")
return
# Read config file
if not os.path.exists("config.ini"):
# Create a new config file
@@ -121,20 +135,29 @@ def main():
logging.error("Could not start ADB server. Exiting...")
return
# Disconnect all devices to start fresh
subprocess.run([adb_path, "disconnect"], stdout=subprocess.PIPE)
run = True
first_run = True
connected = False
while run:
# Get the list of devices
result = subprocess.run([adb_path, "devices"], stdout=subprocess.PIPE)
output = result.stdout.decode("utf-8")
devices = output.split(os.linesep)[1:-2]
devices = [device.split("\t")[0] for device in devices]
devices = [device.split("\t")[0] for device in devices if "offline" not in device]
if len(devices) == 0:
# Device is not yet connnected
logging.info("No device connected. Retrying in 60 seconds.")
time.sleep(60)
# Device is not yet connected
device = config["Global"]["device"] # reset device to config value, in case it was TCP/IP before
first_run = True
if not connected:
logging.info("No device connected. Waiting silently until a device is connected.")
time.sleep(10)
continue
connected = True
if device and device not in devices:
time.sleep(60)
continue
@@ -143,6 +166,19 @@ def main():
if first_run:
logging.info(f"Device connected: {device}")
if args.tcpip:
# Reconnect over TCP/IP
logging.info("Reconnecting device over TCP/IP...")
try:
tcpip_device = reconnect_as_tcpip(device, adb_path)
except (RuntimeError, TimeoutError) as e:
logging.warning(f"Failed to reconnect device over TCP/IP: {e}")
logging.warning("Continuing with the current connection after 10 seconds.")
time.sleep(10)
else:
device = tcpip_device
logging.info(f"Device reconnected over TCP/IP at {tcpip_device}")
logging.info(f"Found {len(config.sections()) - 1} path{'s' if len(config.sections()) > 2 else ''} to sync.")
logging.info('')