Skip to content

Commit fc5e2db

Browse files
authored
Merge pull request #22 from auscompgeek/team
Add --team option to specify server to connect to
2 parents 57ef0a8 + 5d79183 commit fc5e2db

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

example/aiohttp_server.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import asyncio
1515
from aiohttp import web
1616

17-
from networktables import NetworkTable
17+
from networktables import NetworkTables
1818
from pynetworktables2js import nt2js_static_resources, networktables_websocket
1919

2020
import logging
@@ -25,15 +25,17 @@
2525

2626
def init_networktables(options):
2727

28-
if options.dashboard:
29-
logger.info("Connecting to networktables in Dashboard mode")
30-
NetworkTable.setDashboardMode()
28+
if options.team:
29+
logger.info("Connecting to NetworkTables for team %s", options.team)
30+
NetworkTables.startClientTeam(options.team)
3131
else:
3232
logger.info("Connecting to networktables at %s", options.robot)
33-
NetworkTable.setIPAddress(options.robot)
34-
NetworkTable.setClientMode()
33+
NetworkTables.initialize(server=options.robot)
34+
35+
if options.dashboard:
36+
logger.info("Enabling driver station override mode")
37+
NetworkTables.startDSClient()
3538

36-
NetworkTable.initialize()
3739
logger.info("Networktables Initialized")
3840

3941
@asyncio.coroutine
@@ -45,7 +47,7 @@ def forward_request(request):
4547
# Setup options here
4648
parser = OptionParser()
4749

