From 8a64a155f10f394e73a1b0980ab5da19ac826ef7 Mon Sep 17 00:00:00 2001 From: henrik Date: Mon, 7 Aug 2023 22:43:54 +0200 Subject: [PATCH] ping method, data model changes and ui prototype --- lib/screens/home/home.dart | 19 +++++++++++++++++++ lib/services/data.dart | 14 ++++++++++++++ lib/services/network.dart | 13 +++++++++++++ lib/widgets/layout_elements.dart | 22 +++++++++++++++++++--- 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/lib/screens/home/home.dart b/lib/screens/home/home.dart index 67c3ecd..1ede9f9 100644 --- a/lib/screens/home/home.dart +++ b/lib/screens/home/home.dart @@ -78,6 +78,24 @@ class _HomePageState extends State { }); } + /// ping devices periodically in the background to get the current status + /// of the devices and update the ui accordingly + // void _pingDevices() { + // for (StorageDevice device in _devices) { + // Network.ping(device.hostName!).then((value) { + // if (value) { + // setState(() { + // device.status = DeviceStatus.online; + // }); + // } else { + // setState(() { + // device.status = DeviceStatus.offline; + // }); + // } + // }); + // } + // } + /// sort devices by selectedMenu value. [alphabetical], [recently] and [type] are possible. void sortDevices() { switch (selectedMenu) { @@ -325,6 +343,7 @@ class _HomePageState extends State { title = device.ipAddress; } return DeviceCard( + isOnline: device.isOnline, title: title, subtitle: subtitle, deviceType: device.deviceType, diff --git a/lib/services/data.dart b/lib/services/data.dart index 834da66..434de7a 100644 --- a/lib/services/data.dart +++ b/lib/services/data.dart @@ -1,5 +1,9 @@ +import 'dart:io'; + import 'package:simple_wake_on_lan/services/utilities.dart'; +import 'network.dart'; + abstract class Device implements Comparable { final String hostName; final String ipAddress; @@ -30,6 +34,7 @@ abstract class Device implements Comparable { class StorageDevice extends Device { final String id; final DateTime modified; + bool isOnline = false; StorageDevice( {required this.id, @@ -37,6 +42,7 @@ class StorageDevice extends Device { required ipAddress, required macAddress, wolPort, + isOnline, required this.modified, deviceType}) : super( @@ -106,6 +112,14 @@ class StorageDevice extends Device { deviceType: deviceType, ); } + + /// pings the device with this ip address and returns true if the device is online + Future checkStatus() async { + while (true) { + isOnline = await pingDevice(ipAddress: ipAddress); + await Future.delayed(const Duration(seconds: 10)); + } + } } class NetworkDevice extends Device { diff --git a/lib/services/network.dart b/lib/services/network.dart index a128ae4..143f7b9 100644 --- a/lib/services/network.dart +++ b/lib/services/network.dart @@ -157,6 +157,19 @@ Stream> sendWolAndGetMessages( } } +/// ping a list of devices and return their status +Future pingDevice({required String ipAddress}) async { + final ping = Ping(ipAddress, count: 1, timeout: 5); + + // Wait for the current ping to complete + await for (final response in ping.stream) { + if (response.response != null && response.error == null) { + return true; + } + } + return false; +} + /// Playground: Test different Discover methods // void findDevicesMDNS() async { diff --git a/lib/widgets/layout_elements.dart b/lib/widgets/layout_elements.dart index 75cd99e..b17cdd5 100644 --- a/lib/widgets/layout_elements.dart +++ b/lib/widgets/layout_elements.dart @@ -144,6 +144,7 @@ class DeviceCard extends StatelessWidget { final VoidCallback? onTap; final String? title, subtitle, deviceType; final Widget? trailing; + final bool isOnline; const DeviceCard( {super.key, @@ -151,7 +152,8 @@ class DeviceCard extends StatelessWidget { this.title, this.subtitle, this.deviceType, - this.trailing}); + this.trailing, + this.isOnline = false}); @override Widget build(BuildContext context) { @@ -170,8 +172,22 @@ class DeviceCard extends StatelessWidget { leading: deviceType != null && getIcon(deviceType!) != null ? SizedBox( height: double.infinity, - child: Icon( - getIcon(deviceType!), + child: Stack( + alignment: Alignment.center, + children: [ + Icon( + getIcon(deviceType!), + ), + Positioned( + // draw a red marble + top: 15.0, + right: 0.0, + child: Icon(Icons.brightness_1, + size: 10.0, + color: + isOnline ? Colors.green : Colors.redAccent), + ) + ], )) : null, trailing: trailing,