Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Can't try to connect to two brokers at the same time and if on differ… #97

Merged
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
98 changes: 98 additions & 0 deletions lib/pages/after_setup_pages/connections_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'package:flutter/material.dart';
import 'package:panduza_sandbox_flutter/pages/after_setup_pages/add_cloud_or_self_managed.dart';
import 'package:panduza_sandbox_flutter/pages/after_setup_pages/userspace_page.dart';
import 'package:panduza_sandbox_flutter/pages/setup_pages/authentification_page.dart';
import 'package:panduza_sandbox_flutter/utils/utils_functions.dart';
import 'package:panduza_sandbox_flutter/utils/utils_objects/broker_connection_info.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:panduza_sandbox_flutter/utils/const.dart';
Expand Down Expand Up @@ -28,6 +32,99 @@ class _ConnectionsPageState extends State<ConnectionsPage> {
late final SharedPreferences _prefs;
late final _prefsFuture = SharedPreferences.getInstance().then((v) => _prefs = v);

// If user is trying to connect to a broker
bool isConnecting = false;

// Connection list display on the home page load from the disk
Widget getConnectionsButtonsList(SharedPreferences prefs, List<String> platformNames,
BuildContext context, State<ConnectionsPage> state) {
return ListView.separated(
padding: const EdgeInsets.all(20),
itemCount: platformNames.length,
itemBuilder: (BuildContext context, int index) {
return MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
child: Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: black,
),
child: Row (
children: <Widget>[
getCloudOrLocalIcon((prefs.getStringList(platformNames[index]) as List<String>)[3]),
getConnectionButton(prefs, platformNames, index),
Column(
children: [
getEditConnectionButton(prefs, platformNames, index, context, state),
getDeleteConnectionButton(prefs, platformNames, index, context, state)
],
),
]
)
),
// If the user click on a connection, it will try to connect
// to the mqtt broker if it fail then a small banner will appear
// else he will be redirect on the userpage corresponding
onTap: () {
String host = (prefs.getStringList(platformNames[index]) as List<String>)[1];
String port = (prefs.getStringList(platformNames[index]) as List<String>)[2];

// Here check if it's local or cloud connection
String isCloud = (prefs.getStringList(platformNames[index]) as List<String>)[3];

if (isCloud == "1") {
// authentification page in the cloud
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AuthentificationPage(
hostIp: host,
port: port
)
),
);
} else {
// Try connecting only if connection has not been tried on another broker
if (!isConnecting) {
isConnecting = true;
// If local just permit to user to go on the broker directly
tryConnecting(host, port, "", "").then((client) {
if (isConnecting) {
if (client != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserspacePage(
brokerConnectionInfo: BrokerConnectionInfo(
host,
int.parse(port),
client
),
)
),
).then((value) {
isConnecting = false;
client.disconnect();
});
} else {
isConnecting = false;
showMyDialogError(context, "Connection to the broker failed");
}
}
});
}
}
},
),
);
},
separatorBuilder: (context, index) => const Divider(),
);
}


