Connection status updates and tested connection
This commit is contained in:
parent
4b6fb04983
commit
804f75db47
|
|
@ -8,8 +8,8 @@ void main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
MyApp({super.key}){
|
MyApp({super.key}) {
|
||||||
NTUtils().connect("10.5.78.2");
|
NTUtils().connect("localhost");
|
||||||
}
|
}
|
||||||
|
|
||||||
// This widget is the root of your application.
|
// This widget is the root of your application.
|
||||||
|
|
@ -33,7 +33,10 @@ class MyApp extends StatelessWidget {
|
||||||
//
|
//
|
||||||
// This works for code too, not just values: Most code changes can be
|
// This works for code too, not just values: Most code changes can be
|
||||||
// tested with just a hot reload.
|
// tested with just a hot reload.
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue, brightness: Brightness.dark),
|
colorScheme: ColorScheme.fromSeed(
|
||||||
|
seedColor: Colors.blue,
|
||||||
|
brightness: Brightness.dark,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
home: const Dashboard(),
|
home: const Dashboard(),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
|
import "dart:ui";
|
||||||
|
|
||||||
import "package:nt4/nt4.dart";
|
import "package:nt4/nt4.dart";
|
||||||
|
|
||||||
class NTUtils{
|
class NTUtils {
|
||||||
|
|
||||||
static final NTUtils _instance = NTUtils._internal();
|
static final NTUtils _instance = NTUtils._internal();
|
||||||
|
|
||||||
factory NTUtils() {
|
factory NTUtils() {
|
||||||
|
|
@ -16,27 +17,57 @@ class NTUtils{
|
||||||
|
|
||||||
final Map<String, NT4Subscription> _nt4Subscriptions = {};
|
final Map<String, NT4Subscription> _nt4Subscriptions = {};
|
||||||
|
|
||||||
void connect(String serverAddress){
|
final List<VoidCallback> connectCallbacks = [];
|
||||||
_serverAddress = serverAddress;
|
final List<VoidCallback> disconnectCallbacks = [];
|
||||||
_isAttemptingToConnect = true;
|
|
||||||
ntClient = NT4Client(serverBaseAddress: serverAddress, onConnect: () { isConnected = true; _isAttemptingToConnect = false; }, onDisconnect: () { isConnected = false; });
|
void addConnectCallback(VoidCallback cb) {
|
||||||
|
connectCallbacks.add(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
String connectionStatus(){
|
void addDisconnectCallback(VoidCallback cb) {
|
||||||
if(_isAttemptingToConnect){
|
disconnectCallbacks.add(cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
void connect(String serverAddress) {
|
||||||
|
_serverAddress = serverAddress;
|
||||||
|
_isAttemptingToConnect = true;
|
||||||
|
ntClient = NT4Client(
|
||||||
|
serverBaseAddress: serverAddress,
|
||||||
|
onConnect: () {
|
||||||
|
isConnected = true;
|
||||||
|
_isAttemptingToConnect = false;
|
||||||
|
|
||||||
|
for (VoidCallback cb in connectCallbacks) {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onDisconnect: () {
|
||||||
|
isConnected = false;
|
||||||
|
|
||||||
|
for (VoidCallback cb in disconnectCallbacks) {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
String connectionStatus() {
|
||||||
|
if (_isAttemptingToConnect) {
|
||||||
return "Attempting to connect to $_serverAddress";
|
return "Attempting to connect to $_serverAddress";
|
||||||
}
|
}
|
||||||
if(isConnected){
|
if (isConnected) {
|
||||||
return "Connected";
|
return "Connected";
|
||||||
}else{
|
} else {
|
||||||
return "Not Connected";
|
return "Not Connected";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NT4Subscription subscribe(String topic){
|
NT4Subscription subscribe(String topic) {
|
||||||
if (!_nt4Subscriptions.containsKey(topic))
|
if (!_nt4Subscriptions.containsKey(topic)) {
|
||||||
{
|
NT4Subscription subscrip = ntClient!.subscribe(
|
||||||
NT4Subscription subscrip = ntClient!.subscribe(topic, NT4SubscriptionOptions());
|
topic,
|
||||||
|
NT4SubscriptionOptions(),
|
||||||
|
);
|
||||||
_nt4Subscriptions[topic] = subscrip;
|
_nt4Subscriptions[topic] = subscrip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,8 +76,8 @@ class NTUtils{
|
||||||
|
|
||||||
final Map<String, NT4Topic> _nt4Topics = {};
|
final Map<String, NT4Topic> _nt4Topics = {};
|
||||||
|
|
||||||
NT4Topic getPublicTopic(String topic, String type){
|
NT4Topic getPublicTopic(String topic, String type) {
|
||||||
if(!_nt4Topics.containsKey(topic)){
|
if (!_nt4Topics.containsKey(topic)) {
|
||||||
NT4Topic top = ntClient!.publishNewTopic(topic, type);
|
NT4Topic top = ntClient!.publishNewTopic(topic, type);
|
||||||
_nt4Topics[topic] = top;
|
_nt4Topics[topic] = top;
|
||||||
}
|
}
|
||||||
|
|
@ -56,4 +87,3 @@ class NTUtils{
|
||||||
|
|
||||||
NTUtils._internal();
|
NTUtils._internal();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,31 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:helm/ntutils.dart';
|
import 'package:helm/ntutils.dart';
|
||||||
import 'package:helm/widgets/base_card.dart';
|
import 'package:helm/widgets/base_card.dart';
|
||||||
|
import 'package:helm/widgets/posty_results_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';
|
||||||
|
|
||||||
class Dashboard extends StatelessWidget {
|
class Dashboard extends StatefulWidget {
|
||||||
const Dashboard({super.key});
|
const Dashboard({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<Dashboard> createState() => _DashboardState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _DashboardState extends State<Dashboard> {
|
||||||
|
String connectionState = NTUtils().connectionStatus();
|
||||||
|
|
||||||
|
_DashboardState() {
|
||||||
|
NTUtils().addConnectCallback(updateConnectionState);
|
||||||
|
NTUtils().addDisconnectCallback(updateConnectionState);
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateConnectionState() {
|
||||||
|
setState(() {
|
||||||
|
connectionState = NTUtils().connectionStatus();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
// This method is rerun every time setState is called, for instance as done
|
// This method is rerun every time setState is called, for instance as done
|
||||||
|
|
@ -23,7 +42,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 - ${NTUtils().connectionStatus()}"),
|
title: Text("Helm - $connectionState"),
|
||||||
),
|
),
|
||||||
body: ResponsiveGridRow(
|
body: ResponsiveGridRow(
|
||||||
children: [
|
children: [
|
||||||
|
|
@ -41,7 +60,10 @@ class Dashboard extends StatelessWidget {
|
||||||
children: [
|
children: [
|
||||||
ResponsiveGridCol(
|
ResponsiveGridCol(
|
||||||
md: 6,
|
md: 6,
|
||||||
child: TextCard(cardTitle: "Match State"),
|
child: TextCard(
|
||||||
|
cardTitle: "Match State",
|
||||||
|
ntTopic: "/FMSInfo/StationNumber",
|
||||||
|
),
|
||||||
),
|
),
|
||||||
ResponsiveGridCol(
|
ResponsiveGridCol(
|
||||||
md: 6,
|
md: 6,
|
||||||
|
|
@ -49,7 +71,7 @@ class Dashboard extends StatelessWidget {
|
||||||
),
|
),
|
||||||
ResponsiveGridCol(
|
ResponsiveGridCol(
|
||||||
md: 6,
|
md: 6,
|
||||||
child: TextCard(cardTitle: "Blah Blah"),
|
child: TextCard(cardTitle: "Robot State"),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -24,25 +24,22 @@ class TextCard extends StatefulWidget {
|
||||||
class _TextCardState extends State<TextCard> {
|
class _TextCardState extends State<TextCard> {
|
||||||
String _val = "";
|
String _val = "";
|
||||||
|
|
||||||
void setVal( String newVal )
|
void setVal(String newVal) {
|
||||||
{
|
setState(() {
|
||||||
_val = newVal;
|
_val = newVal;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if(NTUtils().isConnected){
|
NT4Subscription sub = NTUtils().subscribe(widget.ntTopic!);
|
||||||
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,
|
||||||
width: widget.width,
|
width: widget.width,
|
||||||
height: widget.height,
|
height: widget.height,
|
||||||
children: [
|
children: [Text(_val)],
|
||||||
Text(_val)
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue