集成双 MQTT 客户端实现设备实时数据监控(未测试)
1.集成两个独立的 MQTT 客户端,支持 WebSocket 和 TCP 协议 2.实现无人机/机场 OSD 实时监控(ws://1.95.137.212:8083/mqtt) 3.实现任务状态消息订阅(tcp://1.95.137.212:59020) 4.采用清洁架构设计,分层清晰(Domain → Data → Presentation)
This commit is contained in:
18
lib/core/network/mqtt/domain/entities/drone_osd_entity.dart
Normal file
18
lib/core/network/mqtt/domain/entities/drone_osd_entity.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class DroneOsdEntity extends Equatable {
|
||||
final Map<String, dynamic> rawData;
|
||||
|
||||
const DroneOsdEntity({required this.rawData});
|
||||
|
||||
factory DroneOsdEntity.fromJson(Map<String, dynamic> json) {
|
||||
return DroneOsdEntity(rawData: json);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return rawData;
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [rawData];
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class RealTimeMessageEntity extends Equatable {
|
||||
final List<DeviceDataPoint> data;
|
||||
final String type;
|
||||
|
||||
const RealTimeMessageEntity({
|
||||
required this.data,
|
||||
required this.type,
|
||||
});
|
||||
|
||||
factory RealTimeMessageEntity.fromJson(Map<String, dynamic> json) {
|
||||
final dataList = json['data'] as List<dynamic>? ?? [];
|
||||
return RealTimeMessageEntity(
|
||||
data: dataList.map((item) => DeviceDataPoint.fromJson(item)).toList(),
|
||||
type: json['type'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
double? getValueByName(String name) {
|
||||
final point = data.firstWhere(
|
||||
(p) => p.name == name,
|
||||
orElse: () => DeviceDataPoint(name: '', value: '', unit: ''),
|
||||
);
|
||||
if (point.value.isEmpty) return null;
|
||||
return double.tryParse(point.value);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'data': data.map((e) => e.toJson()).toList(),
|
||||
'type': type,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [data, type];
|
||||
}
|
||||
|
||||
class DeviceDataPoint extends Equatable {
|
||||
final String name;
|
||||
final String value;
|
||||
final String unit;
|
||||
|
||||
const DeviceDataPoint({
|
||||
required this.name,
|
||||
required this.value,
|
||||
required this.unit,
|
||||
});
|
||||
|
||||
factory DeviceDataPoint.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceDataPoint(
|
||||
name: json['name'] as String? ?? '',
|
||||
value: json['value'] as String? ?? '',
|
||||
unit: json['unit'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'name': name,
|
||||
'value': value,
|
||||
'unit': unit,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [name, value, unit];
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class TaskArriveEntity extends Equatable {
|
||||
final String type;
|
||||
final String deviceId;
|
||||
final int taskId;
|
||||
final dynamic status;
|
||||
final ArriveLocation entity;
|
||||
|
||||
const TaskArriveEntity({
|
||||
required this.type,
|
||||
required this.deviceId,
|
||||
required this.taskId,
|
||||
this.status,
|
||||
required this.entity,
|
||||
});
|
||||
|
||||
factory TaskArriveEntity.fromJson(Map<String, dynamic> json) {
|
||||
return TaskArriveEntity(
|
||||
type: json['type'] as String? ?? '',
|
||||
deviceId: json['deviceId'] as String? ?? '',
|
||||
taskId: json['taskId'] as int? ?? 0,
|
||||
status: json['status'],
|
||||
entity: ArriveLocation.fromJson(json['entity'] as Map<String, dynamic>? ?? {}),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'type': type,
|
||||
'deviceId': deviceId,
|
||||
'taskId': taskId,
|
||||
'status': status,
|
||||
'entity': entity.toJson(),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [type, deviceId, taskId, status, entity];
|
||||
}
|
||||
|
||||
class ArriveLocation extends Equatable {
|
||||
final double lat;
|
||||
final double lng;
|
||||
|
||||
const ArriveLocation({
|
||||
required this.lat,
|
||||
required this.lng,
|
||||
});
|
||||
|
||||
factory ArriveLocation.fromJson(Map<String, dynamic> json) {
|
||||
return ArriveLocation(
|
||||
lat: (json['lat'] as num?)?.toDouble() ?? 0.0,
|
||||
lng: (json['lng'] as num?)?.toDouble() ?? 0.0,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'lat': lat,
|
||||
'lng': lng,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [lat, lng];
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class TaskStatusEntity extends Equatable {
|
||||
final String type;
|
||||
final String deviceId;
|
||||
final int taskId;
|
||||
final String? status;
|
||||
final Map<String, dynamic>? extraData;
|
||||
|
||||
const TaskStatusEntity({
|
||||
required this.type,
|
||||
required this.deviceId,
|
||||
required this.taskId,
|
||||
this.status,
|
||||
this.extraData,
|
||||
});
|
||||
|
||||
factory TaskStatusEntity.fromJson(Map<String, dynamic> json) {
|
||||
return TaskStatusEntity(
|
||||
type: json['type'] as String? ?? '',
|
||||
deviceId: json['deviceId'] as String? ?? '',
|
||||
taskId: json['taskId'] as int? ?? 0,
|
||||
status: json['status'] as String?,
|
||||
extraData: json,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'type': type,
|
||||
'deviceId': deviceId,
|
||||
'taskId': taskId,
|
||||
'status': status,
|
||||
if (extraData != null) ...extraData!,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [type, deviceId, taskId, status, extraData];
|
||||
}
|
||||
14
lib/core/network/mqtt/domain/interfaces/mqtt_client.dart
Normal file
14
lib/core/network/mqtt/domain/interfaces/mqtt_client.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'dart:async';
|
||||
import '../models/mqtt_config.dart';
|
||||
import '../models/mqtt_message.dart';
|
||||
|
||||
abstract class MqttClient {
|
||||
Stream<MqttMessage>? get messageStream;
|
||||
|
||||
Future<void> connect(MqttConfig config);
|
||||
Future<void> disconnect();
|
||||
Future<void> subscribe(String topic);
|
||||
Future<void> unsubscribe(String topic);
|
||||
Future<void> publish(String topic, String message);
|
||||
bool get isConnected;
|
||||
}
|
||||
90
lib/core/network/mqtt/domain/models/mqtt_config.dart
Normal file
90
lib/core/network/mqtt/domain/models/mqtt_config.dart
Normal file
@@ -0,0 +1,90 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
enum MqttProtocol { tcp, websocket, wss }
|
||||
|
||||
class MqttConfig extends Equatable {
|
||||
final String host;
|
||||
final int port;
|
||||
final String username;
|
||||
final String password;
|
||||
final MqttProtocol protocol;
|
||||
final String clientId;
|
||||
final bool cleanSession;
|
||||
final int keepAlivePeriod;
|
||||
final int reconnectDelayMs;
|
||||
|
||||
const MqttConfig({
|
||||
required this.host,
|
||||
required this.port,
|
||||
required this.username,
|
||||
required this.password,
|
||||
required this.protocol,
|
||||
required this.clientId,
|
||||
this.cleanSession = true,
|
||||
this.keepAlivePeriod = 60,
|
||||
this.reconnectDelayMs = 3000,
|
||||
});
|
||||
|
||||
factory MqttConfig.droneOsd() {
|
||||
return const MqttConfig(
|
||||
host: '1.95.137.212',
|
||||
port: 8083,
|
||||
username: 'maibu',
|
||||
password: 'jsmbzn520',
|
||||
protocol: MqttProtocol.websocket,
|
||||
clientId: 'drone_osd_client',
|
||||
cleanSession: true,
|
||||
reconnectDelayMs: 3000,
|
||||
);
|
||||
}
|
||||
|
||||
factory MqttConfig.taskMessage() {
|
||||
return const MqttConfig(
|
||||
host: '1.95.137.212',
|
||||
port: 59020,
|
||||
username: 'maibu',
|
||||
password: 'jsmbzn520',
|
||||
protocol: MqttProtocol.tcp,
|
||||
clientId: 'task_message_client',
|
||||
cleanSession: true,
|
||||
reconnectDelayMs: 3000,
|
||||
);
|
||||
}
|
||||
|
||||
MqttConfig copyWith({
|
||||
String? host,
|
||||
int? port,
|
||||
String? username,
|
||||
String? password,
|
||||
MqttProtocol? protocol,
|
||||
String? clientId,
|
||||
bool? cleanSession,
|
||||
int? keepAlivePeriod,
|
||||
int? reconnectDelayMs,
|
||||
}) {
|
||||
return MqttConfig(
|
||||
host: host ?? this.host,
|
||||
port: port ?? this.port,
|
||||
username: username ?? this.username,
|
||||
password: password ?? this.password,
|
||||
protocol: protocol ?? this.protocol,
|
||||
clientId: clientId ?? this.clientId,
|
||||
cleanSession: cleanSession ?? this.cleanSession,
|
||||
keepAlivePeriod: keepAlivePeriod ?? this.keepAlivePeriod,
|
||||
reconnectDelayMs: reconnectDelayMs ?? this.reconnectDelayMs,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
host,
|
||||
port,
|
||||
username,
|
||||
password,
|
||||
protocol,
|
||||
clientId,
|
||||
cleanSession,
|
||||
keepAlivePeriod,
|
||||
reconnectDelayMs,
|
||||
];
|
||||
}
|
||||
24
lib/core/network/mqtt/domain/models/mqtt_message.dart
Normal file
24
lib/core/network/mqtt/domain/models/mqtt_message.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class MqttMessage extends Equatable {
|
||||
final String topic;
|
||||
final String payload;
|
||||
final DateTime timestamp;
|
||||
|
||||
MqttMessage({
|
||||
required this.topic,
|
||||
required this.payload,
|
||||
DateTime? timestamp,
|
||||
}) : timestamp = timestamp ?? DateTime.now();
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'topic': topic,
|
||||
'payload': payload,
|
||||
'timestamp': timestamp.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [topic, payload, timestamp];
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import 'dart:async';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
import '../../../../../core/error/failure.dart';
|
||||
import '../entities/drone_osd_entity.dart';
|
||||
|
||||
abstract class DroneOsdRepository {
|
||||
Stream<DroneOsdEntity> get droneOsdStream;
|
||||
Stream<DroneOsdEntity> get stationOsdStream;
|
||||
|
||||
Future<Either<Failure, void>> startListening({
|
||||
required String deviceSn,
|
||||
required String gatewaySn,
|
||||
});
|
||||
|
||||
Future<void> stopListening();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import 'dart:async';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
import '../../../../../core/error/failure.dart';
|
||||
import '../entities/task_arrive_entity.dart';
|
||||
import '../entities/task_status_entity.dart';
|
||||
import '../entities/real_time_message_entity.dart';
|
||||
|
||||
abstract class TaskMessageRepository {
|
||||
Stream<RealTimeMessageEntity> get realTimeMessageStream;
|
||||
Stream<TaskArriveEntity> get taskArriveStream;
|
||||
Stream<TaskStatusEntity> get taskStatusStream;
|
||||
|
||||
Future<Either<Failure, void>> startListening({required String deviceId});
|
||||
Future<void> stopListening();
|
||||
}
|
||||
Reference in New Issue
Block a user