2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-07-23 11:16:18 +08:00
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
|
|
|
|
|
import 'package:get/get.dart';
|
2023-12-13 11:54:34 +08:00
|
|
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
|
|
|
2024-07-23 11:16:18 +08:00
|
|
|
|
import '../../../../app_settings/app_settings.dart';
|
|
|
|
|
|
import '../../../../blue/blue_manage.dart';
|
|
|
|
|
|
import '../../../../blue/io_protocol/io_addRemoteControlWithTimeCycleCoercion.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 '../../../../login/login/entity/LoginEntity.dart';
|
|
|
|
|
|
import '../../../../network/api_repository.dart';
|
|
|
|
|
|
import '../../../../tools/dateTool.dart';
|
|
|
|
|
|
import '../../../../tools/eventBusEventManage.dart';
|
|
|
|
|
|
import '../../../../tools/storage.dart';
|
|
|
|
|
|
import 'addRemoteControl_entity.dart';
|
2023-12-13 11:54:34 +08:00
|
|
|
|
import 'addRemoteControl_state.dart';
|
|
|
|
|
|
|
2024-07-23 11:16:18 +08:00
|
|
|
|
class AddRemoteControlLogic extends BaseGetXController{
|
2023-12-13 11:54:34 +08:00
|
|
|
|
AddRemoteControlState state = AddRemoteControlState();
|
|
|
|
|
|
|
2024-07-23 11:16:18 +08:00
|
|
|
|
// 监听设备返回的数据
|
|
|
|
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
|
|
|
|
void _initReplySubscription() {
|
|
|
|
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((Reply reply) async {
|
|
|
|
|
|
// 添加遥控开始
|
|
|
|
|
|
if((reply is SenderAddRemoteControlWithTimeCycleCoercionReply) && (state.ifCurrentScreen.value == true)) {
|
|
|
|
|
|
_replyAddRemoteControlBegin(reply);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加遥控确认
|
|
|
|
|
|
if(reply is SenderAddRemoteControlConfirmationReply) {
|
|
|
|
|
|
_replyAddRemoteControlConfirmation(reply);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _replyAddRemoteControlBegin(Reply reply) async {
|
|
|
|
|
|
final int status = reply.data[2];
|
|
|
|
|
|
|
|
|
|
|
|
switch(status){
|
|
|
|
|
|
case 0x00:
|
|
|
|
|
|
//成功
|
|
|
|
|
|
final List<int> remoteControlNumberList = reply.data.sublist(reply.data.length - 2);
|
|
|
|
|
|
final String remoteControlNumber = listChangInt(remoteControlNumberList).toString();
|
|
|
|
|
|
// AppLog.log("添加卡号:$cardNumberList cardNumber:$cardNumber");
|
|
|
|
|
|
state.remoteControlNumber.value = remoteControlNumber.toString();
|
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
|
|
|
|
|
|
|
state.ifAddState.value = true;
|
|
|
|
|
|
state.ifConnectScuess.value = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0x06:
|
|
|
|
|
|
//无权限
|
|
|
|
|
|
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
|
|
|
|
|
|
|
|
final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
|
|
|
|
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
|
|
|
|
|
|
|
|
// var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
|
|
|
|
// List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
|
|
|
|
|
|
|
|
|
|
final List<int> token = reply.data.sublist(5, 9);
|
|
|
|
|
|
final List<String> saveStrList = changeIntListToStringList(token);
|
|
|
|
|
|
Storage.setStringList(saveBlueToken, saveStrList);
|
|
|
|
|
|
AppLog.log('添加卡token:$token');
|
|
|
|
|
|
|
|
|
|
|
|
IoSenderManage.senderAddRemoteControlWithTimeCycleCoercionCommand(
|
|
|
|
|
|
keyID:'1',
|
|
|
|
|
|
userID:await Storage.getUid(),
|
|
|
|
|
|
remoteControlNo:0,
|
|
|
|
|
|
useCountLimit:0xffff,
|
|
|
|
|
|
operate:0, // 0:注册 1:修改 2:删除 3:删除全部
|
|
|
|
|
|
isAdmin:state.isAdministrator.value == '2' ? 1 : 0,
|
|
|
|
|
|
isForce:state.isCoerced.value == '1' ? 1 : 0, // 是否是胁迫
|
|
|
|
|
|
isRound:state.selectType.value == '2' ? 1: 0, // 是否是循环
|
|
|
|
|
|
weekRound:DateTool().accordingTheCycleIntoTheCorrespondingNumber(state.weekDay.value), // 周循环
|
|
|
|
|
|
startDate: int.parse(state.startDate.value)~/1000,
|
|
|
|
|
|
endDate: int.parse(state.endDate.value)~/1000,
|
|
|
|
|
|
startTime:DateTool().dateToHNString(state.effectiveDateTime.value),
|
|
|
|
|
|
endTime:DateTool().dateToHNString(state.failureDateTime.value),
|
|
|
|
|
|
needAuthor:1,
|
|
|
|
|
|
signKey:signKeyDataList,
|
|
|
|
|
|
privateKey:getPrivateKeyList,
|
|
|
|
|
|
token: token,
|
|
|
|
|
|
isBeforeAddUser: false
|
|
|
|
|
|
);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
//失败
|
|
|
|
|
|
state.ifAddState.value = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _replyAddRemoteControlConfirmation(Reply reply) async {
|
|
|
|
|
|
final int status = reply.data[2];
|
|
|
|
|
|
state.ifAddState.value = false;
|
|
|
|
|
|
switch(status){
|
|
|
|
|
|
case 0x00:
|
|
|
|
|
|
//成功
|
|
|
|
|
|
switch(reply.data[5]){
|
|
|
|
|
|
case 0xff:
|
|
|
|
|
|
// 注册指纹失败
|
|
|
|
|
|
showToast('退出添加'.tr);
|
|
|
|
|
|
Get.close(1);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0xFE:
|
|
|
|
|
|
// 管理员已满
|
|
|
|
|
|
showToast('管理员已满'.tr);
|
|
|
|
|
|
Get.close(1);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0xFD:
|
|
|
|
|
|
// 用户已满
|
|
|
|
|
|
showToast('用户已满'.tr);
|
|
|
|
|
|
Get.close(1);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0xFC:
|
|
|
|
|
|
// 遥控已满
|
2024-07-30 20:00:06 +08:00
|
|
|
|
showToast('锁上面遥控已满'.tr);
|
2024-07-23 11:16:18 +08:00
|
|
|
|
Get.close(1);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 0xFB:
|
|
|
|
|
|
// 遥控已存在
|
|
|
|
|
|
showToast('遥控已存在'.tr);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
// 添加指纹中
|
|
|
|
|
|
// 当前注册数
|
|
|
|
|
|
_addRemoteControlData();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
//失败
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-08 11:00:03 +08:00
|
|
|
|
// 添加遥控
|
2024-07-23 11:16:18 +08:00
|
|
|
|
Future<void> senderAddRemoteControl() async {
|
|
|
|
|
|
showBlueConnetctToastTimer(action: (){
|
|
|
|
|
|
Get.close(1);
|
|
|
|
|
|
});
|
|
|
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState deviceConnectionState) async {
|
|
|
|
|
|
if (deviceConnectionState == BluetoothConnectionState.connected){
|
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
|
|
|
|
|
|
|
final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
|
|
|
|
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
|
|
|
|
|
|
|
|
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
|
|
|
|
|
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
|
|
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
|
|
|
|
|
|
|
|
|
|
|
IoSenderManage.senderAddRemoteControlWithTimeCycleCoercionCommand(
|
|
|
|
|
|
keyID:'1',
|
|
|
|
|
|
userID:await Storage.getUid(),
|
|
|
|
|
|
remoteControlNo:0,
|
|
|
|
|
|
useCountLimit:0xffff,
|
|
|
|
|
|
operate:0, // 0:注册 1:修改 2:删除 3:删除全部
|
|
|
|
|
|
isAdmin:state.isAdministrator.value == '2' ? 1 : 0,
|
|
|
|
|
|
isForce:state.isCoerced.value == '1' ? 1 : 0, // 是否是胁迫
|
|
|
|
|
|
isRound:state.selectType.value == '2' ? 1: 0, // 是否是循环
|
|
|
|
|
|
weekRound:DateTool().accordingTheCycleIntoTheCorrespondingNumber(state.weekDay.value), // 周循环
|
|
|
|
|
|
startDate: int.parse(state.startDate.value)~/1000,
|
|
|
|
|
|
endDate: int.parse(state.endDate.value)~/1000,
|
|
|
|
|
|
startTime:DateTool().dateToHNString(state.effectiveDateTime.value),
|
|
|
|
|
|
endTime:DateTool().dateToHNString(state.failureDateTime.value),
|
|
|
|
|
|
needAuthor:1,
|
|
|
|
|
|
signKey:signKeyDataList,
|
|
|
|
|
|
privateKey:getPrivateKeyList,
|
|
|
|
|
|
token: getTokenList,
|
|
|
|
|
|
isBeforeAddUser: false
|
|
|
|
|
|
);
|
|
|
|
|
|
} else if (deviceConnectionState == BluetoothConnectionState.disconnected){
|
|
|
|
|
|
if(state.ifCurrentScreen.value == true){
|
|
|
|
|
|
showBlueConnetctToast();
|
|
|
|
|
|
}
|
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
|
Get.close(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 取消添加指纹
|
|
|
|
|
|
Future<void> senderCancelAddRemoteControlCommand() async {
|
|
|
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState deviceConnectionState) async {
|
|
|
|
|
|
if (deviceConnectionState == BluetoothConnectionState.connected){
|
|
|
|
|
|
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
|
|
|
|
|
|
|
|
final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
|
|
|
|
|
|
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
|
|
|
|
|
|
|
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
|
|
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
|
|
|
|
|
|
|
|
|
|
|
IoSenderManage.senderCancelAddRemoteControlCommand(
|
|
|
|
|
|
keyID:'1',
|
|
|
|
|
|
userID:await Storage.getUid(),
|
|
|
|
|
|
needAuthor:1,
|
|
|
|
|
|
signKey:signKeyDataList,
|
|
|
|
|
|
privateKey:getPrivateKeyList,
|
|
|
|
|
|
token: getTokenList,
|
|
|
|
|
|
);
|
|
|
|
|
|
}else if (deviceConnectionState == BluetoothConnectionState.disconnected){
|
|
|
|
|
|
if(state.ifCurrentScreen.value == true){
|
|
|
|
|
|
showBlueConnetctToast();
|
|
|
|
|
|
}
|
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
|
Get.close(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _addRemoteControlData() async {
|
|
|
|
|
|
final AddRemoteControlEntity entity = await ApiRepository.to.addRemoteControlData(
|
|
|
|
|
|
lockId: state.lockId.value.toString(),
|
|
|
|
|
|
remoteName: state.remoteControlName.value.toString(),
|
|
|
|
|
|
remoteNumber: state.remoteControlNumber.value.toString(),
|
|
|
|
|
|
remoteType: int.parse(state.remoteControlType.value),
|
|
|
|
|
|
startDate: int.parse(state.startDate.value),
|
|
|
|
|
|
endDate: int.parse(state.endDate.value),
|
|
|
|
|
|
addType: int.parse(state.addType.value),
|
|
|
|
|
|
weekDay: state.weekDay.value,
|
|
|
|
|
|
startTime: int.parse(state.effectiveDateTime.value),
|
|
|
|
|
|
endTime: int.parse(state.failureDateTime.value),
|
|
|
|
|
|
// remoteRight: state.isAdministrator.value == '2' ? 1 : 0,
|
|
|
|
|
|
remoteRight: 0,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
|
|
|
|
updateRemoteUserNoLoadData(entity.data!.remoteId ?? 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> updateRemoteUserNoLoadData(int cardId) async{
|
|
|
|
|
|
final LoginEntity entity = await ApiRepository.to.updateRemoteUserNoLoadData(
|
|
|
|
|
|
lockId: state.lockId.value,
|
|
|
|
|
|
remoteId: cardId,
|
|
|
|
|
|
remoteUserNo: state.remoteControlNumber.value,
|
|
|
|
|
|
);
|
|
|
|
|
|
if(entity.errorCode!.codeIsSuccessful){
|
|
|
|
|
|
showToast('添加成功'.tr, something:(){
|
|
|
|
|
|
eventBus.fire(OtherTypeRefreshListEvent());
|
|
|
|
|
|
Get.close(2);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void onReady() {
|
|
|
|
|
|
super.onReady();
|
|
|
|
|
|
|
|
|
|
|
|
_initReplySubscription();
|
2024-06-01 17:25:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-13 11:54:34 +08:00
|
|
|
|
@override
|
|
|
|
|
|
void onInit() {
|
2024-07-23 11:16:18 +08:00
|
|
|
|
super.onInit();
|
|
|
|
|
|
|
|
|
|
|
|
senderAddRemoteControl();
|
|
|
|
|
|
}
|
2023-12-13 11:54:34 +08:00
|
|
|
|
|
2024-07-23 11:16:18 +08:00
|
|
|
|
@override
|
|
|
|
|
|
void onClose() {
|
|
|
|
|
|
_replySubscription.cancel();
|
2023-12-13 11:54:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|