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>
This commit is contained in:
Administrator
2026-01-19 08:14:17 +01:00
parent c9e83e4277
commit a92b909539
76 changed files with 62101 additions and 176 deletions

View File

@@ -0,0 +1,964 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../blocs/geviscope/geviscope_bloc.dart';
import '../../blocs/geviscope/geviscope_event.dart';
import '../../blocs/geviscope/geviscope_state.dart';
import '../../widgets/app_drawer.dart';
class GeViScopeScreen extends StatefulWidget {
const GeViScopeScreen({super.key});
@override
State<GeViScopeScreen> createState() => _GeViScopeScreenState();
}
class _GeViScopeScreenState extends State<GeViScopeScreen> {
// Connection form controllers
final _addressController = TextEditingController(text: 'localhost');
final _usernameController = TextEditingController(text: 'sysadmin');
final _passwordController = TextEditingController(text: 'masterkey');
// CrossSwitch form controllers
final _videoInputController = TextEditingController(text: '1');
final _videoOutputController = TextEditingController(text: '1');
// PTZ controls
final _cameraController = TextEditingController(text: '1');
final _ptzSpeedController = TextEditingController(text: '50');
final _presetController = TextEditingController(text: '1');
// Digital I/O controller
final _contactIdController = TextEditingController(text: '1');
// Custom action controllers
final _customActionTypeIdController = TextEditingController(text: '1');
final _customActionTextController = TextEditingController(text: 'Test message');
// Raw action controller
final _rawActionController = TextEditingController();
bool _didCheckStatus = false;
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (!_didCheckStatus) {
_didCheckStatus = true;
context.read<GeViScopeBloc>().add(const CheckGeViScopeStatusEvent());
}
}
@override
void dispose() {
_addressController.dispose();
_usernameController.dispose();
_passwordController.dispose();
_videoInputController.dispose();
_videoOutputController.dispose();
_cameraController.dispose();
_ptzSpeedController.dispose();
_presetController.dispose();
_contactIdController.dispose();
_customActionTypeIdController.dispose();
_customActionTextController.dispose();
_rawActionController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('GeViScope Control'),
actions: [
BlocBuilder<GeViScopeBloc, GeViScopeState>(
builder: (context, state) {
return Row(
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: _getStatusColor(state.connectionStatus).withOpacity(0.2),
borderRadius: BorderRadius.circular(16),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
_getStatusIcon(state.connectionStatus),
size: 16,
color: _getStatusColor(state.connectionStatus),
),
const SizedBox(width: 6),
Text(
_getStatusText(state.connectionStatus),
style: TextStyle(
color: _getStatusColor(state.connectionStatus),
fontWeight: FontWeight.bold,
),
),
if (state.channelCount > 0) ...[
const SizedBox(width: 8),
Text(
'(${state.channelCount} channels)',
style: TextStyle(
color: _getStatusColor(state.connectionStatus),
fontSize: 12,
),
),
],
],
),
),
const SizedBox(width: 16),
],
);
},
),
],
),
drawer: const AppDrawer(currentRoute: '/geviscope'),
body: BlocConsumer<GeViScopeBloc, GeViScopeState>(
listener: (context, state) {
if (state.lastActionResult != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.lastActionResult!),
backgroundColor: state.lastActionSuccess ? Colors.green : Colors.red,
duration: const Duration(seconds: 2),
),
);
context.read<GeViScopeBloc>().add(const ClearGeViScopeActionResultEvent());
}
},
builder: (context, state) {
return Row(
children: [
// Left panel - Controls
Expanded(
flex: 2,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildConnectionCard(context, state),
const SizedBox(height: 16),
if (state.isConnected) ...[
_buildPTZControlCard(context, state),
const SizedBox(height: 16),
_buildVideoControlCard(context, state),
const SizedBox(height: 16),
_buildDigitalIOCard(context, state),
const SizedBox(height: 16),
_buildCustomActionCard(context, state),
const SizedBox(height: 16),
_buildRawActionCard(context, state),
],
],
),
),
),
// Right panel - Channels & Message log
Expanded(
flex: 1,
child: _buildRightPanel(context, state),
),
],
);
},
),
);
}
Widget _buildConnectionCard(BuildContext context, GeViScopeState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.power_settings_new,
color: state.isConnected ? Colors.green : Colors.grey,
),
const SizedBox(width: 8),
Text(
'Connection',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
if (!state.isConnected) ...[
Row(
children: [
Expanded(
child: TextField(
controller: _addressController,
decoration: const InputDecoration(
labelText: 'Server Address',
hintText: 'localhost',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: 'Username',
hintText: 'sysadmin',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(
labelText: 'Password',
hintText: 'masterkey',
border: OutlineInputBorder(),
isDense: true,
),
),
),
],
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViScopeBloc>().add(
ConnectGeViScopeEvent(
address: _addressController.text,
username: _usernameController.text,
password: _passwordController.text,
),
);
},
icon: state.isLoading
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.link),
label: Text(state.isLoading ? 'Connecting...' : 'Connect'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
),
),
] else ...[
ListTile(
leading: const Icon(Icons.videocam, color: Colors.green),
title: Text('Connected to ${state.serverAddress}'),
subtitle: Text('User: ${state.username} | ${state.channelCount} channels'),
contentPadding: EdgeInsets.zero,
),
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViScopeBloc>().add(
const DisconnectGeViScopeEvent(),
);
},
icon: const Icon(Icons.link_off),
label: const Text('Disconnect'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
),
),
],
],
),
),
);
}
Widget _buildPTZControlCard(BuildContext context, GeViScopeState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.control_camera, color: Colors.orange),
const SizedBox(width: 8),
Text(
'PTZ Camera Control',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
child: TextField(
controller: _cameraController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Camera #',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _ptzSpeedController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Speed (1-100)',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _presetController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Preset #',
border: OutlineInputBorder(),
isDense: true,
),
),
),
],
),
const SizedBox(height: 16),
// PTZ Direction pad
Center(
child: SizedBox(
width: 200,
height: 200,
child: Stack(
children: [
// Up
Positioned(
top: 0,
left: 70,
child: _ptzButton(
context,
Icons.arrow_upward,
'Up',
() => _sendPTZ(context, 'tilt', 'up'),
),
),
// Down
Positioned(
bottom: 0,
left: 70,
child: _ptzButton(
context,
Icons.arrow_downward,
'Down',
() => _sendPTZ(context, 'tilt', 'down'),
),
),
// Left
Positioned(
left: 0,
top: 70,
child: _ptzButton(
context,
Icons.arrow_back,
'Left',
() => _sendPTZ(context, 'pan', 'left'),
),
),
// Right
Positioned(
right: 0,
top: 70,
child: _ptzButton(
context,
Icons.arrow_forward,
'Right',
() => _sendPTZ(context, 'pan', 'right'),
),
),
// Stop (center)
Positioned(
left: 70,
top: 70,
child: SizedBox(
width: 60,
height: 60,
child: ElevatedButton(
onPressed: state.isLoading
? null
: () {
final camera = int.tryParse(_cameraController.text) ?? 1;
context.read<GeViScopeBloc>().add(
CameraStopEvent(camera: camera),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
shape: const CircleBorder(),
padding: EdgeInsets.zero,
),
child: const Icon(Icons.stop, size: 30),
),
),
),
],
),
),
),
const SizedBox(height: 16),
// Zoom controls
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton.icon(
onPressed: state.isLoading
? null
: () => _sendPTZ(context, 'zoom', 'out'),
icon: const Icon(Icons.zoom_out),
label: const Text('Zoom Out'),
),
const SizedBox(width: 16),
ElevatedButton.icon(
onPressed: state.isLoading
? null
: () => _sendPTZ(context, 'zoom', 'in'),
icon: const Icon(Icons.zoom_in),
label: const Text('Zoom In'),
),
],
),
const SizedBox(height: 16),
// Preset
Row(
children: [
Expanded(
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
final camera = int.tryParse(_cameraController.text) ?? 1;
final preset = int.tryParse(_presetController.text) ?? 1;
context.read<GeViScopeBloc>().add(
CameraPresetEvent(camera: camera, preset: preset),
);
},
icon: const Icon(Icons.bookmark),
label: const Text('Go to Preset'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
foregroundColor: Colors.white,
),
),
),
],
),
],
),
),
);
}
Widget _ptzButton(
BuildContext context,
IconData icon,
String tooltip,
VoidCallback onPressed,
) {
final state = context.read<GeViScopeBloc>().state;
return SizedBox(
width: 60,
height: 60,
child: ElevatedButton(
onPressed: state.isLoading ? null : onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
shape: const CircleBorder(),
padding: EdgeInsets.zero,
),
child: Icon(icon, size: 30),
),
);
}
void _sendPTZ(BuildContext context, String type, String direction) {
final camera = int.tryParse(_cameraController.text) ?? 1;
final speed = int.tryParse(_ptzSpeedController.text) ?? 50;
switch (type) {
case 'pan':
context.read<GeViScopeBloc>().add(
CameraPanEvent(camera: camera, direction: direction, speed: speed),
);
break;
case 'tilt':
context.read<GeViScopeBloc>().add(
CameraTiltEvent(camera: camera, direction: direction, speed: speed),
);
break;
case 'zoom':
context.read<GeViScopeBloc>().add(
CameraZoomEvent(camera: camera, direction: direction, speed: speed),
);
break;
}
}
Widget _buildVideoControlCard(BuildContext context, GeViScopeState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.videocam, color: Colors.blue),
const SizedBox(width: 8),
Text(
'Video CrossSwitch',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
child: TextField(
controller: _videoInputController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Video Input',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
const Icon(Icons.arrow_forward, color: Colors.grey),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _videoOutputController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Video Output',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViScopeBloc>().add(
SendGeViScopeCrossSwitchEvent(
videoInput: int.tryParse(_videoInputController.text) ?? 1,
videoOutput: int.tryParse(_videoOutputController.text) ?? 1,
),
);
},
icon: const Icon(Icons.swap_horiz),
label: const Text('Switch'),
),
],
),
],
),
),
);
}
Widget _buildDigitalIOCard(BuildContext context, GeViScopeState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.toggle_on, color: Colors.purple),
const SizedBox(width: 8),
Text(
'Digital I/O',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
flex: 2,
child: TextField(
controller: _contactIdController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Contact ID',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViScopeBloc>().add(
GeViScopeCloseContactEvent(
contactId: int.tryParse(_contactIdController.text) ?? 1,
),
);
},
icon: const Icon(Icons.lock),
label: const Text('Close'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViScopeBloc>().add(
GeViScopeOpenContactEvent(
contactId: int.tryParse(_contactIdController.text) ?? 1,
),
);
},
icon: const Icon(Icons.lock_open),
label: const Text('Open'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
),
),
],
),
],
),
),
);
}
Widget _buildCustomActionCard(BuildContext context, GeViScopeState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.send, color: Colors.teal),
const SizedBox(width: 8),
Text(
'Custom Action',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
child: TextField(
controller: _customActionTypeIdController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Type ID',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
flex: 3,
child: TextField(
controller: _customActionTextController,
decoration: const InputDecoration(
labelText: 'Text',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViScopeBloc>().add(
SendGeViScopeCustomActionEvent(
typeId: int.tryParse(_customActionTypeIdController.text) ?? 1,
text: _customActionTextController.text,
),
);
},
icon: const Icon(Icons.send),
label: const Text('Send'),
),
],
),
],
),
),
);
}
Widget _buildRawActionCard(BuildContext context, GeViScopeState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.code, color: Colors.indigo),
const SizedBox(width: 8),
Text(
'Raw Action',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
child: TextField(
controller: _rawActionController,
decoration: const InputDecoration(
labelText: 'Action String',
hintText: 'e.g., CrossSwitch(1, 2, 0)',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: state.isLoading || _rawActionController.text.isEmpty
? null
: () {
context.read<GeViScopeBloc>().add(
SendGeViScopeActionEvent(
action: _rawActionController.text,
),
);
_rawActionController.clear();
},
icon: const Icon(Icons.send),
label: const Text('Send'),
),
],
),
],
),
),
);
}
Widget _buildRightPanel(BuildContext context, GeViScopeState state) {
return Container(
decoration: BoxDecoration(
border: Border(
left: BorderSide(color: Colors.grey.shade300),
),
),
child: Column(
children: [
// Channels section
if (state.isConnected && state.channels.isNotEmpty) ...[
Container(
padding: const EdgeInsets.all(12),
color: Colors.blue.shade50,
child: Row(
children: [
const Icon(Icons.videocam, size: 18, color: Colors.blue),
const SizedBox(width: 8),
Text(
'Channels (${state.channels.length})',
style: const TextStyle(fontWeight: FontWeight.bold),
),
const Spacer(),
IconButton(
icon: const Icon(Icons.refresh, size: 18),
onPressed: () {
context.read<GeViScopeBloc>().add(const RefreshChannelsEvent());
},
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
),
],
),
),
SizedBox(
height: 150,
child: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: state.channels.length,
itemBuilder: (context, index) {
final channel = state.channels[index];
return ListTile(
dense: true,
leading: Icon(
Icons.camera_alt,
size: 16,
color: channel.isActive ? Colors.green : Colors.grey,
),
title: Text(
channel.name.isNotEmpty ? channel.name : 'Channel ${channel.channelId}',
style: const TextStyle(fontSize: 12),
),
subtitle: Text(
'ID: ${channel.channelId} | Global: ${channel.globalNumber}',
style: const TextStyle(fontSize: 10),
),
contentPadding: EdgeInsets.zero,
);
},
),
),
const Divider(height: 1),
],
// Message log section
Container(
padding: const EdgeInsets.all(12),
color: Colors.grey.shade100,
child: Row(
children: [
const Icon(Icons.list_alt, size: 18),
const SizedBox(width: 8),
const Text(
'Message Log',
style: TextStyle(fontWeight: FontWeight.bold),
),
const Spacer(),
Text(
'${state.messageLog.length}',
style: TextStyle(color: Colors.grey.shade600, fontSize: 12),
),
],
),
),
Expanded(
child: state.messageLog.isEmpty
? const Center(
child: Text(
'No messages yet',
style: TextStyle(color: Colors.grey),
),
)
: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: state.messageLog.length,
itemBuilder: (context, index) {
final reversedIndex = state.messageLog.length - 1 - index;
return Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
margin: const EdgeInsets.only(bottom: 4),
decoration: BoxDecoration(
color: Colors.grey.shade50,
borderRadius: BorderRadius.circular(4),
),
child: Text(
state.messageLog[reversedIndex],
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 11,
),
),
);
},
),
),
],
),
);
}
Color _getStatusColor(GeViScopeConnectionStatus status) {
switch (status) {
case GeViScopeConnectionStatus.connected:
return Colors.green;
case GeViScopeConnectionStatus.connecting:
return Colors.orange;
case GeViScopeConnectionStatus.error:
return Colors.red;
case GeViScopeConnectionStatus.disconnected:
return Colors.grey;
}
}
IconData _getStatusIcon(GeViScopeConnectionStatus status) {
switch (status) {
case GeViScopeConnectionStatus.connected:
return Icons.check_circle;
case GeViScopeConnectionStatus.connecting:
return Icons.sync;
case GeViScopeConnectionStatus.error:
return Icons.error;
case GeViScopeConnectionStatus.disconnected:
return Icons.cancel;
}
}
String _getStatusText(GeViScopeConnectionStatus status) {
switch (status) {
case GeViScopeConnectionStatus.connected:
return 'Connected';
case GeViScopeConnectionStatus.connecting:
return 'Connecting...';
case GeViScopeConnectionStatus.error:
return 'Error';
case GeViScopeConnectionStatus.disconnected:
return 'Disconnected';
}
}
}

