Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI arguments for listen() #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytelemetry.egg-info/
__pycache__/
build/
dist/

.spyproject
.mypy_cache
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Datagrams specification can be found [here](https://forums.codemasters.com/discu
- works only in Time Trial mode
- all telemetry data get collected, but only a restricted minority (speed, throttle, brake, gear)
are effectively used

## Contributions
I would love to receive pull requests to improve this package.

Expand All @@ -19,7 +19,9 @@ While in **Time Trial** mode, run

`python3 cli.py listen`

to start collecting UDP packages containing telemetry data.
to start collecting UDP packages containing telemetry data. Optionally, specify IP address and/or port:

`python3 cli.py listen --ip 10.0.0.136 --port 6789`

At the end of the session, telemetry data get saved in `sessions/session_{datetime}.pickle`.

Expand All @@ -29,7 +31,7 @@ At the end of the session, telemetry data get saved in `sessions/session_{dateti
To view lap times, run:

`python3 cli.py view sessions/session_{datetime}.pickle laptimes`

###### Example:
```bash
python3 cli.py view sessions/session_20180930_0658.pickle laptimes
Expand Down Expand Up @@ -70,8 +72,3 @@ To view the telemetry of a specific lap, run:
To view the telemetry of a specific lap, compared with the telemetry of the best lap, run:

`python3 cli.py view sessions/session_{datetime}.pickle lap --lap=9 --vs-best=true`





13 changes: 12 additions & 1 deletion cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import sys
import netifaces as ni

from pytelemetry.listen import Telemetry
from pytelemetry.viewer import Viewer
Expand Down Expand Up @@ -29,7 +30,17 @@ def __init__(self):

@staticmethod
def listen() -> None:
telemetry = Telemetry()
parser = argparse.ArgumentParser(description='Register session data')
parser.add_argument('--port', type=int, help='Port for udp listener', default=20777)
parser.add_argument('--ip', type=str, help='IP address')

args = parser.parse_args(sys.argv[2:])

# this assumes wireless interface, not ethernet
iface = [x for x in ni.interfaces() if 'wlp' in x][0]
ipaddr = ni.ifaddresses(iface)[ni.AF_INET][0]['addr']

telemetry = Telemetry(args.port, args.ip or ipaddr)
try:
telemetry.save_data()
except KeyboardInterrupt:
Expand Down
6 changes: 2 additions & 4 deletions pytelemetry/listen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import socket
import netifaces as ni
import pickle
import datetime

Expand Down Expand Up @@ -31,13 +30,12 @@ def parse_car_telemetry_packet(packet):
data.append(packet.cars_telemetry_data[0].m_drs)
return data

def __init__(self, port: int = 20777):
def __init__(self, port: int, ipaddr: str):
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.ip = ni.ifaddresses('wlp58s0')[ni.AF_INET][0]['addr']
self.ip = ipaddr
self.socket.bind((self.ip, port))
self.buffer_size = 1341

self.laps_dict = dict()
self.lap_count = 0
self.laps_dict[self.lap_count] = list()
Expand Down