fakegps.py
Android emulator fake gps track player
01:
02:
03: import os,sys
04: from lxml import etree
05: from telnetlib import Telnet
06: from time import sleep
07: from math import *
08: from dateutil.parser import parse as parse_date
09: import math
10: tl = Telnet('127.0.0.1',5554)
11: def read():
12: resp = tl.read_until(b'OK').decode('utf-8')
13:
14: return resp
15: def send(msg):
16:
17: tl.write(msg.encode('utf-8'))
18: def get_pressure_at(altitude):
19: p0 = 101325
20: g = 9.80665
21: R = 8.3144598
22: M = 0.0289644
23: T = 293.15
24: return p0*math.exp(-g*M*altitude/R/T) / 100
25: read()
26: with open(os.path.expanduser('~/.emulator_console_auth_token')) as tok:
27: send(f"auth {tok.read()}\r\n")
28: read()
29: filename = 'route.gpx'
30: route = etree.parse(filename)
31: last_time =0
32: send("sensor get acceleration\r\n")
33: a_x,a_y,a_z = tuple(float(x) for x in read().split()[2].split(':'))
34: print("gravity:",a_y)
35:
36:
37: def jerk(seconds):
38: dt = .03
39: frq = 12
40: for i in range(int(seconds/dt+.5)):
41: new_acc = a_y + 5*sin(i*pi*2/frq)
42: acc = ":".join(str(x) for x in (a_x,new_acc,a_z))
43: send(f"sensor set acceleration {acc}\r\n")
44: read()
45: sleep(dt)
46: track_speed = 1
47: try:
48: for i,trackpoint in enumerate(route.iterfind('.//{*}trkpt')):
49: lon,lat = trackpoint.get('lon'),trackpoint.get('lat')
50: elevation = float(trackpoint.find('{*}ele').text)
51: time = parse_date(trackpoint.find('{*}time').text).timestamp()
52: pressure = get_pressure_at(elevation)
53: if last_time:
54:
55: jerk((time-last_time)/track_speed)
56: send(f"geo fix {lon} {lat} {elevation} 10\r\n")
57: read()
58: send(f"sensor set pressure {pressure}\r\n")
59: print(f"#{i} Position {lat} {lon} {elevation} {pressure} hPa")
60: read()
61: last_time=time
62: finally:
63: acc = ":".join(str(x) for x in (a_x,a_y,a_z))
64: send(f"sensor set acceleration {acc}\r\n")
65: read()
66: