# Conflicts: # star_lock/lib/main/lockDetail/lcokSet/lockSet/lockSet_page.dart # star_lock/lib/mine/addLock/saveLock/saveLock_logic.dart 代码合并
214 lines
6.8 KiB
Dart
214 lines
6.8 KiB
Dart
import 'dart:async';
|
|
|
|
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_getStarLockStatusInfo.dart';
|
|
import '../../../blue/io_reply.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();
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription =
|
|
EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if (reply is AddUserReply) {
|
|
_replyAddUserKey(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 reply.data:${reply.data}");
|
|
|
|
print("status:$status");
|
|
switch (status) {
|
|
case 0x00:
|
|
//成功
|
|
print("添加用户数据解析成功");
|
|
state.lockUserNo = reply.data[47] + 1;
|
|
|
|
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: DateTime.now().millisecondsSinceEpoch,
|
|
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;
|
|
}
|
|
}
|
|
|
|
// 保存事件,先调用蓝牙,蓝牙调用成功后再调用后台接口
|
|
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 {
|
|
print("state.lockInfo:${state.lockInfo}");
|
|
|
|
var positionMap = {};
|
|
positionMap['longitude'] = state.addressInfo["longitude"];
|
|
positionMap['latitude'] = state.addressInfo["latitude"];
|
|
positionMap['country'] = state.addressInfo["country"];
|
|
positionMap['province'] = state.addressInfo["province"];
|
|
positionMap['city'] = state.addressInfo["city"];
|
|
positionMap['district'] = state.addressInfo["district"];
|
|
positionMap['township'] = state.addressInfo["street"];
|
|
positionMap['address'] = state.addressInfo["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(
|
|
lockAlias: state.aliName.value,
|
|
position: positionMap,
|
|
bluetooth: bluetooth,
|
|
lockInfo: state.lockInfo,
|
|
lockUserNo: state.lockUserNo.toString(),
|
|
pwdTimestamp: state.pwdTimestamp.value.toString(),
|
|
featureValue: state.featureValue,
|
|
featureSettingValue: state.featureSettingValue,
|
|
featureSettingParams: state.featureSettingParams,
|
|
);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
eventBus.fire(RefreshLockListInfoDataEvent());
|
|
Get.offAllNamed(Routers.starLockMain);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
}
|