import 'package:star_lock/talk/startChart/constant/payload_type_constant.dart'; import 'package:star_lock/talk/startChart/constant/protocol_flag_constant.dart'; import 'package:star_lock/talk/startChart/entity/scp_message.dart'; class MessageCommand { /// 客户端去中继上线命令 static List goOnlineRelay() { String serializedBytesString = ScpMessage( ProtocolFlag: ProtocolFlagConstant.scp01, MessageType: PayloadTypeConstant.goOnline, MessageId: 1, SpTotal: 0, SpIndex: 0, ).serialize(); return _hexToBytes(serializedBytesString); } // 回声测试消息 static List echoMessage({ required String ToPeerId, required String FromPeerId, }) { ScpMessage message = ScpMessage( ProtocolFlag: ProtocolFlagConstant.scp01, MessageType: PayloadTypeConstant.echoTest, MessageId: 1, SpTotal: 0, SpIndex: 0, FromPeerId: FromPeerId, ToPeerId: ToPeerId, Payload: 'hello', PayloadCRC: 55230, PayloadLength: 5, PayloadType: 1, ); String serializedBytesString = message.serialize(); return _hexToBytes(serializedBytesString); } // 心跳消息 static List heartbeatMessage() { ScpMessage message = ScpMessage( ProtocolFlag: ProtocolFlagConstant.scp01, MessageType: PayloadTypeConstant.heartbeat, MessageId: 1, SpTotal: 0, SpIndex: 0, // FromPeerId: FromPeerId, // ToPeerId: ToPeerId, // Payload: 'hello', // PayloadCRC: 55230, // PayloadLength: 5, // PayloadType: 1, ); String serializedBytesString = message.serialize(); return _hexToBytes(serializedBytesString); } // 辅助方法:将16进制字符串转换为字节列表 static List _hexToBytes(String hex) { final bytes = []; for (int i = 0; i < hex.length; i += 2) { bytes.add(int.parse(hex.substring(i, i + 2), radix: 16)); } return bytes; } }