249 lines
8.3 KiB
Dart
249 lines
8.3 KiB
Dart
|
||
import 'dart:async';
|
||
|
||
import 'package:flutter_blue_plus/flutter_blue_plus.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/eventBusEventManage.dart';
|
||
|
||
import '../../../app_settings/app_settings.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();
|
||
|
||
if(indexList[0] == 2){
|
||
var passwordData = indexList.sublist(7, 17);
|
||
var password = utf8String(passwordData);
|
||
indexMap["user"] = password.toString();
|
||
}else{
|
||
int userNo = (indexList[1]*255) + 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);
|
||
}
|
||
lockRecordUploadData(uploadList);
|
||
|
||
if(dataLength == state.logCountPage){
|
||
state.ifHaveNext = true;
|
||
}
|
||
}
|
||
break;
|
||
case 0x06:
|
||
//无权限 需要鉴权
|
||
|
||
break;
|
||
default:
|
||
//失败
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 查询事件记录(时间查询)
|
||
Future<void> senderReferEventRecordTime() async {
|
||
showEasyLoading();
|
||
showBlueConnetctToastTimer(action: (){
|
||
dismissEasyLoading();
|
||
});
|
||
BlueManage().bludSendData(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() async {
|
||
DoorLockLogEntity entity = await ApiRepository.to.lockEventList(
|
||
lockId: state.keyInfos.value.lockId!,
|
||
lockEventType: state.dropdownValue.value,
|
||
pageNo: 1,
|
||
pageSize: int.parse(pageSize),
|
||
startDate: state.startDate.value,
|
||
endDate: state.endDate.value);
|
||
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
state.lockLogItemList.value = entity.data!.itemList!;
|
||
}
|
||
}
|
||
|
||
/// 刷新门锁日志列表
|
||
StreamSubscription? _getDoorLockLogListRefreshUIEvent;
|
||
void _getDoorLockLogListRefreshUIAction() {
|
||
_getDoorLockLogListRefreshUIEvent =
|
||
eventBus.on<DoorLockLogListRefreshUI>().listen((event) {
|
||
// 设置startDate为当天的0点
|
||
state.startDate.value = DateTime(event.getDoorLockLogTime.year,
|
||
event.getDoorLockLogTime.month, event.getDoorLockLogTime.day)
|
||
.millisecondsSinceEpoch;
|
||
// 设置endDate为下一天的0点,然后减去1毫秒
|
||
state.endDate.value = (DateTime(
|
||
event.getDoorLockLogTime.year,
|
||
event.getDoorLockLogTime.month,
|
||
event.getDoorLockLogTime.day + 1)
|
||
.subtract(const Duration(milliseconds: 1)))
|
||
.millisecondsSinceEpoch;
|
||
|
||
pageNo = 1;
|
||
mockNetworkDataRequest();
|
||
});
|
||
}
|
||
|
||
// 查询锁记录最后时间
|
||
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{
|
||
pageNo = 1;
|
||
mockNetworkDataRequest();
|
||
}
|
||
}
|
||
}
|
||
|
||
//清空操作记录
|
||
void clearOperationRecordRequest() async {
|
||
KeyOperationRecordEntity entity =
|
||
await ApiRepository.to.clearOperationRecord('28');
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
showToast("清除数据成功");
|
||
}
|
||
}
|
||
|
||
@override
|
||
Future<void> onReady() async {
|
||
// TODO: implement onReady
|
||
super.onReady();
|
||
|
||
// 获取是否是演示模式 演示模式不获取接口
|
||
var isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
|
||
if (isDemoMode == false) {
|
||
// _initReplySubscription();
|
||
|
||
mockNetworkDataRequest();
|
||
_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();
|
||
}
|
||
}
|
||
}
|