232 lines
6.1 KiB
Dart
232 lines
6.1 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.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 '../../../blue/blue_manage.dart';
|
|
import '../../../blue/io_protocol/io_addUser.dart';
|
|
import '../../../blue/io_protocol/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/storage.dart';
|
|
import 'nearbyLock_state.dart';
|
|
|
|
class NearbyLockLogic extends BaseGetXController{
|
|
|
|
final NearbyLockState state = NearbyLockState();
|
|
|
|
// 点击复合要求的设备之后 连接
|
|
void connect(String lockId, String deviceName){
|
|
BlueManage().connect(lockId, deviceName, isFrist: false);
|
|
}
|
|
|
|
// 获取解析后的数据
|
|
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);
|
|
}
|
|
|
|
if(reply is AddUserReply) {
|
|
_replyAddUserKey(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
void _replyGetPublicKey(Reply reply){
|
|
// 获取公钥
|
|
switch(reply.status){
|
|
case 0x00:
|
|
//成功
|
|
print("获取公钥成功");
|
|
// 储存公钥
|
|
var tokenData = reply.data.sublist(3);
|
|
var saveStrList = changeIntListToStringList(tokenData);
|
|
Storage.setStringList(saveBluePublicKey, saveStrList);
|
|
|
|
IoSenderManage.getPrivateKey(
|
|
lockId:BlueManage().connectDeviceName,
|
|
keyID:"1",
|
|
authUserID:"1",
|
|
nowTime:1,
|
|
publicKeyData:tokenData,
|
|
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);
|
|
|
|
// 私钥
|
|
List<int> privateKey = reply.data.sublist(0, 16);
|
|
var savePrivateKeyList = changeIntListToStringList(privateKey);
|
|
Storage.setStringList(saveBluePrivateKey, savePrivateKeyList);
|
|
|
|
// signKey
|
|
List<int> signKey = reply.data.sublist(16);
|
|
var savesignKeyList = changeIntListToStringList(signKey);
|
|
Storage.setStringList(saveBlueSignKey, savesignKeyList);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> getTokenList = [0,0,0,0];
|
|
if(token != null){
|
|
getTokenList = changeStringListToIntList(token);
|
|
}
|
|
|
|
print("privateKey:$privateKey signKey:$signKey");
|
|
IoSenderManage.senderAddUser(
|
|
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:privateKey,
|
|
token: getTokenList
|
|
);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print('获取私钥无权限');
|
|
|
|
break;
|
|
case 0x0f:
|
|
//用户已存在
|
|
print('获取私钥:用户已存在');
|
|
break;
|
|
default:
|
|
//失败
|
|
print('获取私钥失败');
|
|
break;
|
|
}
|
|
}
|
|
|
|
Future<void> _replyAddUserKey(Reply reply) async {
|
|
var lockId = reply.data.sublist(2, 42);
|
|
print("lockId:$lockId");
|
|
|
|
var token = reply.data.sublist(42, 46);
|
|
List<String> strTokenList = changeIntListToStringList(token);
|
|
Storage.setStringList(saveBlueToken, strTokenList);
|
|
print("token:$token");
|
|
|
|
int status = reply.data[46];
|
|
print("status:$status");
|
|
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("添加用户数据解析成功");
|
|
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("需要鉴权");
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
|
|
IoSenderManage.senderAddUser(
|
|
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("用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("添加用户权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("领锁失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
// 进来第一步开始扫描
|
|
BlueManage().startScan((v){
|
|
state.devices.clear();
|
|
state.devices.addAll(v);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
|
|
}
|
|
|