Check status method #9

Merged
Jeremy Kescher merged 2 commits from jeremy/0128/checkstatus into master 2020-01-28 09:22:34 +00:00
3 changed files with 42 additions and 19 deletions

View file

@ -1,13 +1,12 @@
enum ServerStatus{ enum ServerStatus { unknown, timeout, responding, dnserror }
unknown, timeout, responding, dnserror
}
class Server { class Server {
String uri; String uri;
String displayName; String displayName;
ServerStatus status = ServerStatus.unknown; ServerStatus status = ServerStatus.unknown;
Server(this.displayName,this.uri,); Server(
this.displayName,
this.uri,
} );
}

View file

@ -1,9 +1,13 @@
import 'dart:async'; import 'dart:async';
import 'dart:math'; import 'dart:math';
import 'dart:io';
import 'package:basic_utils/basic_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:server_pinger/server.dart'; import 'package:server_pinger/server.dart';
import 'server.dart';
class ServerList extends StatefulWidget { class ServerList extends StatefulWidget {
ServerList({Key key, this.title}) : super(key: key); ServerList({Key key, this.title}) : super(key: key);
@ -48,16 +52,16 @@ class _ServerListState extends State<ServerList> {
Random rnd = new Random(); Random rnd = new Random();
_ServerListState(){ _ServerListState() {
timer = new Timer.periodic(new Duration(seconds: 5), (timer) {
timer = new Timer.periodic(new Duration(seconds: 5), (timer){
server.forEach(_updateServerStatus); server.forEach(_updateServerStatus);
}); });
} }
void _updateServerStatus(Server server){ void _updateServerStatus(Server server) {
setState(() { setState(() {
server.status=ServerStatus.values[rnd.nextInt(ServerStatus.values.length-1)+1]; server.status =
ServerStatus.values[rnd.nextInt(ServerStatus.values.length - 1) + 1];
}); });
} }
@ -68,7 +72,6 @@ class _ServerListState extends State<ServerList> {
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done // This method is rerun every time setState is called, for instance as done
@ -114,9 +117,7 @@ class _ServerListState extends State<ServerList> {
} }
List<Widget> _createChildren() { List<Widget> _createChildren() {
List<String> status = ["ON", "OFF?", "ON", "ON"];
return new List<Widget>.generate(server.length, (int index) { return new List<Widget>.generate(server.length, (int index) {
return Container( return Container(
height: 75, height: 75,
@ -158,13 +159,35 @@ class _ServerListState extends State<ServerList> {
}); });
} }
static Future<ServerStatus> checkStatus(String uri) async {
Uri parsedUri;
try {
parsedUri = Uri.parse(uri);
} catch (FormatException) {
print("The URI was invalid, returning DNS_ERROR");
return ServerStatus.dnserror;
}
if (parsedUri.isScheme("http") || parsedUri.isScheme("http")) {
}
try {
InternetAddress ia = InternetAddress(parsedUri.host);
parsedUri.scheme
} catch (ArgumentError) {
List<RRecord> record = await DnsUtils.lookupRecord(
parsedUri.host, RRecordType.ANY);
}
return ServerStatus.responding;
}
Icon _generateIcon(Server server) { Icon _generateIcon(Server server) {
if (server.status == ServerStatus.unknown) if (server.status == ServerStatus.unknown)
return new Icon( return new Icon(
Icons.check_box_outline_blank, Icons.check_box_outline_blank,
color: Colors.yellow, color: Colors.yellow,
); );
else if(server.status == ServerStatus.responding) else if (server.status == ServerStatus.responding)
return new Icon( return new Icon(
Icons.check_box, Icons.check_box,
color: Colors.green, color: Colors.green,
@ -175,5 +198,4 @@ class _ServerListState extends State<ServerList> {
color: Colors.red, color: Colors.red,
); );
} }
}
}

View file

@ -22,7 +22,9 @@ dependencies:
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2 cupertino_icons: ^0.1.3
basic_utils: ^2.4.8
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: