81 lines
2.2 KiB
Dart
81 lines
2.2 KiB
Dart
|
||
import 'dart:convert';
|
||
import '../io_tool/io_tool.dart';
|
||
import '../sm4Encipher/sm4.dart';
|
||
import '../io_reply.dart';
|
||
import '../io_sender.dart';
|
||
import '../io_type.dart';
|
||
import 'package:crypto/crypto.dart' as crypto;
|
||
|
||
//TODO:自动落锁开关
|
||
class AutomaticPadlockCommand extends SenderProtocol {
|
||
String? lockID;
|
||
String? userID;
|
||
int? autoLockFlag;
|
||
List<int>? token;
|
||
int? needAuthor;
|
||
List<int>? signKey;
|
||
List<int>? privateKey;
|
||
AutomaticPadlockCommand({
|
||
this.lockID,
|
||
this.userID,
|
||
this.autoLockFlag,
|
||
this.token,
|
||
this.needAuthor,
|
||
this.signKey,
|
||
this.privateKey
|
||
}) : super(CommandType.generalExtendedCommond);
|
||
|
||
@override
|
||
List<int> messageDetail() {
|
||
List<int> data = [];
|
||
List<int> subData = [];
|
||
List<int> ebcData = [];
|
||
|
||
// 指令类型
|
||
int type = commandType!.typeValue;
|
||
double typeDouble = type / 256;
|
||
int type1 = typeDouble.toInt();
|
||
int type2 = type % 256;
|
||
data.add(type1);
|
||
data.add(type2);
|
||
|
||
// 子命令类型
|
||
data.add(4);
|
||
|
||
//lockID 40
|
||
int lockIDLength = utf8.encode(lockID!).length;
|
||
subData.addAll(utf8.encode(lockID!));
|
||
subData = getFixedLengthList(subData, 40 - lockIDLength);
|
||
|
||
//userID 要接受钥匙的用户的useid 20
|
||
int userIDLength = utf8.encode(userID!).length;
|
||
print("openDoorUserId:${utf8.encode(userID!)} utf8.encode(userID!).length:${utf8.encode(userID!).length}");
|
||
subData.addAll(utf8.encode(userID!));
|
||
subData = getFixedLengthList(subData, 20 - userIDLength);
|
||
|
||
// autoLockFlag 0:不自动落锁,1:自动落锁
|
||
subData.add(autoLockFlag!);
|
||
|
||
data.add(subData.length);
|
||
data.addAll(subData);
|
||
|
||
print("data:$data data.length:${data.length} (data.length % 16):${(data.length % 16)}");
|
||
if ((data.length % 16) != 0) {
|
||
int add = (16 - data.length % 16);
|
||
for (int i = 0; i < add; i++) {
|
||
data.add(0);
|
||
}
|
||
}
|
||
print("${commandType!.typeName} SM4Data:$data");
|
||
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
|
||
return ebcData;
|
||
}
|
||
}
|
||
|
||
class AutomaticPadlockReply extends Reply {
|
||
AutomaticPadlockReply.parseData(CommandType commandType, List<int> dataDetail)
|
||
: super.parseData(commandType, dataDetail) {
|
||
data = dataDetail;
|
||
}
|
||
} |