安卓权限添加

This commit is contained in:
2026-10-20 16:45:51 +08:00
parent 68984aa7d2
commit 178cd2533c
9 changed files with 340 additions and 54 deletions

View File

@@ -39,6 +39,8 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
// 🔥 MQTT机器状态订阅(车辆实时+定位,替代TCP 0x02)
StreamSubscription? _mqttVehicleSubscription;
StreamSubscription? _mqttLocationSubscription;
// 🔥 主机实时消息订阅(device/{sn}/host/realTime/message,新增,纯附加)
StreamSubscription? _mqttHostSubscription;
// 🔥 节流相关:500ms节流控制0x02数据推送频率
Timer? _throttleTimer;
@@ -301,6 +303,48 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
}
});
// 🔥 主机实时消息(device/{sn}/host/realTime/message):新值到达即 copyWith 覆盖,复用 500ms 节流 emit
_mqttHostSubscription ??= _mowerRealtimeRepo.hostRealtimeStream.listen((
host,
) {
final base = _cachedStatus ?? RunningStatusEntity();
_cachedStatus = base.copyWith(
voltage: host.batteryVoltage,
battery: host.batteryPercentage.toString(),
leftCurrent: host.leftMotorCurrent,
rightCurrent: host.rightMotorCurrent,
leftMotorTemp: host.leftMotorTemperature,
rightMotorTemp: host.rightMotorTemperature,
chipTemp: host.mainTemperature,
pitch: host.pitch,
roll: host.roll,
leftTargetSpeed: host.leftTargetRpm,
rightTargetSpeed: host.rightTargetRpm,
leftMeasureSpeed: host.leftMeasuredRpm,
rightMeasureSpeed: host.rightMeasuredRpm,
controlMode:
host.controlMode.isNotEmpty ? host.controlMode : base.controlMode,
workingArea: host.totalWorkArea.toString(),
// 🔥 供全功能模式(pv_control)展示:累计作业里程 + host 已推数据标记
totalWorkDistance: host.totalWorkDistance,
hostDataReceived: true,
);
// 🔥 host 无坐标:GPS 缺省时用当前经纬度占位,保证能即时刷新(不覆盖 location 已写入的真坐标)
_cachedGps ??= GPSEntity(_cachedStatus!.latitude, _cachedStatus!.longitude);
debugPrint(
'${LogTime.now()} 🛰️ [MQTT-主机实时] 已合并 → 电量=${host.batteryPercentage}% 电压=${host.batteryVoltage}V 控制模式=${host.controlMode}',
);
// 500ms 节流:与 location/车辆实时 发射链路一致
if (_throttleTimer == null || !_throttleTimer!.isActive) {
_throttleTimer = Timer(_throttleDuration, () {
_emitCachedStatus();
});
}
});
debugPrint('${LogTime.now()} ✅ [DeviceStatusBloc] MQTT机器状态监听已启动: $deviceSn');
_logger.log('✅ [DeviceStatusBloc] MQTT机器状态监听已启动: $deviceSn');
}
@@ -434,10 +478,13 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
}
// 🔥 节流发射:500ms到期后发射缓存的最新数据
// 🔥 放宽:允许仅有机器状态(host 先到、GPS 未到)时也能刷新 UI
void _emitCachedStatus() {
if (_cachedStatus != null && _cachedGps != null && !isClosed) {
debugPrint('📤 [节流发射] 电压=${_cachedStatus!.voltage}V 电量=${_cachedStatus!.battery}% 经纬度=(${_cachedGps!.latitude}, ${_cachedGps!.longitude}) yaw=${_cachedStatus!.yaw} | ${DateTime.now().toString().substring(11, 19)}');
emit(DeviceStatusUpdated(_cachedStatus!, _cachedGps!));
if (_cachedStatus != null && !isClosed) {
final gps = _cachedGps ??
GPSEntity(_cachedStatus!.latitude, _cachedStatus!.longitude);
debugPrint('📤 [节流发射] 电压=${_cachedStatus!.voltage}V 电量=${_cachedStatus!.battery}% 经纬度=(${gps.latitude}, ${gps.longitude}) yaw=${_cachedStatus!.yaw} 控制模式=${_cachedStatus!.controlMode} | ${DateTime.now().toString().substring(11, 19)}');
emit(DeviceStatusUpdated(_cachedStatus!, gps));
}
}
@@ -569,6 +616,8 @@ class DeviceStatusBloc extends Bloc<DeviceStatusEvent, DeviceStatusState> {
_mqttVehicleSubscription = null;
_mqttLocationSubscription?.cancel();
_mqttLocationSubscription = null;
_mqttHostSubscription?.cancel();
_mqttHostSubscription = null;
// 🔥 清理节流timer和缓存
_throttleTimer?.cancel();