feat: Geutebruck GeViScope/GeViSoft Action Mapping System - MVP
This MVP release provides a complete full-stack solution for managing action mappings in Geutebruck's GeViScope and GeViSoft video surveillance systems. ## Features ### Flutter Web Application (Port 8081) - Modern, responsive UI for managing action mappings - Action picker dialog with full parameter configuration - Support for both GSC (GeViScope) and G-Core server actions - Consistent UI for input and output actions with edit/delete capabilities - Real-time action mapping creation, editing, and deletion - Server categorization (GSC: prefix for GeViScope, G-Core: prefix for G-Core servers) ### FastAPI REST Backend (Port 8000) - RESTful API for action mapping CRUD operations - Action template service with comprehensive action catalog (247 actions) - Server management (G-Core and GeViScope servers) - Configuration tree reading and writing - JWT authentication with role-based access control - PostgreSQL database integration ### C# SDK Bridge (gRPC, Port 50051) - Native integration with GeViSoft SDK (GeViProcAPINET_4_0.dll) - Action mapping creation with correct binary format - Support for GSC and G-Core action types - Proper Camera parameter inclusion in action strings (fixes CrossSwitch bug) - Action ID lookup table with server-specific action IDs - Configuration reading/writing via SetupClient ## Bug Fixes - **CrossSwitch Bug**: GSC and G-Core actions now correctly display camera/PTZ head parameters in GeViSet - Action strings now include Camera parameter: `@ PanLeft (Comment: "", Camera: 101028)` - Proper filter flags and VideoInput=0 for action mappings - Correct action ID assignment (4198 for GSC, 9294 for G-Core PanLeft) ## Technical Stack - **Frontend**: Flutter Web, Dart, Dio HTTP client - **Backend**: Python FastAPI, PostgreSQL, Redis - **SDK Bridge**: C# .NET 8.0, gRPC, GeViSoft SDK - **Authentication**: JWT tokens - **Configuration**: GeViSoft .set files (binary format) ## Credentials - GeViSoft/GeViScope: username=sysadmin, password=masterkey - Default admin: username=admin, password=admin123 ## Deployment All services run on localhost: - Flutter Web: http://localhost:8081 - FastAPI: http://localhost:8000 - SDK Bridge gRPC: localhost:50051 - GeViServer: localhost (default port) Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import '../../models/action_mapping_hive_model.dart';
|
||||
import '../../models/action_mapping_model.dart';
|
||||
|
||||
/// Abstract interface for local action mapping data operations
|
||||
abstract class ActionMappingLocalDataSource {
|
||||
Future<List<ActionMappingHiveModel>> getAllActionMappings();
|
||||
Future<ActionMappingHiveModel?> getActionMappingById(String id);
|
||||
Future<void> saveActionMapping(ActionMappingHiveModel mapping);
|
||||
Future<void> deleteActionMapping(String id);
|
||||
Future<List<ActionMappingHiveModel>> getDirtyActionMappings();
|
||||
Future<void> markActionMappingAsSynced(String id);
|
||||
Future<void> replaceAllActionMappings(List<ActionMappingModel> mappings, {bool force = false});
|
||||
Future<List<ActionMappingHiveModel>> searchActionMappings(String query);
|
||||
Future<void> clearAll();
|
||||
}
|
||||
|
||||
/// Implementation of local action mapping data source using Hive
|
||||
class ActionMappingLocalDataSourceImpl implements ActionMappingLocalDataSource {
|
||||
static const String _boxName = 'action_mappings';
|
||||
|
||||
Future<Box<ActionMappingHiveModel>> get _box async =>
|
||||
await Hive.openBox<ActionMappingHiveModel>(_boxName);
|
||||
|
||||
@override
|
||||
Future<List<ActionMappingHiveModel>> getAllActionMappings() async {
|
||||
final box = await _box;
|
||||
return box.values
|
||||
.where((mapping) => mapping.syncOperation != 'delete')
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ActionMappingHiveModel?> getActionMappingById(String id) async {
|
||||
final box = await _box;
|
||||
final mapping = box.get(id);
|
||||
|
||||
// Don't return deleted items
|
||||
if (mapping?.syncOperation == 'delete') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return mapping;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveActionMapping(ActionMappingHiveModel mapping) async {
|
||||
final box = await _box;
|
||||
await box.put(mapping.id, mapping);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteActionMapping(String id) async {
|
||||
final box = await _box;
|
||||
final existing = box.get(id);
|
||||
|
||||
if (existing != null) {
|
||||
// Soft delete: mark as dirty with delete operation
|
||||
final deleted = existing.copyWith(
|
||||
isDirty: true,
|
||||
syncOperation: 'delete',
|
||||
lastModified: DateTime.now(),
|
||||
);
|
||||
await box.put(id, deleted);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ActionMappingHiveModel>> getDirtyActionMappings() async {
|
||||
final box = await _box;
|
||||
return box.values.where((mapping) => mapping.isDirty).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> markActionMappingAsSynced(String id) async {
|
||||
final box = await _box;
|
||||
final mapping = box.get(id);
|
||||
|
||||
if (mapping != null) {
|
||||
if (mapping.syncOperation == 'delete') {
|
||||
// Actually delete after successful sync
|
||||
await box.delete(id);
|
||||
} else {
|
||||
// Clear dirty flag and sync operation
|
||||
final synced = mapping.copyWith(
|
||||
isDirty: false,
|
||||
syncOperation: null,
|
||||
);
|
||||
await box.put(id, synced);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> replaceAllActionMappings(List<ActionMappingModel> mappings, {bool force = false}) async {
|
||||
final box = await _box;
|
||||
|
||||
if (force) {
|
||||
// Force mode: Clear ALL items (including dirty) to get fresh server data
|
||||
await box.clear();
|
||||
} else {
|
||||
// Normal mode: Clear existing non-dirty items
|
||||
final keysToDelete = box.values
|
||||
.where((mapping) => !mapping.isDirty)
|
||||
.map((mapping) => mapping.id)
|
||||
.toList();
|
||||
|
||||
for (final key in keysToDelete) {
|
||||
await box.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
// Add new items from server
|
||||
for (final model in mappings) {
|
||||
final hiveModel = ActionMappingHiveModel.fromActionMappingModel(
|
||||
model,
|
||||
isDirty: false,
|
||||
);
|
||||
await box.put(hiveModel.id, hiveModel);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ActionMappingHiveModel>> searchActionMappings(String query) async {
|
||||
final box = await _box;
|
||||
final lowerQuery = query.toLowerCase();
|
||||
|
||||
return box.values
|
||||
.where((mapping) =>
|
||||
mapping.syncOperation != 'delete' &&
|
||||
(mapping.name.toLowerCase().contains(lowerQuery) ||
|
||||
(mapping.description?.toLowerCase().contains(lowerQuery) ?? false)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clearAll() async {
|
||||
final box = await _box;
|
||||
await box.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import '../../../core/errors/exceptions.dart';
|
||||
import '../../../core/storage/token_manager.dart';
|
||||
|
||||
class SecureStorageManager {
|
||||
final FlutterSecureStorage storage;
|
||||
|
||||
SecureStorageManager({required this.storage});
|
||||
|
||||
// Token storage
|
||||
Future<void> saveAccessToken(String token) async {
|
||||
// Save to memory for immediate use
|
||||
TokenManager().saveTokens(accessToken: token);
|
||||
|
||||
try {
|
||||
await storage.write(key: 'access_token', value: token);
|
||||
} catch (e) {
|
||||
// Silently fail on web when storage is not available (HTTP context)
|
||||
print('Warning: Failed to save access token to secure storage (using memory): $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveRefreshToken(String token) async {
|
||||
// Save to memory for immediate use
|
||||
TokenManager().saveTokens(refreshToken: token);
|
||||
|
||||
try {
|
||||
await storage.write(key: 'refresh_token', value: token);
|
||||
} catch (e) {
|
||||
// Silently fail on web when storage is not available (HTTP context)
|
||||
print('Warning: Failed to save refresh token to secure storage (using memory): $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getAccessToken() async {
|
||||
try {
|
||||
final token = await storage.read(key: 'access_token');
|
||||
if (token != null) return token;
|
||||
} catch (e) {
|
||||
print('Warning: Failed to read access token from secure storage, using memory');
|
||||
}
|
||||
// Fallback to memory storage
|
||||
return TokenManager().accessToken;
|
||||
}
|
||||
|
||||
Future<String?> getRefreshToken() async {
|
||||
try {
|
||||
final token = await storage.read(key: 'refresh_token');
|
||||
if (token != null) return token;
|
||||
} catch (e) {
|
||||
print('Warning: Failed to read refresh token from secure storage, using memory');
|
||||
}
|
||||
// Fallback to memory storage
|
||||
return TokenManager().refreshToken;
|
||||
}
|
||||
|
||||
// User data storage
|
||||
Future<void> saveUsername(String username) async {
|
||||
// Save to memory for immediate use
|
||||
TokenManager().saveTokens(username: username);
|
||||
|
||||
try {
|
||||
await storage.write(key: 'username', value: username);
|
||||
} catch (e) {
|
||||
// Silently fail on web when storage is not available (HTTP context)
|
||||
print('Warning: Failed to save username to secure storage (using memory): $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveUserRole(String role) async {
|
||||
// Save to memory for immediate use
|
||||
TokenManager().saveTokens(userRole: role);
|
||||
|
||||
try {
|
||||
await storage.write(key: 'user_role', value: role);
|
||||
} catch (e) {
|
||||
// Silently fail on web when storage is not available (HTTP context)
|
||||
print('Warning: Failed to save user role to secure storage (using memory): $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getUsername() async {
|
||||
try {
|
||||
return await storage.read(key: 'username');
|
||||
} catch (e) {
|
||||
throw CacheException('Failed to read username');
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> getUserRole() async {
|
||||
try {
|
||||
return await storage.read(key: 'user_role');
|
||||
} catch (e) {
|
||||
throw CacheException('Failed to read user role');
|
||||
}
|
||||
}
|
||||
|
||||
// Clear all data
|
||||
Future<void> clearAll() async {
|
||||
// Clear memory storage
|
||||
TokenManager().clear();
|
||||
|
||||
try {
|
||||
await storage.deleteAll();
|
||||
} catch (e) {
|
||||
print('Warning: Failed to clear secure storage: $e');
|
||||
// Not throwing exception since memory is already cleared
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
import 'package:hive/hive.dart';
|
||||
import '../../models/server_hive_model.dart';
|
||||
import '../../models/server_model.dart';
|
||||
|
||||
abstract class ServerLocalDataSource {
|
||||
/// Get all servers from local storage
|
||||
Future<List<ServerHiveModel>> getAllServers();
|
||||
|
||||
/// Get servers by type
|
||||
Future<List<ServerHiveModel>> getServersByType(String type);
|
||||
|
||||
/// Get server by ID and type
|
||||
Future<ServerHiveModel?> getServerById(String id, String type);
|
||||
|
||||
/// Save server to local storage (create or update)
|
||||
Future<void> saveServer(ServerHiveModel server);
|
||||
|
||||
/// Delete server from local storage
|
||||
Future<void> deleteServer(String id, String type);
|
||||
|
||||
/// Get all servers with pending sync operations
|
||||
Future<List<ServerHiveModel>> getDirtyServers();
|
||||
|
||||
/// Clear dirty flag after successful sync
|
||||
Future<void> markServerAsSynced(String id, String type);
|
||||
|
||||
/// Replace all servers (used after fetching from API)
|
||||
Future<void> replaceAllServers(List<ServerModel> servers);
|
||||
|
||||
/// Clear all local data
|
||||
Future<void> clearAll();
|
||||
}
|
||||
|
||||
class ServerLocalDataSourceImpl implements ServerLocalDataSource {
|
||||
static const String _boxName = 'servers';
|
||||
|
||||
Box<ServerHiveModel>? _box;
|
||||
|
||||
Future<Box<ServerHiveModel>> get box async {
|
||||
if (_box != null && _box!.isOpen) {
|
||||
return _box!;
|
||||
}
|
||||
_box = await Hive.openBox<ServerHiveModel>(_boxName);
|
||||
return _box!;
|
||||
}
|
||||
|
||||
String _getKey(String id, String type) => '${type}_$id';
|
||||
|
||||
@override
|
||||
Future<List<ServerHiveModel>> getAllServers() async {
|
||||
final b = await box;
|
||||
return b.values.where((s) => s.syncOperation != 'delete').toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ServerHiveModel>> getServersByType(String type) async {
|
||||
final b = await box;
|
||||
return b.values
|
||||
.where((s) => s.serverType == type && s.syncOperation != 'delete')
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ServerHiveModel?> getServerById(String id, String type) async {
|
||||
final b = await box;
|
||||
final key = _getKey(id, type);
|
||||
final server = b.get(key);
|
||||
|
||||
// Don't return deleted servers
|
||||
if (server?.syncOperation == 'delete') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> saveServer(ServerHiveModel server) async {
|
||||
final b = await box;
|
||||
final key = _getKey(server.id, server.serverType);
|
||||
await b.put(key, server);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteServer(String id, String type) async {
|
||||
final b = await box;
|
||||
final key = _getKey(id, type);
|
||||
final existing = b.get(key);
|
||||
|
||||
if (existing != null) {
|
||||
// Mark as deleted instead of actually deleting
|
||||
// This allows us to sync the deletion to the server
|
||||
final deleted = existing.copyWith(
|
||||
isDirty: true,
|
||||
syncOperation: 'delete',
|
||||
lastModified: DateTime.now(),
|
||||
);
|
||||
await b.put(key, deleted);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ServerHiveModel>> getDirtyServers() async {
|
||||
final b = await box;
|
||||
return b.values.where((s) => s.isDirty).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> markServerAsSynced(String id, String type) async {
|
||||
final b = await box;
|
||||
final key = _getKey(id, type);
|
||||
final server = b.get(key);
|
||||
|
||||
if (server != null) {
|
||||
if (server.syncOperation == 'delete') {
|
||||
// Actually delete after successful sync
|
||||
await b.delete(key);
|
||||
} else {
|
||||
// Clear dirty flag
|
||||
final synced = server.copyWith(
|
||||
isDirty: false,
|
||||
syncOperation: null,
|
||||
);
|
||||
await b.put(key, synced);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> replaceAllServers(List<ServerModel> servers) async {
|
||||
final b = await box;
|
||||
|
||||
// Don't clear dirty servers - keep them for sync
|
||||
final dirtyServers = await getDirtyServers();
|
||||
final dirtyKeys = dirtyServers.map((s) => _getKey(s.id, s.serverType)).toSet();
|
||||
|
||||
// Clear only non-dirty servers
|
||||
await b.clear();
|
||||
|
||||
// Re-add dirty servers
|
||||
for (final dirty in dirtyServers) {
|
||||
await b.put(_getKey(dirty.id, dirty.serverType), dirty);
|
||||
}
|
||||
|
||||
// Add all fetched servers (but don't overwrite dirty ones)
|
||||
for (final server in servers) {
|
||||
final key = _getKey(server.id, server.serverType);
|
||||
if (!dirtyKeys.contains(key)) {
|
||||
await b.put(key, ServerHiveModel.fromServerModel(server));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clearAll() async {
|
||||
final b = await box;
|
||||
await b.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/constants/api_constants.dart';
|
||||
import '../../../core/errors/exceptions.dart';
|
||||
import '../../models/action_mapping_model.dart';
|
||||
|
||||
/// Abstract interface for remote action mapping data operations
|
||||
abstract class ActionMappingRemoteDataSource {
|
||||
Future<List<ActionMappingModel>> getAllActionMappings();
|
||||
Future<ActionMappingModel> getActionMappingById(String id);
|
||||
Future<void> createActionMapping(ActionMappingModel mapping);
|
||||
Future<void> updateActionMapping(String id, ActionMappingModel mapping);
|
||||
Future<void> deleteActionMapping(String id);
|
||||
}
|
||||
|
||||
/// Implementation of remote action mapping data source using Dio
|
||||
class ActionMappingRemoteDataSourceImpl implements ActionMappingRemoteDataSource {
|
||||
final Dio dio;
|
||||
|
||||
ActionMappingRemoteDataSourceImpl({required this.dio});
|
||||
|
||||
@override
|
||||
Future<List<ActionMappingModel>> getAllActionMappings() async {
|
||||
try {
|
||||
final response = await dio.get(ApiConstants.actionMappingsEndpoint);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final List<dynamic> mappingsList = data['mappings'] as List<dynamic>;
|
||||
return mappingsList.map((json) => ActionMappingModel.fromJson(json)).toList();
|
||||
} else {
|
||||
throw ActionMappingException('Failed to fetch action mappings');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ActionMappingModel> getActionMappingById(String id) async {
|
||||
try {
|
||||
final response = await dio.get('${ApiConstants.actionMappingsEndpoint}/$id');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return ActionMappingModel.fromJson(response.data);
|
||||
} else {
|
||||
throw ActionMappingException('Failed to fetch action mapping');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createActionMapping(ActionMappingModel mapping) async {
|
||||
try {
|
||||
final response = await dio.post(
|
||||
ApiConstants.actionMappingsEndpoint,
|
||||
data: mapping.toJson(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||
throw ActionMappingException('Failed to create action mapping');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateActionMapping(String id, ActionMappingModel mapping) async {
|
||||
try {
|
||||
final response = await dio.put(
|
||||
'${ApiConstants.actionMappingsEndpoint}/$id',
|
||||
data: mapping.toJson(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw ActionMappingException('Failed to update action mapping');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteActionMapping(String id) async {
|
||||
try {
|
||||
final response = await dio.delete('${ApiConstants.actionMappingsEndpoint}/$id');
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 204) {
|
||||
throw ActionMappingException('Failed to delete action mapping');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Exception _handleDioException(DioException e) {
|
||||
if (e.type == DioExceptionType.connectionTimeout ||
|
||||
e.type == DioExceptionType.receiveTimeout) {
|
||||
return NetworkException('Connection timeout');
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
return NetworkException('No internet connection');
|
||||
} else if (e.response?.statusCode == 401) {
|
||||
return AuthenticationException('Unauthorized');
|
||||
} else {
|
||||
return ActionMappingException('Server error: ${e.message}');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/constants/api_constants.dart';
|
||||
import '../../../core/errors/exceptions.dart';
|
||||
import '../../models/auth_model.dart';
|
||||
|
||||
abstract class AuthRemoteDataSource {
|
||||
Future<AuthResponseModel> login(String username, String password);
|
||||
Future<void> logout();
|
||||
}
|
||||
|
||||
class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
|
||||
final Dio dio;
|
||||
|
||||
AuthRemoteDataSourceImpl({required this.dio});
|
||||
|
||||
@override
|
||||
Future<AuthResponseModel> login(String username, String password) async {
|
||||
try {
|
||||
final response = await dio.post(
|
||||
ApiConstants.loginEndpoint,
|
||||
data: {
|
||||
'username': username,
|
||||
'password': password,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return AuthResponseModel.fromJson(response.data);
|
||||
} else {
|
||||
throw ServerException('Login failed: ${response.statusMessage}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.response?.statusCode == 401) {
|
||||
throw AuthenticationException('Invalid credentials');
|
||||
} else if (e.type == DioExceptionType.connectionTimeout ||
|
||||
e.type == DioExceptionType.receiveTimeout) {
|
||||
throw NetworkException('Connection timeout');
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
throw NetworkException('No internet connection');
|
||||
} else {
|
||||
throw ServerException('Server error: ${e.message}');
|
||||
}
|
||||
} catch (e) {
|
||||
throw ServerException('Unexpected error: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> logout() async {
|
||||
// In this implementation, logout is handled locally (clearing tokens)
|
||||
// If the API has a logout endpoint, implement it here
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import '../../../core/errors/exceptions.dart';
|
||||
import '../../models/auth_model.dart';
|
||||
import 'auth_remote_data_source.dart';
|
||||
|
||||
/// Mock implementation of AuthRemoteDataSource for development/testing
|
||||
/// Accepts hardcoded credentials: admin/admin123
|
||||
class AuthRemoteDataSourceMock implements AuthRemoteDataSource {
|
||||
@override
|
||||
Future<AuthResponseModel> login(String username, String password) async {
|
||||
// Simulate network delay
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
|
||||
// Check credentials
|
||||
if (username == 'admin' && password == 'admin123') {
|
||||
// Return mock successful auth response
|
||||
return const AuthResponseModel(
|
||||
accessToken: 'mock_access_token_12345',
|
||||
refreshToken: 'mock_refresh_token_67890',
|
||||
username: 'admin',
|
||||
role: 'Administrator',
|
||||
);
|
||||
} else {
|
||||
// Invalid credentials
|
||||
throw AuthenticationException('Invalid username or password');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> logout() async {
|
||||
// Mock logout - nothing to do
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/constants/api_constants.dart';
|
||||
import '../../../core/errors/exceptions.dart';
|
||||
import '../../models/server_model.dart';
|
||||
|
||||
abstract class ServerRemoteDataSource {
|
||||
Future<List<ServerModel>> getAllServers();
|
||||
Future<List<ServerModel>> getGCoreServers();
|
||||
Future<List<ServerModel>> getGeViScopeServers();
|
||||
Future<ServerModel> getServerById(String id, String type);
|
||||
Future<void> createServer(ServerModel server);
|
||||
Future<void> updateServer(ServerModel server);
|
||||
Future<void> deleteServer(String id, String type);
|
||||
}
|
||||
|
||||
class ServerRemoteDataSourceImpl implements ServerRemoteDataSource {
|
||||
final Dio dio;
|
||||
|
||||
ServerRemoteDataSourceImpl({required this.dio});
|
||||
|
||||
@override
|
||||
Future<List<ServerModel>> getAllServers() async {
|
||||
try {
|
||||
final response = await dio.get(ApiConstants.serversEndpoint);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final List<ServerModel> servers = [];
|
||||
|
||||
// Parse gcore servers
|
||||
if (data['gcore_servers'] != null) {
|
||||
final gcoreList = data['gcore_servers'] as List<dynamic>;
|
||||
servers.addAll(
|
||||
gcoreList.map((json) => ServerModel.fromJson(json, 'gcore')),
|
||||
);
|
||||
}
|
||||
|
||||
// Parse geviscope servers
|
||||
if (data['geviscope_servers'] != null) {
|
||||
final geviscopeList = data['geviscope_servers'] as List<dynamic>;
|
||||
servers.addAll(
|
||||
geviscopeList.map((json) => ServerModel.fromJson(json, 'geviscope')),
|
||||
);
|
||||
}
|
||||
|
||||
return servers;
|
||||
} else {
|
||||
throw ServerException('Failed to fetch servers');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ServerModel>> getGCoreServers() async {
|
||||
try {
|
||||
final response = await dio.get(ApiConstants.gcoreServersEndpoint);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final List<dynamic> serversList = data['servers'] as List<dynamic>;
|
||||
return serversList.map((json) => ServerModel.fromJson(json, 'gcore')).toList();
|
||||
} else {
|
||||
throw ServerException('Failed to fetch G-Core servers');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ServerModel>> getGeViScopeServers() async {
|
||||
try {
|
||||
final response = await dio.get(ApiConstants.geviscopeServersEndpoint);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data as Map<String, dynamic>;
|
||||
final List<dynamic> serversList = data['servers'] as List<dynamic>;
|
||||
return serversList.map((json) => ServerModel.fromJson(json, 'geviscope')).toList();
|
||||
} else {
|
||||
throw ServerException('Failed to fetch GeViScope servers');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ServerModel> getServerById(String id, String type) async {
|
||||
try {
|
||||
final endpoint = type == 'gcore'
|
||||
? '${ApiConstants.gcoreServersEndpoint}/$id'
|
||||
: '${ApiConstants.geviscopeServersEndpoint}/$id';
|
||||
|
||||
final response = await dio.get(endpoint);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return ServerModel.fromJson(response.data, type);
|
||||
} else {
|
||||
throw ServerException('Failed to fetch server');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createServer(ServerModel server) async {
|
||||
try {
|
||||
final endpoint = server.serverType == 'gcore'
|
||||
? ApiConstants.gcoreServersEndpoint
|
||||
: ApiConstants.geviscopeServersEndpoint;
|
||||
|
||||
final response = await dio.post(
|
||||
endpoint,
|
||||
data: server.toJson(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||
throw ServerException('Failed to create server');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateServer(ServerModel server) async {
|
||||
try {
|
||||
final endpoint = server.serverType == 'gcore'
|
||||
? '${ApiConstants.gcoreServersEndpoint}/${server.id}'
|
||||
: '${ApiConstants.geviscopeServersEndpoint}/${server.id}';
|
||||
|
||||
final response = await dio.put(
|
||||
endpoint,
|
||||
data: server.toJson(),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw ServerException('Failed to update server');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteServer(String id, String type) async {
|
||||
try {
|
||||
final endpoint = type == 'gcore'
|
||||
? '${ApiConstants.gcoreServersEndpoint}/$id'
|
||||
: '${ApiConstants.geviscopeServersEndpoint}/$id';
|
||||
|
||||
final response = await dio.delete(endpoint);
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 204) {
|
||||
throw ServerException('Failed to delete server');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
throw _handleDioException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Exception _handleDioException(DioException e) {
|
||||
if (e.type == DioExceptionType.connectionTimeout ||
|
||||
e.type == DioExceptionType.receiveTimeout) {
|
||||
return NetworkException('Connection timeout');
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
return NetworkException('No internet connection');
|
||||
} else if (e.response?.statusCode == 401) {
|
||||
return AuthenticationException('Unauthorized');
|
||||
} else {
|
||||
return ServerException('Server error: ${e.message}');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user