@override
Widget build(BuildContext context) {

Expand Down Expand Up @@ -56,6 +153,7 @@ class _ConnectionsPageState extends State<ConnectionsPage> {
// Button to add a connection
floatingActionButton: FloatingActionButton(
onPressed: () async {
isConnecting = false;
await Navigator.push(
context,
MaterialPageRoute(
Expand Down
93 changes: 45 additions & 48 deletions lib/utils/utils_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:fluttertoast/fluttertoast.dart';
import 'package:panduza_sandbox_flutter/utils/const.dart';

late MqttServerClient _client;
bool _isConnecting = false;

final _chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
final Random _rnd = Random();
Expand Down Expand Up @@ -174,58 +174,55 @@ String generateRandomMqttIdentifier() {

Future<MqttServerClient?> tryConnecting(String host, String portStr, String username, String password) async {

if (!_isConnecting) {
_isConnecting = true;
try {

int port = int.parse(portStr);


_client = MqttServerClient.withPort(
host, generateRandomMqttIdentifier(), port);
_client.setProtocolV311();

_client.keepAlivePeriod = 20;

await _client.connect(username, password);

_client.onConnected = () {
print("MQTT connected");
};

_isConnecting = false;
return _client;
} catch (error) {
print(error);

_isConnecting = false;
if (Platform.isAndroid || Platform.isIOS) {
Fluttertoast.showToast(
msg: "Connection failed",
textColor: Colors.black,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
toastLength: Toast.LENGTH_LONG
);
}

// never return because _client not init
return null;
try {

int port = int.parse(portStr);

_client = MqttServerClient.withPort(
host, generateRandomMqttIdentifier(), port);
_client.setProtocolV311();

_client.keepAlivePeriod = 5;

// Let 5 sec to connect
_client.connectTimeoutPeriod = 5000;

await _client.connect(username, password);

_client.onConnected = () {
print("MQTT connected");
};

return _client;
} catch (error) {
print(error);

if (Platform.isAndroid || Platform.isIOS) {
Fluttertoast.showToast(
msg: "Connection failed",
textColor: Colors.black,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
toastLength: Toast.LENGTH_LONG
);
}

// never return because _client not init
return null;
}

if (Platform.isAndroid || Platform.isIOS) {
Fluttertoast.showToast(
msg: "A connection is already in process",
textColor: Colors.black,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.red,
toastLength: Toast.LENGTH_LONG
);
}
// if (Platform.isAndroid || Platform.isIOS) {
// Fluttertoast.showToast(
// msg: "A connection is already in process",
// textColor: Colors.black,
// gravity: ToastGravity.BOTTOM,
// backgroundColor: Colors.red,
// toastLength: Toast.LENGTH_LONG
// );
// }


return null;
// return null;
}

// If user mistake show a pop up to describe his mistake with
Expand Down
82 changes: 0 additions & 82 deletions lib/widgets/utils_widgets/utils_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,85 +377,3 @@ Widget getSimpleTextField(BuildContext context, TextEditingController ctrl,
style: Theme.of(context).textTheme.displayMedium
);
}

// Connection list display on the home page load from the disk
Widget getConnectionsButtonsList(SharedPreferences prefs, List<String> platformNames,
BuildContext context, State<ConnectionsPage> state) {
return ListView.separated(
padding: const EdgeInsets.all(20),
itemCount: platformNames.length,
itemBuilder: (BuildContext context, int index) {
return MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
child: Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: black,
),
child: Row (
children: <Widget>[
getCloudOrLocalIcon((prefs.getStringList(platformNames[index]) as List<String>)[3]),
getConnectionButton(prefs, platformNames, index),
Column(
children: [
getEditConnectionButton(prefs, platformNames, index, context, state),
getDeleteConnectionButton(prefs, platformNames, index, context, state)
],
),
]
)
),
// If the user click on a connection, it will try to connect
// to the mqtt broker if it fail then a small banner will appear
// else he will be redirect on the userpage corresponding
onTap: () {
String host = (prefs.getStringList(platformNames[index]) as List<String>)[1];
String port = (prefs.getStringList(platformNames[index]) as List<String>)[2];

// Here check if it's local or cloud connection
String isCloud = (prefs.getStringList(platformNames[index]) as List<String>)[3];

if (isCloud == "1") {
// authentification page in the cloud
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AuthentificationPage(
hostIp: host,
port: port
)
),
);
} else {
// If local just permit to user to go on the broker directly
tryConnecting(host, port, "", "").then((client) {
if (client != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UserspacePage(
brokerConnectionInfo: BrokerConnectionInfo(
host,
int.parse(port),
client
),
)
),
).then((value) {
client.disconnect();
});
} else {
showMyDialogError(context, "Connection to the broker failed");
}
});
}
},
),
);
},
separatorBuilder: (context, index) => const Divider(),
);
}