Implement websocket

This commit is contained in:
2026-01-19 12:09:57 +01:00
parent 198aac5054
commit ac5bd1a211
6 changed files with 506 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/device.dart';
@@ -5,6 +6,7 @@ import '../models/static_talkgroup.dart';
import '../models/device_profile.dart';
import '../services/authentication_manager.dart';
import '../services/brandmeister_client.dart';
import '../services/brandmeister_websocket_client.dart';
import 'link_talkgroup_view.dart';
class DeviceDetailView extends StatefulWidget {
@@ -24,10 +26,68 @@ class _DeviceDetailViewState extends State<DeviceDetailView> {
bool _isLoadingDeviceDetails = false;
String? _errorMessage;
BrandmeisterWebSocketClient? _wsClient;
StreamSubscription<Map<String, dynamic>>? _wsSubscription;
int? _autoStaticTalkgroup;
@override
void initState() {
super.initState();
_loadData();
_connectWebSocket();
}
@override
void dispose() {
_wsSubscription?.cancel();
_wsClient?.dispose();
super.dispose();
}
Future<void> _connectWebSocket() async {
_wsClient = BrandmeisterWebSocketClient();
await _wsClient!.connect();
// Wait for the open packet (0{...}) to be received
await _wsClient!.ready;
// Request device status after connection is ready
_wsClient!.getDeviceStatus(widget.device.id.toString());
// Listen for device status updates
_wsSubscription = _wsClient!.messageStream.listen((message) {
if (message['event'] == 'deviceStatus' && message['data'] is List) {
_handleDeviceStatus(message['data'] as List);
}
});
}
void _handleDeviceStatus(List deviceStatusList) {
if (deviceStatusList.isEmpty) return;
for (final status in deviceStatusList) {
if (status is Map<String, dynamic>) {
final number = status['number'];
if (number == widget.device.id) {
final values = status['values'] as List?;
if (values != null && values.length >= 20) {
final autoStaticValue = values[19];
if (autoStaticValue != null &&
autoStaticValue != 0 &&
autoStaticValue != 4000) {
setState(() {
_autoStaticTalkgroup = autoStaticValue as int;
});
} else {
setState(() {
_autoStaticTalkgroup = null;
});
}
}
break;
}
}
}
}
Future<void> _loadData() async {
@@ -103,6 +163,35 @@ class _DeviceDetailViewState extends State<DeviceDetailView> {
}
}
Future<void> _unlinkAutoStatic() async {
try {
final authManager = context.read<AuthenticationManager>();
await authManager.dropAutoStaticGroup(widget.device.id);
setState(() {
_autoStaticTalkgroup = null;
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Auto-static talkgroup unlinked successfully')),
);
}
} on BrandmeisterError catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to unlink auto-static: ${e.message}')),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: ${e.toString()}')),
);
}
}
}
void _showLinkTalkgroupSheet() {
showModalBottomSheet(
context: context,
@@ -242,7 +331,8 @@ class _DeviceDetailViewState extends State<DeviceDetailView> {
(_deviceProfile?.staticSubscriptions.isNotEmpty ?? false) ||
(_deviceProfile?.dynamicSubscriptions.isNotEmpty ?? false) ||
(_deviceProfile?.timedSubscriptions.isNotEmpty ?? false) ||
(_deviceProfile?.blockedGroups.isNotEmpty ?? false);
(_deviceProfile?.blockedGroups.isNotEmpty ?? false) ||
_autoStaticTalkgroup != null;
return Container(
padding: const EdgeInsets.all(16),
@@ -299,6 +389,11 @@ class _DeviceDetailViewState extends State<DeviceDetailView> {
),
const SizedBox(height: 16),
],
// Auto-Static Talkgroup (from WebSocket)
if (_autoStaticTalkgroup != null) ...[
_buildAutoStaticSection(),
const SizedBox(height: 16),
],
// Dynamic Subscriptions (Autostatic)
if (_deviceProfile?.dynamicSubscriptions.isNotEmpty ?? false) ...[
_buildTalkgroupCategory(
@@ -337,6 +432,91 @@ class _DeviceDetailViewState extends State<DeviceDetailView> {
);
}
Widget _buildAutoStaticSection() {
const color = Colors.purple;
final talkgroupName = _allTalkgroups[_autoStaticTalkgroup.toString()] ?? 'Unknown';
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(Icons.flash_on, size: 20, color: color),
const SizedBox(width: 8),
Text(
'Auto Static',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: color,
),
),
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: color.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
),
child: const Text(
'1',
style: TextStyle(
color: color,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
),
],
),
const SizedBox(height: 8),
Card(
margin: const EdgeInsets.only(bottom: 8),
child: ListTile(
leading: CircleAvatar(
backgroundColor: color.withValues(alpha: 0.2),
child: const Icon(
Icons.flash_on,
size: 20,
color: color,
),
),
title: Text(talkgroupName),
subtitle: Text('ID: $_autoStaticTalkgroup'),
trailing: IconButton(
icon: const Icon(Icons.delete_outline),
color: Colors.red,
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Unlink Auto-Static Talkgroup'),
content: Text(
'Are you sure you want to unlink auto-static talkgroup $_autoStaticTalkgroup?',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
_unlinkAutoStatic();
},
style: TextButton.styleFrom(foregroundColor: Colors.red),
child: const Text('Unlink'),
),
],
),
);
},
),
),
),
],
);
}
Widget _buildTalkgroupCategory(
String title,
List<StaticTalkgroup> talkgroups,