Compare commits
No commits in common. "7ce14aafda7e5e1264bd47bd8ea5684c10c91c6d" and "8ece0f751aa30872a736fa6b485badd435338add" have entirely different histories.
7ce14aafda
...
8ece0f751a
1 changed files with 4 additions and 7 deletions
11
main.py
11
main.py
|
|
@ -12,31 +12,28 @@ cur = con.cursor()
|
||||||
# init DB
|
# init DB
|
||||||
cur.execute("CREATE TABLE IF NOT EXISTS pings (ping REAL, pingcount INT, latitude REAL, longitude REAL, time INT);")
|
cur.execute("CREATE TABLE IF NOT EXISTS pings (ping REAL, pingcount INT, latitude REAL, longitude REAL, time INT);")
|
||||||
|
|
||||||
# init vars
|
|
||||||
prev_latitude, prev_longitude, latitude, longitude = (0, 0, 0, 0)
|
prev_latitude, prev_longitude, latitude, longitude = (0, 0, 0, 0)
|
||||||
ping_sum, ping_count, prev_sec, sec_now = (0, 0, 0, 0)
|
|
||||||
|
|
||||||
|
ping_sum = 0
|
||||||
|
ping_count = 0
|
||||||
|
prev_sec = 0
|
||||||
|
sec_now = 0
|
||||||
while True:
|
while True:
|
||||||
sec_now = time.localtime().tm_sec
|
sec_now = time.localtime().tm_sec
|
||||||
|
|
||||||
if sec_now != prev_sec:
|
if sec_now != prev_sec:
|
||||||
# Get the train's location from the iceportal API
|
|
||||||
ice_status = requests.get("https://iceportal.de/api1/rs/status").json()
|
ice_status = requests.get("https://iceportal.de/api1/rs/status").json()
|
||||||
latitude = ice_status["latitude"]
|
latitude = ice_status["latitude"]
|
||||||
longitude = ice_status["longitude"]
|
longitude = ice_status["longitude"]
|
||||||
|
|
||||||
# if the train moved, commit values to db, print to console
|
|
||||||
if (prev_latitude != latitude or prev_longitude != longitude) and ping_count > 0:
|
if (prev_latitude != latitude or prev_longitude != longitude) and ping_count > 0:
|
||||||
print(f"({latitude}, {longitude}): \t {ping_sum / ping_count} Sekunden avg von {ping_count} {'Pings' if ping_count > 1 else 'Ping'}")
|
print(f"({latitude}, {longitude}): \t {ping_sum / ping_count} Sekunden avg von {ping_count} {'Pings' if ping_count > 1 else 'Ping'}")
|
||||||
cur.execute("INSERT INTO pings (ping, pingcount, latitude, longitude, time) VALUES(?, ?, ?, ?, ?);", (ping_sum/ping_count, ping_count, latitude, longitude, int(time.time())))
|
cur.execute("INSERT INTO pings (ping, pingcount, latitude, longitude, time) VALUES(?, ?, ?, ?, ?);", (ping_sum/ping_count, ping_count, latitude, longitude, int(time.time())))
|
||||||
con.commit()
|
|
||||||
|
|
||||||
# Reset values
|
|
||||||
ping_sum = 0
|
ping_sum = 0
|
||||||
ping_count = 0
|
ping_count = 0
|
||||||
prev_latitude, prev_longitude, prev_sec = latitude, longitude, sec_now
|
prev_latitude, prev_longitude, prev_sec = latitude, longitude, sec_now
|
||||||
|
|
||||||
# Ping the desired IP
|
|
||||||
ping_now = ping(ip, timeout=2)
|
ping_now = ping(ip, timeout=2)
|
||||||
ping_sum += 2 if ping_now is None else ping_now
|
ping_sum += 2 if ping_now is None else ping_now
|
||||||
ping_count += 1
|
ping_count += 1
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue