app-starlock/lib/talk/startChart/command/message_command.dart

118 lines
3.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:crc32_checksum/crc32_checksum.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';
import 'package:star_lock/talk/startChart/proto/gateway_reset.pb.dart';
class MessageCommand {
/// 客户端去中继上线命令
static List<int> goOnlineRelay({
required String FromPeerId,
required String ToPeerId,
}) {
String serializedBytesString = 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.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({
required String FromPeerId,
required String ToPeerId,
}) {
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);
}
// 网关初始化
static List<int> gatewayReset({
required int gatewayId,
required String FromPeerId,
required String ToPeerId,
required int time,
}) {
final gatewayResetReq = GatewayResetReq(
iD: gatewayId,
time: time,
);
final payloadLength = gatewayResetReq.writeToBuffer().length;
ScpMessage message = ScpMessage(
ProtocolFlag: ProtocolFlagConstant.scp01,
MessageType: MessageTypeConstant.Req,
MessageId: 1,
SpTotal: 0,
SpIndex: 0,
FromPeerId: FromPeerId,
ToPeerId: ToPeerId,
Payload: gatewayResetReq,
PayloadCRC: 55230,
PayloadLength: payloadLength,
PayloadType: PayloadTypeConstant.gatewayReset,
);
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;
}
static int calculationCrc(payload) {
var checkSumResult = Crc32.calculate(payload);
return checkSumResult;
}
}