|
| 1 | + |
| 2 | +from os.path import abspath, dirname, join |
| 3 | + |
| 4 | +try: |
| 5 | + from ujson import loads |
| 6 | +except ImportError: |
| 7 | + from json import loads |
| 8 | + |
| 9 | + |
| 10 | +from tornado.ioloop import IOLoop |
| 11 | +from tornado.web import StaticFileHandler |
| 12 | +from tornado.websocket import WebSocketHandler, WebSocketClosedError |
| 13 | + |
| 14 | + |
| 15 | +from networktables import NetworkTable |
| 16 | + |
| 17 | +import logging |
| 18 | +logger = logging.getLogger('net2js') |
| 19 | + |
| 20 | +__all__ = ['get_handlers', 'NetworkTablesWebSocket', 'NonCachingStaticFileHandler'] |
| 21 | + |
| 22 | +class NetworkTablesWebSocket(WebSocketHandler): |
| 23 | + ''' |
| 24 | + A tornado web handler that forwards values between NetworkTables |
| 25 | + and a webpage via a websocket |
| 26 | + ''' |
| 27 | + |
| 28 | + def open(self): |
| 29 | + logger.info("NetworkTables websocket opened") |
| 30 | + |
| 31 | + self.ioloop = IOLoop.current() |
| 32 | + self.nt = NetworkTable.getGlobalTable() |
| 33 | + NetworkTable.addGlobalListener(self.on_nt_change, immediateNotify=True) |
| 34 | + self.nt.addConnectionListener(self, immediateNotify=True) |
| 35 | + |
| 36 | + def on_message(self, message): |
| 37 | + # called when message is received from the dashboard |
| 38 | + data = loads(message) |
| 39 | + self.nt.putValue(data['k'], data['v']) |
| 40 | + |
| 41 | + def on_close(self): |
| 42 | + logger.info("NetworkTables websocket closed") |
| 43 | + NetworkTable.removeGlobalListener(self.on_nt_change) |
| 44 | + |
| 45 | + def send_to_dashboard(self, msg): |
| 46 | + try: |
| 47 | + self.write_message(msg, False) |
| 48 | + except WebSocketClosedError: |
| 49 | + logger.warn("websocket closed when sending message") |
| 50 | + |
| 51 | + # |
| 52 | + # NetworkTables API |
| 53 | + # |
| 54 | + # These functions cannot directly access the websocket, as they are |
| 55 | + # called from another thread |
| 56 | + |
| 57 | + def on_nt_change(self, key, value, isNew): |
| 58 | + self.ioloop.add_callback(self.send_to_dashboard, |
| 59 | + {'k': key, 'v': value, 'n': isNew}) |
| 60 | + |
| 61 | + def connected(self, table): |
| 62 | + self.ioloop.add_callback(self.send_to_dashboard, |
| 63 | + {'r': True}) |
| 64 | + |
| 65 | + def disconnected(self, table): |
| 66 | + self.ioloop.add_callback(self.send_to_dashboard, |
| 67 | + {'r': False}) |
| 68 | + |
| 69 | + |
| 70 | +class NonCachingStaticFileHandler(StaticFileHandler): |
| 71 | + ''' |
| 72 | + This static file handler disables caching, to allow for easy |
| 73 | + development of your Dashboard |
| 74 | + ''' |
| 75 | + |
| 76 | + # This is broken in tornado, disable it |
| 77 | + def check_etag_header(self): |
| 78 | + return False |
| 79 | + |
| 80 | + def set_extra_headers(self, path): |
| 81 | + # Disable caching |
| 82 | + self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') |
| 83 | + |
| 84 | + |
| 85 | +def get_handlers(): |
| 86 | + ''' |
| 87 | + Returns a list that can be concatenated to the list of handlers |
| 88 | + passed to the ``tornado.web.Application`` object. This list contains |
| 89 | + handlers for the NetworkTables websocket and the necessary javascript |
| 90 | + to use it. |
| 91 | + |
| 92 | + Example usage:: |
| 93 | + |
| 94 | + import pynetworktables2js |
| 95 | + import tornado.web |
| 96 | + |
| 97 | + ... |
| 98 | + |
| 99 | + app = tornado.web.Application( |
| 100 | + pynetworktables2js.get_handlers() + [ |
| 101 | + # tornado handlers here |
| 102 | + ]) |
| 103 | + ''' |
| 104 | + |
| 105 | + js_path_opts = {'path': abspath(join(dirname(__file__), 'js', 'networktables.js'))} |
| 106 | + |
| 107 | + return [ |
| 108 | + ('/networktables/ws', NetworkTablesWebSocket), |
| 109 | + ('/networktables/networktables.js()', NonCachingStaticFileHandler, js_path_opts), |
| 110 | + ] |
| 111 | + |
0 commit comments