退出登录+机器状态+切换设备+历史作业记录+设备信息

This commit is contained in:
2026-02-25 13:41:23 +08:00
parent 3c48a96d26
commit d81d0c04ea
32 changed files with 1940 additions and 142 deletions

View File

@@ -13,6 +13,11 @@ class TcpClient {
final _controller = StreamController<RawPacket>.broadcast();
Stream<RawPacket> get packetStream => _controller.stream;
// 新增:心跳定时器
Timer? _heartbeatTimer;
// 通过参数配置,不硬编码
Future<void> connect({required String host, required int port}) async {
try {
@@ -22,10 +27,14 @@ class TcpClient {
timeout: const Duration(seconds: 5),
);
_socket!.listen(
(data) {
_socket!.listen((data) {
var packets = _decoder.decode(data);
for (var packet in packets) {
// 🔥 关键修改:若收到服务端心跳(cmd == 0xFF),立即回复一个心跳包
if (packet.command == 0xFF) {
debugPrint('收到服务端心跳,自动回复...');
sendHeartbeat(); // 回复 AB AA FF AA AB
}
_controller.add(packet);
}
},
@@ -57,10 +66,47 @@ class TcpClient {
_socket!.add(builder.takeBytes());
}
void sendHeartbeat() {
if (_socket == null) return;
final builder = BytesBuilder()
..addByte(0xAB)
..addByte(0xAA)
..addByte(0xFF)
..addByte(0xAA)
..addByte(0xAB);
_socket!.add(builder.takeBytes());
}
// 新增:启动心跳(每 4 秒发送一次 0xFF 指令)
void startHeartbeat({Duration interval = const Duration(seconds: 4)}) {
if (_heartbeatTimer != null) return; // 防止重复启动
_heartbeatTimer = Timer.periodic(interval, (_) {
// 发送心跳帧:AB AA FF 00 00 AA AB(与 sendRaw 一致)
//sendRaw(0xFF, []);
sendHeartbeat();
});
debugPrint("TCP Connected");
}
// 新增:停止心跳
void stopHeartbeat() {
_heartbeatTimer?.cancel();
_heartbeatTimer = null;
}
void disconnect() {
stopHeartbeat(); // 先停心跳
_socket?.destroy();
_socket = null;
_controller.close();
debugPrint("TCP Disconnected");
}
}