328 lines
8.3 KiB
Dart
328 lines
8.3 KiB
Dart
import 'package:brick_offline_first_with_supabase/brick_offline_first_with_supabase.dart';
|
|
import 'package:brick_supabase/brick_supabase.dart';
|
|
|
|
enum NetworkDeviceStatus {
|
|
online,
|
|
offline,
|
|
warning,
|
|
unknown;
|
|
|
|
String get wire {
|
|
switch (this) {
|
|
case NetworkDeviceStatus.online:
|
|
return 'online';
|
|
case NetworkDeviceStatus.offline:
|
|
return 'offline';
|
|
case NetworkDeviceStatus.warning:
|
|
return 'warning';
|
|
case NetworkDeviceStatus.unknown:
|
|
return 'unknown';
|
|
}
|
|
}
|
|
|
|
String get label {
|
|
switch (this) {
|
|
case NetworkDeviceStatus.online:
|
|
return 'Online';
|
|
case NetworkDeviceStatus.offline:
|
|
return 'Offline';
|
|
case NetworkDeviceStatus.warning:
|
|
return 'Warning';
|
|
case NetworkDeviceStatus.unknown:
|
|
return 'Unknown';
|
|
}
|
|
}
|
|
|
|
static NetworkDeviceStatus fromWire(String? value) {
|
|
switch (value) {
|
|
case 'online':
|
|
return NetworkDeviceStatus.online;
|
|
case 'offline':
|
|
return NetworkDeviceStatus.offline;
|
|
case 'warning':
|
|
return NetworkDeviceStatus.warning;
|
|
default:
|
|
return NetworkDeviceStatus.unknown;
|
|
}
|
|
}
|
|
}
|
|
|
|
enum NetworkDeviceKind {
|
|
router,
|
|
switchDevice,
|
|
ap,
|
|
firewall,
|
|
server,
|
|
endpoint,
|
|
patchPanel,
|
|
other;
|
|
|
|
String get wire {
|
|
switch (this) {
|
|
case NetworkDeviceKind.router:
|
|
return 'router';
|
|
case NetworkDeviceKind.switchDevice:
|
|
return 'switch';
|
|
case NetworkDeviceKind.ap:
|
|
return 'ap';
|
|
case NetworkDeviceKind.firewall:
|
|
return 'firewall';
|
|
case NetworkDeviceKind.server:
|
|
return 'server';
|
|
case NetworkDeviceKind.endpoint:
|
|
return 'endpoint';
|
|
case NetworkDeviceKind.patchPanel:
|
|
return 'patch_panel';
|
|
case NetworkDeviceKind.other:
|
|
return 'other';
|
|
}
|
|
}
|
|
|
|
String get label {
|
|
switch (this) {
|
|
case NetworkDeviceKind.router:
|
|
return 'Router';
|
|
case NetworkDeviceKind.switchDevice:
|
|
return 'Switch';
|
|
case NetworkDeviceKind.ap:
|
|
return 'Access Point';
|
|
case NetworkDeviceKind.firewall:
|
|
return 'Firewall';
|
|
case NetworkDeviceKind.server:
|
|
return 'Server';
|
|
case NetworkDeviceKind.endpoint:
|
|
return 'Endpoint';
|
|
case NetworkDeviceKind.patchPanel:
|
|
return 'Patch Panel';
|
|
case NetworkDeviceKind.other:
|
|
return 'Other';
|
|
}
|
|
}
|
|
|
|
static NetworkDeviceKind fromWire(String? value) {
|
|
switch (value) {
|
|
case 'router':
|
|
return NetworkDeviceKind.router;
|
|
case 'switch':
|
|
return NetworkDeviceKind.switchDevice;
|
|
case 'ap':
|
|
return NetworkDeviceKind.ap;
|
|
case 'firewall':
|
|
return NetworkDeviceKind.firewall;
|
|
case 'server':
|
|
return NetworkDeviceKind.server;
|
|
case 'endpoint':
|
|
return NetworkDeviceKind.endpoint;
|
|
case 'patch_panel':
|
|
return NetworkDeviceKind.patchPanel;
|
|
default:
|
|
return NetworkDeviceKind.other;
|
|
}
|
|
}
|
|
}
|
|
|
|
enum NetworkDeviceRole {
|
|
core,
|
|
distribution,
|
|
access,
|
|
edge,
|
|
endpoint;
|
|
|
|
String get wire {
|
|
switch (this) {
|
|
case NetworkDeviceRole.core:
|
|
return 'core';
|
|
case NetworkDeviceRole.distribution:
|
|
return 'distribution';
|
|
case NetworkDeviceRole.access:
|
|
return 'access';
|
|
case NetworkDeviceRole.edge:
|
|
return 'edge';
|
|
case NetworkDeviceRole.endpoint:
|
|
return 'endpoint';
|
|
}
|
|
}
|
|
|
|
String get label {
|
|
switch (this) {
|
|
case NetworkDeviceRole.core:
|
|
return 'Core';
|
|
case NetworkDeviceRole.distribution:
|
|
return 'Distribution';
|
|
case NetworkDeviceRole.access:
|
|
return 'Access';
|
|
case NetworkDeviceRole.edge:
|
|
return 'Edge';
|
|
case NetworkDeviceRole.endpoint:
|
|
return 'Endpoint';
|
|
}
|
|
}
|
|
|
|
static NetworkDeviceRole? fromWire(String? value) {
|
|
switch (value) {
|
|
case 'core':
|
|
return NetworkDeviceRole.core;
|
|
case 'distribution':
|
|
return NetworkDeviceRole.distribution;
|
|
case 'access':
|
|
return NetworkDeviceRole.access;
|
|
case 'edge':
|
|
return NetworkDeviceRole.edge;
|
|
case 'endpoint':
|
|
return NetworkDeviceRole.endpoint;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
enum NetworkImportSource {
|
|
manual,
|
|
aiImport,
|
|
agent;
|
|
|
|
String get wire {
|
|
switch (this) {
|
|
case NetworkImportSource.manual:
|
|
return 'manual';
|
|
case NetworkImportSource.aiImport:
|
|
return 'ai_import';
|
|
case NetworkImportSource.agent:
|
|
return 'agent';
|
|
}
|
|
}
|
|
|
|
static NetworkImportSource fromWire(String? value) {
|
|
switch (value) {
|
|
case 'ai_import':
|
|
return NetworkImportSource.aiImport;
|
|
case 'agent':
|
|
return NetworkImportSource.agent;
|
|
default:
|
|
return NetworkImportSource.manual;
|
|
}
|
|
}
|
|
}
|
|
|
|
@ConnectOfflineFirstWithSupabase(
|
|
supabaseConfig: SupabaseSerializable(tableName: 'network_devices'),
|
|
)
|
|
class NetworkDevice extends OfflineFirstWithSupabaseModel {
|
|
final String id;
|
|
final String name;
|
|
final NetworkDeviceKind kind;
|
|
final NetworkDeviceRole? role;
|
|
final String? vendor;
|
|
final String? model;
|
|
final String? serial;
|
|
final String? mgmtIp;
|
|
final String? mac;
|
|
final String? locationId;
|
|
final NetworkImportSource importSource;
|
|
final NetworkDeviceStatus status;
|
|
final String? notes;
|
|
final DateTime createdAt;
|
|
final DateTime updatedAt;
|
|
|
|
NetworkDevice({
|
|
required this.id,
|
|
required this.name,
|
|
required this.kind,
|
|
this.role,
|
|
this.vendor,
|
|
this.model,
|
|
this.serial,
|
|
this.mgmtIp,
|
|
this.mac,
|
|
this.locationId,
|
|
this.importSource = NetworkImportSource.manual,
|
|
this.status = NetworkDeviceStatus.unknown,
|
|
this.notes,
|
|
required this.createdAt,
|
|
required this.updatedAt,
|
|
});
|
|
|
|
factory NetworkDevice.fromMap(Map<String, dynamic> map) {
|
|
return NetworkDevice(
|
|
id: map['id'] as String,
|
|
name: (map['name'] as String?) ?? '',
|
|
kind: NetworkDeviceKind.fromWire(map['kind'] as String?),
|
|
role: NetworkDeviceRole.fromWire(map['role'] as String?),
|
|
vendor: map['vendor'] as String?,
|
|
model: map['model'] as String?,
|
|
serial: map['serial'] as String?,
|
|
mgmtIp: map['mgmt_ip']?.toString(),
|
|
mac: map['mac']?.toString(),
|
|
locationId: map['location_id'] as String?,
|
|
importSource: NetworkImportSource.fromWire(map['import_source'] as String?),
|
|
status: NetworkDeviceStatus.fromWire(map['status'] as String?),
|
|
notes: map['notes'] as String?,
|
|
createdAt:
|
|
DateTime.tryParse(map['created_at']?.toString() ?? '') ?? DateTime.now(),
|
|
updatedAt:
|
|
DateTime.tryParse(map['updated_at']?.toString() ?? '') ?? DateTime.now(),
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toInsertMap() => {
|
|
'name': name,
|
|
'kind': kind.wire,
|
|
if (role != null) 'role': role!.wire,
|
|
if (vendor != null) 'vendor': vendor,
|
|
if (model != null) 'model': model,
|
|
if (serial != null) 'serial': serial,
|
|
if (mgmtIp != null) 'mgmt_ip': mgmtIp,
|
|
if (mac != null) 'mac': mac,
|
|
if (locationId != null) 'location_id': locationId,
|
|
'import_source': importSource.wire,
|
|
'status': status.wire,
|
|
if (notes != null) 'notes': notes,
|
|
};
|
|
|
|
Map<String, dynamic> toUpdateMap() => {
|
|
'name': name,
|
|
'kind': kind.wire,
|
|
'role': role?.wire,
|
|
'vendor': vendor,
|
|
'model': model,
|
|
'serial': serial,
|
|
'mgmt_ip': mgmtIp,
|
|
'mac': mac,
|
|
'location_id': locationId,
|
|
'status': status.wire,
|
|
'notes': notes,
|
|
};
|
|
|
|
NetworkDevice copyWith({
|
|
String? name,
|
|
NetworkDeviceKind? kind,
|
|
NetworkDeviceRole? role,
|
|
String? vendor,
|
|
String? model,
|
|
String? serial,
|
|
String? mgmtIp,
|
|
String? mac,
|
|
String? locationId,
|
|
NetworkDeviceStatus? status,
|
|
String? notes,
|
|
}) {
|
|
return NetworkDevice(
|
|
id: id,
|
|
name: name ?? this.name,
|
|
kind: kind ?? this.kind,
|
|
role: role ?? this.role,
|
|
vendor: vendor ?? this.vendor,
|
|
model: model ?? this.model,
|
|
serial: serial ?? this.serial,
|
|
mgmtIp: mgmtIp ?? this.mgmtIp,
|
|
mac: mac ?? this.mac,
|
|
locationId: locationId ?? this.locationId,
|
|
importSource: importSource,
|
|
status: status ?? this.status,
|
|
notes: notes ?? this.notes,
|
|
createdAt: createdAt,
|
|
updatedAt: updatedAt,
|
|
);
|
|
}
|
|
}
|