import 'dart:convert'; import 'package:star_lock/tools/dateTool.dart'; import '../io_reply.dart'; import '../io_sender.dart'; import '../io_tool/io_tool.dart'; import '../io_type.dart'; import '../sm4Encipher/sm4.dart'; // 校时 class TimingCommand extends SenderProtocol { String? lockID; String? userID; int? nowTime; List? token; int? needAuthor; List? signKey; List? privateKey; TimingCommand({ this.lockID, this.userID, this.nowTime, this.token, this.needAuthor, this.signKey, this.privateKey }) : super(CommandType.calibrationTime); @override String toString() { return 'TimingCommand{lockID: $lockID, userID: $userID, ' 'nowTime: ${DateTool().dateIntToYMDHNString(nowTime)}, ' 'token: $token, needAuthor: $needAuthor, ' 'signKey: $signKey, privateKey: $privateKey}'; } @override List messageDetail() { List data = []; List ebcData = []; // 指令类型 final int type = commandType!.typeValue; final double typeDouble = type / 256; final int type1 = typeDouble.toInt(); final int type2 = type % 256; data.add(type1); data.add(type2); //lockID 40 final int lockIDLength = utf8.encode(lockID!).length; data.addAll(utf8.encode(lockID!)); data = getFixedLengthList(data, 40 - lockIDLength); //userID 要接受钥匙的用户的useid 20 final int userIDLength = utf8.encode(userID!).length; data.addAll(utf8.encode(userID!)); data = getFixedLengthList(data, 20 - userIDLength); // nowTime 4 final int? d1 = nowTime; data.add((d1! & 0xff000000) >> 24); data.add((d1 & 0xff0000) >> 16); data.add((d1 & 0xff00) >> 8); data.add(d1 & 0xff); if ((data.length % 16) != 0) { final int add = 16 - data.length % 16; for (int i = 0; i < add; i++) { data.add(0); } } printLog(data); ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB); return ebcData; } } class TimingReply extends Reply { TimingReply.parseData(CommandType commandType, List dataDetail) : super.parseData(commandType, dataDetail) { data = dataDetail; final int status = data[2]; errorWithStstus(status); } }