174 lines
5.9 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import '../../../../../blue/blue_manage.dart';
import '../../../../../blue/io_protocol/io_getStarLockStatusInfo.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';
2024-01-16 16:52:05 +08:00
import '../../../../../network/api_repository.dart';
import '../../../../../tools/baseGetXController.dart';
import '../../../../../tools/eventBusEventManage.dart';
import '../../../../../tools/storage.dart';
import 'uploadElectricQuantity_entity.dart';
2024-01-16 16:52:05 +08:00
import 'uploadElectricQuantity_state.dart';
class UploadElectricQuantityLogic extends BaseGetXController {
final UploadElectricQuantityState state = UploadElectricQuantityState();
//电量更新请求
Future<void> uploadElectricQuantityRequest(
String electricQuantity, String electricQuantityStandby) async {
final UploadElectricQuantityEntity entity = await ApiRepository.to
.uploadElectricQuantity(
electricQuantity: electricQuantity,
electricQuantityStandby: electricQuantityStandby,
lockId: state.lockSetInfoData.value.lockId.toString(),
isUnShowLoading: false);
2024-01-16 16:52:05 +08:00
if (entity.errorCode!.codeIsSuccessful) {
state.uploadElectricQuantityDate.value = entity.data!.electricQuantityDate!;
state.lockSetInfoData.value.lockBasicInfo!.electricQuantityDate = state.uploadElectricQuantityDate.value;
showToast('锁电量更新成功'.tr, something: () {
eventBus
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
eventBus.fire(LockSetChangeSetRefreshLockDetailWithType(4, <String,dynamic>{
'electricQuantity': electricQuantity,
'uploadElectricQuantityDate': state.uploadElectricQuantityDate.value,
}));
2024-01-23 18:37:03 +08:00
eventBus.fire(RefreshLockListInfoDataEvent());
});
2024-01-16 16:52:05 +08:00
}
}
// 获取锁状态
Future<void> getStarLockStatus() async {
if (state.sureBtnState.value == 1) {
return;
}
state.sureBtnState.value = 1;
showEasyLoading();
showBlueConnetctToastTimer(action: () {
dismissEasyLoading();
state.sureBtnState.value = 0;
});
BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState deviceConnectionState) async {
if (deviceConnectionState == BluetoothConnectionState.connected) {
dismissEasyLoading();
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
IoSenderManage.senderGetStarLockStatuInfo(
lockID: BlueManage().connectDeviceName,
userID: await Storage.getUid(),
utcTimeStamp: 0,
unixTimeStamp: 0,
isBeforeAddUser: false,
privateKey: getPrivateKeyList,
);
} else if (deviceConnectionState ==
BluetoothConnectionState.disconnected) {
dismissEasyLoading();
cancelBlueConnetctToastTimer();
state.sureBtnState.value = 0;
if (state.ifCurrentScreen.value == true) {
showBlueConnetctToast();
}
}
});
}
// 获取解析后的数据
late StreamSubscription<Reply> _replySubscription;
void _initReplySubscription() {
_replySubscription =
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
// 获取锁状态信息
if (reply is GetStarLockStatuInfoReply) {
_replyGetStarLockStatusInfo(reply);
}
});
}
// 获取星锁状态
Future<void> _replyGetStarLockStatusInfo(Reply reply) async {
final int status = reply.data[2];
switch (status) {
case 0x00:
//成功
state.sureBtnState.value = 0;
dismissEasyLoading();
cancelBlueConnetctToastTimer();
// 电池剩余电量
final int battRemCap = reply.data[132];
state.battRemCap.value = battRemCap>100?100:battRemCap;
state.lockSetInfoData.value.lockBasicInfo!.electricQuantity = state.battRemCap.value;
2024-04-30 16:16:06 +08:00
// 备用电池剩余电量
final int battRemCapStandby = reply.data[133];
state.battRemCapStandby.value = battRemCapStandby>100?100:battRemCapStandby;
state.lockSetInfoData.value.lockBasicInfo!.electricQuantityStandby = state.battRemCapStandby.value;
uploadElectricQuantityRequest(
battRemCap.toString(), battRemCapStandby.toString());
break;
case 0x06:
//无权限
final List<String>? privateKey =
await Storage.getStringList(saveBluePrivateKey);
final List<int> getPrivateKeyList =
changeStringListToIntList(privateKey!);
IoSenderManage.senderGetStarLockStatuInfo(
lockID: BlueManage().connectDeviceName,
userID: await Storage.getUid(),
utcTimeStamp: 0,
unixTimeStamp: 0,
isBeforeAddUser: false,
privateKey: getPrivateKeyList,
);
break;
default:
//失败
break;
}
}
// // 从服务器获取锁的时间 开锁时传入
// Future<void> getServerDatetime() async {
// final GetServerDatetimeEntity entity = await ApiRepository.to.getServerDatetimeData(isUnShowLoading:false);
// if (entity.errorCode!.codeIsSuccessful) {
// state.serverTime = entity.data!.date! ~/ 1000;
// getStarLockStatus();
// }
// }
//
// int getLocalTime(){
// final DateTime now = DateTime.now();
// final Duration timeZoneOffset = now.timeZoneOffset;
// AppLog.log('timeZoneOffset.inSeconds:$timeZoneOffset.inSeconds');
// return state.serverTime + timeZoneOffset.inSeconds;
// }
@override
void onReady() {
super.onReady();
_initReplySubscription();
// getServerDatetime();
}
@override
void onClose() {
super.onClose();
_replySubscription.cancel();
}
}