322 lines
12 KiB
Dart
Executable File
322 lines
12 KiB
Dart
Executable File
import 'package:get/get.dart';
|
||
import 'package:star_lock/app_settings/app_settings.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_tool/io_tool.dart';
|
||
import 'package:star_lock/main/lockDetail/lockSet/catEyeSet/catEyeCustomMode/catEyeCustomMode_state.dart';
|
||
import 'package:star_lock/main/lockDetail/lockSet/catEyeSet/catEyeSet/catEyeSet_state.dart';
|
||
import 'package:star_lock/main/lockDetail/lockSet/lockSet/lockSetInfo_entity.dart';
|
||
import 'package:star_lock/network/api_repository.dart';
|
||
import 'package:star_lock/talk/starChart/star_chart_manage.dart';
|
||
import 'package:star_lock/tools/baseGetXController.dart';
|
||
import 'package:star_lock/tools/storage.dart';
|
||
|
||
class CatEyeCustomModeLogic extends BaseGetXController {
|
||
final CatEyeCustomModeState state = CatEyeCustomModeState();
|
||
|
||
// 获取锁设置信息
|
||
Future<LockSetInfoEntity> getLockSettingInfoData() async {
|
||
final LockSetInfoEntity entity =
|
||
await ApiRepository.to.getLockSettingInfoData(
|
||
lockId: state.lockSetInfoData.value.lockId.toString(),
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
state.lockSetInfoData.value = entity.data!;
|
||
if (entity.data!.lockSettingInfo!.catEyeConfig!.isNotEmpty) {
|
||
state.catEyeConfigData.value =
|
||
entity.data!.lockSettingInfo!.catEyeConfig![0];
|
||
|
||
if (state.catEyeConfigData.value.catEyeModeConfig != null) {
|
||
if (state.catEyeConfigData.value.catEyeModeConfig!.recordMode == 1) {
|
||
state.selectVideoSlot.value = '自定义时段'.tr;
|
||
} else {
|
||
state.selectVideoSlot.value = '全天'.tr;
|
||
}
|
||
|
||
state.recordTime.value =
|
||
state.catEyeConfigData.value.catEyeModeConfig!.recordTime ?? '';
|
||
state.detectionDistance.value = state
|
||
.catEyeConfigData.value.catEyeModeConfig!.detectionDistance ??
|
||
'';
|
||
state.realTimeMode.value =
|
||
state.catEyeConfigData.value.catEyeModeConfig!.realTimeMode == 0
|
||
? '发生事件时查看'.tr
|
||
: '实时查看'.tr;
|
||
_getConfigAndGenerateBleData();
|
||
updateCatEyeSetConfig();
|
||
}
|
||
}
|
||
}
|
||
await _getConfigAndGenerateBleData();
|
||
updateCatEyeSetConfig();
|
||
return entity;
|
||
}
|
||
|
||
//设置猫眼工作模式 0-有人出现时录像 1-人体侦测距离
|
||
void updateCatEyeModeConfig(int clickIndex) async {
|
||
var entity = await ApiRepository.to.updateCatEyeModeConfig(
|
||
lockId: state.lockSetInfoData.value.lockId!,
|
||
catEyeConfig: [
|
||
{
|
||
'catEyeMode': 4,
|
||
'catEyeModeConfig': {
|
||
'recordMode':
|
||
state.catEyeConfigData.value.catEyeModeConfig!.recordMode,
|
||
'recordStartTime':
|
||
state.catEyeConfigData.value.catEyeModeConfig!.recordStartTime,
|
||
'recordEndTime':
|
||
state.catEyeConfigData.value.catEyeModeConfig!.recordEndTime,
|
||
'recordTime': clickIndex == 0
|
||
? state.recordTime.value
|
||
: state.catEyeConfigData.value.catEyeModeConfig!.recordTime,
|
||
'detectionDistance': clickIndex == 1
|
||
? state.detectionDistance.value
|
||
: state
|
||
.catEyeConfigData.value.catEyeModeConfig!.detectionDistance,
|
||
'realTimeMode':
|
||
state.catEyeConfigData.value.catEyeModeConfig!.realTimeMode,
|
||
}
|
||
}
|
||
],
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast('设置成功'.tr);
|
||
await getLockSettingInfoData();
|
||
}
|
||
}
|
||
|
||
_getConfigAndGenerateBleData() {
|
||
int originalCatEyeMode = state
|
||
.lockSetInfoData.value.lockSettingInfo!.catEyeConfig![0].catEyeMode!;
|
||
int recordMode = state.lockSetInfoData.value.lockSettingInfo!
|
||
.catEyeConfig![0].catEyeModeConfig!.recordMode ==
|
||
0
|
||
? 1
|
||
: 0;
|
||
|
||
int stayWarn = state.lockSetInfoData.value.lockSettingInfo!.stayWarn!;
|
||
int abnormalWarn =
|
||
state.lockSetInfoData.value.lockSettingInfo!.abnormalWarn!;
|
||
int autoLightScreen =
|
||
state.lockSetInfoData.value.lockSettingInfo!.autoLightScreen!;
|
||
|
||
int recordStartTime = _handleTimeToM(state.lockSetInfoData.value
|
||
.lockSettingInfo!.catEyeConfig![0].catEyeModeConfig!.recordStartTime!);
|
||
int recordEndTime = _handleTimeToM(state.lockSetInfoData.value
|
||
.lockSettingInfo!.catEyeConfig![0].catEyeModeConfig!.recordEndTime!);
|
||
|
||
int recordTime = _handleGetIntNumber(state.lockSetInfoData.value
|
||
.lockSettingInfo!.catEyeConfig![0].catEyeModeConfig!.recordTime!);
|
||
|
||
int detectionDistance = _handleDetectionDistance(state
|
||
.lockSetInfoData
|
||
.value
|
||
.lockSettingInfo!
|
||
.catEyeConfig![0]
|
||
.catEyeModeConfig!
|
||
.detectionDistance!);
|
||
int realTimeMode = state.lockSetInfoData.value.lockSettingInfo!
|
||
.catEyeConfig![0].catEyeModeConfig!.realTimeMode!;
|
||
|
||
// 根据需要调整 catEyeMode 的值
|
||
int adjustedCatEyeMode =
|
||
originalCatEyeMode > 0 ? originalCatEyeMode - 1 : originalCatEyeMode;
|
||
int brightScreenTime =
|
||
state.lockSetInfoData.value.lockSettingInfo!.autoLightScreenTime!;
|
||
|
||
/// 对照星锁接口文档v0.2 的猫眼设置接口,获取出猫眼的所有设置
|
||
state.catEyeConfig.value = CatEyeSetEntity(
|
||
catEyeMode: adjustedCatEyeMode,
|
||
// 猫眼模式 看常量表
|
||
recordMode: recordMode,
|
||
//录像时段 0全天 1自定义时间
|
||
recordStartTime: recordStartTime,
|
||
//自定义时间需要填:recordStartTime与recordEndTime参数
|
||
recordEndTime: recordEndTime,
|
||
//自定义时间需要填:recordStartTime与recordEndTime参数
|
||
realTimeMode: realTimeMode,
|
||
autoLightScreenTime: brightScreenTime,
|
||
stayWarn: stayWarn,
|
||
autoLightScreen: autoLightScreen,
|
||
// 打开自动亮屏
|
||
abnormalWarn: abnormalWarn,
|
||
// 有人出现时录像时间
|
||
recordTime: recordTime,
|
||
// 人体检测距离
|
||
detectionDistance: detectionDistance,
|
||
realTimePicture: realTimeMode,
|
||
);
|
||
state.catEyeConfig.refresh();
|
||
}
|
||
|
||
/// 更新锁板上的猫眼配置
|
||
void updateCatEyeSetConfig() async {
|
||
final int operatingMode = state.catEyeConfig.value.catEyeMode ?? 1; // 自定义模式
|
||
final int isAllDay = state.catEyeConfig.value.recordMode ?? 1; // 是否是全天
|
||
final int startTime =
|
||
state.catEyeConfig.value.recordStartTime ?? 480; // 8:00 AM
|
||
final int endTime =
|
||
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,
|
||
);
|
||
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 command = SetSupportFunctionsWithParametersCommand(
|
||
keyID: state.lockSetInfoData.value.lockBasicInfo!.keyId.toString(),
|
||
userID: await Storage.getUid(),
|
||
featureBit: 64,
|
||
featureParaLength: config.length,
|
||
featureData: config,
|
||
token: getTokenList,
|
||
needAuthor: 1,
|
||
publicKey: getPublicKeyList,
|
||
privateKey: getPrivateKeyList,
|
||
);
|
||
final packageData = command.packageData();
|
||
/// 发送星图
|
||
StartChartManage().sendBleMessage(
|
||
bluetoothDeviceName: BlueManage().connectDeviceName,
|
||
bleStructData: packageData,
|
||
);
|
||
}
|
||
|
||
// 根据提供的参数生成配置列表
|
||
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);
|
||
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;
|
||
}
|
||
|
||
int _handleTimeToM(int recordStartTime) {
|
||
// 转换为 DateTime 对象
|
||
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(recordStartTime);
|
||
|
||
// 提取小时和分钟
|
||
int hour = dateTime.hour;
|
||
int minute = dateTime.minute;
|
||
|
||
print("时: $hour, 分: $minute");
|
||
|
||
// 计算从当天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;
|
||
}
|
||
}
|
||
|
||
int _handleDetectionDistance(String s) {
|
||
if (s == '约0.8米') {
|
||
return 0;
|
||
}
|
||
if (s == '约1.5米') {
|
||
return 1;
|
||
}
|
||
if (s == '约3.0米') {
|
||
return 2;
|
||
}
|
||
return 0;
|
||
}
|
||
}
|