From 81bef758d702af2897c05a2a3e3b7925aba487d4 Mon Sep 17 00:00:00 2001 From: Damien Albisson Date: Fri, 17 May 2024 12:28:53 +0200 Subject: [PATCH] Show the platform name to the platform discovered + Suggested to the user to use this platform name when adding this new connection --- .../add_connection_with_discovery.dart | 3 +++ lib/after_setup_pages/discovery_page.dart | 8 ++++---- .../manual_connection_page.dart | 3 +++ lib/data/utils.dart | 17 ++++++++++------- lib/forms/add_connection_form.dart | 6 +++++- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/after_setup_pages/add_connection_with_discovery.dart b/lib/after_setup_pages/add_connection_with_discovery.dart index 8dc3fb0..cda2a45 100644 --- a/lib/after_setup_pages/add_connection_with_discovery.dart +++ b/lib/after_setup_pages/add_connection_with_discovery.dart @@ -11,10 +11,12 @@ class ManualConnectionPage extends StatelessWidget { const ManualConnectionPage({ super.key, + required this.name, required this.ip, required this.port }); + final String name; final String ip; final String port; @@ -28,6 +30,7 @@ class ManualConnectionPage extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ AddConnectionForm( + name: name, ip: ip, port: port, ) diff --git a/lib/after_setup_pages/discovery_page.dart b/lib/after_setup_pages/discovery_page.dart index 4e95b2e..bff6204 100644 --- a/lib/after_setup_pages/discovery_page.dart +++ b/lib/after_setup_pages/discovery_page.dart @@ -22,11 +22,11 @@ class DiscoveryPage extends StatefulWidget { class _DiscoveryPageState extends State { - List<(InternetAddress, int)> platformsIpsPorts = []; + List<(InternetAddress, int, String)> platformsIpsPorts = []; bool isLoading = false; // List of button of local platform detected - Widget localDiscoveryConnections(List<(InternetAddress, int)> platformsIpsPorts, bool isLoading) { + Widget localDiscoveryConnections(List<(InternetAddress, int, String)> platformsIpsPorts, bool isLoading) { if (isLoading) { return Center( @@ -53,8 +53,7 @@ class _DiscoveryPageState extends State { child: Column ( children: [ AutoSizeText( - // '${platformsIpsPorts[index].$1.host}', - "local", + platformsIpsPorts[index].$3, style: TextStyle( color: blue ), @@ -74,6 +73,7 @@ class _DiscoveryPageState extends State { context, MaterialPageRoute( builder: (context) => ManualConnectionPage( + name: platformsIpsPorts[index].$3, ip: platformsIpsPorts[index].$1.address, port: "1883" ), diff --git a/lib/after_setup_pages/manual_connection_page.dart b/lib/after_setup_pages/manual_connection_page.dart index f865515..fabc836 100644 --- a/lib/after_setup_pages/manual_connection_page.dart +++ b/lib/after_setup_pages/manual_connection_page.dart @@ -8,10 +8,12 @@ import 'package:panduza_sandbox_flutter/utils_widgets/appBar.dart'; class ManualConnectionPage extends StatelessWidget { const ManualConnectionPage({ super.key, + this.name = "", this.ip = "", this.port = "", }); + final String name; final String ip; final String port; @@ -25,6 +27,7 @@ class ManualConnectionPage extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ AddConnectionForm( + name: name, ip: ip, port: port ) diff --git a/lib/data/utils.dart b/lib/data/utils.dart index 02a5e64..2080600 100644 --- a/lib/data/utils.dart +++ b/lib/data/utils.dart @@ -285,9 +285,9 @@ Future checkIfConnectionValid(BuildContext context, String name, String ho broadcast with UTF-8 format and getting an answer for each platform */ -Future> platformDiscovery() async { +Future> platformDiscovery() async { - List<(InternetAddress, int)> ipPort = []; + List<(InternetAddress, int, String)> ipPort = []; String waitedAnswer = '{"name": "panduza_platform","version": 1.0}'; // Could have some problem with some android phone ? @@ -300,12 +300,15 @@ Future> platformDiscovery() async { socket.listen((e) { Datagram? datagram = socket.receive(); if (datagram != null) { - // String answer = String.fromCharCodes(datagram.data); String answer = utf8.decode(datagram.data); - - // Here send the port of platform and not broker, how to get the port of the broker ? - if(answer == waitedAnswer) { - if (!ipPort.contains((datagram.address, datagram.port))) ipPort.add((datagram.address, datagram.port)); + + Map answerMap = jsonDecode(answer); + String? platformName = answerMap["name"]; + + // if platform name is not given in the answer payload do not add this platform + if (platformName != null) { + // Get addr, port and platform name + if (!ipPort.contains((datagram.address, datagram.port))) ipPort.add((datagram.address, datagram.port, platformName)); } } }); diff --git a/lib/forms/add_connection_form.dart b/lib/forms/add_connection_form.dart index 8012df1..3691c03 100644 --- a/lib/forms/add_connection_form.dart +++ b/lib/forms/add_connection_form.dart @@ -14,17 +14,21 @@ class AddConnectionForm extends StatelessWidget { const AddConnectionForm({ super.key, + required this.name, required this.ip, required this.port, }); + final String name; final String ip; final String port; @override Widget build(BuildContext context) { - final ctrlName = TextEditingController(); + final ctrlName = TextEditingController( + text: name + ); final ctrlHostIp = TextEditingController( text: ip, );