229 lines
6.6 KiB
Dart
229 lines
6.6 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
import 'package:star_lock/blue/io_protocol/io_getLockStatu.dart';
|
|
|
|
import '../../../blue/blue_manage.dart';
|
|
import '../../../blue/io_protocol/io_openLock.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/baseGetXController.dart';
|
|
import '../../../tools/storage.dart';
|
|
import 'lockDetail_state.dart';
|
|
|
|
class LockDetailLogic extends BaseGetXController{
|
|
final LockDetailState state = LockDetailState();
|
|
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) async {
|
|
if(reply is OpenDoorReply) {
|
|
_replyOpenLock(reply);
|
|
}
|
|
|
|
if(reply is GetLockStatuReply) {
|
|
_replyGetLockStatus(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _replyOpenLock(Reply reply) async {
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
var tokenData = reply.data.sublist(2, 6);
|
|
var saveStrList = changeIntListToStringList(tokenData);
|
|
print("openDoorToken:$tokenData");
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
|
|
|
// 电量
|
|
int power = reply.data[7];
|
|
|
|
int status = reply.data[6];
|
|
print("status:$status");
|
|
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("${reply.commandType}需要鉴权");
|
|
IoSenderManage.senderOpenLock(
|
|
keyID: "1",
|
|
userID: "100001",
|
|
openMode: 1,
|
|
openTime: 0x11223344,
|
|
token: tokenData,
|
|
needAuthor: 1,
|
|
signKey: signKeyDataList,
|
|
privateKey: getPrivateKeyList,
|
|
);
|
|
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("${reply.commandType}用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("${reply.commandType}校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("${reply.commandType}失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
Future<void> _replyGetLockStatus(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
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("${reply.commandType}需要鉴权");
|
|
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("${reply.commandType}用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("${reply.commandType}权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("${reply.commandType}失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
Future<void> openDoorAction() async {
|
|
BlueManage().judgeReconnect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", (DeviceConnectionState state) async {
|
|
if (state == DeviceConnectionState.connected){
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> 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<void> connectBlue() async {
|
|
// 进来之后首先连接
|
|
// BlueManage().connect("AD01447A-30B5-A780-E778-DED3BDCB613E", "TMH_c3570480da8d", connectStateCallBack: (DeviceConnectionState state) async {
|
|
// if (state == DeviceConnectionState.connected){
|
|
// var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
// List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
// IoSenderManage.senderGetLockStatu(
|
|
// lockID:BlueManage().connectDeviceName,
|
|
// userID:"100001",
|
|
// privateKey:getPrivateKeyList,
|
|
// );
|
|
// }
|
|
// });
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
// 进来第一步开始扫描
|
|
connectBlue();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
_replySubscription.cancel();
|
|
}
|
|
} |