472 lines
16 KiB
Dart
Executable File
472 lines
16 KiB
Dart
Executable File
import 'dart:convert';
|
||
|
||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||
import 'package:star_lock/app_settings/app_settings.dart';
|
||
import 'package:star_lock/blue/blue_manage.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_addFace.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_changeAdministratorPassword.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_cleanUpUsers.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_deletUser.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_editUser.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_factoryDataReset.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_otaUpgrade.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_processOtaUpgrade.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_queryingFaceStatus.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_readAdminPassword.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_readSupportFunctionsNoParameters.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_readSupportFunctionsWithParameters.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_referEventRecordTime.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_setSupportFunctionsNoParameters.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_setSupportFunctionsWithParameters.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_timing.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_transferPermissions.dart';
|
||
|
||
import '../tools/storage.dart';
|
||
|
||
import 'io_protocol/io_addFingerprintWithTimeCycleCoercion.dart';
|
||
import 'io_protocol/io_addICCardWithTimeCycleCoercion.dart';
|
||
import 'io_protocol/io_addStressPassword.dart';
|
||
import 'io_protocol/io_addUser.dart';
|
||
import 'io_protocol/io_checkingCardStatus.dart';
|
||
import 'io_protocol/io_checkingUserInfoCount.dart';
|
||
import 'io_protocol/io_configuringWifi.dart';
|
||
import 'io_protocol/io_getPrivateKey.dart';
|
||
import 'io_protocol/io_getPublicKey.dart';
|
||
import 'io_protocol/io_getStarLockStatusInfo.dart';
|
||
import 'io_protocol/io_getWifiList.dart';
|
||
import 'io_protocol/io_openLock.dart';
|
||
import 'io_protocol/io_queryingFingerprintStatus.dart';
|
||
import 'io_protocol/io_referEventRecordNumber.dart';
|
||
import 'io_protocol/io_senderResetPasswords.dart';
|
||
import 'io_protocol/io_updataLockCardList.dart';
|
||
import 'io_protocol/io_updataLockFaceList.dart';
|
||
import 'io_protocol/io_updataLockFingerprintList.dart';
|
||
import 'io_protocol/io_updataLockPalmVeinList.dart';
|
||
import 'io_protocol/io_updataLockPasswordList.dart';
|
||
import 'io_reply.dart';
|
||
import 'io_protocol/io_senderCustomPasswords.dart';
|
||
import 'io_type.dart';
|
||
import 'io_tool/io_manager.dart';
|
||
import 'io_tool/io_tool.dart';
|
||
import 'io_tool/manager_event_bus.dart';
|
||
import 'sm4Encipher/sm4.dart';
|
||
|
||
class CommandReciverManager {
|
||
static void appDataReceive(List<int> data) async {
|
||
///解析数据
|
||
if (data.isEmpty) {
|
||
return;
|
||
}
|
||
int dataSize = data.length;
|
||
// 当小于包头加起来13个字节
|
||
if (dataSize < 13) {
|
||
return;
|
||
}
|
||
|
||
if ((data[0] == 0xEF) &&
|
||
(data[1] == 0x01) &&
|
||
(data[2] == 0xEE) &&
|
||
(data[3] == 0x02)) {
|
||
var tmpType = (data[7] & 0x0f); // 包标识
|
||
// AppLog.log("temType:$tmpType");
|
||
|
||
var dataLen = data[8] * 256 + data[9]; // 高16位用来指示后面数据块内容的长度
|
||
var oriLen = data[10] * 256 + data[11]; // 低16位用来指示数据加密前的原长度
|
||
// AppLog.log("dataLen:$dataLen oriLen:$oriLen");
|
||
List<int> oriDataList = [];
|
||
switch (tmpType) {
|
||
case 0: //不加密
|
||
// for (var i = 0; i < oriLen ; i++) {
|
||
// oriDataList.add(data[12 + i]);
|
||
// }
|
||
oriDataList = data.sublist(12, 12 + dataLen);
|
||
AppLog.log("不加密 :$oriDataList");
|
||
break;
|
||
case 1:
|
||
//AES128
|
||
break;
|
||
case 2:
|
||
// SM4(事先约定密钥)
|
||
// 获取的加密数组
|
||
var getDataList = data.sublist(12, 12 + dataLen);
|
||
|
||
// 解密
|
||
// String key = SM4.createHexKey(key: IoManager().getCurrentDeviceLockId);
|
||
oriDataList = SM4.decrypt(getDataList,
|
||
key: utf8.encode(BlueManage().connectDeviceName),
|
||
mode: SM4CryptoMode.ECB);
|
||
oriDataList = oriDataList.sublist(0, oriLen);
|
||
AppLog.log("APP收到的解密后的数据:$oriDataList");
|
||
break;
|
||
case 3:
|
||
//SM4(设备指定密钥)
|
||
// 获取的加密数组
|
||
var getDataList = data.sublist(12, 12 + dataLen);
|
||
|
||
var res = await Storage.getStringList(saveBluePrivateKey);
|
||
List<int> getPrivateKeyList = changeStringListToIntList(res!);
|
||
|
||
// 解密
|
||
oriDataList = SM4.decrypt(getDataList,
|
||
key: getPrivateKeyList, mode: SM4CryptoMode.ECB);
|
||
oriDataList = oriDataList.sublist(0, oriLen);
|
||
AppLog.log("APP收到的解密后的数据:$oriDataList");
|
||
break;
|
||
}
|
||
parseData(oriDataList).then((Reply? value) async {
|
||
EasyLoading.dismiss();
|
||
await EventBusManager().eventBusFir(value);
|
||
}).catchError((error) {
|
||
AppLog.log("APP解析数据时发生错误: $error");
|
||
});
|
||
}
|
||
}
|
||
|
||
static Future<Reply?> parseData(List<int> data) async {
|
||
if (data.isNotEmpty) {
|
||
var cmd = data[0] * 256 + data[1];
|
||
CommandType commandType = ExtensionCommandType.getCommandType(cmd);
|
||
await IoManager().increaseCommandIndex();
|
||
// data.removeRange(0, 2);
|
||
var reply;
|
||
switch (commandType) {
|
||
case CommandType.getLockPublicKey:
|
||
{
|
||
reply = GetPublicKeyReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.getLockPrivateKey:
|
||
{
|
||
reply = GetPrivateKeyReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.addUser:
|
||
{
|
||
reply = AddUserReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.deletUser:
|
||
{
|
||
reply = DeletUserReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.openLock:
|
||
{
|
||
reply = OpenDoorReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
// 弃用
|
||
// case CommandType.readLockStatusInfo:
|
||
// {
|
||
// reply = GetLockStatuReply.parseData(commandType, data);
|
||
// }
|
||
// break;
|
||
case CommandType.editUser:
|
||
{
|
||
reply = EditUserReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.transferPermissions:
|
||
{
|
||
reply = TransferPermissionsReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.factoryDataReset:
|
||
{
|
||
reply = FactoryDataResetReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.calibrationTime:
|
||
{
|
||
reply = TimingReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.readStarLockStatusInfo:
|
||
{
|
||
reply = GetStarLockStatuInfoReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.startOATUpgrade:
|
||
{
|
||
reply = OTAUpgradeReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.confirmationOTAUpgrade:
|
||
{
|
||
reply = ConfirmationOTAUpgradeReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.processOTAUpgrade:
|
||
{
|
||
reply = ProcessOtaUpgradeReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.cleanUpUsers:
|
||
{
|
||
reply = CleanUpUsersReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.updataLockPasswordList:
|
||
{
|
||
reply = UpdataLockPasswordListReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.updataLockCardList:
|
||
{
|
||
reply = UpdataLockCardListReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.updataLockFingerprintList:
|
||
{
|
||
reply = UpdataLockFingerprintListReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.updataLockFaceList:
|
||
{
|
||
reply = UpdataLockFaceListReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.updataLockPalmVeinList:
|
||
{
|
||
reply = UpdataLockPalmVeinListReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.generalExtendedCommond:
|
||
{
|
||
// 子命令类型
|
||
int subType = data[3];
|
||
switch (subType) {
|
||
case 2:
|
||
{
|
||
// 修改管理员密码
|
||
reply = ChangeAdministratorPasswordReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 3:
|
||
{
|
||
// 设置开锁密码
|
||
reply =
|
||
SenderCustomPasswordsReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
// case 4:
|
||
// {
|
||
// // 自动落锁开关
|
||
// reply = AutomaticPadlockReply.parseData(commandType, data);
|
||
// }
|
||
// break;
|
||
case 15:
|
||
{
|
||
// 查询用户、指纹、密码、卡片数量(用于判断是否同步)
|
||
reply = SenderCheckingUserInfoCountReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 19:
|
||
{
|
||
// 重置开锁密码
|
||
reply = SenderResetPasswordsReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 20:
|
||
{
|
||
// 查询卡片状态
|
||
reply = SenderCheckingCardStatusReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
// case 21:
|
||
// {
|
||
// // 注册卡片开始
|
||
// reply = SenderAddICCardReply.parseData(commandType, data);
|
||
// }
|
||
// break;
|
||
case 22:
|
||
{
|
||
// 注册卡片确认
|
||
reply = SenderAddICCardConfirmationReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 24:
|
||
{
|
||
// 注册卡片开始
|
||
reply = SenderAddICCardWithTimeCycleCoercionReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 30:
|
||
{
|
||
// 查询指纹状态
|
||
reply = SenderQueryingFingerprintStatusReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
// case 31:
|
||
// {
|
||
// // 注册指纹开始
|
||
// reply =
|
||
// SenderAddFingerprintReply.parseData(commandType, data);
|
||
// }
|
||
// break;
|
||
case 32:
|
||
{
|
||
// 注册指纹确认
|
||
reply = SenderAddFingerprintConfirmationReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 33:
|
||
{
|
||
// 注册指纹过程
|
||
reply = SenderAddFingerprintProcessReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 36:
|
||
{
|
||
// 注册指纹开始(带限时、循环、胁迫...)
|
||
reply =
|
||
SenderAddFingerprintWithTimeCycleCoercionReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 40:
|
||
{
|
||
// 事件查询记录
|
||
reply = SenderReferEventRecordNumberReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 41:
|
||
{
|
||
// 时间条件查询事件记录
|
||
reply = SenderReferEventRecordTimeReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
// case 50:
|
||
// {
|
||
// // wifi配网
|
||
// reply = SenderConfiguringWifiReply.parseData(commandType, data);
|
||
// }
|
||
// break;
|
||
case 51:
|
||
{
|
||
// wifi配网结果
|
||
reply =
|
||
SenderConfiguringWifiReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case 53:
|
||
{
|
||
// 获取wifilist
|
||
reply = SenderGetWifiReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case 54:
|
||
{
|
||
// 门锁搜索2.4G WIFI SSID 结果
|
||
reply = SenderGetWifiListReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case 60:
|
||
{
|
||
// 注册胁迫密码
|
||
reply =
|
||
SenderAddStressPasswordReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
// case 61:
|
||
// {
|
||
// // 注册胁迫卡片
|
||
// reply =
|
||
// SenderAddStressICCardReply.parseData(commandType, data);
|
||
// }
|
||
// break;
|
||
// case 62:
|
||
// {
|
||
// // 注册胁迫指纹
|
||
// reply = SenderAddStressFingerprintReply.parseData(
|
||
// commandType, data);
|
||
// }
|
||
// break;
|
||
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;
|
||
case 74:
|
||
{
|
||
// 读取管理员密码
|
||
reply =
|
||
SenderReadAdminPasswordReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case 80:
|
||
{
|
||
// 查询人脸状态
|
||
reply = SenderQueryingFaceStatusReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
case 81:
|
||
{
|
||
// 注册人脸开始
|
||
reply = SenderAddFaceReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case 82:
|
||
{
|
||
// 注册人脸确认
|
||
reply = SenderAddFaceConfirmationReply.parseData(
|
||
commandType, data);
|
||
}
|
||
break;
|
||
|
||
case 83:
|
||
{
|
||
// 注册人脸过程
|
||
reply =
|
||
SenderAddFaceProcessReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
break;
|
||
default:
|
||
{
|
||
reply = GetStarLockStatuInfoReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
}
|
||
return reply;
|
||
}
|
||
}
|
||
}
|