app-starlock/lib/main/lockDetail/doorLockLog/doorLockLog_logic.dart

286 lines
9.9 KiB
Dart
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:star_lock/main/lockDetail/doorLockLog/doorLockLog_entity.dart';
import 'package:star_lock/main/lockDetail/doorLockLog/doorLockLog_state.dart';
import 'package:star_lock/main/lockDetail/lockOperatingRecord/lockOperatingRecordGetLastRecordTime_entity.dart';
import 'package:star_lock/tools/commonDataManage.dart';
import 'package:star_lock/tools/eventBusEventManage.dart';
import '../../../blue/blue_manage.dart';
import '../../../blue/io_protocol/io_referEventRecordTime.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/storage.dart';
import '../lockOperatingRecord/keyOperationRecord_entity.dart';
class DoorLockLogLogic extends BaseGetXController {
DoorLockLogState state = DoorLockLogState();
// 获取解析后的数据
late StreamSubscription<Reply> _replySubscription;
void _initReplySubscription() {
_replySubscription =
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
if (reply is SenderReferEventRecordTimeReply &&
state.ifCurrentScreen.value == true) {
_replyReferEventRecordTime(reply);
}
});
}
// 根据时间查解析数据
Future<void> _replyReferEventRecordTime(Reply reply) async {
cancelBlueConnetctToastTimer();
final int status = reply.data[2];
switch (status) {
case 0x00:
//成功
final int dataLength = (reply.data[5] << 8) + reply.data[6];
// AppLog.log("dataLength:$dataLength");
// var dataLength = reply.data[5];
if (dataLength > 0) {
reply.data.removeRange(0, 7);
// 把得到的数据按8位分割成数组 然后塞进一个新的数组里面
if (reply.data.length < 17) {
return;
}
final List<List<int>> getList = splitList(reply.data, 17);
// AppLog.log("getList:$getList");
final List uploadList = [];
for (int i = 0; i < getList.length; i++) {
final List<int> indexList = getList[i];
// AppLog.log("indexList:$indexList");
final Map indexMap = {};
indexMap['type'] = indexList[0].toString();
final int userNo = (indexList[1] * 256) + indexList[2];
indexMap['user'] = userNo.toString();
// AppLog.log('userNouserNouserNouserNo:$userNo');
final List<int> passwordData = indexList.sublist(7, 17);
final String password = utf8String(passwordData);
indexMap['password'] = password.toString();
// AppLog.log('passwordpasswordpassword:$password');
indexMap['success'] = '1';
final int time = (0xff & indexList[3]) << 24 |
(0xff & indexList[4]) << 16 |
(0xff & indexList[5]) << 8 |
(0xFF & indexList[6]);
final operateDate = time * 1000;
final serverTime = state.currentDate;
if (DateTime.fromMillisecondsSinceEpoch(operateDate).isAfter(
DateTime.fromMillisecondsSinceEpoch(serverTime * 1000))) {
// AppLog.log('operateDate:$operateDate state.currentDate:${state.currentDate}');
continue;
}
indexMap['date'] = '$operateDate';
uploadList.add(indexMap);
}
if (dataLength == state.logCountPage) {
state.ifHaveNext = true;
} else {
state.ifHaveNext = false;
}
lockRecordUploadData(uploadList);
}
break;
case 0x06:
//无权限 需要鉴权
break;
default:
//失败
break;
}
}
// 查询事件记录(时间查询)
Future<void> senderReferEventRecordTime() async {
showEasyLoading();
showBlueConnetctToastTimer(action: () {
dismissEasyLoading();
});
BlueManage().blueSendData(BlueManage().connectDeviceName,
(BluetoothConnectionState connectionStateState) async {
if (connectionStateState == 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!);
IoSenderManage.senderReferEventRecordTimeCommand(
keyID: BlueManage().connectDeviceName,
userID: await Storage.getUid(),
logsCount: state.logCountPage,
// time:DateTime.now().millisecondsSinceEpoch~/1000,
time: state.operateDate,
currentDate: state.currentDate,
token: getTokenList,
needAuthor: 1,
publicKey: getPublicKeyList,
privateKey: getPrivateKeyList,
);
} else if (connectionStateState ==
BluetoothConnectionState.disconnected) {
dismissEasyLoading();
cancelBlueConnetctToastTimer();
if (state.ifCurrentScreen.value == true) {
showBlueConnetctToast();
}
}
});
}
//请求操作记录列表(门锁日志)
Future<void> mockNetworkDataRequest({required bool isRefresh}) async {
// 如果是下拉刷新,清空已有数据
if (isRefresh) {
state.lockLogItemList.clear();
pageNo = 1;
}
final DoorLockLogEntity entity = await ApiRepository.to.lockEventList(
lockId: state.keyInfos.value.lockId!,
lockEventType: state.dropdownValue.value,
pageNo: pageNo,
pageSize: int.parse(pageSize),
startDate: state.startDate.value,
endDate: state.endDate.value);
if (entity.errorCode!.codeIsSuccessful) {
// 更新数据列表
state.lockLogItemList.addAll(entity.data!.itemList!);
state.lockLogItemList.refresh();
// 更新页码
pageNo++;
}
}
/// 刷新门锁日志列表
StreamSubscription? _getDoorLockLogListRefreshUIEvent;
void _getDoorLockLogListRefreshUIAction() {
_getDoorLockLogListRefreshUIEvent = eventBus
.on<DoorLockLogListRefreshUI>()
.listen((DoorLockLogListRefreshUI event) {
state.currentSelectDate.value = event.getDoorLockLogTime;
// 设置startDate为当天的0点
state.startDate.value = DateTime(
state.currentSelectDate.value.year,
state.currentSelectDate.value.month,
state.currentSelectDate.value.day)
.millisecondsSinceEpoch;
// 设置endDate为下一天的0点然后减去1毫秒
state.endDate.value = DateTime(
state.currentSelectDate.value.year,
state.currentSelectDate.value.month,
state.currentSelectDate.value.day + 1)
.subtract(const Duration(milliseconds: 1))
.millisecondsSinceEpoch;
pageNo = 1;
mockNetworkDataRequest(isRefresh: true);
});
}
// 查询锁记录最后时间
Future<void> getLockRecordLastUploadDataTime() async {
final LockOperatingRecordGetLastRecordTimeEntity entity =
await ApiRepository.to.getLockRecordLastUploadDataTime(
lockId: state.keyInfos.value.lockId.toString());
if (entity.errorCode!.codeIsSuccessful) {
state.operateDate = entity.data!.operateDate! ~/ 1000;
state.currentDate = entity.data!.currentDate! ~/ 1000;
// AppLog.log('entity.data!.currentDate!:${entity.data!.currentDate!} currentDate:${state.currentDate}');
senderReferEventRecordTime();
}
}
// 操作记录上传
Future<void> lockRecordUploadData(List list) async {
// AppLog.log('上传数据:$list');
// 无数据时不上传
if (list.isEmpty) {
return;
}
final KeyOperationRecordEntity entity = await ApiRepository.to
.lockRecordUploadData(
lockId: state.keyInfos.value.lockId.toString(), records: list);
if (entity.errorCode!.codeIsSuccessful) {
if (state.ifHaveNext == true) {
getLockRecordLastUploadDataTime();
} else {
mockNetworkDataRequest(isRefresh: true);
}
}
}
//清空操作记录
Future<void> clearOperationRecordRequest() async {
final KeyOperationRecordEntity entity = await ApiRepository.to
.clearOperationRecord(
CommonDataManage().currentKeyInfo.lockId.toString());
if (entity.errorCode!.codeIsSuccessful) {
showToast('清除数据成功'.tr, something: () {
pageNo = 1;
mockNetworkDataRequest(isRefresh: true);
});
}
}
@override
Future<void> onReady() async {
super.onReady();
// 获取是否是演示模式 演示模式不获取接口
final bool? isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
if (isDemoMode == false) {
// _initReplySubscription();
// mockNetworkDataRequest(isRefresh: true);
_getDoorLockLogListRefreshUIAction();
}
}
@override
Future<void> onInit() async {
super.onInit();
// 获取是否是演示模式 演示模式不获取接口
final bool? isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
if (isDemoMode == false) {
getLockRecordLastUploadDataTime();
_initReplySubscription();
mockNetworkDataRequest(isRefresh: true);
}
}
@override
Future<void> onClose() async {
super.onClose();
//获取是否是演示模式 演示模式不获取接口
final bool? isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
if (isDemoMode == false) {
_replySubscription.cancel();
_getDoorLockLogListRefreshUIEvent?.cancel();
}
}
}