Skip to content

Commit

Permalink
ping method, data model changes and ui prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
herzhenr committed Aug 7, 2023
1 parent 0c9a7c4 commit 8a64a15
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
19 changes: 19 additions & 0 deletions lib/screens/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ class _HomePageState extends State<HomePage> {
});
}

/// 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) {
Expand Down Expand Up @@ -325,6 +343,7 @@ class _HomePageState extends State<HomePage> {
title = device.ipAddress;
}
return DeviceCard(
isOnline: device.isOnline,
title: title,
subtitle: subtitle,
deviceType: device.deviceType,
Expand Down
14 changes: 14 additions & 0 deletions lib/services/data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'dart:io';

import 'package:simple_wake_on_lan/services/utilities.dart';

import 'network.dart';

abstract class Device implements Comparable<NetworkDevice> {
final String hostName;
final String ipAddress;
Expand Down Expand Up @@ -30,13 +34,15 @@ abstract class Device implements Comparable<NetworkDevice> {
class StorageDevice extends Device {
final String id;
final DateTime modified;
bool isOnline = false;

StorageDevice(
{required this.id,
required hostName,
required ipAddress,
required macAddress,
wolPort,
isOnline,
required this.modified,
deviceType})
: super(
Expand Down Expand Up @@ -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<void> checkStatus() async {
while (true) {
isOnline = await pingDevice(ipAddress: ipAddress);
await Future.delayed(const Duration(seconds: 10));
}
}
}

class NetworkDevice extends Device {
Expand Down
13 changes: 13 additions & 0 deletions lib/services/network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ Stream<List<Message>> sendWolAndGetMessages(
}
}

/// ping a list of devices and return their status
Future<bool> 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 {
Expand Down
22 changes: 19 additions & 3 deletions lib/widgets/layout_elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,16 @@ class DeviceCard extends StatelessWidget {
final VoidCallback? onTap;
final String? title, subtitle, deviceType;
final Widget? trailing;
final bool isOnline;

const DeviceCard(
{super.key,
this.onTap,
this.title,
this.subtitle,
this.deviceType,
this.trailing});
this.trailing,
this.isOnline = false});

@override
Widget build(BuildContext context) {
Expand All @@ -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,
Expand Down

0 comments on commit 8a64a15

Please sign in to comment.