317 lines
9.6 KiB
Dart
317 lines
9.6 KiB
Dart
|
|
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
import '../../../appRouters.dart';
|
|
import '../../../blue/blue_manage.dart';
|
|
import '../../../blue/io_protocol/io_addUser.dart';
|
|
import '../../../blue/io_protocol/io_getLockStatu.dart';
|
|
import '../../../blue/io_reply.dart';
|
|
import '../../../blue/io_tool/io_manager.dart';
|
|
import '../../../blue/io_tool/io_tool.dart';
|
|
import '../../../blue/io_tool/manager_event_bus.dart';
|
|
import '../../../blue/sender_manage.dart';
|
|
import '../../../network/api_repository.dart';
|
|
import '../../../tools/baseGetXController.dart';
|
|
import '../../../tools/eventBusEventManage.dart';
|
|
import '../../../tools/storage.dart';
|
|
import 'saveLock_state.dart';
|
|
|
|
class SaveLockLogic extends BaseGetXController {
|
|
|
|
final SaveLockState state = SaveLockState();
|
|
int userNo = 0;
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if(reply is AddUserReply) {
|
|
_replyAddUserKey(reply);
|
|
}
|
|
|
|
// 获取锁状态
|
|
if(reply is GetLockStatuReply) {
|
|
_replyGetLockStatus(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
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");
|
|
|
|
userNo = reply.data[46];
|
|
print("status:$status");
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("添加用户数据解析成功");
|
|
bindBlueAdmin();
|
|
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:await Storage.getUid(),
|
|
keyID:"1",
|
|
userID:await Storage.getUid(),
|
|
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;
|
|
}
|
|
}
|
|
|
|
// 获取锁状态数据解析
|
|
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;
|
|
}
|
|
}
|
|
|
|
// 保存事件,先调用蓝牙,蓝牙调用成功后再调用后台接口
|
|
saveLockAction(){
|
|
addUserConnectBlue();
|
|
}
|
|
|
|
// 添加用户
|
|
Future<void> addUserConnectBlue() async {
|
|
// 进来之后首先连接
|
|
BlueManage().judgeReconnect(BlueManage().connectDeviceMacAddress, BlueManage().connectDeviceName, (DeviceConnectionState state) async {
|
|
if (state == DeviceConnectionState.connected){
|
|
// 私钥
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> getTokenList = [0,0,0,0];
|
|
if(token != null){
|
|
getTokenList = changeStringListToIntList(token);
|
|
}
|
|
|
|
IoSenderManage.senderAddUser(
|
|
lockID: BlueManage().connectDeviceName,
|
|
authUserID:await Storage.getUid(),
|
|
keyID:"1",
|
|
userID:await Storage.getUid(),
|
|
openMode:1,
|
|
keyType:1,
|
|
startDate:DateTime.now().millisecondsSinceEpoch,
|
|
expireDate:0x11223344,
|
|
role:255,
|
|
password:"123456",
|
|
needAuthor:1,
|
|
publicKey:publicKeyDataList,
|
|
privateKey:getPrivateKeyList,
|
|
token: getTokenList
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
void bindBlueAdmin() async{
|
|
var lockDataMap = {};
|
|
lockDataMap['lockName'] = BlueManage().connectDeviceName;
|
|
lockDataMap['lockMac'] = BlueManage().connectDeviceMacAddress;
|
|
|
|
var positionMap = {};
|
|
positionMap['longitude'] = state.addressMap["longitude"];
|
|
positionMap['latitude'] = state.addressMap["latitude"];
|
|
positionMap['country'] = state.addressMap["country"];
|
|
positionMap['province'] = state.addressMap["province"];
|
|
positionMap['city'] = state.addressMap["city"];
|
|
positionMap['district'] = state.addressMap["district"];
|
|
positionMap['township'] = state.addressMap["street"];
|
|
positionMap['address'] = state.addressMap["address"];
|
|
|
|
var bluetooth = {};
|
|
bluetooth['bluetoothDeviceId'] = BlueManage().connectDeviceMacAddress;
|
|
bluetooth['bluetoothDeviceName'] = BlueManage().connectDeviceName;
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
bluetooth['publicKey'] = publicKeyDataList;
|
|
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
bluetooth['privateKey'] = getPrivateKeyList;
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
bluetooth['signKey'] = signKeyDataList;
|
|
|
|
print("addUser:publicKeyDataList$publicKeyDataList getPrivateKeyList:$getPrivateKeyList signKeyDataList:$signKeyDataList");
|
|
|
|
var entity = await ApiRepository.to.bindingBlueAdmin(
|
|
bindingDate:DateTime.now().millisecondsSinceEpoch.toString(),
|
|
hotelMode:"2",
|
|
lockAlias:state.aliName.value,
|
|
lockData:lockDataMap,
|
|
nbInitSuccess:"0",
|
|
position:positionMap,
|
|
bluetooth:bluetooth,
|
|
deviceNo:"123456",
|
|
// lockUserNo:userNo.toString(),
|
|
lockUserNo:"1234",
|
|
pwdTimestamp:"11223344"
|
|
);
|
|
if(entity.errorCode!.codeIsSuccessful){
|
|
eventBus.fire(RefreshLockListInfoDataEvent());
|
|
Get.offAllNamed(Routers.starLockMain);
|
|
}
|
|
}
|
|
|
|
// 获取锁状态
|
|
Future<void> _getLockStatus() async {
|
|
// 进来之后首先连接
|
|
BlueManage().judgeReconnect(BlueManage().connectDeviceMacAddress, BlueManage().connectDeviceName, (DeviceConnectionState state) async {
|
|
if (state == DeviceConnectionState.connected) {
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
IoSenderManage.senderGetLockStatu(
|
|
lockID:BlueManage().connectDeviceName,
|
|
userID:await Storage.getUid(),
|
|
privateKey:getPrivateKeyList,
|
|
);
|
|
}
|
|
}, isShowLoading: false);
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
_getLockStatus();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
|
|
} |