Skip to content

Commit

Permalink
Provide robot's IP address after robot connection is made
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Mar 29, 2016
1 parent 04f3553 commit f320658
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/api_js.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ These functions allow your code to listen for particular NetworkTables events.
Sets a function to be called when the robot connects/disconnects to the
pynetworktables2js server via NetworkTables. It will also be called when
the websocket connects/disconnects.

When a listener function is called with a 'true' parameter, the
NetworkTables.getRobotAddress() function will return a non-null value.

:param f: a function that will be called with a single boolean parameter
that indicates whether the robot is connected
Expand Down Expand Up @@ -120,6 +123,10 @@ NetworkTables Interface
updates values, it is recommended (and simpler) to use
:func:`addKeyListener` or :func:`addGlobalListener` to listen
for changes to values, instead of using this function.

.. js:function:: NetworkTables.getRobotAddress()

:returns: null if the robot is not connected, or a string otherwise

.. js:function:: NetworkTables.isRobotConnected()

Expand Down
3 changes: 2 additions & 1 deletion example/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<div>
NetworkTables websocket: <span id="connectstate">Unknown state</span><br/>
Robot: <span id="robotstate">Unknown state</span>
Robot: <span id="robotstate">Unknown state</span> @ <span id="robotAddress">disconnected</span>
</div>
<hr/>

Expand All @@ -47,6 +47,7 @@

function onRobotConnection(connected) {
$('#robotstate').text(connected ? "Connected!" : "Disconnected");
$('#robotAddress').text(connected ? NetworkTables.getRobotAddress() : "disconnected");
}

function onNetworkTablesConnection(connected) {
Expand Down
8 changes: 8 additions & 0 deletions pynetworktables2js/js/networktables.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ var NetworkTables = new function () {
return val;
};

// returns null if robot is not connected, string otherwise
this.getRobotAddress = function() {
return robotAddress;
};

// returns true if robot is connected
this.isRobotConnected = function() {
return robotConnected;
Expand Down Expand Up @@ -274,6 +279,7 @@ var NetworkTables = new function () {
var socket;
var socketOpen = false;
var robotConnected = false;
var robotAddress = null;

// construct the websocket URI
var loc = window.location;
Expand Down Expand Up @@ -309,6 +315,7 @@ var NetworkTables = new function () {
// robot connection event
if (data.r !== undefined) {
robotConnected = data.r;
robotAddress = data.a;
for (var i in robotConnectionListeners) {
robotConnectionListeners[i](robotConnected);
}
Expand Down Expand Up @@ -354,6 +361,7 @@ var NetworkTables = new function () {

socketOpen = false;
robotConnected = false;
robotAddress = null;
console.log("Socket closed");
}

Expand Down
4 changes: 2 additions & 2 deletions pynetworktables2js/nt_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def _nt_on_change(self, key, value, isNew):

# NetworkTables connection listener callbacks
def _nt_connected(self, table):
self._send_update({'r': True})
self._send_update({'r': True, 'a': self.nt.getRemoteAddress()})

def _nt_disconnected(self, table):
self._send_update({'r': False})
self._send_update({'r': False, 'a': None})

def close(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@
zip_safe=False,
install_requires=[
'tornado>=4.0',
'pynetworktables>=2015.2.1'
'pynetworktables>=2015.3.1'
],
entry_points={'console_scripts': [ 'pynetworktables2js = pynetworktables2js.__main__:main' ]},
license="BSD License",
classifiers=[
"Development Status :: 4 - Beta",
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Intended Audience :: Education",
"License :: OSI Approved :: BSD License",
Expand Down

0 comments on commit f320658

Please sign in to comment.