feat(device): TX/RX 日志附加完整 JSON 字符串
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../models/device_state.dart';
|
||||
import '../services/device_log.dart';
|
||||
import '../services/device_message.dart';
|
||||
import '../services/device_message_service.dart';
|
||||
import 'serial_provider.dart';
|
||||
@@ -27,18 +28,30 @@ class DeviceInfoNotifier extends StateNotifier<DeviceState> {
|
||||
final light = _parseLight(data['light_status']);
|
||||
final door = _parseDoor(data['door_status']);
|
||||
final task = _parseTask(data['task_status']);
|
||||
final prev = state;
|
||||
final changed = prev.lightStatus != light ||
|
||||
prev.doorStatus != door ||
|
||||
prev.taskStatus != task;
|
||||
state = state.copyWith(
|
||||
lightStatus: light,
|
||||
doorStatus: door,
|
||||
taskStatus: task,
|
||||
lastInfoAt: DateTime.now(),
|
||||
);
|
||||
if (changed) {
|
||||
DeviceLog.info(
|
||||
'device_info updated: light=${light.name} door=${door.name} task=${task.name}',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 主动查询设备信息(发送 need_ack=true 的 create_task 之外的 device_info 请求,
|
||||
/// 具体取决于下位机协议约定;目前仅作占位)。
|
||||
Future<void> queryDeviceInfo() async {
|
||||
if (!_msgService.canSend) return;
|
||||
if (!_msgService.canSend) {
|
||||
DeviceLog.warn('queryDeviceInfo skipped: serial disconnected');
|
||||
return;
|
||||
}
|
||||
final id = _msgService.nextId();
|
||||
await _msgService.send(DeviceMessage.request(
|
||||
messageId: id,
|
||||
@@ -53,7 +66,10 @@ class DeviceInfoNotifier extends StateNotifier<DeviceState> {
|
||||
/// 主动切换后,下位机通常会通过 device_info 上报新的 light_status
|
||||
/// 覆盖本地状态。
|
||||
Future<bool> toggleLight() async {
|
||||
if (!_msgService.canSend) return false;
|
||||
if (!_msgService.canSend) {
|
||||
DeviceLog.warn('toggleLight skipped: serial disconnected');
|
||||
return false;
|
||||
}
|
||||
final next = state.lightStatus == DeviceLightStatus.on ? 'off' : 'on';
|
||||
final id = _msgService.nextId();
|
||||
final ok = await _msgService.send(DeviceMessage.request(
|
||||
@@ -67,13 +83,17 @@ class DeviceInfoNotifier extends StateNotifier<DeviceState> {
|
||||
state = state.copyWith(
|
||||
lightStatus: next == 'on' ? DeviceLightStatus.on : DeviceLightStatus.off,
|
||||
);
|
||||
DeviceLog.info('toggleLight optimistic: -> $next');
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
/// 显式设定灯光
|
||||
Future<bool> setLight(bool on) async {
|
||||
if (!_msgService.canSend) return false;
|
||||
if (!_msgService.canSend) {
|
||||
DeviceLog.warn('setLight skipped: serial disconnected');
|
||||
return false;
|
||||
}
|
||||
if (on && state.lightStatus == DeviceLightStatus.on) return true;
|
||||
if (!on && state.lightStatus == DeviceLightStatus.off) return true;
|
||||
final id = _msgService.nextId();
|
||||
@@ -87,6 +107,7 @@ class DeviceInfoNotifier extends StateNotifier<DeviceState> {
|
||||
state = state.copyWith(
|
||||
lightStatus: on ? DeviceLightStatus.on : DeviceLightStatus.off,
|
||||
);
|
||||
DeviceLog.info('setLight optimistic: -> ${on ? "on" : "off"}');
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user