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