Skip to content

Added function to get value type associated with a key #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pynetworktables2js/js/networktables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion pynetworktables2js/nt_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down Expand Up @@ -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):
Expand Down