From 450b721cf87dadb86850c82b72b6e6eb4788970f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E5=B0=91=E9=98=B3?= <786612630@qq.com> Date: Fri, 18 Aug 2023 16:25:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=93=9D=E7=89=99=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E3=80=81=E8=B0=83=E8=AF=95=E7=BC=96=E8=BE=91=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=93=9D=E7=89=99=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- star_lock/lib/blue/blue_manage.dart | 25 ++- .../lib/blue/io_protocol/io_addUser.dart | 10 +- .../lib/blue/io_protocol/io_deletUser.dart | 10 +- .../lib/blue/io_protocol/io_editUser.dart | 166 ++++++++++++++++++ .../lib/blue/io_protocol/io_getLockStatu.dart | 16 +- .../blue/io_protocol/io_getPrivateKey.dart | 2 +- .../lib/blue/io_protocol/io_openLock.dart | 2 +- star_lock/lib/blue/io_protocol/io_sender.dart | 4 +- star_lock/lib/blue/io_protocol/io_type.dart | 6 +- star_lock/lib/blue/reciver_data.dart | 6 + star_lock/lib/blue/sender_manage.dart | 37 ++++ .../lockDetail/lockDetail_logic.dart | 101 +++++++++++ .../lockDetail/lockDetail_page.dart | 3 +- .../addLock/nearbyLock/nearbyLock_logic.dart | 2 +- 14 files changed, 354 insertions(+), 36 deletions(-) create mode 100644 star_lock/lib/blue/io_protocol/io_editUser.dart diff --git a/star_lock/lib/blue/blue_manage.dart b/star_lock/lib/blue/blue_manage.dart index e20f7386..1cd981c4 100644 --- a/star_lock/lib/blue/blue_manage.dart +++ b/star_lock/lib/blue/blue_manage.dart @@ -92,7 +92,7 @@ class BlueManage{ Future connect(String deviceMAC, String deviceName, {ConnectStateCallBack? connectStateCallBack, bool? isFrist = false}) async { connectDeviceId = deviceMAC; connectDeviceName = deviceName; - print("connectDeviceId:$connectDeviceId"); + print("connectDeviceId:$connectDeviceId connectDeviceName:$connectDeviceName"); EasyLoading.show(); _flutterReactiveBle!.connectToDevice(id: connectDeviceId, connectionTimeout: const Duration(seconds: 15)).listen((connectionStateUpdate) async { @@ -100,6 +100,10 @@ class BlueManage{ deviceConnectionState = connectionStateUpdate.connectionState; print('ConnectionState for device $deviceMAC : ${connectionStateUpdate.connectionState}'); + if(connectionStateUpdate.connectionState != DeviceConnectionState.connected){ + EasyLoading.dismiss(); + } + if(connectionStateUpdate.connectionState == DeviceConnectionState.connected){ // 如果状态是连接的开始发现服务 // await discoverServices(connectDeviceId, connectDeviceName); @@ -127,15 +131,17 @@ class BlueManage{ } } } - _subScribeToCharacteristic(QualifiedCharacteristic(characteristicId: characteristicIdSubscription, serviceId: Uuid.parse("fff0"), deviceId: deviceMAC)); - print('Discovering services finished'); - - if(isFrist == true){ - // 第一次添加锁的时候需要先获取公钥 - IoSenderManage.getPublicKey(lockId: deviceName); - } - connectStateCallBack!(connectionStateUpdate.connectionState); + }else{ + EasyLoading.dismiss(); } + _subScribeToCharacteristic(QualifiedCharacteristic(characteristicId: characteristicIdSubscription, serviceId: Uuid.parse("fff0"), deviceId: deviceMAC)); + print('Discovering services finished'); + + if(isFrist == true){ + // 第一次添加锁的时候需要先获取公钥 + IoSenderManage.getPublicKey(lockId: deviceName); + } + connectStateCallBack!(connectionStateUpdate.connectionState); } on Exception catch (e) { EasyLoading.dismiss(); print('Error occurred when discovering services: $e'); @@ -167,6 +173,7 @@ class BlueManage{ // 重新连接 Future judgeReconnect(String deviceMAC, String deviceName, ConnectStateCallBack? connectStateCallBack) async { if(deviceConnectionState == DeviceConnectionState.connected){ + EasyLoading.show(); connectStateCallBack!(deviceConnectionState!); }else{ connect(deviceMAC, deviceName, connectStateCallBack: (state){ diff --git a/star_lock/lib/blue/io_protocol/io_addUser.dart b/star_lock/lib/blue/io_protocol/io_addUser.dart index 4bd21ab5..5349a461 100644 --- a/star_lock/lib/blue/io_protocol/io_addUser.dart +++ b/star_lock/lib/blue/io_protocol/io_addUser.dart @@ -57,25 +57,25 @@ class AddUserCommand extends SenderProtocol { // 锁id 40 int lockIDLength = utf8.encode(lockID!).length; - print("${commandType!.typeValue}LockIDLength:$lockIDLength utf8.encode(lockID!)${utf8.encode(lockID!)}"); + // print("${commandType!.typeValue}LockIDLength:$lockIDLength utf8.encode(lockID!)${utf8.encode(lockID!)}"); data.addAll(utf8.encode(lockID!)); data = getFixedLengthList(data, 40 - lockIDLength); //authUserID 20 int authUserIDLength = utf8.encode(authUserID!).length; - print("${commandType!.typeValue}IDLength:$authUserIDLength utf8.encode(authUserID!)${utf8.encode(authUserID!)}"); + // print("${commandType!.typeValue}IDLength:$authUserIDLength utf8.encode(authUserID!)${utf8.encode(authUserID!)}"); data.addAll(utf8.encode(authUserID!)); data = getFixedLengthList(data, 20 - authUserIDLength); //KeyID 40 int keyIDLength = utf8.encode(keyID!).length; - print("${commandType!.typeValue}keyIDLength:$keyIDLength utf8.encode(keyID!)${utf8.encode(keyID!)}"); + // print("${commandType!.typeValue}keyIDLength:$keyIDLength utf8.encode(keyID!)${utf8.encode(keyID!)}"); data.addAll(utf8.encode(keyID!)); data = getFixedLengthList(data, 40 - keyIDLength); //userID 要接受钥匙的用户的useid 20 int userIDLength = utf8.encode(userID!).length; - print("${commandType!.typeValue}userIDLength:$userIDLength utf8.encode(userID!)${utf8.encode(userID!)}"); + // print("${commandType!.typeValue}userIDLength:$userIDLength utf8.encode(userID!)${utf8.encode(userID!)}"); data.addAll(utf8.encode(userID!)); data = getFixedLengthList(data, 20 - userIDLength); @@ -150,7 +150,7 @@ class AddUserCommand extends SenderProtocol { data.add(0); } } - print("${commandType!.typeValue} SM4Data:$data"); + print("${commandType!.typeName} SM4Data:$data"); // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB); return ebcData; diff --git a/star_lock/lib/blue/io_protocol/io_deletUser.dart b/star_lock/lib/blue/io_protocol/io_deletUser.dart index bc9a0ce0..27996ec5 100644 --- a/star_lock/lib/blue/io_protocol/io_deletUser.dart +++ b/star_lock/lib/blue/io_protocol/io_deletUser.dart @@ -46,19 +46,19 @@ class DeletUserCommand extends SenderProtocol { // 锁id 40 int lockIDLength = utf8.encode(lockID!).length; - print("${commandType!.typeValue}LockIDLength:$lockIDLength utf8.encode(lockID!)${utf8.encode(lockID!)}"); + // print("${commandType!.typeValue}LockIDLength:$lockIDLength utf8.encode(lockID!)${utf8.encode(lockID!)}"); data.addAll(utf8.encode(lockID!)); data = getFixedLengthList(data, 40 - lockIDLength); //authUserID 20 int authUserIDLength = utf8.encode(authUserID!).length; - print("${commandType!.typeValue}authUserIDLength:$authUserIDLength utf8.encode(authUserID!)${utf8.encode(authUserID!)}"); + // print("${commandType!.typeValue}authUserIDLength:$authUserIDLength utf8.encode(authUserID!)${utf8.encode(authUserID!)}"); data.addAll(utf8.encode(authUserID!)); data = getFixedLengthList(data, 20 - authUserIDLength); //KeyID 40 int keyIDLength = utf8.encode(keyID!).length; - print("${commandType!.typeValue}keyIDLength:$keyIDLength utf8.encode(keyID!)${utf8.encode(keyID!)}"); + // print("${commandType!.typeValue}keyIDLength:$keyIDLength utf8.encode(keyID!)${utf8.encode(keyID!)}"); data.addAll(utf8.encode(keyID!)); data = getFixedLengthList(data, 40 - keyIDLength); @@ -79,7 +79,7 @@ class DeletUserCommand extends SenderProtocol { authCodeData.addAll(publicKey!); - print("${commandType!.typeValue}-authCodeData:$authCodeData"); + // print("${commandType!.typeValue}-authCodeData:$authCodeData"); // 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode var authCode = crypto.md5.convert(authCodeData); @@ -94,7 +94,7 @@ class DeletUserCommand extends SenderProtocol { data.add(0); } } - print("${commandType!.typeValue} SM4Data:$data"); + print("${commandType!.typeName} SM4Data:$data"); // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB); return ebcData; diff --git a/star_lock/lib/blue/io_protocol/io_editUser.dart b/star_lock/lib/blue/io_protocol/io_editUser.dart new file mode 100644 index 00000000..fac0f88f --- /dev/null +++ b/star_lock/lib/blue/io_protocol/io_editUser.dart @@ -0,0 +1,166 @@ + + +//TODO:修改用户 +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; + +class EditUserCommand extends SenderProtocol { + + String? lockID; + String? authUserID; + String? keyID; + String? userID; + int? openMode; + int? keyType; + int? startDate; + int? expireDate; + int? role; + String? password; + int? needAuthor; + List? publicKey; + List? privateKey; + List? token; + EditUserCommand({ + this.lockID, + this.authUserID, + this.keyID, + this.userID, + this.openMode, + this.keyType, + this.startDate, + this.expireDate, + this.role, + this.password, + this.needAuthor, + this.publicKey, + this.privateKey, + this.token + }) : super(CommandType.editUser); + + @override + List messageDetail() { + List data = []; + List ebcData = []; + + // 指令类型 + int type = commandType!.typeValue; + double typeDouble = type / 256; + int type1 = typeDouble.toInt(); + int type2 = type % 256; + data.add(type1); + data.add(type2); + + // 锁id 40 + int lockIDLength = utf8.encode(lockID!).length; + // print("${commandType!.typeValue}LockIDLength:$lockIDLength utf8.encode(lockID!)${utf8.encode(lockID!)}"); + data.addAll(utf8.encode(lockID!)); + data = getFixedLengthList(data, 40 - lockIDLength); + + //authUserID 20 + int authUserIDLength = utf8.encode(authUserID!).length; + // print("${commandType!.typeValue}IDLength:$authUserIDLength utf8.encode(authUserID!)${utf8.encode(authUserID!)}"); + data.addAll(utf8.encode(authUserID!)); + data = getFixedLengthList(data, 20 - authUserIDLength); + + //KeyID 40 + int keyIDLength = utf8.encode(keyID!).length; + // print("${commandType!.typeValue}keyIDLength:$keyIDLength utf8.encode(keyID!)${utf8.encode(keyID!)}"); + data.addAll(utf8.encode(keyID!)); + data = getFixedLengthList(data, 40 - keyIDLength); + + //userID 要接受钥匙的用户的useid 20 + int userIDLength = utf8.encode(userID!).length; + // print("${commandType!.typeValue}userIDLength:$userIDLength utf8.encode(userID!)${utf8.encode(userID!)}"); + data.addAll(utf8.encode(userID!)); + data = getFixedLengthList(data, 20 - userIDLength); + + // openModel + data.add(openMode!); + + // keyType + data.add(keyType!); + + int? d1, d2; + if(role == 255){ + d1 = 0;//Date.parse(new Date()) / 1000; + d2 = 0xffffffff;//d1 + 86440; + }else{ + d1 = startDate; + d2 = expireDate; + } + + // StartDate 4 + data.add((d1! & 0xff000000) >> 24); + data.add((d1 & 0xff0000) >> 16); + data.add((d1 & 0xff00) >> 8); + data.add((d1 & 0xff)); + + // expireDate 4 + data.add((d2! & 0xff000000) >> 24); + data.add((d2 & 0xff0000) >> 16); + data.add((d2 & 0xff00) >> 8); + data.add((d2 & 0xff)); + + // role 长度1 用户角色,0:普通用户,1:管理员,0xff:超级管理员 + data.add(role!); + + //password 超级管理员领锁时需验证该密码 20 + int passwordLength = utf8.encode(password!).length; + data.addAll(utf8.encode(password!)); + data = getFixedLengthList(data, 20 - passwordLength); + + // token 长度4 首次请求 Token 填 0,如果锁需要鉴权 操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。 当token失效或者第一次发送的时候token为0 + data.addAll(token!); + print("pubToken:$token"); + + if(needAuthor == 0){ + //AuthCodeLen 1 + data.add(0); + } else { + List authCodeData = []; + + //authUserID + authCodeData.addAll(utf8.encode(authUserID!)); + + //KeyID + authCodeData.addAll(utf8.encode(keyID!)); + + //token 4 首次请求 Token 填 0,如果锁需要鉴权操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。 + authCodeData.addAll(token!); + + authCodeData.addAll(publicKey!); + + // print("${commandType!.typeValue}-authCodeData:$authCodeData"); + + // 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode + var authCode = crypto.md5.convert(authCodeData); + + data.add(authCode.bytes.length); + data.addAll(authCode.bytes); + } + + 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"); + // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 + ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB); + return ebcData; + } +} + +class EditUserReply extends Reply { + EditUserReply.parseData(CommandType commandType, List dataDetail) + : super.parseData(commandType, dataDetail) { + data = dataDetail; + } +} \ No newline at end of file diff --git a/star_lock/lib/blue/io_protocol/io_getLockStatu.dart b/star_lock/lib/blue/io_protocol/io_getLockStatu.dart index 96d7d358..fffd7e26 100644 --- a/star_lock/lib/blue/io_protocol/io_getLockStatu.dart +++ b/star_lock/lib/blue/io_protocol/io_getLockStatu.dart @@ -34,13 +34,13 @@ class GetLockStatuCommand extends SenderProtocol { // 锁id 40 int lockIDLength = utf8.encode(lockID!).length; - print("addUserLockIDLength:$lockIDLength utf8.encode(lockID!)${utf8.encode(lockID!)}"); + // print("addUserLockIDLength:$lockIDLength utf8.encode(lockID!)${utf8.encode(lockID!)}"); data.addAll(utf8.encode(lockID!)); data = getFixedLengthList(data, 40 - lockIDLength); //userID 要接受钥匙的用户的useid 20 int userIDLength = utf8.encode(userID!).length; - print("userIDLength:$userIDLength utf8.encode(userID!)${utf8.encode(userID!)}"); + // print("userIDLength:$userIDLength utf8.encode(userID!)${utf8.encode(userID!)}"); data.addAll(utf8.encode(userID!)); data = getFixedLengthList(data, 20 - userIDLength); @@ -50,7 +50,7 @@ class GetLockStatuCommand extends SenderProtocol { data.add(0); } } - print("addUserSM4Data:$data"); + print("${commandType!.typeName} SM4Data:$data"); // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB); return ebcData; @@ -66,7 +66,7 @@ class GetLockStatuReply extends Reply { switch(status){ case 0x00: //成功 - print("获取锁状态数据解析成功"); + print("${commandType.typeValue} 数据解析成功"); var softVersion = data.sublist(3, 7); // print("softVersion:$softVersion"); @@ -111,22 +111,22 @@ class GetLockStatuReply extends Reply { break; case 0x06: //无权限 - // print("获取锁状态需要鉴权"); + print("${commandType.typeValue} 需要鉴权"); break; case 0x07: //无权限 - print("获取锁状态用户无权限"); + print("${commandType.typeValue} 用户无权限"); break; case 0x09: // 权限校验错误 - print("获取锁状态权限校验错误"); + print("${commandType.typeValue} 权限校验错误"); break; default: //失败 - print("获取锁状态领锁失败"); + print("${commandType.typeValue} 领锁失败"); break; } diff --git a/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart b/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart index c422f7e0..de992327 100644 --- a/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart +++ b/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart @@ -106,7 +106,7 @@ class GetPrivateKeyCommand extends SenderProtocol { data.add(0); } } - // print("SM4Data:$data"); + print("${commandType!.typeName} SM4Data:$data"); // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 ebcData = SM4.encrypt(data, key: utf8.encode(BlueManage().connectDeviceName), mode: SM4CryptoMode.ECB); diff --git a/star_lock/lib/blue/io_protocol/io_openLock.dart b/star_lock/lib/blue/io_protocol/io_openLock.dart index 6f3aa8de..88eecf3c 100644 --- a/star_lock/lib/blue/io_protocol/io_openLock.dart +++ b/star_lock/lib/blue/io_protocol/io_openLock.dart @@ -95,7 +95,7 @@ class OpenLockCommand extends SenderProtocol { data.add(0); } } - print("addUserSM4Data:$data"); + print("${commandType!.typeName} SM4Data:$data"); // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB); return ebcData; diff --git a/star_lock/lib/blue/io_protocol/io_sender.dart b/star_lock/lib/blue/io_protocol/io_sender.dart index 72db1350..54824b0b 100644 --- a/star_lock/lib/blue/io_protocol/io_sender.dart +++ b/star_lock/lib/blue/io_protocol/io_sender.dart @@ -50,12 +50,12 @@ abstract class SenderProtocol extends IOData { // 包标识 // 指令类型 高 4 位表示包版本,低 4 位用来指示后面数据的加密类型,长度为 1 字节,加密类型取值说明,0:明文,1:AES128,2:SM4(事先约定密钥),3:SM4(设备指定密钥) commandList.add(commandType!.identifierValue); - print("commandType!.identifierValue:${commandType!.identifierValue}"); + // print("commandType!.identifierValue:${commandType!.identifierValue}"); // 数据长度 int dataLen = dataSourceLength(); // commandList.add(dataLength); - print("dataLen:$dataLen"); + // print("dataLen:$dataLen"); // var dataLen = 42; double dataLength = dataLen/256; commandList.add(dataLength.toInt()); diff --git a/star_lock/lib/blue/io_protocol/io_type.dart b/star_lock/lib/blue/io_protocol/io_type.dart index c4db02a0..76697485 100644 --- a/star_lock/lib/blue/io_protocol/io_type.dart +++ b/star_lock/lib/blue/io_protocol/io_type.dart @@ -4,7 +4,7 @@ import 'package:get/get.dart'; enum CommandType { addUser, //增加用户 = 0x3001 deletUser , //删除用户 = 0x3002 - modifyingUser , //修改用户 = 0x3003 + editUser , //修改用户 = 0x3003 factoryDataReset , //恢复出厂设置 = 0x3004 openLock , //开门 = 0x3005 readLockStatusInfo, //读取锁状态信息 = 0x300A @@ -37,7 +37,7 @@ extension ExtensionCommandType on CommandType { break; case 0x3003: { - type = CommandType.modifyingUser; + type = CommandType.editUser; } break; case 0x3004: @@ -118,7 +118,7 @@ extension ExtensionCommandType on CommandType { case CommandType.deletUser: type = 0x3002; break; - case CommandType.modifyingUser: + case CommandType.editUser: type = 0x3003; break; case CommandType.factoryDataReset: diff --git a/star_lock/lib/blue/reciver_data.dart b/star_lock/lib/blue/reciver_data.dart index a5aaced5..5b0de909 100644 --- a/star_lock/lib/blue/reciver_data.dart +++ b/star_lock/lib/blue/reciver_data.dart @@ -5,6 +5,7 @@ import 'dart:convert'; 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'; +import 'package:star_lock/blue/io_protocol/io_editUser.dart'; import 'package:star_lock/blue/io_protocol/io_getLockStatu.dart'; import '../tools/storage.dart'; @@ -126,6 +127,11 @@ class CommandReciverManager { reply = GetLockStatuReply.parseData(commandType, data); } break; + case CommandType.editUser: + { + reply = EditUserReply.parseData(commandType, data); + } + break; } return reply; } diff --git a/star_lock/lib/blue/sender_manage.dart b/star_lock/lib/blue/sender_manage.dart index f11ef8e1..ff2c89be 100644 --- a/star_lock/lib/blue/sender_manage.dart +++ b/star_lock/lib/blue/sender_manage.dart @@ -3,6 +3,7 @@ import 'package:star_lock/blue/io_protocol/io_deletUser.dart'; import 'package:star_lock/blue/io_protocol/io_getLockStatu.dart'; import 'io_protocol/io_addUser.dart'; +import 'io_protocol/io_editUser.dart'; import 'io_protocol/io_getPrivateKey.dart'; import 'io_protocol/io_getPublicKey.dart'; import 'io_protocol/io_openLock.dart'; @@ -72,6 +73,42 @@ class IoSenderManage { ), callBack:callBack); } + //todo:修改用户 + static void senderEditUser({ + String? lockID, + String? authUserID, + String? keyID, + String? userID, + int? openMode, + int? keyType, + int? startDate, + int? expireDate, + int? role, + String? password, + int? needAuthor, + List? publicKey, + List? privateKey, + List? token, + CommandSendCallBack? callBack}) { + CommandSenderManager().managerSendData( + command: EditUserCommand( + lockID: lockID, + authUserID: authUserID, + keyID: keyID, + userID: userID, + openMode: openMode, + keyType: keyType, + startDate: startDate, + expireDate: expireDate, + role: role, + password: password, + needAuthor: needAuthor, + publicKey: publicKey, + privateKey: privateKey, + token: token + ), callBack:callBack); + } + //todo:删除 static void deletUser({ String? lockID, diff --git a/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart b/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart index ed371a2f..43f9c6b0 100644 --- a/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart +++ b/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart @@ -2,7 +2,9 @@ import 'dart:async'; import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; +import 'package:star_lock/blue/io_protocol/io_editUser.dart'; import 'package:star_lock/blue/io_protocol/io_getLockStatu.dart'; +import 'package:star_lock/blue/io_protocol/io_type.dart'; import '../../../blue/blue_manage.dart'; import '../../../blue/io_protocol/io_openLock.dart'; @@ -26,9 +28,15 @@ class LockDetailLogic extends BaseGetXController{ _replyOpenLock(reply); } + // 获取锁状态 if(reply is GetLockStatuReply) { _replyGetLockStatus(reply); } + + // 编辑锁用户 + if(reply is EditUserReply){ + _replyEditUserKey(reply); + } }); } @@ -161,6 +169,65 @@ class LockDetailLogic extends BaseGetXController{ } } + Future _replyEditUserKey(Reply reply) async { + var token = reply.data.sublist(2, 6); + var saveStrList = changeIntListToStringList(token); + print("_replyEditUserKeyToken:$token"); + Storage.setStringList(saveBlueToken, saveStrList); + + int status = reply.data[6]; + print("status:$status"); + + switch(status){ + case 0x00: + //成功 + print("${reply.commandType!.typeValue} 数据解析成功"); + + break; + case 0x06: + //无权限 + print("${reply.commandType!.typeValue} 需要鉴权"); + var privateKey = await Storage.getStringList(saveBluePrivateKey); + List getPrivateKeyList = changeStringListToIntList(privateKey!); + + var publicKey = await Storage.getStringList(saveBluePublicKey); + List publicKeyDataList = changeStringListToIntList(publicKey!); + + IoSenderManage.senderEditUser( + lockID:BlueManage().connectDeviceName, + authUserID:"100001", + keyID:"1", + userID:"100001", + openMode:1, + keyType:1, + startDate:0x11223344, + expireDate:0x11223344, + role:255, + password:"123456", + needAuthor:1, + publicKey:publicKeyDataList, + privateKey:getPrivateKeyList, + token: token + ); + break; + case 0x07: + //无权限 + print("${reply.commandType!.typeValue} 用户无权限"); + + break; + case 0x09: + // 权限校验错误 + print("${reply.commandType!.typeValue} 添加用户权限校验错误"); + + break; + default: + //失败 + print("${reply.commandType!.typeValue} 失败"); + + break; + } + } + Future openDoorAction() async { BlueManage().judgeReconnect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", (DeviceConnectionState state) async { if (state == DeviceConnectionState.connected){ @@ -188,6 +255,40 @@ class LockDetailLogic extends BaseGetXController{ }); } + Future editLockUserAction() async { + BlueManage().judgeReconnect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", (DeviceConnectionState state) async { + if (state == DeviceConnectionState.connected){ + var publicKey = await Storage.getStringList(saveBluePublicKey); + List publicKeyDataList = changeStringListToIntList(publicKey!); + + var privateKey = await Storage.getStringList(saveBluePrivateKey); + List getPrivateKeyList = changeStringListToIntList(privateKey!); + + var token = await Storage.getStringList(saveBlueToken); + List getTokenList = changeStringListToIntList(token!); + print("openDoorTokenPubToken:$getTokenList"); + + print("privateKey:$privateKey token:$token"); + IoSenderManage.senderEditUser( + lockID:BlueManage().connectDeviceName, + authUserID:"100001", + keyID:"1", + userID:"100001", + openMode:1, + keyType:1, + startDate:0x11223344, + expireDate:0x11223344, + role:255, + password:"123456", + needAuthor:1, + publicKey:publicKeyDataList, + privateKey:getPrivateKeyList, + token: getTokenList + ); + } + }); + } + Future connectBlue() async { // 进来之后首先连接 // BlueManage().connect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", connectStateCallBack: (DeviceConnectionState state) async { diff --git a/star_lock/lib/main/lockDetail/lockDetail/lockDetail_page.dart b/star_lock/lib/main/lockDetail/lockDetail/lockDetail_page.dart index cfc70292..d09d89f0 100644 --- a/star_lock/lib/main/lockDetail/lockDetail/lockDetail_page.dart +++ b/star_lock/lib/main/lockDetail/lockDetail/lockDetail_page.dart @@ -76,7 +76,8 @@ class _LockDetailPageState extends State { Center( child: GestureDetector( onTap: (){ - logic.openDoorAction(); + // logic.openDoorAction(); + logic.editLockUserAction(); }, child: Image.asset('images/main/icon_main_openLockBtn.png', width: 268.w, height: 268.w), diff --git a/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart b/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart index a83dc66c..1303d9c4 100644 --- a/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart +++ b/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart @@ -24,7 +24,7 @@ class NearbyLockLogic extends BaseGetXController{ // 点击复合要求的设备之后 连接 void connect(String lockId, String deviceName){ - BlueManage().connect(lockId, deviceName, isFrist: false); + BlueManage().connect(lockId, deviceName, isFrist: true); } // 获取解析后的数据