77 lines
2.2 KiB
Dart
77 lines
2.2 KiB
Dart
import 'package:star_lock/talk/startChart/constant/message_type_constant.dart';
|
||
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<int> goOnlineRelay() {
|
||
String serializedBytesString = ScpMessage(
|
||
ProtocolFlag: ProtocolFlagConstant.scp01,
|
||
MessageType: MessageTypeConstant.Req,
|
||
MessageId: 1,
|
||
SpTotal: 0,
|
||
SpIndex: 0,
|
||
FromPeerId: 'ToPeerId',
|
||
ToPeerId: 'ToPeerId',
|
||
Payload: 'hello',
|
||
PayloadCRC: 55230,
|
||
PayloadLength: 5,
|
||
PayloadType: PayloadTypeConstant.goOnline,
|
||
).serialize();
|
||
return _hexToBytes(serializedBytesString);
|
||
}
|
||
|
||
// 回声测试消息
|
||
static List<int> echoMessage({
|
||
required String ToPeerId,
|
||
required String FromPeerId,
|
||
}) {
|
||
ScpMessage message = ScpMessage(
|
||
ProtocolFlag: ProtocolFlagConstant.scp01,
|
||
MessageType: MessageTypeConstant.Req,
|
||
MessageId: 1,
|
||
SpTotal: 0,
|
||
SpIndex: 0,
|
||
FromPeerId: FromPeerId,
|
||
ToPeerId: ToPeerId,
|
||
Payload: 'hello',
|
||
PayloadCRC: 55230,
|
||
PayloadLength: 5,
|
||
PayloadType: PayloadTypeConstant.echoTest,
|
||
);
|
||
|
||
String serializedBytesString = message.serialize();
|
||
return _hexToBytes(serializedBytesString);
|
||
}
|
||
|
||
// 心跳消息
|
||
static List<int> heartbeatMessage() {
|
||
ScpMessage message = ScpMessage(
|
||
ProtocolFlag: ProtocolFlagConstant.scp01,
|
||
MessageType: MessageTypeConstant.Req,
|
||
MessageId: 1,
|
||
SpTotal: 0,
|
||
SpIndex: 0,
|
||
FromPeerId: 'FromPeerId',
|
||
ToPeerId: 'ToPeerId',
|
||
Payload: 'hello',
|
||
PayloadCRC: 55230,
|
||
PayloadLength: 5,
|
||
PayloadType: PayloadTypeConstant.heartbeat,
|
||
);
|
||
|
||
String serializedBytesString = message.serialize();
|
||
return _hexToBytes(serializedBytesString);
|
||
}
|
||
|
||
// 辅助方法:将16进制字符串转换为字节列表
|
||
static List<int> _hexToBytes(String hex) {
|
||
final bytes = <int>[];
|
||
for (int i = 0; i < hex.length; i += 2) {
|
||
bytes.add(int.parse(hex.substring(i, i + 2), radix: 16));
|
||
}
|
||
return bytes;
|
||
}
|
||
}
|