519 lines
19 KiB
Dart
Executable File
519 lines
19 KiB
Dart
Executable File
import 'dart:async';
|
||
|
||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:star_lock/blue/blue_manage.dart';
|
||
import 'package:star_lock/blue/io_protocol/io_setSupportFunctionsWithParameters.dart';
|
||
import 'package:star_lock/blue/io_reply.dart';
|
||
import 'package:star_lock/blue/io_tool/io_tool.dart';
|
||
import 'package:star_lock/blue/io_tool/manager_event_bus.dart';
|
||
import 'package:star_lock/blue/sender_manage.dart';
|
||
import 'package:star_lock/common/XSConstantMacro/XSConstantMacro.dart';
|
||
import 'package:star_lock/main/lockDetail/lockSet/lockSet/lockSetInfo_entity.dart';
|
||
import 'package:star_lock/network/api_repository.dart';
|
||
import 'package:star_lock/tools/baseGetXController.dart';
|
||
import 'package:star_lock/tools/eventBusEventManage.dart';
|
||
import 'package:star_lock/tools/storage.dart';
|
||
import 'package:star_lock/versionUndate/versionUndate_entity.dart';
|
||
|
||
import 'catEyeSet_state.dart';
|
||
|
||
class CatEyeSetLogic extends BaseGetXController {
|
||
final CatEyeSetState state = CatEyeSetState();
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
_initReplySubscription();
|
||
|
||
eventBus
|
||
.on<PassCurrentLockInformationEvent>()
|
||
.listen((PassCurrentLockInformationEvent event) {
|
||
state.selectCatEyeWorkMode.value = ''; //猫眼工作模式
|
||
if (event.lockSetInfoData.lockSettingInfo != null &&
|
||
event.lockSetInfoData.lockSettingInfo!.catEyeConfig != null &&
|
||
event.lockSetInfoData.lockSettingInfo!.catEyeConfig!.length > 0) {
|
||
if (event
|
||
.lockSetInfoData.lockSettingInfo!.catEyeConfig![0].catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModePowerSaving) {
|
||
state.selectCatEyeWorkMode.value = '省电模式'.tr;
|
||
} else if (event
|
||
.lockSetInfoData.lockSettingInfo!.catEyeConfig![0].catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModeStayCapture) {
|
||
state.selectCatEyeWorkMode.value = '逗留抓拍模式'.tr;
|
||
} else if (event
|
||
.lockSetInfoData.lockSettingInfo!.catEyeConfig![0].catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModeRealTimeMonitoring) {
|
||
state.selectCatEyeWorkMode.value = '实时监控模式'.tr;
|
||
} else if (event
|
||
.lockSetInfoData.lockSettingInfo!.catEyeConfig![0].catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModeCustom) {
|
||
state.selectCatEyeWorkMode.value = '自定义模式'.tr;
|
||
}
|
||
}
|
||
state.updateCatEyeSetByState();
|
||
});
|
||
}
|
||
|
||
// 获取解析后的数据
|
||
late StreamSubscription<Reply> _replySubscription;
|
||
|
||
void _initReplySubscription() {
|
||
_replySubscription =
|
||
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
|
||
// 设置支持功能解析(带参数)
|
||
if (reply is SetSupportFunctionsWithParametersReply) {
|
||
_replySetSupportFunctionsWithParameters(reply);
|
||
}
|
||
});
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
_replySubscription.cancel();
|
||
super.onClose();
|
||
}
|
||
|
||
// 设置支持功能解析
|
||
Future<void> _replySetSupportFunctionsWithParameters(Reply reply) async {
|
||
final int status = reply.data[2];
|
||
switch (status) {
|
||
case 0x00:
|
||
//成功
|
||
cancelBlueConnetctToastTimer();
|
||
dismissEasyLoading();
|
||
switch (state.settingOptions.value) {
|
||
case 1: //自动亮屏
|
||
{
|
||
updateAutoLightScreenConfig();
|
||
}
|
||
break;
|
||
case 2: //逗留警告
|
||
{
|
||
updateStayWarnConfig();
|
||
}
|
||
break;
|
||
case 3: //异常警告
|
||
{
|
||
updateAbnormalWarnConfig();
|
||
}
|
||
break;
|
||
case 4: //设置亮屏持续时间
|
||
{
|
||
updateLightScreenTimeConfig();
|
||
}
|
||
break;
|
||
case 5: //修改猫眼工作模式
|
||
{
|
||
updateCatEyeModeConfig();
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
break;
|
||
case 0x06:
|
||
//无权限
|
||
showToast('无权限');
|
||
break;
|
||
default:
|
||
showToast('修改失败');
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 获取锁设置信息
|
||
Future<void> getLockSettingInfoData() async {
|
||
final LockSetInfoEntity entity =
|
||
await ApiRepository.to.getLockSettingInfoData(
|
||
lockId: state.lockSetInfoData.value.lockId.toString(),
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
state.lockSetInfoData.value = entity.data!;
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
||
|
||
state.selectCatEyeWorkMode.value = ''; //猫眼工作模式
|
||
if (state.lockSetInfoData.value.lockSettingInfo != null &&
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig != null &&
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig!.length >
|
||
0) {
|
||
if (state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0]
|
||
.catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModePowerSaving) {
|
||
state.selectCatEyeWorkMode.value = '省电模式'.tr;
|
||
} else if (state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0]
|
||
.catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModeStayCapture) {
|
||
state.selectCatEyeWorkMode.value = '逗留抓拍模式'.tr;
|
||
} else if (state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0]
|
||
.catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModeRealTimeMonitoring) {
|
||
state.selectCatEyeWorkMode.value = '实时监控模式'.tr;
|
||
} else if (state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0]
|
||
.catEyeMode ==
|
||
XSConstantMacro.catEyeWorkModeCustom) {
|
||
state.selectCatEyeWorkMode.value = '自定义模式'.tr;
|
||
}
|
||
}
|
||
state.updateCatEyeSetByState();
|
||
}
|
||
}
|
||
|
||
//设置自动亮屏
|
||
Future<void> updateAutoLightScreenConfig() async {
|
||
final VersionUndateEntity entity =
|
||
await ApiRepository.to.updateAutoLightScreenConfig(
|
||
lockId: state.lockSetInfoData.value.lockId!,
|
||
autoLightScreen: state.isAutoBright.value == true ? 1 : 0,
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast(state.settingSuccess);
|
||
state.catEyeConfig.value.autoLightScreen =
|
||
state.isAutoBright.value == true ? 1 : 0;
|
||
state.lockSetInfoData.value.lockSettingInfo!.autoLightScreen =
|
||
state.isAutoBright.value == true ? 1 : 0;
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
||
state.catEyeConfig.refresh();
|
||
}
|
||
}
|
||
|
||
//设置亮屏持续时间
|
||
Future<void> updateLightScreenTimeConfig() async {
|
||
final VersionUndateEntity entity =
|
||
await ApiRepository.to.updateLightScreenTimeConfig(
|
||
lockId: state.lockSetInfoData.value.lockId!,
|
||
autoLightScreenTime:
|
||
int.parse(state.selectBrightDuration.value.replaceAll('秒'.tr, '')),
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast(state.settingSuccess);
|
||
state.catEyeConfig.value.autoLightScreenTime =
|
||
int.parse(state.selectBrightDuration.value.replaceAll('秒'.tr, ''));
|
||
state.lockSetInfoData.value.lockSettingInfo!.autoLightScreenTime =
|
||
int.parse(state.selectBrightDuration.value.replaceAll('秒'.tr, ''));
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
||
state.catEyeConfig.refresh();
|
||
}
|
||
}
|
||
|
||
//设置逗留警告
|
||
Future<void> updateStayWarnConfig() async {
|
||
final VersionUndateEntity entity =
|
||
await ApiRepository.to.updateStayWarnConfig(
|
||
lockId: state.lockSetInfoData.value.lockId!,
|
||
stayWarn: state.isStayWarning.value == true ? 1 : 0,
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast(state.settingSuccess);
|
||
state.catEyeConfig.value.stayWarn =
|
||
state.isStayWarning.value == true ? 1 : 0;
|
||
state.lockSetInfoData.value.lockSettingInfo!.stayWarn =
|
||
state.isStayWarning.value == true ? 1 : 0;
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
||
state.catEyeConfig.refresh();
|
||
}
|
||
}
|
||
|
||
//设置猫眼工作模式
|
||
Future<void> updateCatEyeModeConfig() async {
|
||
final VersionUndateEntity entity =
|
||
await ApiRepository.to.updateCatEyeModeConfig(
|
||
lockId: state.lockSetInfoData.value.lockId!,
|
||
catEyeConfig: [
|
||
<String, Object>{
|
||
'catEyeMode': state.catEyeConfig.value.catEyeMode!,
|
||
'catEyeModeConfig': <String, Object?>{
|
||
'recordMode': state.catEyeConfig.value.recordMode,
|
||
'recordStartTime': state.catEyeConfig.value.recordStartTime,
|
||
'recordEndTime': state.catEyeConfig.value.recordEndTime,
|
||
'recordTime': state.catEyeConfig.value.recordTime,
|
||
'detectionDistance': state.catEyeConfig.value.detectionDistance,
|
||
'realTimeMode': state.catEyeConfig.value.realTimeMode,
|
||
}
|
||
}
|
||
],
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
if (state.lockSetInfoData.value.lockSettingInfo != null &&
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig != null &&
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig!.length ==
|
||
0) {
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig!.add(
|
||
CatEyeConfig(
|
||
catEyeMode: 0,
|
||
catEyeModeConfig: CatEyeModeConfig(
|
||
realTimeMode: 0,
|
||
recordEndTime: 0,
|
||
recordMode: 0,
|
||
recordStartTime: 0,
|
||
recordTime: '0',
|
||
detectionDistance: '0')));
|
||
}
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0].catEyeMode =
|
||
state.catEyeConfig.value.catEyeMode;
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0]
|
||
.catEyeModeConfig?.recordMode = state.catEyeConfig.value.recordMode;
|
||
state
|
||
.lockSetInfoData
|
||
.value
|
||
.lockSettingInfo!
|
||
.catEyeConfig![0]
|
||
.catEyeModeConfig
|
||
?.recordStartTime = state.catEyeConfig.value.recordStartTime;
|
||
state
|
||
.lockSetInfoData
|
||
.value
|
||
.lockSettingInfo!
|
||
.catEyeConfig![0]
|
||
.catEyeModeConfig
|
||
?.recordEndTime = state.catEyeConfig.value.recordEndTime;
|
||
state
|
||
.lockSetInfoData
|
||
.value
|
||
.lockSettingInfo!
|
||
.catEyeConfig![0]
|
||
.catEyeModeConfig
|
||
?.recordTime = state.catEyeConfig.value.recordTime.toString();
|
||
state.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0]
|
||
.catEyeModeConfig?.detectionDistance =
|
||
state.catEyeConfig.value.detectionDistance.toString();
|
||
state
|
||
.lockSetInfoData
|
||
.value
|
||
.lockSettingInfo!
|
||
.catEyeConfig![0]
|
||
.catEyeModeConfig
|
||
?.realTimeMode = state.catEyeConfig.value.realTimeMode;
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
||
}
|
||
}
|
||
|
||
//设置异常警告
|
||
Future<void> updateAbnormalWarnConfig() async {
|
||
final VersionUndateEntity entity =
|
||
await ApiRepository.to.updateAbnormalWarnConfig(
|
||
lockId: state.lockSetInfoData.value.lockId!,
|
||
abnormalWarn: state.isExceptionWarning.value == true ? 1 : 0,
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast(state.settingSuccess);
|
||
state.catEyeConfig.value.abnormalWarn =
|
||
state.isExceptionWarning.value == true ? 1 : 0;
|
||
state.lockSetInfoData.value.lockSettingInfo!.abnormalWarn =
|
||
state.isExceptionWarning.value == true ? 1 : 0;
|
||
eventBus
|
||
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
||
state.catEyeConfig.refresh();
|
||
// await getLockSettingInfoData();
|
||
}
|
||
}
|
||
|
||
/// 构建蓝牙设置命令
|
||
List<int> _buildCatEyeSetBlueMessage() {
|
||
final int operatingMode = state.catEyeConfig.value.catEyeMode ?? 1; // 自定义模式
|
||
|
||
final int isAllDay = state.catEyeConfig.value.recordMode ?? 1; // 是否是全天
|
||
final int startTime =
|
||
_handleTimeToM(state.catEyeConfig.value.recordStartTime!) ??
|
||
480; // 8:00 AM
|
||
final int endTime =
|
||
_handleTimeToM(state.catEyeConfig.value.recordEndTime!) ??
|
||
1080; // 6:00 PM
|
||
final int recordingTime = state.catEyeConfig.value.recordTime ?? 0; // 立即录像
|
||
final int detectionDistance =
|
||
state.catEyeConfig.value.detectionDistance ?? 0; //0:0.8米
|
||
final int realTimePicture =
|
||
state.catEyeConfig.value.realTimePicture ?? 0; // 实时查看
|
||
final int automaticBrightening =
|
||
state.catEyeConfig.value.autoLightScreen ?? 1; // 打开逗留警告
|
||
final int brightScreenTime =
|
||
state.catEyeConfig.value.autoLightScreenTime ?? 10; // 亮屏持续时间为15秒
|
||
final int stayWarning = state.catEyeConfig.value.stayWarn ?? 1; // 打开逗留警告
|
||
final int exceptionWarning =
|
||
state.catEyeConfig.value.abnormalWarn ?? 1; // 打开逗留警告
|
||
|
||
/// 生成配置,非自定义功能字段默认填充0
|
||
final List<int> config = generateConfig(
|
||
operatingMode: operatingMode,
|
||
isAllDay: isAllDay,
|
||
startTime: startTime,
|
||
endTime: endTime,
|
||
recordingTime: recordingTime,
|
||
detectionDistance: detectionDistance,
|
||
realTimePicture: realTimePicture,
|
||
automaticBrightening: automaticBrightening,
|
||
brightScreenTime: brightScreenTime,
|
||
stayWarning: stayWarning,
|
||
exceptionWarning: exceptionWarning,
|
||
);
|
||
return config;
|
||
}
|
||
|
||
// 根据提供的参数生成配置列表
|
||
List<int> generateConfig({
|
||
required int operatingMode,
|
||
required int isAllDay,
|
||
required int startTime,
|
||
required int endTime,
|
||
required int recordingTime,
|
||
required int detectionDistance,
|
||
required int realTimePicture,
|
||
required int automaticBrightening,
|
||
required int brightScreenTime,
|
||
required int stayWarning,
|
||
required int exceptionWarning,
|
||
}) {
|
||
// 初始化一个空的列表
|
||
List<int> config = [];
|
||
|
||
// 添加 operatingMode
|
||
config.add(operatingMode);
|
||
|
||
// 如果不是自定义模式,则填充0;否则添加对应的值
|
||
if (operatingMode != 3) {
|
||
config.addAll(
|
||
[0, 0, 0, 0, 0, 0]); // 对应 IsAllDay, StartTime, EndTime, recordingTime
|
||
} else {
|
||
config.add(isAllDay);
|
||
if (isAllDay == 1) {
|
||
config.addAll(_intToBytes(0, 2)); // StartTime 占用2个字节
|
||
config.addAll(_intToBytes(0, 2)); // EndTime 占用2个字节
|
||
} else {
|
||
config.addAll(_intToBytes(startTime, 2)); // StartTime 占用2个字节
|
||
config.addAll(_intToBytes(endTime, 2)); // EndTime 占用2个字节
|
||
}
|
||
|
||
config.add(recordingTime);
|
||
}
|
||
|
||
// 如果不是自定义模式,则 detectionDistance 和 realTimePicture 也填充0
|
||
if (operatingMode != 3) {
|
||
config.addAll([0, 0]);
|
||
} else {
|
||
config.add(detectionDistance);
|
||
config.add(realTimePicture);
|
||
}
|
||
|
||
// 自动亮屏、亮屏持续时间、逗留警告、异常警告
|
||
config.add(automaticBrightening);
|
||
config.add(brightScreenTime);
|
||
config.add(stayWarning);
|
||
config.add(exceptionWarning);
|
||
|
||
return config;
|
||
}
|
||
|
||
// 将整数转换为指定长度的字节表示形式
|
||
List<int> _intToBytes(int value, int length) {
|
||
List<int> bytes = [];
|
||
for (int i = length - 1; i >= 0; i--) {
|
||
bytes.add((value >> (8 * i)) & 0xFF);
|
||
}
|
||
return bytes;
|
||
}
|
||
|
||
void updateSettingOptions(int clickIndex, value) {
|
||
switch (clickIndex) {
|
||
case 1: //自动亮屏
|
||
{
|
||
state.isAutoBright.value = value;
|
||
state.settingOptions.value = 1;
|
||
state.catEyeConfig.value.autoLightScreen = value == true ? 1 : 0;
|
||
}
|
||
break;
|
||
case 2: //逗留警告
|
||
{
|
||
state.isStayWarning.value = value;
|
||
state.settingOptions.value = 2;
|
||
state.catEyeConfig.value.stayWarn = value == true ? 1 : 0;
|
||
}
|
||
break;
|
||
case 3: //异常警告
|
||
{
|
||
state.isExceptionWarning.value = value;
|
||
state.settingOptions.value = 3;
|
||
state.catEyeConfig.value.abnormalWarn = value == true ? 1 : 0;
|
||
}
|
||
break;
|
||
case 4: //设置亮屏持续时间
|
||
{
|
||
state.selectBrightDuration.value =
|
||
state.brightDurationTimeList[value];
|
||
state.settingOptions.value = 4;
|
||
state.catEyeConfig.value.autoLightScreenTime =
|
||
_handleGetIntNumber(state.selectBrightDuration.value);
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
sendBlueMessage();
|
||
}
|
||
|
||
void sendBlueMessage() {
|
||
final message = _buildCatEyeSetBlueMessage();
|
||
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
||
(BluetoothConnectionState connectionState) async {
|
||
if (connectionState == BluetoothConnectionState.connected) {
|
||
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!);
|
||
final List<String>? publicKey =
|
||
await Storage.getStringList(saveBluePublicKey);
|
||
final List<int> getPublicKeyList =
|
||
changeStringListToIntList(publicKey!);
|
||
final userId = await Storage.getUid();
|
||
IoSenderManage.setSupportFunctionsWithParametersCommand(
|
||
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
|
||
userID: userId,
|
||
featureBit: 64,
|
||
featureParaLength: message.length,
|
||
featureData: message,
|
||
token: getTokenList,
|
||
needAuthor: 1,
|
||
publicKey: getPublicKeyList,
|
||
privateKey: getPrivateKeyList);
|
||
} else if (connectionState == BluetoothConnectionState.disconnected) {
|
||
dismissEasyLoading();
|
||
cancelBlueConnetctToastTimer();
|
||
}
|
||
});
|
||
}
|
||
|
||
int _handleTimeToM(int recordStartTime) {
|
||
// 转换为 DateTime 对象
|
||
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(recordStartTime);
|
||
// 计算从当天0点开始的分钟数
|
||
int minutesSinceMidnight = dateTime.hour * 60 + dateTime.minute;
|
||
return minutesSinceMidnight;
|
||
}
|
||
|
||
int _handleGetIntNumber(String recordTime) {
|
||
if (recordTime == '不录像') {
|
||
return 0;
|
||
}
|
||
if (recordTime == '立即录像') {
|
||
return 1;
|
||
}
|
||
// 使用正则表达式匹配字符串中的数字
|
||
RegExp regExp = RegExp(r'(\d+)秒');
|
||
Match? match = regExp.firstMatch(recordTime);
|
||
|
||
if (match != null && match.groupCount >= 1) {
|
||
// 提取出的数字是字符串形式,需要转换成整数
|
||
int number = int.parse(match.group(1)!);
|
||
return number;
|
||
} else {
|
||
return 0;
|
||
}
|
||
}
|
||
}
|