View File

@@ -0,0 +1,691 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../blocs/geviserver/geviserver_bloc.dart';
import '../../blocs/geviserver/geviserver_event.dart';
import '../../blocs/geviserver/geviserver_state.dart';
import '../../widgets/app_drawer.dart';
class GeViServerScreen extends StatefulWidget {
const GeViServerScreen({super.key});
@override
State<GeViServerScreen> createState() => _GeViServerScreenState();
}
class _GeViServerScreenState extends State<GeViServerScreen> {
// Connection form controllers
final _addressController = TextEditingController(text: 'localhost');
final _usernameController = TextEditingController(text: 'sysadmin');
final _passwordController = TextEditingController(text: 'masterkey');
// CrossSwitch form controllers
final _videoInputController = TextEditingController(text: '1');
final _videoOutputController = TextEditingController(text: '1');
final _switchModeController = TextEditingController(text: '0');
// Digital I/O controller
final _contactIdController = TextEditingController(text: '1');
// Custom action controllers
final _customActionTypeIdController = TextEditingController(text: '1');
final _customActionTextController = TextEditingController(text: 'Test message');
// Raw message controller
final _rawMessageController = TextEditingController();
bool _didCheckStatus = false;
@override
void didChangeDependencies() {
super.didChangeDependencies();
// Check status on init (only once)
if (!_didCheckStatus) {
_didCheckStatus = true;
context.read<GeViServerBloc>().add(const CheckStatusEvent());
}
}
@override
void dispose() {
_addressController.dispose();
_usernameController.dispose();
_passwordController.dispose();
_videoInputController.dispose();
_videoOutputController.dispose();
_switchModeController.dispose();
_contactIdController.dispose();
_customActionTypeIdController.dispose();
_customActionTextController.dispose();
_rawMessageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('GeViServer Control'),
actions: [
BlocBuilder<GeViServerBloc, GeViServerState>(
builder: (context, state) {
return Row(
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
decoration: BoxDecoration(
color: _getStatusColor(state.connectionStatus).withOpacity(0.2),
borderRadius: BorderRadius.circular(16),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
_getStatusIcon(state.connectionStatus),
size: 16,
color: _getStatusColor(state.connectionStatus),
),
const SizedBox(width: 6),
Text(
_getStatusText(state.connectionStatus),
style: TextStyle(
color: _getStatusColor(state.connectionStatus),
fontWeight: FontWeight.bold,
),
),
],
),
),
const SizedBox(width: 16),
],
);
},
),
],
),
drawer: const AppDrawer(currentRoute: '/geviserver'),
body: BlocConsumer<GeViServerBloc, GeViServerState>(
listener: (context, state) {
if (state.lastActionResult != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.lastActionResult!),
backgroundColor: state.lastActionSuccess ? Colors.green : Colors.red,
duration: const Duration(seconds: 2),
),
);
context.read<GeViServerBloc>().add(const ClearActionResultEvent());
}
},
builder: (context, state) {
return Row(
children: [
// Left panel - Controls
Expanded(
flex: 2,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildConnectionCard(context, state),
const SizedBox(height: 16),
if (state.isConnected) ...[
_buildVideoControlCard(context, state),
const SizedBox(height: 16),
_buildDigitalIOCard(context, state),
const SizedBox(height: 16),
_buildCustomActionCard(context, state),
const SizedBox(height: 16),
_buildRawMessageCard(context, state),
],
],
),
),
),
// Right panel - Message log
Expanded(
flex: 1,
child: _buildMessageLogPanel(context, state),
),
],
);
},
),
);
}
Widget _buildConnectionCard(BuildContext context, GeViServerState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.power_settings_new,
color: state.isConnected ? Colors.green : Colors.grey,
),
const SizedBox(width: 8),
Text(
'Connection',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
if (!state.isConnected) ...[
Row(
children: [
Expanded(
child: TextField(
controller: _addressController,
decoration: const InputDecoration(
labelText: 'Server Address',
hintText: 'localhost',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: 'Username',
hintText: 'sysadmin',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(
labelText: 'Password',
border: OutlineInputBorder(),
isDense: true,
),
),
),
],
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViServerBloc>().add(
ConnectGeViServerEvent(
address: _addressController.text,
username: _usernameController.text,
password: _passwordController.text,
),
);
},
icon: state.isLoading
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.link),
label: Text(state.isLoading ? 'Connecting...' : 'Connect'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
),
),
] else ...[
ListTile(
leading: const Icon(Icons.computer, color: Colors.green),
title: Text('Connected to ${state.serverAddress}'),
subtitle: Text('User: ${state.username}'),
contentPadding: EdgeInsets.zero,
),
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViServerBloc>().add(
const DisconnectGeViServerEvent(),
);
},
icon: const Icon(Icons.link_off),
label: const Text('Disconnect'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
),
),
],
],
),
),
);
}
Widget _buildVideoControlCard(BuildContext context, GeViServerState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.videocam, color: Colors.blue),
const SizedBox(width: 8),
Text(
'Video Control',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
child: TextField(
controller: _videoInputController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Video Input',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _videoOutputController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Video Output',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: TextField(
controller: _switchModeController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Switch Mode',
border: OutlineInputBorder(),
isDense: true,
),
),
),
],
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViServerBloc>().add(
SendCrossSwitchEvent(
videoInput: int.tryParse(_videoInputController.text) ?? 1,
videoOutput: int.tryParse(_videoOutputController.text) ?? 1,
switchMode: int.tryParse(_switchModeController.text) ?? 0,
),
);
},
icon: const Icon(Icons.swap_horiz),
label: const Text('CrossSwitch'),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViServerBloc>().add(
ClearVideoOutputEvent(
videoOutput: int.tryParse(_videoOutputController.text) ?? 1,
),
);
},
icon: const Icon(Icons.clear),
label: const Text('Clear Output'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
),
),
),
],
),
],
),
),
);
}
Widget _buildDigitalIOCard(BuildContext context, GeViServerState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.toggle_on, color: Colors.purple),
const SizedBox(width: 8),
Text(
'Digital I/O',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
flex: 2,
child: TextField(
controller: _contactIdController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Contact ID',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViServerBloc>().add(
CloseContactEvent(
contactId: int.tryParse(_contactIdController.text) ?? 1,
),
);
},
icon: const Icon(Icons.lock),
label: const Text('Close'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
),
),
const SizedBox(width: 8),
Expanded(
child: ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViServerBloc>().add(
OpenContactEvent(
contactId: int.tryParse(_contactIdController.text) ?? 1,
),
);
},
icon: const Icon(Icons.lock_open),
label: const Text('Open'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
),
),
],
),
],
),
),
);
}
Widget _buildCustomActionCard(BuildContext context, GeViServerState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.send, color: Colors.teal),
const SizedBox(width: 8),
Text(
'Custom Action',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
child: TextField(
controller: _customActionTypeIdController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Type ID',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
Expanded(
flex: 3,
child: TextField(
controller: _customActionTextController,
decoration: const InputDecoration(
labelText: 'Text',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: state.isLoading
? null
: () {
context.read<GeViServerBloc>().add(
SendCustomActionEvent(
typeId: int.tryParse(_customActionTypeIdController.text) ?? 1,
text: _customActionTextController.text,
),
);
},
icon: const Icon(Icons.send),
label: const Text('Send'),
),
],
),
],
),
),
);
}
Widget _buildRawMessageCard(BuildContext context, GeViServerState state) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.code, color: Colors.indigo),
const SizedBox(width: 8),
Text(
'Raw Message',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
const Divider(),
Row(
children: [
Expanded(
child: TextField(
controller: _rawMessageController,
decoration: const InputDecoration(
labelText: 'Action Message',
hintText: 'e.g., CrossSwitch(7, 3, 0)',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
ElevatedButton.icon(
onPressed: state.isLoading || _rawMessageController.text.isEmpty
? null
: () {
context.read<GeViServerBloc>().add(
SendMessageEvent(
message: _rawMessageController.text,
),
);
_rawMessageController.clear();
},
icon: const Icon(Icons.send),
label: const Text('Send'),
),
],
),
],
),
),
);
}
Widget _buildMessageLogPanel(BuildContext context, GeViServerState state) {
return Container(
decoration: BoxDecoration(
border: Border(
left: BorderSide(color: Colors.grey.shade300),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.all(16),
color: Colors.grey.shade100,
child: Row(
children: [
const Icon(Icons.list_alt, size: 20),
const SizedBox(width: 8),
const Text(
'Message Log',
style: TextStyle(fontWeight: FontWeight.bold),
),
const Spacer(),
Text(
'${state.messageLog.length} messages',
style: TextStyle(color: Colors.grey.shade600, fontSize: 12),
),
],
),
),
Expanded(
child: state.messageLog.isEmpty
? const Center(
child: Text(
'No messages yet',
style: TextStyle(color: Colors.grey),
),
)
: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: state.messageLog.length,
itemBuilder: (context, index) {
final reversedIndex = state.messageLog.length - 1 - index;
return Container(
padding: const EdgeInsets.symmetric(
horizontal: 8,
vertical: 4,
),
margin: const EdgeInsets.only(bottom: 4),
decoration: BoxDecoration(
color: Colors.grey.shade50,
borderRadius: BorderRadius.circular(4),
),
child: Text(
state.messageLog[reversedIndex],
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 12,
),
),
);
},
),
),
],
),
);
}
Color _getStatusColor(ConnectionStatus status) {
switch (status) {
case ConnectionStatus.connected:
return Colors.green;
case ConnectionStatus.connecting:
return Colors.orange;
case ConnectionStatus.error:
return Colors.red;
case ConnectionStatus.disconnected:
return Colors.grey;
}
}
IconData _getStatusIcon(ConnectionStatus status) {
switch (status) {
case ConnectionStatus.connected:
return Icons.check_circle;
case ConnectionStatus.connecting:
return Icons.sync;
case ConnectionStatus.error:
return Icons.error;
case ConnectionStatus.disconnected:
return Icons.cancel;
}
}
String _getStatusText(ConnectionStatus status) {
switch (status) {
case ConnectionStatus.connected:
return 'Connected';
case ConnectionStatus.connecting:
return 'Connecting...';
case ConnectionStatus.error:
return 'Error';
case ConnectionStatus.disconnected:
return 'Disconnected';
}
}
}

