170 lines
4.9 KiB
Dart
170 lines
4.9 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:star_lock/blue/io_protocol/io_getPrivateKey.dart';
|
|
import 'package:star_lock/blue/io_protocol/io_getPublicKey.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
import '../../../appRouters.dart';
|
|
import '../../../blue/blue_manage.dart';
|
|
import '../../../blue/io_protocol/io_addUser.dart';
|
|
import '../../../blue/io_reply.dart';
|
|
import '../../../blue/io_tool/io_manager.dart';
|
|
import '../../../blue/io_tool/io_model.dart';
|
|
import '../../../blue/io_tool/io_tool.dart';
|
|
import '../../../blue/io_tool/manager_event_bus.dart';
|
|
import '../../../blue/sender_manage.dart';
|
|
import '../../../tools/dateTool.dart';
|
|
import '../../../tools/storage.dart';
|
|
import 'nearbyLock_state.dart';
|
|
|
|
class NearbyLockLogic extends BaseGetXController{
|
|
|
|
final NearbyLockState state = NearbyLockState();
|
|
|
|
// 点击连接设备
|
|
void connect(String lockId, String deviceName){
|
|
// BlueManage().stopScan();
|
|
// BlueManage().judgeReconnect(lockId, deviceName, (DeviceConnectionState state) async {
|
|
// if (state == DeviceConnectionState.connected) {
|
|
// IoSenderManage.getPublicKey(lockId: deviceName);
|
|
// }
|
|
// }, isShowLoading: true);
|
|
BlueManage().connect(lockId, deviceName, isFrist: true, isShowLoading: true);
|
|
}
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if(reply is GetPublicKeyReply) {
|
|
_replyGetPublicKey(reply);
|
|
}
|
|
|
|
if(reply is GetPrivateKeyReply) {
|
|
_replyGetPrivateKeyKey(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _replyGetPublicKey(Reply reply) async {
|
|
// 获取公钥
|
|
switch(reply.status){
|
|
case 0x00:
|
|
//成功
|
|
print("获取公钥成功");
|
|
// 储存公钥
|
|
var publicKey = reply.data.sublist(3);
|
|
var saveStrList = changeIntListToStringList(publicKey);
|
|
Storage.setStringList(saveBluePublicKey, saveStrList);
|
|
|
|
IoSenderManage.getPrivateKey(
|
|
lockId:BlueManage().connectDeviceName,
|
|
keyID:"1",
|
|
authUserID:await Storage.getUid(),
|
|
nowTime:DateTime.now().millisecondsSinceEpoch~/1000,
|
|
publicKeyData:publicKey,
|
|
needAuthor:1);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("获取公钥无权限");
|
|
break;
|
|
case 0x0f:
|
|
//用户已存在
|
|
print("获取公钥用户已存在");
|
|
break;
|
|
default:
|
|
//失败
|
|
print("获取公钥失败");
|
|
break;
|
|
}
|
|
}
|
|
|
|
Future<void> _replyGetPrivateKeyKey(Reply reply) async {
|
|
switch(reply.status){
|
|
case 0x00:
|
|
//成功
|
|
print('获取私钥成功');
|
|
reply.data.removeAt(0);
|
|
print("reply.data:${reply.data}");
|
|
|
|
// 私钥
|
|
List<int> privateKey = reply.data.sublist(0, 16);
|
|
print("privateKey:$privateKey");
|
|
var savePrivateKeyList = changeIntListToStringList(privateKey);
|
|
Storage.setStringList(saveBluePrivateKey, savePrivateKeyList);
|
|
|
|
// signKey
|
|
List<int> signKey = reply.data.sublist(16, 32);
|
|
print("signKey:$signKey");
|
|
var saveSignKeyList = changeIntListToStringList(signKey);
|
|
Storage.setStringList(saveBlueSignKey, saveSignKeyList);
|
|
|
|
// 时间戳
|
|
List<int> timestamp = reply.data.sublist(32, 36);
|
|
int timestampValue = (
|
|
(0xff & timestamp[(0)]) << 24 |
|
|
(0xff & timestamp[1]) << 16 |
|
|
(0xff & timestamp[2]) << 8 |
|
|
(0xFF & timestamp[3]));
|
|
String timestampValueStr = DateTool().dateToYMDHNSString(timestampValue.toString());
|
|
print("timestamp:$timestamp timestampValue:$timestampValue timestampValueStr:$timestampValueStr");
|
|
|
|
Get.toNamed(Routers.lockAddressGaoDePage, arguments: {
|
|
"pwdTimestamp": timestampValue*1000,
|
|
});
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print('获取私钥无权限');
|
|
|
|
break;
|
|
case 0x0f:
|
|
//用户已存在
|
|
print('获取私钥:用户已存在');
|
|
break;
|
|
default:
|
|
//失败
|
|
print('获取私钥失败');
|
|
break;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("NearbyLockLogic onReady()");
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("NearbyLockLogic onInit()");
|
|
|
|
// 进来第一步开始扫描
|
|
BlueManage().startScan(false ,(v){
|
|
state.devices.clear();
|
|
state.devices.addAll(v);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
// BlueManage().stopScan();
|
|
}
|
|
|
|
}
|
|
|