Skip to content
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

feat: add prefix topics #158

Open
wants to merge 6 commits 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
78 changes: 73 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# ntcore-ts-client

A TypeScript library for communication over [WPILib's NetworkTables 4.0 protocol](https://github.com/wpilibsuite/allwpilib/blob/main/ntcore/doc/networktables4.adoc).
A TypeScript library for communication over [WPILib's NetworkTables 4.1 protocol](https://github.com/wpilibsuite/allwpilib/blob/main/ntcore/doc/networktables4.adoc).

## Features

- NodeJS and DOM support
- Togglable auto-reconnect
- Callbacks for new data on subscriptions
- Callbacks for connection listeners
- Wildcard prefix listeners for multiple topics
- Retrying for messages queued during a connection loss
- On-the-fly server switching with resubscribing and republishing
- Generic types for Topics
Expand Down Expand Up @@ -84,8 +85,7 @@ Once a topic has been created, it can be used as a subscriber:

```typescript
subscribe(
callback: (_: T | null) => void,
immediateNotify = false,
callback: (value: T | null, params: AnnounceMessageParams) => void,
options: SubscribeOptions = {},
id?: number,
save = true
Expand All @@ -112,7 +112,12 @@ const gyroTopic = ntcore.createTopic<number>('/MyTable/Gyro', NetworkTablesTypeI
// Subscribe and immediately call the callback with the current value
gyroTopic.subscribe((value) => {
console.log(`Got Gyro Value: ${value}`);
}, true);
});

// Or you can use the topic's announce parameters to get more info, like the topic ID
gyroTopic.subscribe((value, params) => {
console.log(`Got Gyro Value: ${value} at from topic id ${params.id}`);
});
```

Or a publisher for an auto mode:
Expand All @@ -133,7 +138,70 @@ await autoModeTopic.publish();
autoModeTopic.setValue('25 Ball Auto and Climb');
```

### More info
### Subscribing to Multiple Topics

You can also subscribe to multiple topics by using a "wildcard" through creating a prefix topic.

For example, here's a subscription for an Accelerometer with topics `/MyTable/Accelerometer/X`, `/MyTable/Accelerometer/Y`, and `/MyTable/Accelerometer/Z`:

```typescript
import { NetworkTables } from 'ntcore-ts-client';

// Get or create the NT client instance
const ntcore = NetworkTables.getInstanceByTeam(973);

// Create the accelerator topic
const accelerometerTopic = ntcore.createPrefixTopic('/MyTable/Accelerometer/');

let x, y, z;

// Subscribe to all topics under the prefix /MyTable/Accelerometer/
accelerometerTopic.subscribe((value, params) => {
console.log(`Got Accelerometer Value: ${value} from topic ${params.name}`); // i.e. Got Accelerometer Value: 9.81 from topic /MyTable/Accelerometer/Y

// You can also use the topic name to determine which value to set
if (params.name.endsWith('X')) {
x = value;
} else if (params.name.endsWith('Y')) {
y = value;
} else if (params.name.endsWith('Z')) {
z = value;
}

// Since there can be many types in subtopics,
// you can use the type information for other checks...
if (params.type === 'int') {
console.warn('Hmm... the accelerometer seems low precision');
} else if (params.type === 'double') {
console.log('The accelerometer is high precision');
}
});

// x, y, and z will be updated as new values come in
```

### Subscribing to All Topics

You can also subscribe to all topics by doing the above, but with a prefix of `/`.

For example, here's a subscription for all topics:

```typescript
import { NetworkTables } from 'ntcore-ts-client';

// Get or create the NT client instance
const ntcore = NetworkTables.getInstanceByTeam(973);

// Create a prefix for all topics
const allTopics = ntcore.createPrefixTopic('/');

// Subscribe to all topics
allTopics.subscribe((value, params) => {
console.log(`Got Value: ${value} from topic ${params.name}`);
});
```

### More Info

The API for Topics is much more exhaustive than this quick example. Feel free to view the docs at [https://ntcore.chrislawson.dev](https://ntcore.chrislawson.dev).

Expand Down
2 changes: 1 addition & 1 deletion docs/assets/navigation.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/assets/search.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading