Files
geutebruck/geutebruck_app/lib/presentation/blocs/geviserver/geviserver_bloc.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

382 lines
11 KiB
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../data/data_sources/remote/geviserver_remote_data_source.dart';
import 'geviserver_event.dart';
import 'geviserver_state.dart';
class GeViServerBloc extends Bloc<GeViServerEvent, GeViServerState> {
final GeViServerRemoteDataSource remoteDataSource;
GeViServerBloc({required this.remoteDataSource})
: super(const GeViServerState()) {
on<ConnectGeViServerEvent>(_onConnect);
on<DisconnectGeViServerEvent>(_onDisconnect);
on<CheckStatusEvent>(_onCheckStatus);
on<SendCrossSwitchEvent>(_onSendCrossSwitch);
on<ClearVideoOutputEvent>(_onClearVideoOutput);
on<CloseContactEvent>(_onCloseContact);
on<OpenContactEvent>(_onOpenContact);
on<SendCustomActionEvent>(_onSendCustomAction);
on<SendMessageEvent>(_onSendMessage);
on<StartTimerEvent>(_onStartTimer);
on<StopTimerEvent>(_onStopTimer);
on<ClearActionResultEvent>(_onClearActionResult);
}
Future<void> _onConnect(
ConnectGeViServerEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(
connectionStatus: ConnectionStatus.connecting,
isLoading: true,
clearErrorMessage: true,
));
try {
final result = await remoteDataSource.connect(
address: event.address,
username: event.username,
password: event.password,
);
if (result['success'] == true) {
emit(state.copyWith(
connectionStatus: ConnectionStatus.connected,
serverAddress: event.address,
username: event.username,
connectedAt: DateTime.now(),
isLoading: false,
lastActionResult: 'Connected to ${event.address}',
lastActionSuccess: true,
));
} else {
emit(state.copyWith(
connectionStatus: ConnectionStatus.error,
isLoading: false,
errorMessage: result['message'] ?? 'Connection failed',
lastActionResult: result['message'] ?? 'Connection failed',
lastActionSuccess: false,
));
}
} catch (e) {
emit(state.copyWith(
connectionStatus: ConnectionStatus.error,
isLoading: false,
errorMessage: e.toString(),
lastActionResult: 'Connection error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onDisconnect(
DisconnectGeViServerEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
await remoteDataSource.disconnect();
emit(state.copyWith(
connectionStatus: ConnectionStatus.disconnected,
isLoading: false,
clearServerAddress: true,
clearUsername: true,
clearConnectedAt: true,
lastActionResult: 'Disconnected',
lastActionSuccess: true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
errorMessage: e.toString(),
lastActionResult: 'Disconnect error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onCheckStatus(
CheckStatusEvent event,
Emitter<GeViServerState> emit,
) async {
try {
final result = await remoteDataSource.getStatus();
final isConnected = result['is_connected'] == true;
emit(state.copyWith(
connectionStatus: isConnected
? ConnectionStatus.connected
: ConnectionStatus.disconnected,
serverAddress: result['address']?.toString(),
username: result['username']?.toString(),
));
} catch (e) {
emit(state.copyWith(
connectionStatus: ConnectionStatus.error,
errorMessage: e.toString(),
));
}
}
Future<void> _onSendCrossSwitch(
SendCrossSwitchEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.crossSwitch(
videoInput: event.videoInput,
videoOutput: event.videoOutput,
switchMode: event.switchMode,
);
final message = 'CrossSwitch(${event.videoInput}, ${event.videoOutput}, ${event.switchMode})';
_addToLog(emit, message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'CrossSwitch sent successfully'
: result['message'] ?? 'CrossSwitch failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'CrossSwitch error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onClearVideoOutput(
ClearVideoOutputEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.clearOutput(
videoOutput: event.videoOutput,
);
final message = 'ClearVideoOutput(${event.videoOutput})';
_addToLog(emit, message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'ClearOutput sent successfully'
: result['message'] ?? 'ClearOutput failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'ClearOutput error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onCloseContact(
CloseContactEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.closeContact(
contactId: event.contactId,
);
final message = 'CloseContact(${event.contactId})';
_addToLog(emit, message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'CloseContact sent successfully'
: result['message'] ?? 'CloseContact failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'CloseContact error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onOpenContact(
OpenContactEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.openContact(
contactId: event.contactId,
);
final message = 'OpenContact(${event.contactId})';
_addToLog(emit, message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'OpenContact sent successfully'
: result['message'] ?? 'OpenContact failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'OpenContact error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onSendCustomAction(
SendCustomActionEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.sendCustomAction(
typeId: event.typeId,
text: event.text,
);
final message = 'CustomAction(${event.typeId}, "${event.text}")';
_addToLog(emit, message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'CustomAction sent successfully'
: result['message'] ?? 'CustomAction failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'CustomAction error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onSendMessage(
SendMessageEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.sendMessage(event.message);
_addToLog(emit, event.message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'Message sent successfully'
: result['message'] ?? 'Send message failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'Send message error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onStartTimer(
StartTimerEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.startTimer(
timerId: event.timerId,
timerName: event.timerName,
);
final message = 'StartTimer(${event.timerId}, "${event.timerName}")';
_addToLog(emit, message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'StartTimer sent successfully'
: result['message'] ?? 'StartTimer failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'StartTimer error: $e',
lastActionSuccess: false,
));
}
}
Future<void> _onStopTimer(
StopTimerEvent event,
Emitter<GeViServerState> emit,
) async {
emit(state.copyWith(isLoading: true));
try {
final result = await remoteDataSource.stopTimer(
timerId: event.timerId,
timerName: event.timerName,
);
final message = 'StopTimer(${event.timerId}, "${event.timerName}")';
_addToLog(emit, message);
emit(state.copyWith(
isLoading: false,
lastActionResult: result['success'] == true
? 'StopTimer sent successfully'
: result['message'] ?? 'StopTimer failed',
lastActionSuccess: result['success'] == true,
));
} catch (e) {
emit(state.copyWith(
isLoading: false,
lastActionResult: 'StopTimer error: $e',
lastActionSuccess: false,
));
}
}
void _onClearActionResult(
ClearActionResultEvent event,
Emitter<GeViServerState> emit,
) {
emit(state.copyWith(clearLastActionResult: true, clearErrorMessage: true));
}
void _addToLog(Emitter<GeViServerState> emit, String message) {
final timestamp = DateTime.now().toIso8601String().substring(11, 19);
final logEntry = '[$timestamp] $message';
final newLog = [...state.messageLog, logEntry];
// Keep only last 100 messages
if (newLog.length > 100) {
newLog.removeRange(0, newLog.length - 100);
}
emit(state.copyWith(messageLog: newLog));
}
}