2023-08-16 18:21:42 +08:00
|
|
|
|
|
|
|
|
|
|
import 'dart:convert';
|
2024-08-08 11:00:03 +08:00
|
|
|
|
|
|
|
|
|
|
import 'package:crypto/crypto.dart' as crypto;
|
2024-05-03 14:38:09 +08:00
|
|
|
|
import 'package:star_lock/tools/dateTool.dart';
|
2024-04-19 18:13:34 +08:00
|
|
|
|
|
2023-08-31 15:56:01 +08:00
|
|
|
|
import '../io_reply.dart';
|
|
|
|
|
|
import '../io_sender.dart';
|
2024-08-08 11:00:03 +08:00
|
|
|
|
import '../io_tool/io_tool.dart';
|
2023-08-31 15:56:01 +08:00
|
|
|
|
import '../io_type.dart';
|
2024-08-08 11:00:03 +08:00
|
|
|
|
import '../sm4Encipher/sm4.dart';
|
2023-08-16 18:21:42 +08:00
|
|
|
|
|
2024-10-15 14:24:35 +08:00
|
|
|
|
// 开门
|
2023-08-16 18:21:42 +08:00
|
|
|
|
class OpenLockCommand extends SenderProtocol {
|
|
|
|
|
|
OpenLockCommand({
|
2024-05-23 10:35:06 +08:00
|
|
|
|
this.lockID,
|
2023-08-16 18:21:42 +08:00
|
|
|
|
this.userID,
|
|
|
|
|
|
this.openMode,
|
|
|
|
|
|
this.openTime,
|
2023-11-24 14:23:36 +08:00
|
|
|
|
this.onlineToken,
|
2023-08-16 18:21:42 +08:00
|
|
|
|
this.token,
|
|
|
|
|
|
this.needAuthor,
|
|
|
|
|
|
this.signKey,
|
|
|
|
|
|
this.privateKey
|
|
|
|
|
|
}) : super(CommandType.openLock);
|
2024-10-15 14:24:35 +08:00
|
|
|
|
String? lockID;
|
|
|
|
|
|
String? userID;
|
|
|
|
|
|
int? openMode;
|
|
|
|
|
|
int? openTime;
|
|
|
|
|
|
String? onlineToken;
|
|
|
|
|
|
List<int>? token;
|
|
|
|
|
|
int? needAuthor;
|
|
|
|
|
|
List<int>? signKey;
|
|
|
|
|
|
List<int>? privateKey;
|
2023-08-16 18:21:42 +08:00
|
|
|
|
|
2024-05-03 14:38:09 +08:00
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
String toString() {
|
2024-05-23 10:35:06 +08:00
|
|
|
|
return 'OpenLockCommand{lockID: $lockID, userID: $userID, '
|
2024-05-03 14:38:09 +08:00
|
|
|
|
'openMode: $openMode, openTime: ${DateTool().dateIntToYMDHNString(openTime)}, '
|
|
|
|
|
|
'onlineToken: $onlineToken, token: $token, '
|
|
|
|
|
|
'needAuthor: $needAuthor, signKey: $signKey, privateKey: $privateKey}';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-16 18:21:42 +08:00
|
|
|
|
@override
|
|
|
|
|
|
List<int> messageDetail() {
|
2024-10-15 14:24:35 +08:00
|
|
|
|
List<int> data = <int>[];
|
|
|
|
|
|
List<int> ebcData = <int>[];
|
2023-08-16 18:21:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 指令类型
|
2024-08-08 11:00:03 +08:00
|
|
|
|
final int type = commandType!.typeValue;
|
|
|
|
|
|
final double typeDouble = type / 256;
|
|
|
|
|
|
final int type1 = typeDouble.toInt();
|
|
|
|
|
|
final int type2 = type % 256;
|
2023-08-16 18:21:42 +08:00
|
|
|
|
data.add(type1);
|
|
|
|
|
|
data.add(type2);
|
|
|
|
|
|
|
|
|
|
|
|
//KeyID 40
|
2024-08-08 11:00:03 +08:00
|
|
|
|
final int keyIDLength = utf8.encode(lockID!).length;
|
2024-05-23 10:35:06 +08:00
|
|
|
|
data.addAll(utf8.encode(lockID!));
|
2023-08-16 18:21:42 +08:00
|
|
|
|
data = getFixedLengthList(data, 40 - keyIDLength);
|
|
|
|
|
|
|
|
|
|
|
|
//userID 要接受钥匙的用户的useid 20
|
2024-08-08 11:00:03 +08:00
|
|
|
|
final int userIDLength = utf8.encode(userID!).length;
|
2023-08-16 18:21:42 +08:00
|
|
|
|
data.addAll(utf8.encode(userID!));
|
|
|
|
|
|
data = getFixedLengthList(data, 20 - userIDLength);
|
|
|
|
|
|
|
|
|
|
|
|
// openModel
|
|
|
|
|
|
data.add(openMode!);
|
|
|
|
|
|
|
|
|
|
|
|
// OpenTime 4
|
2024-08-08 11:00:03 +08:00
|
|
|
|
final int? d1 = openTime;
|
2024-04-28 15:00:13 +08:00
|
|
|
|
// AppLog.log("开门时间是:$d1");
|
2023-09-04 15:00:42 +08:00
|
|
|
|
|
2023-08-16 18:21:42 +08:00
|
|
|
|
data.add((d1! & 0xff000000) >> 24);
|
|
|
|
|
|
data.add((d1 & 0xff0000) >> 16);
|
|
|
|
|
|
data.add((d1 & 0xff00) >> 8);
|
2024-08-08 11:00:03 +08:00
|
|
|
|
data.add(d1 & 0xff);
|
2023-08-16 18:21:42 +08:00
|
|
|
|
|
|
|
|
|
|
// token 长度4 首次请求 Token 填 0,如果锁需要鉴权 操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。 当token失效或者第一次发送的时候token为0
|
|
|
|
|
|
data.addAll(token!);
|
|
|
|
|
|
|
|
|
|
|
|
if(needAuthor == 0){
|
|
|
|
|
|
//AuthCodeLen 1
|
|
|
|
|
|
data.add(0);
|
|
|
|
|
|
} else {
|
2024-10-15 14:24:35 +08:00
|
|
|
|
final List<int> authCodeData = <int>[];
|
2023-08-16 18:21:42 +08:00
|
|
|
|
//KeyID
|
2024-05-23 10:35:06 +08:00
|
|
|
|
authCodeData.addAll(utf8.encode(lockID!));
|
2023-08-16 18:21:42 +08:00
|
|
|
|
|
|
|
|
|
|
//UserID
|
|
|
|
|
|
authCodeData.addAll(utf8.encode(userID!));
|
|
|
|
|
|
|
|
|
|
|
|
//token 4 首次请求 Token 填 0,如果锁需要鉴权操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。
|
|
|
|
|
|
authCodeData.addAll(token!);
|
|
|
|
|
|
|
|
|
|
|
|
authCodeData.addAll(signKey!);
|
|
|
|
|
|
|
|
|
|
|
|
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
2024-10-15 14:24:35 +08:00
|
|
|
|
final crypto.Digest authCode = crypto.md5.convert(authCodeData);
|
2023-08-16 18:21:42 +08:00
|
|
|
|
|
|
|
|
|
|
data.add(authCode.bytes.length);
|
|
|
|
|
|
data.addAll(authCode.bytes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-24 14:23:36 +08:00
|
|
|
|
//onlineToken 16
|
|
|
|
|
|
if(onlineToken!.isNotEmpty){
|
2024-08-08 11:00:03 +08:00
|
|
|
|
final int onlineTokenLength = utf8.encode(onlineToken!).length;
|
2023-11-24 14:23:36 +08:00
|
|
|
|
data.addAll(utf8.encode(onlineToken!));
|
|
|
|
|
|
data = getFixedLengthList(data, 16 - onlineTokenLength);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-16 18:21:42 +08:00
|
|
|
|
if ((data.length % 16) != 0) {
|
2024-08-08 11:00:03 +08:00
|
|
|
|
final int add = 16 - data.length % 16;
|
2023-08-16 18:21:42 +08:00
|
|
|
|
for (int i = 0; i < add; i++) {
|
|
|
|
|
|
data.add(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-04-19 18:13:34 +08:00
|
|
|
|
|
2024-05-03 14:38:09 +08:00
|
|
|
|
printLog(data);
|
2023-08-16 18:21:42 +08:00
|
|
|
|
// 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864
|
|
|
|
|
|
ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB);
|
|
|
|
|
|
return ebcData;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class OpenDoorReply extends Reply {
|
|
|
|
|
|
OpenDoorReply.parseData(CommandType commandType, List<int> dataDetail)
|
|
|
|
|
|
: super.parseData(commandType, dataDetail) {
|
|
|
|
|
|
data = dataDetail;
|
2024-08-08 11:00:03 +08:00
|
|
|
|
final int status = data[6];
|
2024-04-19 18:13:34 +08:00
|
|
|
|
errorWithStstus(status);
|
2023-08-16 18:21:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|