Files
geutebruck/geutebruck_app/lib/data/services/sync_service.dart
Administrator a92b909539 feat: GeViScope SDK integration with C# Bridge and Flutter app
- Add GeViScope Bridge (C# .NET 8.0) on port 7720
  - Full SDK wrapper for camera control, PTZ, actions/events
  - 17 REST API endpoints for GeViScope server interaction
  - Support for MCS (Media Channel Simulator) with 16 test channels
  - Real-time action/event streaming via PLC callbacks

- Add GeViServer Bridge (C# .NET 8.0) on port 7710
  - Integration with GeViSoft orchestration layer
  - Input/output control and event management

- Update Python API with new routers
  - /api/geviscope/* - Proxy to GeViScope Bridge
  - /api/geviserver/* - Proxy to GeViServer Bridge
  - /api/excel/* - Excel import functionality

- Add Flutter app GeViScope integration
  - GeViScopeRemoteDataSource with 17 API methods
  - GeViScopeBloc for state management
  - GeViScopeScreen with PTZ controls
  - App drawer navigation to GeViScope

- Add SDK documentation (extracted from PDFs)
  - GeViScope SDK docs (7 parts + action reference)
  - GeViSoft SDK docs (12 chunks)

- Add .mcp.json for Claude Code MCP server config

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-19 08:14:17 +01:00

234 lines
7.8 KiB
Dart

import 'package:dartz/dartz.dart';
import '../../core/errors/exceptions.dart';
import '../../core/errors/failures.dart';
import '../data_sources/local/server_local_data_source.dart';
import '../data_sources/remote/server_remote_data_source.dart';
import '../data_sources/local/action_mapping_local_data_source.dart';
import '../data_sources/remote/action_mapping_remote_data_source.dart';
enum SyncStatus {
idle,
syncing,
success,
error,
}
class SyncResult {
final SyncStatus status;
final String? message;
final int? syncedCount;
final List<String>? errors;
SyncResult({
required this.status,
this.message,
this.syncedCount,
this.errors,
});
factory SyncResult.idle() => SyncResult(status: SyncStatus.idle);
factory SyncResult.syncing() => SyncResult(
status: SyncStatus.syncing,
message: 'Syncing changes...',
);
factory SyncResult.success(int count) => SyncResult(
status: SyncStatus.success,
message: 'Successfully synced $count change${count != 1 ? 's' : ''}',
syncedCount: count,
);
factory SyncResult.error(List<String> errors) => SyncResult(
status: SyncStatus.error,
message: 'Sync failed: ${errors.length} error${errors.length != 1 ? 's' : ''}',
errors: errors,
);
}
abstract class SyncService {
/// Sync all dirty servers with the remote API
Future<Either<Failure, SyncResult>> syncServers();
/// Download fresh data from server and update local storage
Future<Either<Failure, int>> downloadServers();
/// Sync all dirty action mappings with the remote API
Future<Either<Failure, SyncResult>> syncActionMappings();
/// Download fresh action mappings from server and update local storage
Future<Either<Failure, int>> downloadActionMappings();
}
class SyncServiceImpl implements SyncService {
final ServerLocalDataSource localDataSource;
final ServerRemoteDataSource remoteDataSource;
final ActionMappingLocalDataSource actionMappingLocalDataSource;
final ActionMappingRemoteDataSource actionMappingRemoteDataSource;
SyncServiceImpl({
required this.localDataSource,
required this.remoteDataSource,
required this.actionMappingLocalDataSource,
required this.actionMappingRemoteDataSource,
});
@override
Future<Either<Failure, SyncResult>> syncServers() async {
try {
// Get all dirty servers
final dirtyServers = await localDataSource.getDirtyServers();
if (dirtyServers.isEmpty) {
return Right(SyncResult.success(0));
}
final errors = <String>[];
int successCount = 0;
// Process each dirty server
for (final server in dirtyServers) {
try {
final operation = server.syncOperation;
final serverModel = server.toServerModel();
if (operation == 'create') {
await remoteDataSource.createServer(serverModel);
await localDataSource.markServerAsSynced(server.id, server.serverType);
successCount++;
} else if (operation == 'update') {
await remoteDataSource.updateServer(serverModel);
await localDataSource.markServerAsSynced(server.id, server.serverType);
successCount++;
} else if (operation == 'delete') {
await remoteDataSource.deleteServer(server.id, server.serverType);
await localDataSource.markServerAsSynced(server.id, server.serverType);
successCount++;
}
} catch (e) {
errors.add('Failed to sync ${server.alias}: ${e.toString()}');
}
}
if (errors.isEmpty) {
return Right(SyncResult.success(successCount));
} else if (successCount > 0) {
// Partial success
return Right(SyncResult(
status: SyncStatus.success,
message: 'Synced $successCount/${dirtyServers.length} changes',
syncedCount: successCount,
errors: errors,
));
} else {
// Complete failure
return Right(SyncResult.error(errors));
}
} on ServerException catch (e) {
return Left(ServerFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} catch (e) {
return Left(ServerFailure('Unexpected error during sync: ${e.toString()}'));
}
}
@override
Future<Either<Failure, int>> downloadServers() async {
try {
// Fetch all servers from API
final servers = await remoteDataSource.getAllServers();
// Replace local storage with force=true to discard all local changes
await localDataSource.replaceAllServers(servers, force: true);
return Right(servers.length);
} on ServerException catch (e) {
return Left(ServerFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} catch (e) {
return Left(ServerFailure('Unexpected error during download: ${e.toString()}'));
}
}
@override
Future<Either<Failure, SyncResult>> syncActionMappings() async {
try {
// Get all dirty action mappings
final dirtyMappings = await actionMappingLocalDataSource.getDirtyActionMappings();
if (dirtyMappings.isEmpty) {
return Right(SyncResult.success(0));
}
final errors = <String>[];
int successCount = 0;
// Process each dirty action mapping
for (final mapping in dirtyMappings) {
try {
final operation = mapping.syncOperation;
final mappingModel = mapping.toActionMappingModel();
if (operation == 'create') {
await actionMappingRemoteDataSource.createActionMapping(mappingModel);
await actionMappingLocalDataSource.markActionMappingAsSynced(mapping.id);
successCount++;
} else if (operation == 'update') {
await actionMappingRemoteDataSource.updateActionMapping(mapping.id, mappingModel);
await actionMappingLocalDataSource.markActionMappingAsSynced(mapping.id);
successCount++;
} else if (operation == 'delete') {
await actionMappingRemoteDataSource.deleteActionMapping(mapping.id);
await actionMappingLocalDataSource.markActionMappingAsSynced(mapping.id);
successCount++;
}
} catch (e) {
errors.add('Failed to sync ${mapping.name}: ${e.toString()}');
}
}
if (errors.isEmpty) {
return Right(SyncResult.success(successCount));
} else if (successCount > 0) {
// Partial success
return Right(SyncResult(
status: SyncStatus.success,
message: 'Synced $successCount/${dirtyMappings.length} changes',
syncedCount: successCount,
errors: errors,
));
} else {
// Complete failure
return Right(SyncResult.error(errors));
}
} on ActionMappingException catch (e) {
return Left(ActionMappingFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} catch (e) {
return Left(ActionMappingFailure('Unexpected error during sync: ${e.toString()}'));
}
}
@override
Future<Either<Failure, int>> downloadActionMappings() async {
try {
// Fetch all action mappings from API
final mappings = await actionMappingRemoteDataSource.getAllActionMappings();
// Replace local storage with force=true to discard local changes and get fresh server data
await actionMappingLocalDataSource.replaceAllActionMappings(mappings, force: true);
return Right(mappings.length);
} on ActionMappingException catch (e) {
return Left(ActionMappingFailure(e.message));
} on NetworkException catch (e) {
return Left(NetworkFailure(e.message));
} catch (e) {
return Left(ActionMappingFailure('Unexpected error during download: ${e.toString()}'));
}
}
}