Files
scripts/find_adb_device.py
Bogomil Vasilev 37e29989fe add some more scripts
Signed-off-by: Bogomil Vasilev <smirky@smirky.net>
2022-02-11 14:36:10 +02:00

66 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
from os import environ
import subprocess
import json
import sys
import re
def main():
adb_cmd = subprocess.Popen(['adb', 'devices', '-l'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
adb_cmd.wait()
out, _ = adb_cmd.communicate()
for num, line in enumerate(out.strip().split('\n')):
if line == 'List of devices attached':
devices = out.strip().split('\n')[num+1:]
break
device = None
if len(sys.argv) == 1:
if len(devices) == 1:
device = devices[0].split()[0]
elif len(devices) > 1:
print(f'Please specify a device: {sys.argv[0]} <device>')
print('More than 1 device detected:')
print('\n'.join(devices))
exit(1)
elif len(devices) == 0:
print('No devices available! Perhaps first try: adb connect <device>')
exit(1)
elif len(sys.argv) == 2:
device = sys.argv[1]
if device not in devices:
print(f'Device not in list, connecting to: {device}')
adb_cmd = subprocess.run(['adb', 'connect', device])
else:
print('Unexpected argument count!')
exit(1)
adb_cmd_string = ['adb', '-s', device, 'shell', 'dumpsys', 'location']
print(f'Running: {adb_cmd_string}')
adb_cmd = subprocess.Popen(
adb_cmd_string,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
adb_cmd.wait()
out, err = adb_cmd.communicate()
if adb_cmd.returncode != 0:
print("adb failed:\n{err}")
exit(1)
gps = {}
for line in out.split('\n'):
if 'Gnss Location Data' in line:
gps_data = re.sub('.*Gnss Location Data:: ', '', line)
for attr in gps_data.split(','):
gps[attr.split()[0].replace(':', '')] = attr.split()[1]
# url = f'https://www.google.com/maps/place/{gps["LatitudeDegrees"]},{gps["LongitudeDegrees"]}'
url = f'http://www.openstreetmap.org/?mlat={gps["LatitudeDegrees"]}&mlon={gps["LongitudeDegrees"]}&zoom=17'
if environ.get('DISPLAY'):
subprocess.run(['xdg-open', url])
else:
print(url)
main()