235 lines
6.2 KiB
Dart
235 lines
6.2 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);
|
|
}
|
|
|
|
// 监听蓝牙连接的状态
|
|
late StreamSubscription _streamSubscription;
|
|
void _startListenIO(){
|
|
_streamSubscription = EventBusManager().eventBus!.on<QualifiedCharacteristic>().listen((event) async {
|
|
IoSenderManage.getPublicKey(lockId:state.seletLockName.value);
|
|
});
|
|
}
|
|
|
|
// 获取公钥
|
|
// void getPublicKey(String lockId){
|
|
// // print("seletGetPublicKey:${lockId}");
|
|
// IoSenderManage.getPublicKey(lockId);
|
|
// }
|
|
|
|
// 组装好数据后监听要发送消息的事件
|
|
late StreamSubscription<EventSendModel> _sendStreamSubscription;
|
|
void _initSendStreamSubscription() {
|
|
_sendStreamSubscription = EventBusManager().eventBus!.on<EventSendModel>().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<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:
|
|
//成功
|
|
// 储存公钥
|
|
var tokenData = reply.data.sublist(3);
|
|
var saveStrList = changeIntListToStringList(tokenData);
|
|
Storage.setStringList(saveBluePublicKey, saveStrList);
|
|
IoSenderManage.getPrivateKey(
|
|
lockId:IoManager().getCurrentDeviceLockId,
|
|
keyID:"1",
|
|
authUserID:"1",
|
|
nowTime:1,
|
|
publicKeyData:tokenData,
|
|
needAuthor:1);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
|
|
break;
|
|
case 0x0f:
|
|
//用户已存在
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
void _replyGetPrivateKeyKey(Reply reply){
|
|
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);
|
|
|
|
print("privateKey:$privateKey signKey:$signKey");
|
|
IoSenderManage.senderAddUser(
|
|
lockID:IoManager().getCurrentDeviceLockId,
|
|
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: [0,0,0,0]
|
|
);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print('获取私钥无权限');
|
|
|
|
break;
|
|
case 0x0f:
|
|
//用户已存在
|
|
print('获取私钥:用户已存在');
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print('获取私钥失败');
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
void _replyAddUserKey(Reply reply){
|
|
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("需要鉴权");
|
|
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("领锁失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_startListenIO();
|
|
_initSendStreamSubscription();
|
|
_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();
|
|
_streamSubscription.cancel();
|
|
_sendStreamSubscription.cancel();
|
|
_replySubscription.cancel();
|
|
}
|
|
|
|
}
|
|
|