From 44b6e1edf9ed195662d346eceb59ad11abd5d9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=8F=E5=B0=91=E9=98=B3?= <786612630@qq.com> Date: Thu, 17 Aug 2023 18:45:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=93=9D=E7=89=99=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E9=80=BB=E8=BE=91=EF=BC=8C=E8=B0=83=E8=AF=95=E5=BC=80?= =?UTF-8?q?=E9=94=81=E3=80=81=E5=88=A0=E9=99=A4=E7=94=A8=E6=88=B7=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ios/Runner.xcodeproj/project.pbxproj | 6 +- star_lock/lib/blue/blue_manage.dart | 207 ++++++++++++------ .../lib/blue/io_protocol/io_addUser.dart | 13 +- .../lib/blue/io_protocol/io_deletUser.dart | 110 ++++++++++ .../lib/blue/io_protocol/io_getLockStatu.dart | 69 ++++++ .../blue/io_protocol/io_getPrivateKey.dart | 5 +- star_lock/lib/blue/io_tool/cbc.dart | 97 -------- star_lock/lib/blue/reciver_data.dart | 21 +- star_lock/lib/blue/sender_manage.dart | 25 +++ .../lcokSet/lockSet/lockSet_logic.dart | 112 ++++++++++ .../lcokSet/lockSet/lockSet_page.dart | 8 +- .../lcokSet/lockSet/lockSet_state.dart | 5 + .../lockDetail/lockDetail_logic.dart | 175 ++++++++------- .../addLock/nearbyLock/nearbyLock_logic.dart | 43 +--- star_lock/lib/tools/seletImgTool.dart | 2 + star_lock/lib/tools/storage.dart | 24 +- 16 files changed, 594 insertions(+), 328 deletions(-) create mode 100644 star_lock/lib/blue/io_protocol/io_deletUser.dart delete mode 100644 star_lock/lib/blue/io_tool/cbc.dart create mode 100644 star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_logic.dart create mode 100644 star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_state.dart diff --git a/star_lock/ios/Runner.xcodeproj/project.pbxproj b/star_lock/ios/Runner.xcodeproj/project.pbxproj index f3bc27dd..6fa290ed 100644 --- a/star_lock/ios/Runner.xcodeproj/project.pbxproj +++ b/star_lock/ios/Runner.xcodeproj/project.pbxproj @@ -359,7 +359,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.starLock123; + PRODUCT_BUNDLE_IDENTIFIER = com.example.starLock456; PRODUCT_NAME = "$(TARGET_NAME)"; VERSIONING_SYSTEM = "apple-generic"; }; @@ -486,7 +486,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.starLock123; + PRODUCT_BUNDLE_IDENTIFIER = com.example.starLock456; PRODUCT_NAME = "$(TARGET_NAME)"; VERSIONING_SYSTEM = "apple-generic"; }; @@ -506,7 +506,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.starLock123; + PRODUCT_BUNDLE_IDENTIFIER = com.example.starLock456; PRODUCT_NAME = "$(TARGET_NAME)"; VERSIONING_SYSTEM = "apple-generic"; }; diff --git a/star_lock/lib/blue/blue_manage.dart b/star_lock/lib/blue/blue_manage.dart index 70880ce1..e20f7386 100644 --- a/star_lock/lib/blue/blue_manage.dart +++ b/star_lock/lib/blue/blue_manage.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; import 'package:star_lock/blue/sender_manage.dart'; @@ -14,19 +15,25 @@ import 'io_tool/manager_event_bus.dart'; import 'reciver_data.dart'; typedef ScanResultCallBack = void Function(List devices); +//连接状态回调 +typedef ConnectStateCallBack = Function(DeviceConnectionState connectionState); class BlueManage{ FlutterReactiveBle? _flutterReactiveBle; - List _scanDevices = []; - QualifiedCharacteristic? qualifiedCharacteristic; - DiscoveredCharacteristic? getDiscoveredCharacteristic; + final List _scanDevices = []; + Uuid serviceId = Uuid.parse('0000FFF0-0000-1000-8000-00805F9B34FB'); + Uuid characteristicIdSubscription = Uuid.parse("fff1");//用来订阅的特征id + Uuid characteristicIdWrite = Uuid.parse("fff2");// 用来写入的特征id + String connectDeviceId = ""; + String connectDeviceName = ""; + final int _limitLen = 20; // 监听发送事件 StreamSubscription? _sendStreamSubscription; // 监听蓝牙连接状态 - DeviceConnectionState? deviceConnectionState; + DeviceConnectionState? deviceConnectionState = DeviceConnectionState.disconnected; static BlueManage? _manager; BlueManage._init(); @@ -42,22 +49,18 @@ class BlueManage{ void _initBlue(){ _flutterReactiveBle = FlutterReactiveBle(); - + print("蓝牙功能初始化了"); _initSendStreamSubscription(); } void _initSendStreamSubscription() { - _sendStreamSubscription = EventBusManager().eventBus!.on().listen(( - EventSendModel model) { - if (model.sendChannel == DataChannel.ble) { - // managerAppWriteData(model.data); - writeCharacteristicWithResponse(QualifiedCharacteristic( - deviceId:qualifiedCharacteristic!.deviceId, - characteristicId: qualifiedCharacteristic!.characteristicId, - serviceId: serviceId), - model.data); - } - }); + _sendStreamSubscription ??= EventBusManager().eventBus!.on().listen(( + EventSendModel model) { + if (model.sendChannel == DataChannel.ble) { + // managerAppWriteData(model.data); + writeCharacteristicWithResponse(model.data); + } + }); } /// 开始扫描蓝牙设备 @@ -70,7 +73,6 @@ class BlueManage{ } // print("startScanDevice:${device}"); if (((device.serviceUuids.isNotEmpty ? device.serviceUuids[0] : "").toString().contains("758824")) && (device.rssi >= -100)) { - // print("11111111111111111:${device}"); final knownDeviceIndex = _scanDevices.indexWhere((d) => d.id == device.id); if (knownDeviceIndex >= 0) { @@ -87,92 +89,153 @@ class BlueManage{ } /// 连接监听状态 - Future connect(String deviceMAC, String deviceName) async { - print("connect:$deviceMAC"); - _flutterReactiveBle!.connectToDevice(id: deviceMAC).listen((connectionStateUpdate) async { - print('ConnectionState for device $deviceMAC : ${connectionStateUpdate.connectionState}'); - // EventBusManager().eventBusFir(connectionStateUpdate); + Future connect(String deviceMAC, String deviceName, {ConnectStateCallBack? connectStateCallBack, bool? isFrist = false}) async { + connectDeviceId = deviceMAC; + connectDeviceName = deviceName; + print("connectDeviceId:$connectDeviceId"); + + EasyLoading.show(); + _flutterReactiveBle!.connectToDevice(id: connectDeviceId, connectionTimeout: const Duration(seconds: 15)).listen((connectionStateUpdate) async { + // 获取状态 deviceConnectionState = connectionStateUpdate.connectionState; + print('ConnectionState for device $deviceMAC : ${connectionStateUpdate.connectionState}'); + if(connectionStateUpdate.connectionState == DeviceConnectionState.connected){ - // getPublicKey(update.deviceId); - // 先配置lockId - IoManager().configCurrentDeviceLockId(deviceName); // 如果状态是连接的开始发现服务 - await discoverServices(deviceMAC); + // await discoverServices(connectDeviceId, connectDeviceName); + try { + // print('Start discovering services for: $deviceId'); + List result = await _flutterReactiveBle!.discoverServices(deviceMAC); + // print("mmmmmmmmm$result"); + if(result.isNotEmpty){ + for (var i = 0; i < result.length; i++) { + DiscoveredService discoveredService = result[i]; + if (discoveredService.serviceId.toString() == "fff0"){ + // getDiscoveredService = discoveredService; + for (var j = 0; j < discoveredService.characteristics.length; j++) { + DiscoveredCharacteristic discoveredCharacteristic = discoveredService.characteristics[j]; + if (discoveredCharacteristic.characteristicId.toString() == "fff1") { + // 订阅用 + characteristicIdSubscription = discoveredCharacteristic.characteristicId; + // print("Subscription characteristicId:${result[i].characteristicIds[j].toString()} serviceId:${result[i].serviceId} deviceId:$deviceMAC"); + } + if (discoveredCharacteristic.characteristicId.toString() == "fff2") { + // 用来写入 + characteristicIdWrite= discoveredCharacteristic.characteristicId; + // print("1111111111111111characteristicId:${discoveredCharacteristic.characteristicId} serviceId:${serviceId} deviceId:$deviceId"); + } + } + } + } + _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'); + rethrow; + } } }, onError: (Object e){ - print('Connecting to device $deviceMAC resulted in error $e'); + EasyLoading.dismiss(); + print('Connecting to device $deviceMAC resulted in error $e'); } ); } + // 断开连接 Future disconnect(String deviceMAC) async { try { print('disconnecting to device: $deviceMAC'); } on Exception catch (e, _) { print("Error disconnecting from a device: $e"); } finally { - EventBusManager().eventBusFir(ConnectionStateUpdate( - deviceId: deviceMAC, - connectionState: DeviceConnectionState.disconnected, - failure: null, - )); + // EventBusManager().eventBusFir(ConnectionStateUpdate( + // deviceId: deviceMAC, + // connectionState: DeviceConnectionState.disconnected, + // failure: null, + // )); + } + } + + // 重新连接 + Future judgeReconnect(String deviceMAC, String deviceName, ConnectStateCallBack? connectStateCallBack) async { + if(deviceConnectionState == DeviceConnectionState.connected){ + connectStateCallBack!(deviceConnectionState!); + }else{ + connect(deviceMAC, deviceName, connectStateCallBack: (state){ + connectStateCallBack!(deviceConnectionState!); + }); } } // 扫描服务,并过滤服务 - Future> discoverServices(String deviceId) async { - try { - // print('Start discovering services for: $deviceId'); - List result = await _flutterReactiveBle!.discoverServices(deviceId); - // print("mmmmmmmmm$result"); - if(result.isNotEmpty){ - for (var i = 0; i < result.length; i++) { - DiscoveredService discoveredService = result[i]; - // print("objectdiscoveredService.serviceId.toString() ${discoveredService.serviceId.toString()}"); - if (discoveredService.serviceId.toString() == "fff0"){ - // getDiscoveredService = discoveredService; - for (var j = 0; j < discoveredService.characteristics.length; j++) { - DiscoveredCharacteristic discoveredCharacteristic = discoveredService.characteristics[j]; - - // print("hhhhhhhhhh${result[i].characteristicIds[j].toString()}"); - if (discoveredCharacteristic.characteristicId.toString() == "fff1") { - // 订阅用 - getDiscoveredCharacteristic = discoveredCharacteristic; - // print("1111111111111111characteristicId:${result[i].characteristicIds[j].toString()} serviceId:${result[i].serviceId} deviceId:$deviceId"); - } - if (discoveredCharacteristic.characteristicId.toString() == "fff2") { - // print("1111111111111111characteristicId:${discoveredCharacteristic.characteristicId} serviceId:${serviceId} deviceId:$deviceId"); - qualifiedCharacteristic = QualifiedCharacteristic(characteristicId: discoveredCharacteristic.characteristicId, serviceId:serviceId , deviceId: deviceId); - } - } - } - } - } - subScribeToCharacteristic(QualifiedCharacteristic(characteristicId: getDiscoveredCharacteristic!.characteristicId, serviceId: Uuid.parse("fff0"), deviceId: deviceId)); - print('Discovering services finished'); - - EventBusManager().eventBusFir(qualifiedCharacteristic); - return result; - } on Exception catch (e) { - print('Error occurred when discovering services: $e'); - rethrow; - } - } + // Future> discoverServices(String deviceMAC, String deviceName, {bool? isFrist = false}) async { + // try { + // // print('Start discovering services for: $deviceId'); + // List result = await _flutterReactiveBle!.discoverServices(deviceMAC); + // // print("mmmmmmmmm$result"); + // if(result.isNotEmpty){ + // for (var i = 0; i < result.length; i++) { + // DiscoveredService discoveredService = result[i]; + // // print("objectdiscoveredService.serviceId.toString() ${discoveredService.serviceId.toString()}"); + // if (discoveredService.serviceId.toString() == "fff0"){ + // // getDiscoveredService = discoveredService; + // for (var j = 0; j < discoveredService.characteristics.length; j++) { + // DiscoveredCharacteristic discoveredCharacteristic = discoveredService.characteristics[j]; + // + // // print("hhhhhhhhhh${result[i].characteristicIds[j].toString()}"); + // if (discoveredCharacteristic.characteristicId.toString() == "fff1") { + // // 订阅用 + // characteristicIdSubscription = discoveredCharacteristic.characteristicId; + // // getDiscoveredCharacteristic = discoveredCharacteristic; + // print("1111111111111111characteristicId:${result[i].characteristicIds[j].toString()} serviceId:${result[i].serviceId} deviceId:$deviceMAC"); + // } + // if (discoveredCharacteristic.characteristicId.toString() == "fff2") { + // // 用来写入 + // characteristicIdWrite= discoveredCharacteristic.characteristicId; + // // print("1111111111111111characteristicId:${discoveredCharacteristic.characteristicId} serviceId:${serviceId} deviceId:$deviceId"); + // // qualifiedCharacteristic = QualifiedCharacteristic(characteristicId: discoveredCharacteristic.characteristicId, serviceId:serviceId , deviceId: deviceId); + // } + // } + // } + // } + // } + // _subScribeToCharacteristic(QualifiedCharacteristic(characteristicId: characteristicIdSubscription, serviceId: Uuid.parse("fff0"), deviceId: deviceMAC)); + // print('Discovering services finished'); + // + // if(isFrist == true){ + // // 第一次添加锁的时候需要先获取公钥 + // IoSenderManage.getPublicKey(lockId: deviceName); + // } + // return result; + // } on Exception catch (e) { + // print('Error occurred when discovering services: $e'); + // rethrow; + // } + // } // 听上报来的数据,参数来自前面扫描到的结果 - subScribeToCharacteristic(QualifiedCharacteristic characteristic) { + _subScribeToCharacteristic(QualifiedCharacteristic characteristic) { _flutterReactiveBle!.subscribeToCharacteristic(characteristic).listen((data) { // code to handle incoming data print("subscribeToCharacteristic: deviceId = ${characteristic.deviceId} characteristicId =${characteristic.characteristicId}---上报来的数据data = $data"); CommandReciverManager.appDataReceive(data); }, onError: (dynamic error) { + EasyLoading.dismiss(); print("subscribeToCharacteristic error:$error"); }); } // 写入 - Future writeCharacteristicWithResponse(QualifiedCharacteristic characteristic, List value) async { + Future writeCharacteristicWithResponse(List value) async { + QualifiedCharacteristic characteristic = QualifiedCharacteristic(characteristicId: characteristicIdWrite, serviceId: serviceId, deviceId: connectDeviceId); print('Write with characteristicId:${characteristic.characteristicId} serviceId:${characteristic.serviceId} deviceId:${characteristic.deviceId} value : $value \nhexStr:${radixHex16String(value)}'); try { diff --git a/star_lock/lib/blue/io_protocol/io_addUser.dart b/star_lock/lib/blue/io_protocol/io_addUser.dart index c8d5385f..4bd21ab5 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("addUserLockIDLength:$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("authUserIDLength:$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("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("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); @@ -131,12 +131,11 @@ class AddUserCommand extends SenderProtocol { authCodeData.addAll(utf8.encode(keyID!)); //token 4 首次请求 Token 填 0,如果锁需要鉴权操作者身份,则会分配动态口令并在应答消息中返回,二次请求时带上。 - print("token!.reversed.toList():${token!.reversed.toList()}"); authCodeData.addAll(token!); authCodeData.addAll(publicKey!); - print("addUser-authCodeData:$authCodeData"); + print("${commandType!.typeValue}-authCodeData:$authCodeData"); // 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode var authCode = crypto.md5.convert(authCodeData); @@ -151,7 +150,7 @@ class AddUserCommand extends SenderProtocol { data.add(0); } } - print("addUserSM4Data:$data"); + print("${commandType!.typeValue} 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 new file mode 100644 index 00000000..bc9a0ce0 --- /dev/null +++ b/star_lock/lib/blue/io_protocol/io_deletUser.dart @@ -0,0 +1,110 @@ + + +//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 DeletUserCommand extends SenderProtocol { + + String? lockID; + String? authUserID; + String? keyID; + String? delUserID; + int? needAuthor; + List? publicKey; + List? privateKey; + List? token; + DeletUserCommand({ + this.lockID, + this.authUserID, + this.keyID, + this.delUserID, + this.needAuthor, + this.publicKey, + this.privateKey, + this.token + }) : super(CommandType.deletUser); + + @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}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!)}"); + data.addAll(utf8.encode(keyID!)); + data = getFixedLengthList(data, 40 - keyIDLength); + + 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!.typeValue} SM4Data:$data"); + // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 + ebcData = SM4.encrypt(data, key: privateKey, mode: SM4CryptoMode.ECB); + return ebcData; + } +} + +class DeletUserReply extends Reply { + DeletUserReply.parseData(CommandType commandType, List dataDetail) + : super.parseData(commandType, dataDetail) { + print("$commandType:$dataDetail"); + data = dataDetail; + } +} diff --git a/star_lock/lib/blue/io_protocol/io_getLockStatu.dart b/star_lock/lib/blue/io_protocol/io_getLockStatu.dart index 0b32987e..96d7d358 100644 --- a/star_lock/lib/blue/io_protocol/io_getLockStatu.dart +++ b/star_lock/lib/blue/io_protocol/io_getLockStatu.dart @@ -61,5 +61,74 @@ class GetLockStatuReply extends Reply { GetLockStatuReply.parseData(CommandType commandType, List dataDetail) : super.parseData(commandType, dataDetail) { data = dataDetail; + + int status = data[2]; + switch(status){ + case 0x00: + //成功 + print("获取锁状态数据解析成功"); + var softVersion = data.sublist(3, 7); + // print("softVersion:$softVersion"); + + var power = data[7]; + // print("power:$power"); + + // APP 用户数量 + var appUserCount = data.sublist(50, 53); + // print("appUserCount:$appUserCount"); + + // 黑名单用户数量 + var blacklistCount = data[53]; + // print("blacklistCount:$blacklistCount"); + + // 蓝牙钥匙数量 + var bleKeyCount = data[54]; + // print("bleKeyCount:$bleKeyCount"); + + // 剩余可添加用户数量 + var remainCount = data.sublist(54, 56); + // print("remainCount:$remainCount"); + + // 未上传开锁记录数量 + var notUploadCount = data.sublist(56, 58); + // print("notUploadCount:$notUploadCount"); + + // 已设置开门密码数量 + var pwdCount = data[58]; + // print("pwdCount:$pwdCount"); + + // 已设置开门指纹数量 + var fingerprintCount = data[59]; + // print("fingerprintCount:$fingerprintCount"); + + // 锁当前时间 + var lockTime = data.sublist(60, 64); + // print("lockTime:$lockTime"); + + // 硬件版本信息,为固件升级提供判断依据 + var hardVersion = data.sublist(64, 68); + // print("hardVersion:$hardVersion"); + break; + case 0x06: + //无权限 + // print("获取锁状态需要鉴权"); + + break; + case 0x07: + //无权限 + print("获取锁状态用户无权限"); + + break; + case 0x09: + // 权限校验错误 + print("获取锁状态权限校验错误"); + + break; + default: + //失败 + print("获取锁状态领锁失败"); + + break; + } } } \ No newline at end of file diff --git a/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart b/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart index 22a2ac89..c422f7e0 100644 --- a/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart +++ b/star_lock/lib/blue/io_protocol/io_getPrivateKey.dart @@ -1,11 +1,10 @@ import 'dart:convert'; +import 'package:star_lock/blue/blue_manage.dart'; import 'package:star_lock/blue/sm4Encipher/sm4.dart'; -import '../../tools/storage.dart'; import '../io_tool/io_manager.dart'; import '../io_tool/io_tool.dart'; -import '../sender_manage.dart'; import 'io_reply.dart'; import 'io_sender.dart'; import 'io_type.dart'; @@ -109,7 +108,7 @@ class GetPrivateKeyCommand extends SenderProtocol { } // print("SM4Data:$data"); // 拿到数据之后通过LockId进行SM4 ECB加密 key:544d485f633335373034383064613864 - ebcData = SM4.encrypt(data, key: utf8.encode(IoManager().getCurrentDeviceLockId), mode: SM4CryptoMode.ECB); + ebcData = SM4.encrypt(data, key: utf8.encode(BlueManage().connectDeviceName), mode: SM4CryptoMode.ECB); return ebcData; } diff --git a/star_lock/lib/blue/io_tool/cbc.dart b/star_lock/lib/blue/io_tool/cbc.dart deleted file mode 100644 index b9c685c2..00000000 --- a/star_lock/lib/blue/io_tool/cbc.dart +++ /dev/null @@ -1,97 +0,0 @@ - -class cbc{ - List Sbox = [ - 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, - 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, - 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, - 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6, - 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8, - 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35, - 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87, - 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e, - 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1, - 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3, - 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f, - 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51, - 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8, - 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0, - 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84, - 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48 - ]; - - List CK = [ - 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, - 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, - 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, - 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, - 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, - 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, - 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, - 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279 - ]; - - List FK = [ - 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc - ]; -} - -/** - * ECB加密模式 - * @example encrypt_cbc("1234", "1234567890123456") => "woPrxebr8Xvyo1qG8QxAUA==" - * @param {any} plaintext 要加密的数据 - * @param {String} key - * @param {String} iv - * @param {String} mode base64 | "text" - * @returns {String} 加密后的字符串 - */ - -List encryptEcb (plaintext, key, keyIsStr, {mode = "base64"}) { - List list = []; -//let encryptRoundKeys = EncryptRoundKeys(stringToArray(key)); - var encryptRoundKeys; -// if (keyIsStr) { -// encryptRoundKeys = EncryptRoundKeys(stringToArray(key)); -// } else { -// encryptRoundKeys = EncryptRoundKeys(key); -// } -// let plainByteArray = plaintext; //stringToArray(plaintext); -// let padded = padding(plainByteArray); -// let blockTimes = padded.length / UINT8_BLOCK; -// let outArray = []; -// // CBC mode -// // init chain with iv (transform to uint32 block) -// for (let i = 0; i < blockTimes; i++) { -// // extract the 16 bytes block data for this round to encrypt -// let roundIndex = i * UINT8_BLOCK; -// let block = getChainBlock(padded, roundIndex); -// let cipherBlock = doBlockCrypt(block, encryptRoundKeys); -// for (let l = 0; l < UINT8_BLOCK; l++) { -// outArray[roundIndex + l] = -// cipherBlock[parseInt(l / 4)] >> ((3 - l) % 4 * 8) & 0xff; -// } -// } - return list; -} - -List EncryptRoundKeys(key) { - var keys = []; - var mk = [ - key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3], - key[4] << 24 | key[5] << 16 | key[6] << 8 | key[7], - key[8] << 24 | key[9] << 16 | key[10] << 8 | key[11], - key[12] << 24 | key[13] << 16 | key[14] << 8 | key[15] - ]; - - // var k = List(36); - // k[0] = mk[0] ^ FK[0]; - // k[1] = mk[1] ^ FK[1]; - // k[2] = mk[2] ^ FK[2]; - // k[3] = mk[3] ^ FK[3]; - // - // for (int i = 0; i < 32; i++) { - // k[i + 4] = k[i] ^ tTransform2(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i]); - // keys[i] = k[i + 4]; - // } - - return keys; -} diff --git a/star_lock/lib/blue/reciver_data.dart b/star_lock/lib/blue/reciver_data.dart index 14274f54..a5aaced5 100644 --- a/star_lock/lib/blue/reciver_data.dart +++ b/star_lock/lib/blue/reciver_data.dart @@ -2,6 +2,9 @@ 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_getLockStatu.dart'; import '../tools/storage.dart'; @@ -43,7 +46,7 @@ class CommandReciverManager { // oriDataList.add(data[12 + i]); // } oriDataList = data.sublist(12, 12 + dataLen); - print("不加密 oriDataList:$oriDataList"); + // print("不加密 oriDataList:$oriDataList"); break; case 1: //AES128 @@ -55,9 +58,9 @@ class CommandReciverManager { // 解密 // String key = SM4.createHexKey(key: IoManager().getCurrentDeviceLockId); - oriDataList = SM4.decrypt(getDataList, key: utf8.encode(IoManager().getCurrentDeviceLockId), mode: SM4CryptoMode.ECB); + oriDataList = SM4.decrypt(getDataList, key: utf8.encode(BlueManage().connectDeviceName), mode: SM4CryptoMode.ECB); oriDataList = oriDataList.sublist(0, oriLen); - print("SM4 oriDataList:$oriDataList"); + // print("SM4 oriDataList:$oriDataList"); break; case 3: //SM4(设备指定密钥) @@ -66,17 +69,18 @@ class CommandReciverManager { var res = await Storage.getStringList(saveBluePrivateKey); List getPrivateKeyList = changeStringListToIntList(res!); - print("getPrivateKeyList$getPrivateKeyList"); + // print("getPrivateKeyList$getPrivateKeyList"); // 解密 // String key = SM4.createHexKey(key: radixHex16String(getPrivateKeyList!)); oriDataList = SM4.decrypt(getDataList, key: getPrivateKeyList, mode: SM4CryptoMode.ECB); oriDataList = oriDataList.sublist(0, oriLen); - print("SM4 oriDataList:$oriDataList"); + // print("SM4 oriDataList:$oriDataList"); break; } parseData(oriDataList).then((value) { + EasyLoading.dismiss(); EventBusManager().eventBusFir(value); }); } @@ -89,7 +93,7 @@ class CommandReciverManager { CommandType commandType = ExtensionCommandType.getCommandType(cmd); await IoManager().increaseCommandIndex(); // data.removeRange(0, 2); - print("111111data cmd:$cmd commandType:$commandType data:$data"); + // print("111111data cmd:$cmd commandType:$commandType data:$data"); var reply; switch(commandType) { case CommandType.getLockPublicKey: @@ -107,6 +111,11 @@ class CommandReciverManager { reply = AddUserReply.parseData(commandType, data); } break; + case CommandType.deletUser: + { + reply = DeletUserReply.parseData(commandType, data); + } + break; case CommandType.openLock: { reply = OpenDoorReply.parseData(commandType, data); diff --git a/star_lock/lib/blue/sender_manage.dart b/star_lock/lib/blue/sender_manage.dart index aa0496ed..f11ef8e1 100644 --- a/star_lock/lib/blue/sender_manage.dart +++ b/star_lock/lib/blue/sender_manage.dart @@ -1,4 +1,5 @@ +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'; @@ -71,6 +72,30 @@ class IoSenderManage { ), callBack:callBack); } + //todo:删除 + static void deletUser({ + String? lockID, + String? authUserID, + String? keyID, + String? delUserID, + int? needAuthor, + List? publicKey, + List? privateKey, + List? token, + CommandSendCallBack? callBack}) { + CommandSenderManager().managerSendData( + command: DeletUserCommand( + lockID: lockID, + authUserID: authUserID, + keyID: keyID, + delUserID:delUserID, + needAuthor: needAuthor, + publicKey: publicKey, + privateKey: privateKey, + token: token + ), callBack:callBack); + } + //todo:开锁 static void senderOpenLock({ String? keyID, diff --git a/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_logic.dart b/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_logic.dart new file mode 100644 index 00000000..f84c95b5 --- /dev/null +++ b/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_logic.dart @@ -0,0 +1,112 @@ + + +import 'dart:async'; + +import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; +import 'package:star_lock/blue/io_protocol/io_deletUser.dart'; + +import '../../../../blue/blue_manage.dart'; +import '../../../../blue/io_protocol/io_reply.dart'; +import '../../../../blue/io_tool/io_manager.dart'; +import '../../../../blue/io_tool/io_tool.dart'; +import '../../../../blue/io_tool/manager_event_bus.dart'; +import '../../../../blue/sender_manage.dart'; +import '../../../../tools/baseGetXController.dart'; +import '../../../../tools/storage.dart'; +import 'lockSet_state.dart'; + +class LockSetLogic extends BaseGetXController{ + final LockSetState state = LockSetState(); + + late StreamSubscription _replySubscription; + void _initReplySubscription() { + _replySubscription = EventBusManager().eventBus!.on().listen((reply) async { + if(reply is DeletUserReply) { + var tokenData = reply.data.sublist(2, 6); + var saveStrList = changeIntListToStringList(tokenData); + print("openDoorToken:$tokenData"); + Storage.setStringList(saveBlueToken, saveStrList); + + int status = reply.data[6]; + switch(status){ + case 0x00: + //成功 + print("${reply.commandType}解析成功"); + + break; + case 0x06: + //无权限 + print("${reply.commandType}需要鉴权"); + + break; + case 0x07: + //无权限 + print("${reply.commandType}用户无权限"); + + break; + case 0x09: + // 权限校验错误 + print("${reply.commandType}权限校验错误"); + + break; + default: + //失败 + print("${reply.commandType}失败"); + + break; + } + } + }); + } + + Future deletUserAction() async { + BlueManage().judgeReconnect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", (DeviceConnectionState state) async { + if (state == DeviceConnectionState.connected){ + var privateKey = await Storage.getStringList(saveBluePrivateKey); + List getPrivateKeyList = changeStringListToIntList(privateKey!); + + var publicKey = await Storage.getStringList(saveBluePublicKey); + List publicKeyDataList = changeStringListToIntList(publicKey!); + + var token = await Storage.getStringList(saveBlueToken); + List getTokenList = changeStringListToIntList(token!); + print("openDoorTokenPubToken:$getTokenList"); + + IoSenderManage.deletUser( + lockID:BlueManage().connectDeviceName, + authUserID:"100001", + keyID:"1", + delUserID:"100001", + needAuthor:1, + publicKey:publicKeyDataList, + privateKey:getPrivateKeyList, + token: getTokenList + ); + } + }); + } + + @override + void onReady() { + // TODO: implement onReady + super.onReady(); + print("onReady()"); + _initReplySubscription(); + } + + @override + void onInit() { + // TODO: implement onInit + super.onInit(); + print("onInit()"); + + + } + + @override + void onClose() { + // TODO: implement onClose + _replySubscription.cancel(); + } + +} \ No newline at end of file diff --git a/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_page.dart b/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_page.dart index ea183b7e..ad898cbf 100644 --- a/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_page.dart +++ b/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_page.dart @@ -9,6 +9,7 @@ import '../../../../tools/commonItem.dart'; import '../../../../tools/submitBtn.dart'; import '../../../../tools/titleAppBar.dart'; import '../../../../translations/trans_lib.dart'; +import 'lockSet_logic.dart'; class LockSetPage extends StatefulWidget { const LockSetPage({Key? key}) : super(key: key); @@ -18,6 +19,9 @@ class LockSetPage extends StatefulWidget { } class _LockSetPageState extends State { + final logic = Get.put(LockSetLogic()); + final state = Get.find().state; + @override Widget build(BuildContext context) { return Scaffold( @@ -226,7 +230,9 @@ class _LockSetPageState extends State { child: SubmitBtn( btnName: TranslationLoader.lanKeys!.delete!.tr, isDelete: true, - onClick: () {}), + onClick: () { + logic.deletUserAction(); + }), ), ], ), diff --git a/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_state.dart b/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_state.dart new file mode 100644 index 00000000..273feb6e --- /dev/null +++ b/star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_state.dart @@ -0,0 +1,5 @@ + + +class LockSetState { + +} \ No newline at end of file diff --git a/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart b/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart index 42c2a2da..ed371a2f 100644 --- a/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart +++ b/star_lock/lib/main/lockDetail/lockDetail/lockDetail_logic.dart @@ -53,12 +53,12 @@ class LockDetailLogic extends BaseGetXController{ switch(status){ case 0x00: //成功 - print("开锁数据解析成功"); + print("${reply.commandType}数据解析成功"); break; case 0x06: //无权限 - print("开锁需要鉴权"); + print("${reply.commandType}需要鉴权"); IoSenderManage.senderOpenLock( keyID: "1", userID: "100001", @@ -73,135 +73,134 @@ class LockDetailLogic extends BaseGetXController{ break; case 0x07: //无权限 - print("开锁用户无权限"); + print("${reply.commandType}用户无权限"); break; case 0x09: // 权限校验错误 - print("开锁权限校验错误"); + print("${reply.commandType}校验错误"); break; default: //失败 - print("开锁协议领锁失败"); + print("${reply.commandType}失败"); break; } } Future _replyGetLockStatus(Reply reply) async { - var softVersion = reply.data.sublist(3, 7); - print("softVersion:$softVersion"); - - var power = reply.data[7]; - print("power:$power"); - - // APP 用户数量 - var appUserCount = reply.data.sublist(50, 53); - print("appUserCount:$appUserCount"); - - // 黑名单用户数量 - var blacklistCount = reply.data[53]; - print("blacklistCount:$blacklistCount"); - - // 蓝牙钥匙数量 - var bleKeyCount = reply.data[54]; - print("bleKeyCount:$bleKeyCount"); - - // 剩余可添加用户数量 - var remainCount = reply.data.sublist(54, 56); - print("remainCount:$remainCount"); - - // 未上传开锁记录数量 - var notUploadCount = reply.data.sublist(56, 58); - print("notUploadCount:$notUploadCount"); - - // 已设置开门密码数量 - var pwdCount = reply.data[58]; - print("pwdCount:$pwdCount"); - - // 已设置开门指纹数量 - var fingerprintCount = reply.data[59]; - print("fingerprintCount:$fingerprintCount"); - - // 锁当前时间 - var lockTime = reply.data.sublist(60, 64); - print("lockTime:$lockTime"); - - // 硬件版本信息,为固件升级提供判断依据 - var hardVersion = reply.data.sublist(64, 68); - print("hardVersion:$hardVersion"); - int status = reply.data[2]; switch(status){ case 0x00: //成功 - print("获取锁状态数据解析成功"); + print("${reply.commandType}数据解析成功"); + var softVersion = reply.data.sublist(3, 7); + print("softVersion:$softVersion"); + + var power = reply.data[7]; + print("power:$power"); + + // APP 用户数量 + var appUserCount = reply.data.sublist(50, 53); + print("appUserCount:$appUserCount"); + + // 黑名单用户数量 + var blacklistCount = reply.data[53]; + print("blacklistCount:$blacklistCount"); + + // 蓝牙钥匙数量 + var bleKeyCount = reply.data[54]; + print("bleKeyCount:$bleKeyCount"); + + // 剩余可添加用户数量 + var remainCount = reply.data.sublist(54, 56); + print("remainCount:$remainCount"); + + // 未上传开锁记录数量 + var notUploadCount = reply.data.sublist(56, 58); + print("notUploadCount:$notUploadCount"); + + // 已设置开门密码数量 + var pwdCount = reply.data[58]; + print("pwdCount:$pwdCount"); + + // 已设置开门指纹数量 + var fingerprintCount = reply.data[59]; + print("fingerprintCount:$fingerprintCount"); + + // 锁当前时间 + var lockTime = reply.data.sublist(60, 64); + print("lockTime:$lockTime"); + + // 硬件版本信息,为固件升级提供判断依据 + var hardVersion = reply.data.sublist(64, 68); + print("hardVersion:$hardVersion"); break; case 0x06: //无权限 - print("获取锁状态需要鉴权"); + print("${reply.commandType}需要鉴权"); break; case 0x07: //无权限 - print("获取锁状态用户无权限"); + print("${reply.commandType}用户无权限"); break; case 0x09: // 权限校验错误 - print("获取锁状态权限校验错误"); + print("${reply.commandType}权限校验错误"); break; default: //失败 - print("获取锁状态领锁失败"); + print("${reply.commandType}失败"); break; } } Future openDoorAction() async { - var privateKey = await Storage.getStringList(saveBluePrivateKey); - List getPrivateKeyList = changeStringListToIntList(privateKey!); - IoSenderManage.senderGetLockStatu( - lockID:IoManager().getCurrentDeviceLockId, - userID:"100001", - privateKey:getPrivateKeyList, - ); + BlueManage().judgeReconnect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", (DeviceConnectionState state) async { + if (state == DeviceConnectionState.connected){ + var privateKey = await Storage.getStringList(saveBluePrivateKey); + List getPrivateKeyList = changeStringListToIntList(privateKey!); - // var privateKey = await Storage.getStringList(saveBluePrivateKey); - // List getPrivateKeyList = changeStringListToIntList(privateKey!); - // - // var signKey = await Storage.getStringList(saveBlueSignKey); - // List signKeyDataList = changeStringListToIntList(signKey!); - // - // var token = await Storage.getStringList(saveBlueToken); - // List getTokenList = changeStringListToIntList(token!); - // print("openDoorTokenPubToken:$getTokenList"); - // - // IoSenderManage.senderOpenLock( - // keyID: "1", - // userID: "100001", - // openMode: 1, - // openTime: 0x11223344, - // token: getTokenList, - // needAuthor: 1, - // signKey: signKeyDataList, - // privateKey: getPrivateKeyList, - // ); + var signKey = await Storage.getStringList(saveBlueSignKey); + List signKeyDataList = changeStringListToIntList(signKey!); + + var token = await Storage.getStringList(saveBlueToken); + List getTokenList = changeStringListToIntList(token!); + print("openDoorTokenPubToken:$getTokenList"); + + IoSenderManage.senderOpenLock( + keyID: "1", + userID: "100001", + openMode: 1, + openTime: 0x11223344, + token: getTokenList, + needAuthor: 1, + signKey: signKeyDataList, + privateKey: getPrivateKeyList, + ); + } + }); } Future connectBlue() async { - await BlueManage().connect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d"); - // var privateKey = await Storage.getStringList(saveBluePrivateKey); - // List getPrivateKeyList = changeStringListToIntList(privateKey!); - // IoSenderManage.senderGetLockStatu( - // lockID:IoManager().getCurrentDeviceLockId, - // userID:"100001", - // privateKey:getPrivateKeyList, - // ); + // 进来之后首先连接 + // BlueManage().connect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", connectStateCallBack: (DeviceConnectionState state) async { + // if (state == DeviceConnectionState.connected){ + // var privateKey = await Storage.getStringList(saveBluePrivateKey); + // List getPrivateKeyList = changeStringListToIntList(privateKey!); + // IoSenderManage.senderGetLockStatu( + // lockID:BlueManage().connectDeviceName, + // userID:"100001", + // privateKey:getPrivateKeyList, + // ); + // } + // }); } @override @@ -209,7 +208,6 @@ class LockDetailLogic extends BaseGetXController{ // TODO: implement onReady super.onReady(); print("onReady()"); - // _initSendStreamSubscription(); _initReplySubscription(); } @@ -226,7 +224,6 @@ class LockDetailLogic extends BaseGetXController{ @override void onClose() { // TODO: implement onClose - // _sendStreamSubscription.cancel(); _replySubscription.cancel(); } } \ No newline at end of file diff --git a/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart b/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart index 4ab98bd3..a83dc66c 100644 --- a/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart +++ b/star_lock/lib/mine/addLock/nearbyLock/nearbyLock_logic.dart @@ -24,39 +24,10 @@ class NearbyLockLogic extends BaseGetXController{ // 点击复合要求的设备之后 连接 void connect(String lockId, String deviceName){ - BlueManage().connect(lockId, deviceName); + BlueManage().connect(lockId, deviceName, isFrist: false); } - // 监听获取的特征值 - late StreamSubscription _streamSubscription; - void _startListenIO(){ - _streamSubscription = EventBusManager().eventBus!.on().listen((event) async { - IoSenderManage.getPublicKey(lockId:state.seletLockName.value); - }); - } - - // 获取公钥 - // void getPublicKey(String lockId){ - // // print("seletGetPublicKey:${lockId}"); - // IoSenderManage.getPublicKey(lockId); - // } - - // 组装好数据后监听要发送消息的事件 - // late StreamSubscription _sendStreamSubscription; - // void _initSendStreamSubscription() { - // _sendStreamSubscription = EventBusManager().eventBus!.on().listen(( - // EventSendModel model) { - // if (model.sendChannel == DataChannel.ble) { - // // print("fsdfgsdfgsdfgsdfgsdfgsdfg:${BlueManage().qualifiedCharacteristic}"); - // BlueManage().writeCharacteristicWithResponse(QualifiedCharacteristic( - // deviceId:BlueManage().qualifiedCharacteristic!.deviceId, - // characteristicId: BlueManage().qualifiedCharacteristic!.characteristicId, - // serviceId: BlueManage().serviceId), - // model.data); - // } - // }); - // } - + // 获取解析后的数据 late StreamSubscription _replySubscription; void _initReplySubscription() { _replySubscription = EventBusManager().eventBus!.on().listen((reply) { @@ -86,7 +57,7 @@ class NearbyLockLogic extends BaseGetXController{ Storage.setStringList(saveBluePublicKey, saveStrList); IoSenderManage.getPrivateKey( - lockId:IoManager().getCurrentDeviceLockId, + lockId:BlueManage().connectDeviceName, keyID:"1", authUserID:"1", nowTime:1, @@ -133,7 +104,7 @@ class NearbyLockLogic extends BaseGetXController{ print("privateKey:$privateKey signKey:$signKey"); IoSenderManage.senderAddUser( - lockID:IoManager().getCurrentDeviceLockId, + lockID:BlueManage().connectDeviceName, authUserID:"100001", keyID:"1", userID:"100001", @@ -193,7 +164,7 @@ class NearbyLockLogic extends BaseGetXController{ List publicKeyDataList = changeStringListToIntList(publicKey!); IoSenderManage.senderAddUser( - lockID:IoManager().getCurrentDeviceLockId, + lockID:BlueManage().connectDeviceName, authUserID:"100001", keyID:"1", userID:"100001", @@ -233,8 +204,6 @@ class NearbyLockLogic extends BaseGetXController{ super.onReady(); print("onReady()"); - _startListenIO(); - // _initSendStreamSubscription(); _initReplySubscription(); } @@ -255,8 +224,6 @@ class NearbyLockLogic extends BaseGetXController{ void onClose() { // TODO: implement onClose super.onClose(); - _streamSubscription.cancel(); - // _sendStreamSubscription.cancel(); _replySubscription.cancel(); } diff --git a/star_lock/lib/tools/seletImgTool.dart b/star_lock/lib/tools/seletImgTool.dart index c9181105..b39ca539 100644 --- a/star_lock/lib/tools/seletImgTool.dart +++ b/star_lock/lib/tools/seletImgTool.dart @@ -20,6 +20,7 @@ class SeletImageTool { if (cameraImages != null) { // 获取图像地址 _imageProvider = FileImage(File(cameraImages!.path)); + callback(_imageProvider); } else { _imageProvider = null; } @@ -40,6 +41,7 @@ class SeletImageTool { if (pickedFile != null) { // 获取图像地址 _imageProvider = FileImage(File(pickedFile!.path)); + callback(_imageProvider); } else { _imageProvider = null; } diff --git a/star_lock/lib/tools/storage.dart b/star_lock/lib/tools/storage.dart index 37cc3040..2a440045 100644 --- a/star_lock/lib/tools/storage.dart +++ b/star_lock/lib/tools/storage.dart @@ -4,18 +4,18 @@ import 'package:shared_preferences/shared_preferences.dart'; class Storage { - // Storage._internal(); - // - // factory Storage() => _instance; - // - // static late final Storage _instance = Storage._internal(); - // - // static late SharedPreferences _preferences; - // - // static Future getInstance() async { - // _preferences = await SharedPreferences.getInstance(); - // return _instance; - // } + Storage._internal(); + + factory Storage() => _instance; + + static late final Storage _instance = Storage._internal(); + + static late SharedPreferences _preferences; + + static Future getInstance() async { + _preferences = await SharedPreferences.getInstance(); + return _instance; + } // ///存数据 // static Future setData(key, value) async {