View File

@@ -1,7 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:file_picker/file_picker.dart';
import 'dart:html' as html;
import '../../../domain/entities/server.dart';
import '../../../data/services/excel_import_service.dart';
import '../../../data/data_sources/local/server_local_data_source.dart';
import '../../../injection.dart' as di;
import '../../blocs/auth/auth_bloc.dart';
import '../../blocs/auth/auth_event.dart';
import '../../blocs/auth/auth_state.dart';
@@ -140,6 +145,147 @@ class _ServersManagementScreenState extends State<ServersManagementScreen> {
);
}
Future<void> _importFromExcel() async {
print('[Import] Function called');
try {
print('[Import] Creating ExcelImportService...');
// Create ExcelImportService with ServerLocalDataSource from DI
final localDataSource = di.sl<ServerLocalDataSource>();
final excelImportService = ExcelImportService(localDataSource: localDataSource);
print('[Import] ExcelImportService created');
print('[Import] Opening file picker...');
// Pick Excel file
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['xlsx'],
withData: true, // Important for web - loads file data
);
print('[Import] File picker returned');
if (result == null || result.files.isEmpty) {
return; // User cancelled
}
final file = result.files.first;
if (file.bytes == null) {
throw Exception('Could not read file data');
}
// Show loading dialog
if (!mounted) return;
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => const Center(
child: Card(
child: Padding(
padding: EdgeInsets.all(24.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Importing servers from Excel...'),
],
),
),
),
),
);
// Parse Excel file via backend API
print('[Import] Calling backend API...');
final importedServers = await excelImportService.importServersFromExcel(
file.bytes!,
file.name,
);
print('[Import] Received ${importedServers.length} servers from API');
// Get existing servers
final bloc = context.read<ServerBloc>();
final state = bloc.state;
final existingServers = state is ServerLoaded ? state.servers : <Server>[];
print('[Import] Found ${existingServers.length} existing servers');
// Merge: only add new servers
final newServers = excelImportService.mergeServers(
existing: existingServers,
imported: importedServers,
);
print('[Import] ${newServers.length} new servers to add');
// Check if there are new servers
print('[Import] Import summary: ${importedServers.length} total, ${newServers.length} new');
if (newServers.isEmpty) {
print('[Import] No new servers to add');
// Close loading dialog
if (mounted) {
try {
Navigator.of(context).pop();
} catch (e) {
print('[Import] Error closing dialog: $e');
}
}
return;
}
print('[Import] Proceeding with import of ${newServers.length} servers');
// Save servers directly to storage, bypassing the bloc to avoid triggering rebuilds
print('[Import] Saving ${newServers.length} servers directly to storage...');
try {
await excelImportService.saveImportedServersToStorage(newServers);
print('[Import] All servers saved to storage as unsaved changes');
} catch (e) {
print('[Import] ERROR saving servers to storage: $e');
throw Exception('Failed to save servers: $e');
}
// Import complete - reload page
print('[Import] Successfully imported ${newServers.length} servers');
print('[Import] Servers added as unsaved changes. Use Sync button to upload to GeViServer.');
// Save redirect path so we return to servers page after reload
html.window.localStorage['post_import_redirect'] = '/servers';
// Brief delay to ensure Hive writes complete
await Future.delayed(const Duration(milliseconds: 300));
print('[Import] Reloading page (auth tokens now persist in localStorage)...');
// Reload page WITHOUT closing dialog to avoid crash
// Auth tokens are now stored in localStorage so you'll stay logged in!
html.window.location.reload();
} catch (e, stackTrace) {
print('[Import] ERROR: $e');
print('[Import] Stack trace: $stackTrace');
// Close loading dialog if open
if (mounted) {
try {
Navigator.of(context, rootNavigator: true).pop();
} catch (_) {
// Dialog might already be closed
}
}
// Show error
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Import failed: $e'),
backgroundColor: Colors.red,
duration: const Duration(seconds: 5),
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -194,15 +340,56 @@ class _ServersManagementScreenState extends State<ServersManagementScreen> {
);
},
),
// Download/refresh button
Builder(
builder: (context) => IconButton(
icon: const Icon(Icons.cloud_download),
onPressed: () {
context.read<ServerBloc>().add(const DownloadServersEvent());
},
tooltip: 'Download latest from server',
),
// Download/refresh button with confirmation if there are unsaved changes
BlocBuilder<ServerBloc, ServerState>(
builder: (context, state) {
final dirtyCount = state is ServerLoaded ? state.dirtyCount : 0;
return IconButton(
icon: const Icon(Icons.cloud_download),
onPressed: () async {
// Show confirmation if there are unsaved changes
if (dirtyCount > 0) {
final confirmed = await showDialog<bool>(
context: context,
builder: (dialogContext) => AlertDialog(
title: const Text('Discard Changes?'),
content: Text(
'You have $dirtyCount unsaved change${dirtyCount != 1 ? 's' : ''}. '
'Downloading from server will discard all local changes.\n\n'
'Do you want to continue?'
),
actions: [
TextButton(
onPressed: () => Navigator.of(dialogContext).pop(false),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.of(dialogContext).pop(true),
style: FilledButton.styleFrom(
backgroundColor: Colors.orange,
),
child: const Text('Discard & Download'),
),
],
),
);
if (confirmed == true) {
if (context.mounted) {
context.read<ServerBloc>().add(const DownloadServersEvent());
}
}
} else {
// No unsaved changes, download directly
context.read<ServerBloc>().add(const DownloadServersEvent());
}
},
tooltip: dirtyCount > 0
? 'Discard $dirtyCount change${dirtyCount != 1 ? 's' : ''} & download from server'
: 'Download latest from server',
);
},
),
const SizedBox(width: 8),
BlocBuilder<AuthBloc, AuthState>(
@@ -278,6 +465,23 @@ class _ServersManagementScreenState extends State<ServersManagementScreen> {
),
),
const SizedBox(width: 16),
OutlinedButton.icon(
onPressed: () {
print('[Import] Button clicked');
try {
_importFromExcel();
} catch (e, stackTrace) {
print('[Import] Button handler error: $e');
print('[Import] Button handler stack: $stackTrace');
}
},
icon: const Icon(Icons.upload_file, size: 18),
label: const Text('Import Excel'),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
),
),
const SizedBox(width: 12),
ElevatedButton.icon(
onPressed: () {
_showAddServerDialog(context);