48-
parser.add_option('-p', '--port', default=8888,
50+
parser.add_option('-p', '--port', type=int, default=8888,
4951
help='Port to run web server on')
5052

5153
parser.add_option('-v', '--verbose', default=False, action='store_true',
@@ -54,6 +56,8 @@ def forward_request(request):
5456
parser.add_option('--robot', default='127.0.0.1',
5557
help="Robot's IP address")
5658

59+
parser.add_option('--team', type=int, help='Team number of robot to connect to')
60+
5761
parser.add_option('--dashboard', default=False, action='store_true',
5862
help='Use this instead of --robot to receive the IP from the driver station. WARNING: It will not work if you are not on the same host as the DS!')
5963

@@ -64,8 +68,8 @@ def forward_request(request):
6468
format=log_format,
6569
level=logging.DEBUG if options.verbose else logging.INFO)
6670

67-
if options.dashboard and options.robot != '127.0.0.1':
68-
parser.error("Cannot specify --robot and --dashboard")
71+
if options.team and options.robot != '127.0.0.1':
72+
parser.error("--robot and --team are mutually exclusive")
6973

7074
# Setup NetworkTables
7175
init_networktables(options)
@@ -94,4 +98,3 @@ def forward_request(request):
9498
loop.run_forever()
9599
except KeyboardInterrupt:
96100
pass
97-

example/tornado_server.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import tornado.web
1515
from tornado.ioloop import IOLoop
1616

17-
from networktables import NetworkTable
17+
from networktables import NetworkTables
1818
from pynetworktables2js import get_handlers, NonCachingStaticFileHandler
1919

2020
import logging
@@ -23,46 +23,55 @@
2323
log_datefmt = "%H:%M:%S"
2424
log_format = "%(asctime)s:%(msecs)03d %(levelname)-8s: %(name)-20s: %(message)s"
2525

26+
2627
def init_networktables(options):
28+
NetworkTables.setNetworkIdentity(options.identity)
2729

28-
if options.dashboard:
29-
logger.info("Connecting to networktables in Dashboard mode")
30-
NetworkTable.setDashboardMode()
30+
if options.team:
31+
logger.info("Connecting to NetworkTables for team %s", options.team)
32+
NetworkTables.startClientTeam(options.team)
3133
else:
3234
logger.info("Connecting to networktables at %s", options.robot)
33-
NetworkTable.setIPAddress(options.robot)
34-
NetworkTable.setClientMode()
35-
36-
NetworkTable.initialize()
35+
NetworkTables.initialize(server=options.robot)
36+
37+
if options.dashboard:
38+
logger.info("Enabling driver station override mode")
39+
NetworkTables.startDSClient()
40+
3741
logger.info("Networktables Initialized")
3842

3943

40-
if __name__ == '__main__':
44+
def main():
4145

4246
# Setup options here
4347
parser = OptionParser()
4448

45-
parser.add_option('-p', '--port', default=8888,
49+
parser.add_option('-p', '--port', type=int, default=8888,
4650
help='Port to run web server on')
4751

4852
parser.add_option('-v', '--verbose', default=False, action='store_true',
4953
help='Enable verbose logging')
5054

5155
parser.add_option('--robot', default='127.0.0.1',
5256
help="Robot's IP address")
57+
58+
parser.add_option('--team', type=int, help='Team number of robot to connect to')
5359

5460
parser.add_option('--dashboard', default=False, action='store_true',
5561
help='Use this instead of --robot to receive the IP from the driver station. WARNING: It will not work if you are not on the same host as the DS!')
5662

63+
parser.add_option('--identity', default='pynetworktables2js',
64+
help='Identity to send to NT server')
65+
5766
options, args = parser.parse_args()
5867

5968
# Setup logging
6069
logging.basicConfig(datefmt=log_datefmt,
6170
format=log_format,
6271
level=logging.DEBUG if options.verbose else logging.INFO)
6372

64-
if options.dashboard and options.robot != '127.0.0.1':
65-
parser.error("Cannot specify --robot and --dashboard")
73+
if options.team and options.robot != '127.0.0.1':
74+
parser.error("--robot and --team are mutually exclusive")
6675

6776
# Setup NetworkTables
6877
init_networktables(options)
@@ -76,7 +85,7 @@ def init_networktables(options):
7685
exit(1)
7786

7887
if not exists(index_html):
79-
logger.warn("%s not found" % index_html)
88+
logger.warn("%s not found", index_html)
8089

8190
app = tornado.web.Application(
8291
get_handlers() + [
@@ -90,3 +99,7 @@ def init_networktables(options):
9099

91100
app.listen(options.port)
92101
IOLoop.current().start()
102+
103+
104+
if __name__ == '__main__':
105+
main()

pynetworktables2js/__main__.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@
3232
log_datefmt = "%H:%M:%S"
3333
log_format = "%(asctime)s:%(msecs)03d %(levelname)-8s: %(name)-20s: %(message)s"
3434

35+
3536
def init_networktables(options):
37+
NetworkTables.setNetworkIdentity(options.identity)
38+
39+
if options.team:
40+
logger.info("Connecting to NetworkTables for team %s", options.team)
41+
NetworkTables.startClientTeam(options.team)
42+
else:
43+
logger.info("Connecting to NetworkTables at %s", options.robot)
44+
NetworkTables.initialize(server=options.robot)
3645

3746
#if options.dashboard:
3847
# logger.info("Connecting to networktables in Dashboard mode")
3948
# NetworkTables.setDashboardMode()
40-
#else:
41-
logger.info("Connecting to networktables at %s", options.robot)
42-
NetworkTables.setNetworkIdentity(options.identity)
43-
NetworkTables.initialize(server=options.robot)
49+
4450
logger.info("Networktables Initialized")
4551

4652

@@ -49,14 +55,16 @@ def main():
4955
# Setup options here
5056
parser = OptionParser()
5157

52-
parser.add_option('-p', '--port', default=8888,
58+
parser.add_option('-p', '--port', type='int', default=8888,
5359
help='Port to run web server on')
5460

5561
parser.add_option('-v', '--verbose', default=False, action='store_true',
5662
help='Enable verbose logging')
5763

5864
parser.add_option('--robot', default='127.0.0.1',
5965
help="Robot's IP address")
66+
67+
parser.add_option('--team', type='int', help='Team number of robot to connect to')
6068

6169
#parser.add_option('--dashboard', default=False, action='store_true',
6270
# help='Use this instead of --robot to receive the IP from the driver station. WARNING: It will not work if you are not on the same host as the DS!')
@@ -71,8 +79,8 @@ def main():
7179
format=log_format,
7280
level=logging.DEBUG if options.verbose else logging.INFO)
7381

74-
#if options.dashboard and options.robot != '127.0.0.1':
75-
# parser.error("Cannot specify --robot and --dashboard")
82+
if options.team and options.robot != '127.0.0.1':
83+
parser.error("--robot and --team are mutually exclusive")
7684

7785
# Setup NetworkTables
7886
init_networktables(options)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
tornado>=4.0
2-
pynetworktables>=2018.0.0
2+
pynetworktables>=2018.0.1

0 commit comments

Comments
 (0)