Network Tables

This commit is contained in:
Tyler Wilcox 2025-07-22 22:03:15 -04:00
parent b35ca5460d
commit a0b2287b29
5 changed files with 72 additions and 26 deletions

View File

@ -1,20 +0,0 @@
library;
import "package:nt4/nt4.dart";
NT4Client ntClient = NT4Client(serverBaseAddress: '10.5.78.2', onConnect: () { print("NT4 Connected"); }, onDisconnect: () { print("NT4 Disconnected"); });
Map<String, NT4Subscription> nt4Subscriptions = {};
NT4Subscription subscribe(String topic){
if (!nt4Subscriptions.containsKey(topic))
{
NT4Subscription subscrip = ntClient.subscribe(topic, NT4SubscriptionOptions());
nt4Subscriptions[topic] = subscrip;
}
return nt4Subscriptions[topic]!;
}
List<NT4Topic> nt4Topics = [];

View File

@ -1,12 +1,16 @@
import 'package:helm/ntutils.dart';
import 'pages/dashboard.dart'; import 'pages/dashboard.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
void main() { void main() {
runApp(const MyApp()); runApp(MyApp());
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({super.key}); MyApp({super.key}){
NTUtils().connect("10.5.78.2");
}
// This widget is the root of your application. // This widget is the root of your application.
@override @override

59
lib/ntutils.dart Normal file
View File

@ -0,0 +1,59 @@
import "package:nt4/nt4.dart";
class NTUtils{
static final NTUtils _instance = NTUtils._internal();
factory NTUtils() {
return _instance;
}
bool isConnected = false;
bool _isAttemptingToConnect = false;
String _serverAddress = "";
NT4Client? ntClient;
final Map<String, NT4Subscription> _nt4Subscriptions = {};
void connect(String serverAddress){
_serverAddress = serverAddress;
_isAttemptingToConnect = true;
ntClient = NT4Client(serverBaseAddress: serverAddress, onConnect: () { isConnected = true; _isAttemptingToConnect = false; }, onDisconnect: () { isConnected = false; });
}
String connectionStatus(){
if(_isAttemptingToConnect){
return "Attempting to connect to $_serverAddress";
}
if(isConnected){
return "Connected";
}else{
return "Not Connected";
}
}
NT4Subscription subscribe(String topic){
if (!_nt4Subscriptions.containsKey(topic))
{
NT4Subscription subscrip = ntClient!.subscribe(topic, NT4SubscriptionOptions());
_nt4Subscriptions[topic] = subscrip;
}
return _nt4Subscriptions[topic]!;
}
final Map<String, NT4Topic> _nt4Topics = {};
NT4Topic getPublicTopic(String topic, String type){
if(!_nt4Topics.containsKey(topic)){
NT4Topic top = ntClient!.publishNewTopic(topic, type);
_nt4Topics[topic] = top;
}
return _nt4Topics[topic]!;
}
NTUtils._internal();
}

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:helm/ntutils.dart';
import 'package:helm/widgets/base_card.dart'; import 'package:helm/widgets/base_card.dart';
import 'package:responsive_grid/responsive_grid.dart'; import 'package:responsive_grid/responsive_grid.dart';
import '../widgets/text_card.dart'; import '../widgets/text_card.dart';
@ -22,7 +23,7 @@ class Dashboard extends StatelessWidget {
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by // Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title. // the App.build method, and use it to set our appbar title.
title: Text("Helm"), title: Text("Helm - ${NTUtils().connectionStatus()}"),
), ),
body: ResponsiveGridRow( body: ResponsiveGridRow(
children: [ children: [

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import './base_card.dart'; import './base_card.dart';
import 'package:nt4/nt4.dart'; import 'package:nt4/nt4.dart';
import '../globals.dart' as glob; import 'package:helm/ntutils.dart';
class TextCard extends StatefulWidget { class TextCard extends StatefulWidget {
const TextCard({ const TextCard({
@ -31,8 +31,10 @@ class _TextCardState extends State<TextCard> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
NT4Subscription sub = glob.subscribe(widget.ntTopic!); if(NTUtils().isConnected){
NT4Subscription sub = NTUtils().subscribe(widget.ntTopic!);
sub.listen( (data) => setVal(data.toString()) ); sub.listen( (data) => setVal(data.toString()) );
}
return BaseCard( return BaseCard(
cardTitle: widget.cardTitle, cardTitle: widget.cardTitle,