From ef85db046c0f68c760f85112befd60d97c32e2b2 Mon Sep 17 00:00:00 2001 From: Amory Galili Date: Sun, 2 Dec 2018 13:55:03 -0500 Subject: [PATCH] nt_serial sends value type on value change. Added a function to get value type associated with a key in networktables.js --- pynetworktables2js/js/networktables.js | 23 +++++++++++++++++++++++ pynetworktables2js/nt_serial.py | 23 ++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/pynetworktables2js/js/networktables.js b/pynetworktables2js/js/networktables.js index 8427eb6..6737478 100644 --- a/pynetworktables2js/js/networktables.js +++ b/pynetworktables2js/js/networktables.js @@ -100,6 +100,9 @@ var NetworkTables = new function () { // contents of everything in NetworkTables that we know about var ntCache = new d3_map(); + + // value types of all the contents in Networktables that we know about + var ntTypeCache = new d3_map(); // // NetworkTables JS API @@ -225,6 +228,24 @@ var NetworkTables = new function () { else return val; }; + + /** + Returns the value type that the key maps to. If the websocket is not + open, this will always return null. + + :param key: A networktables key + :returns: value type associated with key if present or ``undefined`` + + .. warning:: This may not return correct results when the websocket + is not connected + */ + this.getValueType = function(key) { + var type = ntTypeCache.get(key); + if (type === undefined) + return null; + else + return type; + } // returns null if robot is not connected, string otherwise this.getRobotAddress = function() { @@ -338,8 +359,10 @@ var NetworkTables = new function () { var key = data['k']; var value = data['v']; var isNew = data['n']; + var type = data['t']; ntCache.set(key, value); + ntTypeCache.set(key, type); // notify global listeners for (var i in globalListeners) { diff --git a/pynetworktables2js/nt_serial.py b/pynetworktables2js/nt_serial.py index f8adf2c..461132f 100644 --- a/pynetworktables2js/nt_serial.py +++ b/pynetworktables2js/nt_serial.py @@ -5,6 +5,26 @@ from networktables import NetworkTables +from ntcore.constants import ( + NT_BOOLEAN, + NT_DOUBLE, + NT_STRING, + NT_RAW, + NT_BOOLEAN_ARRAY, + NT_DOUBLE_ARRAY, + NT_STRING_ARRAY, +) + +_nt_type_dict = { + NT_BOOLEAN: "NT_BOOLEAN", + NT_DOUBLE: "NT_DOUBLE", + NT_STRING: "NT_STRING", + NT_RAW: "NT_RAW", + NT_BOOLEAN_ARRAY: "NT_BOOLEAN_ARRAY", + NT_DOUBLE_ARRAY: "NT_DOUBLE_ARRAY", + NT_STRING_ARRAY: "NT_STRING_ARRAY", +} + __all__ = ["NTSerial"] @@ -35,7 +55,8 @@ def _send_update(self, data): def _nt_on_change(self, key, value, isNew): """NetworkTables global listener callback""" - self._send_update({"k": key, "v": value, "n": isNew}) + value_type = _nt_type_dict[NetworkTables.getEntry(key).getType()] + self._send_update({"k": key, "v": value, "n": isNew, "t": value_type}) # NetworkTables connection listener callbacks def _nt_connected(self, connected, info):