Line data Source code
1 : import 'package:json_annotation/json_annotation.dart';
2 : import 'package:network_tools/src/database/drift_database.dart';
3 : import 'package:universal_io/io.dart';
4 :
5 : part 'arp_data.g.dart';
6 :
7 : @JsonSerializable()
8 : class ARPData {
9 3 : ARPData({
10 : required this.hostname,
11 : required this.iPAddress,
12 : required this.macAddress,
13 : required this.interfaceName,
14 : required this.interfaceType,
15 : required this.createdAt,
16 : });
17 :
18 1 : factory ARPData.fromJson(Map<String, dynamic> json) =>
19 1 : _$ARPDataFromJson(json);
20 :
21 2 : factory ARPData.fromDriftData(ARPDriftData arpData) => ARPData(
22 1 : hostname: arpData.hostname,
23 1 : iPAddress: arpData.iPAddress,
24 1 : macAddress: arpData.macAddress,
25 1 : interfaceName: arpData.interfaceName,
26 1 : interfaceType: arpData.interfaceType,
27 1 : createdAt: arpData.createdAt,
28 : );
29 :
30 6 : factory ARPData.fromRegExpMatch(RegExpMatch match) => ARPData(
31 6 : hostname: match.groupNames.contains('host')
32 6 : ? (match.namedGroup("host")?.trim() ?? nullHostName)
33 : : nullHostName,
34 6 : iPAddress: match.namedGroup("ip")?.trim() ?? ARPData.nullIPAddress,
35 6 : macAddress: match.namedGroup("mac")?.trim() ?? ARPData.nullMacAddress,
36 6 : interfaceName: match.groupNames.contains('intf')
37 6 : ? (match.namedGroup("intf")?.trim() ?? nullHostName)
38 : : nullHostName,
39 6 : interfaceType: match.namedGroup("typ")?.trim() ?? ARPData.nullInterfaceType,
40 3 : createdAt: DateTime.now(),
41 : );
42 :
43 : final String hostname;
44 : final String iPAddress;
45 : static const String primaryKeySembast = 'iPAddress';
46 : static const String nullIPAddress = '0.0.0.0';
47 : static const String nullMacAddress = 'ff:ff:ff:ff:ff:ff';
48 : static const String nullInterfaceType = 'ethernet';
49 : static const String nullHostName = '';
50 :
51 : final String macAddress;
52 : final String interfaceName;
53 : final String interfaceType;
54 : final DateTime createdAt;
55 :
56 2 : Map<String, dynamic> toJson() => _$ARPDataToJson(this);
57 :
58 3 : bool get notNullIPAddress => iPAddress != nullIPAddress;
59 3 : bool get notNullMacAddress => macAddress != nullMacAddress;
60 3 : bool get notNullInterfaceType => interfaceType != nullInterfaceType;
61 :
62 3 : @override
63 : String toString() {
64 3 : if (Platform.isMacOS) {
65 0 : return '$hostname ($iPAddress) at $macAddress on $interfaceName ifscope [$interfaceType]';
66 3 : } else if (Platform.isLinux) {
67 18 : return '$hostname ($iPAddress) at $macAddress [$interfaceType] on $interfaceName';
68 0 : } else if (Platform.isWindows) {
69 0 : return 'Internet Address: $iPAddress, Physical Address: $macAddress, Type: $interfaceType';
70 : }
71 0 : return '$hostname ($iPAddress) at $macAddress on $interfaceName type [$interfaceType]';
72 : }
73 : }
|