2023-08-08 09:42:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-15 18:54:44 +08:00
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
2023-08-17 18:45:10 +08:00
|
|
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
|
|
|
|
import 'package:star_lock/blue/blue_manage.dart';
|
|
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_deletUser.dart';
|
2023-08-18 16:25:41 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_editUser.dart';
|
2023-08-24 14:25:55 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_factoryDataReset.dart';
|
2023-08-16 18:21:42 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_getLockStatu.dart';
|
2023-11-13 11:28:42 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_readAdminPassword.dart';
|
2023-10-07 18:55:59 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_readSupportFunctionsNoParameters.dart';
|
|
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_readSupportFunctionsWithParameters.dart';
|
2023-09-28 18:05:23 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_referEventRecordTime.dart';
|
2023-10-07 18:55:59 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_setSupportFunctionsNoParameters.dart';
|
|
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_setSupportFunctionsWithParameters.dart';
|
2023-08-31 15:56:01 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_timing.dart';
|
2023-08-24 14:25:55 +08:00
|
|
|
|
import 'package:star_lock/blue/io_protocol/io_transferPermissions.dart';
|
2023-08-16 18:21:42 +08:00
|
|
|
|
|
2023-08-15 18:54:44 +08:00
|
|
|
|
import '../tools/storage.dart';
|
2023-08-31 15:56:01 +08:00
|
|
|
|
import 'io_protocol/io_addFingerprint.dart';
|
2023-09-20 14:22:44 +08:00
|
|
|
|
import 'io_protocol/io_addICCard.dart';
|
|
|
|
|
|
import 'io_protocol/io_addStressFingerprint.dart';
|
|
|
|
|
|
import 'io_protocol/io_addStressICCard.dart';
|
|
|
|
|
|
import 'io_protocol/io_addStressPassword.dart';
|
2023-08-08 09:42:35 +08:00
|
|
|
|
import 'io_protocol/io_addUser.dart';
|
2023-09-04 15:00:42 +08:00
|
|
|
|
import 'io_protocol/io_automaticPadlock.dart';
|
|
|
|
|
|
import 'io_protocol/io_checkingCardStatus.dart';
|
2023-09-07 18:36:16 +08:00
|
|
|
|
import 'io_protocol/io_checkingUserInfoCount.dart';
|
|
|
|
|
|
import 'io_protocol/io_configuringWifi.dart';
|
2023-08-11 14:50:42 +08:00
|
|
|
|
import 'io_protocol/io_getPrivateKey.dart';
|
2023-08-08 09:42:35 +08:00
|
|
|
|
import 'io_protocol/io_getPublicKey.dart';
|
2023-10-07 18:55:59 +08:00
|
|
|
|
import 'io_protocol/io_getStarLockStatusInfo.dart';
|
2023-08-16 18:21:42 +08:00
|
|
|
|
import 'io_protocol/io_openLock.dart';
|
2023-09-04 15:00:42 +08:00
|
|
|
|
import 'io_protocol/io_queryingFingerprintStatus.dart';
|
|
|
|
|
|
import 'io_protocol/io_referEventRecordNumber.dart';
|
2023-08-31 15:56:01 +08:00
|
|
|
|
import 'io_reply.dart';
|
2023-08-30 18:36:02 +08:00
|
|
|
|
import 'io_protocol/io_senderCustomPasswords.dart';
|
2023-08-31 15:56:01 +08:00
|
|
|
|
import 'io_type.dart';
|
2023-08-15 18:54:44 +08:00
|
|
|
|
import 'io_tool/io_manager.dart';
|
2023-08-08 09:42:35 +08:00
|
|
|
|
import 'io_tool/io_tool.dart';
|
|
|
|
|
|
import 'io_tool/manager_event_bus.dart';
|
2023-08-11 14:50:42 +08:00
|
|
|
|
import 'sm4Encipher/sm4.dart';
|
2023-08-08 09:42:35 +08:00
|
|
|
|
|
|
|
|
|
|
class CommandReciverManager {
|
|
|
|
|
|
|
2023-08-15 18:54:44 +08:00
|
|
|
|
static void appDataReceive(List<int> data) async {
|
2023-08-08 09:42:35 +08:00
|
|
|
|
///解析数据
|
2023-08-09 09:32:09 +08:00
|
|
|
|
if(data.isEmpty){
|
|
|
|
|
|
return;
|
2023-08-08 09:42:35 +08:00
|
|
|
|
}
|
2023-09-04 15:00:42 +08:00
|
|
|
|
int dataSize = data.length;
|
2023-08-09 09:32:09 +08:00
|
|
|
|
// 当小于包头加起来13个字节
|
2023-09-04 15:00:42 +08:00
|
|
|
|
if(dataSize < 13){
|
2023-08-08 09:42:35 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-09-04 15:00:42 +08:00
|
|
|
|
|
|
|
|
|
|
print("appDataReceiveData:$data"); // &&(data[4] == 0x11)
|
2023-08-31 15:56:01 +08:00
|
|
|
|
if((data[0] == 0xEF)&&(data[1] == 0x01)&&(data[2] == 0xEE)&&(data[3] == 0x02)){
|
2023-08-09 09:32:09 +08:00
|
|
|
|
var tmpType = (data[7] & 0x0f);// 包标识
|
|
|
|
|
|
print("temType:$tmpType");
|
2023-09-04 15:00:42 +08:00
|
|
|
|
|
2023-08-09 09:32:09 +08:00
|
|
|
|
var dataLen = data[8] * 256 + data[9];// 高16位用来指示后面数据块内容的长度
|
|
|
|
|
|
var oriLen = data[10] * 256 + data[11];// 低16位用来指示数据加密前的原长度
|
2023-08-11 14:50:42 +08:00
|
|
|
|
print("dataLen:$dataLen oriLen:$oriLen");
|
2023-08-09 09:32:09 +08:00
|
|
|
|
List<int> dataList = [];
|
|
|
|
|
|
List<int> oriDataList = [];
|
|
|
|
|
|
switch(tmpType){
|
|
|
|
|
|
case 0: //不加密
|
|
|
|
|
|
// for (var i = 0; i < oriLen ; i++) {
|
2023-08-11 14:50:42 +08:00
|
|
|
|
// oriDataList.add(data[12 + i]);
|
2023-08-09 09:32:09 +08:00
|
|
|
|
// }
|
2023-08-12 18:32:49 +08:00
|
|
|
|
oriDataList = data.sublist(12, 12 + dataLen);
|
2023-08-17 18:45:10 +08:00
|
|
|
|
// print("不加密 oriDataList:$oriDataList");
|
2023-08-11 14:50:42 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
//AES128
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
2023-08-12 18:32:49 +08:00
|
|
|
|
// SM4(事先约定密钥)
|
|
|
|
|
|
// 获取的加密数组
|
|
|
|
|
|
var getDataList = data.sublist(12, 12 + dataLen);
|
2023-08-15 18:54:44 +08:00
|
|
|
|
|
2023-08-12 18:32:49 +08:00
|
|
|
|
// 解密
|
2023-08-15 18:54:44 +08:00
|
|
|
|
// String key = SM4.createHexKey(key: IoManager().getCurrentDeviceLockId);
|
2023-08-17 18:45:10 +08:00
|
|
|
|
oriDataList = SM4.decrypt(getDataList, key: utf8.encode(BlueManage().connectDeviceName), mode: SM4CryptoMode.ECB);
|
2023-08-12 18:32:49 +08:00
|
|
|
|
oriDataList = oriDataList.sublist(0, oriLen);
|
2023-08-17 18:45:10 +08:00
|
|
|
|
// print("SM4 oriDataList:$oriDataList");
|
2023-08-09 09:32:09 +08:00
|
|
|
|
break;
|
2023-08-12 18:32:49 +08:00
|
|
|
|
case 3:
|
|
|
|
|
|
//SM4(设备指定密钥)
|
2023-08-15 18:54:44 +08:00
|
|
|
|
// 获取的加密数组
|
|
|
|
|
|
var getDataList = data.sublist(12, 12 + dataLen);
|
|
|
|
|
|
|
|
|
|
|
|
var res = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
|
List<int> getPrivateKeyList = changeStringListToIntList(res!);
|
2023-08-17 18:45:10 +08:00
|
|
|
|
// print("getPrivateKeyList$getPrivateKeyList");
|
2023-08-15 18:54:44 +08:00
|
|
|
|
|
|
|
|
|
|
// 解密
|
|
|
|
|
|
oriDataList = SM4.decrypt(getDataList, key: getPrivateKeyList, mode: SM4CryptoMode.ECB);
|
|
|
|
|
|
oriDataList = oriDataList.sublist(0, oriLen);
|
2023-08-24 14:25:55 +08:00
|
|
|
|
print("SM4 oriDataList:$oriDataList");
|
2023-08-09 09:32:09 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-11 14:50:42 +08:00
|
|
|
|
parseData(oriDataList).then((value) {
|
2023-08-17 18:45:10 +08:00
|
|
|
|
EasyLoading.dismiss();
|
2023-08-11 14:50:42 +08:00
|
|
|
|
EventBusManager().eventBusFir(value);
|
|
|
|
|
|
});
|
2023-08-08 09:42:35 +08:00
|
|
|
|
}
|
2023-08-09 09:32:09 +08:00
|
|
|
|
// print('✅ 执行结束 _buffer:${_buffer.length}');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Future<Reply?> parseData(List<int> data) async {
|
|
|
|
|
|
if(data.isNotEmpty){
|
|
|
|
|
|
var cmd = data[0] * 256 + data[1];
|
|
|
|
|
|
CommandType commandType = ExtensionCommandType.getCommandType(cmd);
|
2023-08-15 18:54:44 +08:00
|
|
|
|
await IoManager().increaseCommandIndex();
|
2023-08-11 14:50:42 +08:00
|
|
|
|
// data.removeRange(0, 2);
|
2023-08-17 18:45:10 +08:00
|
|
|
|
// print("111111data cmd:$cmd commandType:$commandType data:$data");
|
2023-08-09 09:32:09 +08:00
|
|
|
|
var reply;
|
|
|
|
|
|
switch(commandType) {
|
|
|
|
|
|
case CommandType.getLockPublicKey:
|
2023-08-08 09:42:35 +08:00
|
|
|
|
{
|
2023-08-09 09:32:09 +08:00
|
|
|
|
reply = GetPublicKeyReply.parseData(commandType, data);
|
2023-08-08 09:42:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-11 14:50:42 +08:00
|
|
|
|
case CommandType.getLockPrivateKey:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = GetPrivateKeyReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-09 09:32:09 +08:00
|
|
|
|
case CommandType.addUser:
|
2023-08-08 09:42:35 +08:00
|
|
|
|
{
|
2023-08-09 09:32:09 +08:00
|
|
|
|
reply = AddUserReply.parseData(commandType, data);
|
2023-08-08 09:42:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-17 18:45:10 +08:00
|
|
|
|
case CommandType.deletUser:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = DeletUserReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-16 18:21:42 +08:00
|
|
|
|
case CommandType.openLock:
|
2023-08-09 09:32:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
reply = OpenDoorReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
2023-08-08 09:42:35 +08:00
|
|
|
|
break;
|
2023-08-16 18:21:42 +08:00
|
|
|
|
case CommandType.readLockStatusInfo:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = GetLockStatuReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-18 16:25:41 +08:00
|
|
|
|
case CommandType.editUser:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = EditUserReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-24 14:25:55 +08:00
|
|
|
|
case CommandType.transferPermissions:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = TransferPermissionsReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CommandType.factoryDataReset:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = FactoryDataResetReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-31 15:56:01 +08:00
|
|
|
|
case CommandType.calibrationTime:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = TimingReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-10-07 18:55:59 +08:00
|
|
|
|
case CommandType.readStarLockStatusInfo:
|
|
|
|
|
|
{
|
|
|
|
|
|
reply = GetStarLockStatuInfoReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-30 18:36:02 +08:00
|
|
|
|
case CommandType.generalExtendedCommond:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 子命令类型
|
|
|
|
|
|
int subType = data[3];
|
|
|
|
|
|
switch(subType){
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设置开锁密码
|
|
|
|
|
|
reply = SenderCustomPasswordsReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-04 15:00:42 +08:00
|
|
|
|
case 4:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 自动落锁开关
|
|
|
|
|
|
reply = AutomaticPadlockReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-07 18:36:16 +08:00
|
|
|
|
case 15:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 查询用户、指纹、密码、卡片数量(用于判断是否同步)
|
|
|
|
|
|
reply = SenderCheckingUserInfoCountReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-04 15:00:42 +08:00
|
|
|
|
case 20:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 查询卡片状态
|
|
|
|
|
|
reply = SenderCheckingCardStatusReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-20 14:22:44 +08:00
|
|
|
|
case 21:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册卡片开始
|
|
|
|
|
|
reply = SenderAddICCardReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 22:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册卡片确认
|
|
|
|
|
|
reply = SenderAddICCardConfirmationReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-04 15:00:42 +08:00
|
|
|
|
case 30:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 查询指纹状态
|
|
|
|
|
|
reply = SenderQueryingFingerprintStatusReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-31 15:56:01 +08:00
|
|
|
|
case 31:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册指纹开始
|
|
|
|
|
|
reply = SenderAddFingerprintReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 32:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册指纹确认
|
|
|
|
|
|
reply = SenderAddFingerprintConfirmationReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 33:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册指纹过程
|
|
|
|
|
|
reply = SenderAddFingerprintProcessReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-04 15:00:42 +08:00
|
|
|
|
case 40:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 事件查询记录
|
|
|
|
|
|
reply = SenderReferEventRecordNumberReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-28 18:05:23 +08:00
|
|
|
|
case 41:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 事件查询记录
|
|
|
|
|
|
reply = SenderReferEventRecordTimeReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-11-18 10:38:13 +08:00
|
|
|
|
// case 50:
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // wifi配网
|
|
|
|
|
|
// reply = SenderConfiguringWifiReply.parseData(commandType, data);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// break;
|
2023-09-07 18:36:16 +08:00
|
|
|
|
case 51:
|
|
|
|
|
|
{
|
|
|
|
|
|
// wifi配网结果
|
|
|
|
|
|
reply = SenderConfiguringWifiReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-09-20 14:22:44 +08:00
|
|
|
|
case 60:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册胁迫密码
|
|
|
|
|
|
reply = SenderAddStressPasswordReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 61:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册胁迫卡片
|
|
|
|
|
|
reply = SenderAddStressICCardReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 62:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册胁迫指纹
|
|
|
|
|
|
reply = SenderAddStressFingerprintReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-10-07 18:55:59 +08:00
|
|
|
|
case 70:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设置支持功能(不带参数)启用/禁用
|
|
|
|
|
|
reply = SetSupportFunctionsNoParametersReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 71:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 读取支持功能(不带参数)启用/禁用状态
|
|
|
|
|
|
reply = ReadSupportFunctionsNoParametersReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 72:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设置支持功能(带参数)
|
|
|
|
|
|
reply = SetSupportFunctionsWithParametersReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 73:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 读取支持功能(带参数)参数
|
|
|
|
|
|
reply = ReadSupportFunctionsWithParametersReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-11-13 11:28:42 +08:00
|
|
|
|
case 74:
|
|
|
|
|
|
{
|
|
|
|
|
|
// 读取管理员密码
|
|
|
|
|
|
reply = SenderReadAdminPasswordReply.parseData(commandType, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-30 18:36:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2023-08-08 09:42:35 +08:00
|
|
|
|
}
|
2023-08-09 09:32:09 +08:00
|
|
|
|
return reply;
|
|
|
|
|
|
}
|
2023-08-08 09:42:35 +08:00
|
|
|
|
}
|
2023-08-30 18:36:02 +08:00
|
|
|
|
|
2023-08-08 09:42:35 +08:00
|
|
|
|
}
|