270 lines
9.1 KiB
Dart
Executable File
270 lines
9.1 KiB
Dart
Executable File
import 'dart:async';
|
||
|
||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||
import 'package:star_lock/app_settings/app_settings.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) {
|
||
if (reply is SenderReferEventRecordTimeReply &&
|
||
state.ifCurrentScreen.value == true) {
|
||
_replyReferEventRecordTime(reply);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 根据时间查解析数据
|
||
Future<void> _replyReferEventRecordTime(Reply reply) async {
|
||
cancelBlueConnetctToastTimer();
|
||
int status = reply.data[2];
|
||
switch (status) {
|
||
case 0x00:
|
||
//成功
|
||
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;
|
||
}
|
||
var getList = splitList(reply.data, 17);
|
||
// AppLog.log("getList:$getList");
|
||
var uploadList = [];
|
||
for (int i = 0; i < getList.length; i++) {
|
||
var indexList = getList[i];
|
||
// AppLog.log("indexList:$indexList");
|
||
var indexMap = {};
|
||
indexMap["type"] = indexList[0].toString();
|
||
int operateDate = 0;
|
||
if (indexList[0] == 2) {
|
||
var passwordData = indexList.sublist(7, 17);
|
||
var password = utf8String(passwordData);
|
||
indexMap["user"] = password.toString();
|
||
}else {
|
||
int userNo = (indexList[1] * 256) + indexList[2];
|
||
indexMap["user"] = userNo.toString();
|
||
}
|
||
|
||
indexMap["success"] = "1";
|
||
|
||
int time = ((0xff & indexList[(3)]) << 24 |
|
||
(0xff & indexList[4]) << 16 |
|
||
(0xff & indexList[5]) << 8 |
|
||
(0xFF & indexList[6]));
|
||
indexMap["date"] = "${time * 1000}";
|
||
uploadList.add(indexMap);
|
||
|
||
if (i == getList.length - 1) {
|
||
//设置最后的时间戳
|
||
state.operateDate = operateDate;
|
||
}
|
||
}
|
||
lockRecordUploadData(uploadList);
|
||
|
||
if (dataLength == state.logCountPage) {
|
||
state.ifHaveNext = true;
|
||
}
|
||
}
|
||
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) {
|
||
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
||
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
||
|
||
var token = await Storage.getStringList(saveBlueToken);
|
||
List<int> getTokenList = changeStringListToIntList(token!);
|
||
|
||
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
||
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,
|
||
token: getTokenList,
|
||
needAuthor: 1,
|
||
publicKey: getPublicKeyList,
|
||
privateKey: getPrivateKeyList,
|
||
);
|
||
} else if (connectionStateState ==
|
||
BluetoothConnectionState.disconnected) {
|
||
dismissEasyLoading();
|
||
cancelBlueConnetctToastTimer();
|
||
if (state.ifCurrentScreen.value == true) {
|
||
showBlueConnetctToast();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
//请求操作记录列表(门锁日志)
|
||
void mockNetworkDataRequest({required bool isRefresh}) async {
|
||
// 如果是下拉刷新,清空已有数据
|
||
if (isRefresh) {
|
||
state.lockLogItemList.clear();
|
||
pageNo = 1;
|
||
}
|
||
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!);
|
||
// 更新页码
|
||
pageNo++;
|
||
}
|
||
}
|
||
|
||
/// 刷新门锁日志列表
|
||
StreamSubscription? _getDoorLockLogListRefreshUIEvent;
|
||
void _getDoorLockLogListRefreshUIAction() {
|
||
_getDoorLockLogListRefreshUIEvent =
|
||
eventBus.on<DoorLockLogListRefreshUI>().listen((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);
|
||
});
|
||
}
|
||
|
||
// 查询锁记录最后时间
|
||
void getLockRecordLastUploadDataTime() async {
|
||
LockOperatingRecordGetLastRecordTimeEntity entity = await ApiRepository.to
|
||
.getLockRecordLastUploadDataTime(
|
||
lockId: state.keyInfos.value.lockId.toString());
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
state.operateDate = entity.data!.operateDate! ~/ 1000;
|
||
senderReferEventRecordTime();
|
||
}
|
||
}
|
||
|
||
// 操作记录上传
|
||
void lockRecordUploadData(List list) async {
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
|
||
//清空操作记录
|
||
void clearOperationRecordRequest() async {
|
||
KeyOperationRecordEntity entity = await ApiRepository.to
|
||
.clearOperationRecord(
|
||
CommonDataManage().currentKeyInfo.lockId.toString());
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast("清除数据成功", something: () {
|
||
pageNo = 1;
|
||
mockNetworkDataRequest(isRefresh: true);
|
||
});
|
||
}
|
||
}
|
||
|
||
@override
|
||
Future<void> onReady() async {
|
||
// TODO: implement onReady
|
||
super.onReady();
|
||
|
||
// 获取是否是演示模式 演示模式不获取接口
|
||
var isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
|
||
if (isDemoMode == false) {
|
||
// _initReplySubscription();
|
||
|
||
mockNetworkDataRequest(isRefresh: true);
|
||
_getDoorLockLogListRefreshUIAction();
|
||
}
|
||
}
|
||
|
||
@override
|
||
Future<void> onInit() async {
|
||
super.onInit();
|
||
|
||
// 获取是否是演示模式 演示模式不获取接口
|
||
var isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
|
||
if (isDemoMode == false) {
|
||
getLockRecordLastUploadDataTime();
|
||
// senderReferEventRecordTime();
|
||
// senderReferEventRecordNumber();
|
||
_initReplySubscription();
|
||
}
|
||
}
|
||
|
||
@override
|
||
Future<void> onClose() async {
|
||
// TODO: implement onClose
|
||
super.onClose();
|
||
|
||
//获取是否是演示模式 演示模式不获取接口
|
||
var isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
|
||
if (isDemoMode == false) {
|
||
_replySubscription.cancel();
|
||
_getDoorLockLogListRefreshUIEvent?.cancel();
|
||
}
|
||
}
|
||
}
|