View Single Post
  #2   Spotlight this post!  
Unread 10-04-2014, 10:17
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,043
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: pynetworktables check server connection

Yes, NetworkTables has a class called IRemoteConnectionListener that you can use with the AddConnectionListener/RemoveConnectionListener. I use the following code.

Code:
class RemoteListener(IRemoteConnectionListener):
    '''Calls a function when a table value is updated'''
    
    def __init__(self, connect_fn, disconnect_fn):
        '''fn must be a callable that takes (value)'''
        IRemoteConnectionListener.__init__(self)
        self.connect_fn = connect_fn
        self.disconnect_fn = disconnect_fn
        
    def attach(self, table):
        self.table = table
        table.AddConnectionListener(self, True)
        
    def detach(self):
        if hasattr(self, 'table'):
            self.table.RemoveConnectionListener(self)
        
    def Connected(self, remote):
        self.connect_fn(remote)
        
    def Disconnected(self, remote):
        self.disconnect_fn(remote)
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote