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

Commit

Permalink
Merge pull request #23 from Panduza/21-local-discovery-show-the-platf…
Browse files Browse the repository at this point in the history
…orm-name-to-the-connection-discovered

Show the platform name to the platform discovered
  • Loading branch information
wardru authored May 20, 2024
2 parents 43c8fe0 + 81bef75 commit 3727187
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/after_setup_pages/add_connection_with_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,6 +30,7 @@ class ManualConnectionPage extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AddConnectionForm(
name: name,
ip: ip,
port: port,
)
Expand Down
8 changes: 4 additions & 4 deletions lib/after_setup_pages/discovery_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class DiscoveryPage extends StatefulWidget {

class _DiscoveryPageState extends State<DiscoveryPage> {

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(
Expand All @@ -53,8 +53,7 @@ class _DiscoveryPageState extends State<DiscoveryPage> {
child: Column (
children: <Widget>[
AutoSizeText(
// '${platformsIpsPorts[index].$1.host}',
"local",
platformsIpsPorts[index].$3,
style: TextStyle(
color: blue
),
Expand All @@ -74,6 +73,7 @@ class _DiscoveryPageState extends State<DiscoveryPage> {
context,
MaterialPageRoute(
builder: (context) => ManualConnectionPage(
name: platformsIpsPorts[index].$3,
ip: platformsIpsPorts[index].$1.address,
port: "1883"
),
Expand Down
3 changes: 3 additions & 0 deletions lib/after_setup_pages/manual_connection_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,6 +27,7 @@ class ManualConnectionPage extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AddConnectionForm(
name: name,
ip: ip,
port: port
)
Expand Down
17 changes: 10 additions & 7 deletions lib/data/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ Future<bool> checkIfConnectionValid(BuildContext context, String name, String ho
broadcast with UTF-8 format and getting an answer for each platform
*/

Future<List<(InternetAddress, int)>> platformDiscovery() async {
Future<List<(InternetAddress, int, String)>> 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 ?
Expand All @@ -300,12 +300,15 @@ Future<List<(InternetAddress, int)>> 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<String, dynamic> 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));
}
}
});
Expand Down
6 changes: 5 additions & 1 deletion lib/forms/add_connection_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down

0 comments on commit 3727187

Please sign in to comment.