fix: 暂缓,改为sdk接入开发

This commit is contained in:
liyi 2025-09-09 16:51:29 +08:00
parent 59b972f864
commit 9bab2cde56
9 changed files with 72 additions and 147 deletions

View File

@ -8,12 +8,15 @@ PODS:
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
- starcloud (0.0.1):
- Flutter
DEPENDENCIES:
- Flutter (from `Flutter`)
- flutter_blue_plus_darwin (from `.symlinks/plugins/flutter_blue_plus_darwin/darwin`)
- permission_handler_apple (from `.symlinks/plugins/permission_handler_apple/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- starcloud (from `.symlinks/plugins/starcloud/ios`)
EXTERNAL SOURCES:
Flutter:
@ -24,12 +27,15 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/permission_handler_apple/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
starcloud:
:path: ".symlinks/plugins/starcloud/ios"
SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_blue_plus_darwin: 20a08bfeaa0f7804d524858d3d8744bcc1b6dbc3
permission_handler_apple: 4ed2196e43d0651e8ff7ca3483a069d469701f2d
shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7
starcloud: 0d2034397e2a0d81b8c187b99bd2de7371ea5b2d
PODFILE CHECKSUM: 41883e5e56033ab6e4e0f607734d753c5bb7c460

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:starcloud/sdk/starcloud.dart';
import 'package:starwork_flutter/api/base_api_service.dart';
import 'package:starwork_flutter/api/service/common_api_service.dart';
@ -23,6 +24,12 @@ class AppInitialization {
await SharedPreferencesUtils.init();
initEasyLoading();
StarCloudSDK.init(
clientId: F.starCloudClientId,
clientSecret: F.starCloudSecret,
environmentUrl: F.starCloudUrl,
);
Get.put(BaseApiService());
Get.put(StarCloudBaseApiService());
Get.put(CommonApiService(Get.find<BaseApiService>()));

View File

@ -4,7 +4,9 @@ class CacheKeys {
static const String lockPublicKey = 'lockPublicKey';
static const String lockCommKey = 'lockCommKey';
static const String lockSignKey = 'lockSignKey';
static const String starCloudUserInfo = 'starCloudUserInfo';
static const String starCloudUserName = 'starCloudUserName';
static const String starCloudPassword = 'starCloudPassword';
static const String starCloudUid = 'starCloudUid';
static const String starCloudUserLoginInfo = 'starCloudUserLoginInfo';
}

View File

@ -1,6 +1,10 @@
import 'dart:math';
import 'package:get/get.dart';
import 'package:starcloud/entity/star_cloud_lock_list.dart';
import 'package:starcloud/entity/star_cloud_scan_result.dart';
import 'package:starcloud/sdk/sdk_device_operate_extension.dart';
import 'package:starcloud/sdk/starcloud.dart';
import 'package:starwork_flutter/base/app_logger.dart';
import 'package:starwork_flutter/base/app_permission.dart';
import 'package:starwork_flutter/base/base_controller.dart';
@ -24,7 +28,7 @@ class SearchDeviceController extends BaseController {
final RxBool isSearching = false.obs;
//
final RxList<ScanDeviceInfo> deviceList = <ScanDeviceInfo>[].obs;
final RxList<StarCloudScanResult> deviceList = <StarCloudScanResult>[].obs;
//
final RxBool permissionsGranted = false.obs;
@ -34,13 +38,20 @@ class SearchDeviceController extends BaseController {
super.onInit();
//
_initializePermissions();
// await _initializePermissions();
}
@override
void onReady() {
super.onReady();
//
_startBluetoothSearch();
}
@override
void onClose() {
//
BleService().stopBluetoothSearch();
StarCloudSDK.instance.stopScan(onError: (err) {});
isSearching.value = false;
super.onClose();
}
@ -74,25 +85,17 @@ class SearchDeviceController extends BaseController {
///
Future<void> _startBluetoothSearch() async {
if (!permissionsGranted.value) {
AppLogger.warn('⚠️ 权限未就绪,无法开始搜索');
return;
}
try {
AppLogger.highlight('🔍 开始启动蓝牙搜索...');
//
deviceList.clear();
//
isSearching.value = true;
//
BleService().enableBluetoothSearch(onDeviceFound: _onDeviceFound);
//
_updateSearchingStatus();
//
StarCloudSDK.instance.scanDeviceList(onScan: (List<StarCloudScanResult> deviceListResult) {
deviceList.value = deviceListResult;
});
} catch (e, stackTrace) {
AppLogger.error('启动蓝牙搜索失败', error: e, stackTrace: stackTrace);
isSearching.value = false;
@ -100,44 +103,6 @@ class SearchDeviceController extends BaseController {
}
}
///
void _updateSearchingStatus() {
//
Future.delayed(const Duration(seconds: 1), () {
if (isSearching.value) {
bool actuallyScanning = BleService().isScanningNow;
if (isSearching.value != actuallyScanning) {
isSearching.value = actuallyScanning;
if (!actuallyScanning) {
AppLogger.highlight('🔍 搜索已停止');
isSearching.value = false;
}
}
//
if (actuallyScanning) {
_updateSearchingStatus();
}
}
});
}
///
void _onDeviceFound(ScanDeviceInfo device) {
AppLogger.highlight('📲 发现新设备: ${device.advName}');
//
bool exists = deviceList.any((existingDevice) => existingDevice.rawDeviceInfo.device.remoteId == device.rawDeviceInfo.device.remoteId);
if (!exists) {
deviceList.add(device);
deviceList.refresh();
AppLogger.debug('✅ 设备已添加到列表,当前设备数量: ${deviceList.length},设备信息:${deviceList.toString()}');
} else {
AppLogger.debug('⚠️ 设备已存在,跳过添加');
}
}
//
Future<void> refreshDevices() async {
AppLogger.highlight('🔄 开始刷新设备列表');
@ -149,8 +114,7 @@ class SearchDeviceController extends BaseController {
}
try {
//
BleService().stopBluetoothSearch();
StarCloudSDK.instance.stopScan(onError: (err) {});
isSearching.value = false;
//
@ -173,95 +137,29 @@ class SearchDeviceController extends BaseController {
/// 1.
/// 2.
/// 3.
void connectingDevices(ScanDeviceInfo device) async {
void connectingDevices(StarCloudScanResult device) async {
showLoading();
try {
showLoading();
//
BleService().stopBluetoothSearch();
isSearching.value = false;
//
BleCmdGetPublicKey getPublicKeyCmd = BleCmdGetPublicKey(lockId: device.advName);
//
GetPublicKeyResponse? publicKeyResponse = await BleService().sendCommand<GetPublicKeyResponse>(
command: getPublicKeyCmd,
targetDeviceName: device.advName,
//
autoConnectIfNeeded: true,
);
if (publicKeyResponse != null && publicKeyResponse.isSuccess) {
AppLogger.info('🎯 获取公钥成功: ${publicKeyResponse.publicKeyHex}');
await SharedPreferencesUtils.saveIntList(CacheKeys.lockPublicKey, publicKeyResponse.publicKey);
BleCmdGetPrivateKey getPrivateKeyCmd = BleCmdGetPrivateKey(
lockId: device.advName,
keyId: '1',
authUserID: '1',
nowTime: DateTime.now().millisecondsSinceEpoch ~/ 1000,
publicKey: publicKeyResponse.publicKey,
var cacheStarCloudUserName = await SharedPreferencesUtils.getString(CacheKeys.starCloudUserName);
var starCloudPassword = await SharedPreferencesUtils.getString(CacheKeys.starCloudPassword);
var starCloudUid = await SharedPreferencesUtils.getString(CacheKeys.starCloudUid);
if (cacheStarCloudUserName == null || starCloudPassword == null || starCloudUid == null) {
StarCloudSDK.instance.createCloudUser(
onError: (err) {},
onSuccess: (userInfo) {
SharedPreferencesUtils.setString(CacheKeys.starCloudUserName, userInfo.username);
SharedPreferencesUtils.setString(CacheKeys.starCloudPassword, userInfo.password);
SharedPreferencesUtils.setString(CacheKeys.starCloudUid, userInfo.uid.toString());
},
);
//
GetPrivateKeyResponse? privateKeyResponse = await BleService().sendCommand<GetPrivateKeyResponse>(
command: getPrivateKeyCmd,
targetDeviceName: device.advName,
//
autoConnectIfNeeded: true,
);
if (privateKeyResponse != null && privateKeyResponse.isSuccess) {
await SharedPreferencesUtils.saveIntList(CacheKeys.lockCommKey, privateKeyResponse.commKey);
await SharedPreferencesUtils.saveIntList(CacheKeys.lockSignKey, privateKeyResponse.signKey);
AppLogger.info('🎯 获取私钥成功: ${privateKeyResponse.toString()}');
// 便使
BleService.setCachedPrivateKey(privateKeyResponse.commKey);
//
BleCmdReadLockStatus readLockStatusCmd = BleCmdReadLockStatus(
lockId: device.advName,
userId: '1',
timeStamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
localUnix: 0,
privateKey: privateKeyResponse.commKey,
);
//
ReadLockStatusResponse? readLockStatusResponse = await BleService().sendCommand<ReadLockStatusResponse>(
command: readLockStatusCmd,
targetDeviceName: device.advName,
//
autoConnectIfNeeded: true,
);
if (readLockStatusResponse != null && readLockStatusResponse.isSuccess) {
AppLogger.highlight('readLockStatusResponse:${readLockStatusResponse}');
final Random rng = Random();
// 100000 999999
final int number = rng.nextInt(900000) + 100000;
//
BleCmdAddAdmin addAdminCmd = BleCmdAddAdmin(
lockId: device.advName,
userId: '13655',
privateKey: privateKeyResponse.commKey,
authUserId: '13655',
keyId: '1',
startDate: DateTime.now().millisecondsSinceEpoch,
expireDate: 0x11223344,
password: number.toString(),
token: [0, 0, 0, 0],
publicKey: publicKeyResponse.publicKey,
);
//
BleAddAdminResponse? addAdminResponse = await BleService().sendCommand<BleAddAdminResponse>(
command: addAdminCmd,
targetDeviceName: device.advName,
//
autoConnectIfNeeded: true,
);
AppLogger.highlight('addAdminResponse:${addAdminResponse}');
}
}
} else {
AppLogger.warn('⚠️ 命令发送完成,但未收到有效响应');
}
StarCloudSDK.instance.pairDevice(
onError: (err) {},
onSuccess: (StarCloudLock lockInfo) {
AppLogger.highlight('lockInfo:${lockInfo.toString()}');
},
scanResult: device,
);
} catch (e, stackTrace) {
AppLogger.error('连接设备失败', error: e, stackTrace: stackTrace);
showToast('连接失败,请重试');

View File

@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:starcloud/entity/star_cloud_scan_result.dart';
import 'package:starwork_flutter/ble/model/scan_device_info.dart';
import 'package:starwork_flutter/views/device/searchDevice/search_device_controller.dart';
@ -171,7 +172,7 @@ class SearchDeviceView extends GetView<SearchDeviceController> {
);
}
_buildItem({required ScanDeviceInfo device, required int index}) {
_buildItem({required StarCloudScanResult device, required int index}) {
return GestureDetector(
onTap: () {
controller.connectingDevices(device);

View File

@ -1,6 +1,8 @@
import 'dart:async';
import 'package:get/get.dart';
import 'package:starcloud/sdk/entity/cloud_user_info.dart';
import 'package:starcloud/sdk/starcloud.dart';
import 'package:starwork_flutter/api/api_response.dart';
import 'package:starwork_flutter/api/model/common/request/send_validation_code_request.dart';
import 'package:starwork_flutter/api/model/starcloud/request/starcloud_create_user_request.dart';
@ -92,6 +94,7 @@ class InputVerificationCodeController extends BaseController {
CacheKeys.token,
validationCodeLoginResult.data!.token,
);
Get.offAllNamed(AppRoutes.main);
} else {
showError(message: validationCodeLoginResult.errorMsg!);

View File

@ -151,7 +151,7 @@ packages:
source: sdk
version: "0.0.0"
flutter_blue_plus:
dependency: "direct main"
dependency: transitive
description:
name: flutter_blue_plus
sha256: "1901a42ade7a8f9793a3655983ef0899565294b12a2a57a2c3e33813930f4a34"
@ -578,6 +578,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.11.1"
starcloud:
dependency: "direct main"
description:
path: "../starcloud-sdk-flutter"
relative: true
source: path
version: "0.1.2"
stream_channel:
dependency: transitive
description:

View File

@ -36,8 +36,9 @@ dependencies:
carousel_slider: ^5.1.1
# 气泡提示框
super_tooltip: ^2.0.8
# 蓝牙库
flutter_blue_plus: ^1.35.7
# 星云sdk
starcloud:
path: ../starcloud-sdk-flutter
# 加解密库
crypto: ^3.0.3