2024-06-11 14:03:48 +08:00
|
|
|
|
2024-01-18 11:25:56 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
2024-03-18 16:16:51 +08:00
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
2024-04-11 17:06:06 +08:00
|
|
|
import 'package:get/get.dart';
|
2024-01-18 11:25:56 +08:00
|
|
|
|
|
|
|
|
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';
|
2024-01-18 11:25:56 +08:00
|
|
|
import '../../../../../tools/eventBusEventManage.dart';
|
|
|
|
|
import '../../../../../tools/storage.dart';
|
2024-06-11 14:03:48 +08:00
|
|
|
|
|
|
|
|
import 'uploadElectricQuantity_entity.dart';
|
2024-01-16 16:52:05 +08:00
|
|
|
import 'uploadElectricQuantity_state.dart';
|
|
|
|
|
|
|
|
|
|
class UploadElectricQuantityLogic extends BaseGetXController {
|
|
|
|
|
final UploadElectricQuantityState state = UploadElectricQuantityState();
|
|
|
|
|
|
|
|
|
|
//电量更新请求
|
2024-06-05 17:36:46 +08:00
|
|
|
Future<void> uploadElectricQuantityRequest(
|
|
|
|
|
String electricQuantity, String electricQuantityStandby) async {
|
2024-06-11 14:03:48 +08:00
|
|
|
final UploadElectricQuantityEntity entity = await ApiRepository.to
|
2024-06-05 17:36:46 +08:00
|
|
|
.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) {
|
2024-06-11 14:03:48 +08:00
|
|
|
state.uploadElectricQuantityDate.value = entity.data!.electricQuantityDate!;
|
|
|
|
|
state.lockSetInfoData.value.lockBasicInfo!.electricQuantityDate = state.uploadElectricQuantityDate.value;
|
|
|
|
|
|
2024-06-03 13:50:07 +08:00
|
|
|
showToast('锁电量更新成功'.tr, something: () {
|
2024-02-28 15:27:10 +08:00
|
|
|
eventBus
|
|
|
|
|
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
2024-06-05 17:36:46 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 11:25:56 +08:00
|
|
|
// 获取锁状态
|
|
|
|
|
Future<void> getStarLockStatus() async {
|
2024-02-28 15:27:10 +08:00
|
|
|
if (state.sureBtnState.value == 1) {
|
2024-01-18 11:25:56 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
state.sureBtnState.value = 1;
|
|
|
|
|
|
|
|
|
|
showEasyLoading();
|
2024-02-28 15:27:10 +08:00
|
|
|
showBlueConnetctToastTimer(action: () {
|
2024-01-18 11:25:56 +08:00
|
|
|
dismissEasyLoading();
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
});
|
2024-06-07 10:53:24 +08:00
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState deviceConnectionState) async {
|
2024-03-18 16:16:51 +08:00
|
|
|
if (deviceConnectionState == BluetoothConnectionState.connected) {
|
2024-01-18 11:25:56 +08:00
|
|
|
dismissEasyLoading();
|
2024-06-07 10:53:24 +08:00
|
|
|
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
2024-01-18 11:25:56 +08:00
|
|
|
IoSenderManage.senderGetStarLockStatuInfo(
|
|
|
|
|
lockID: BlueManage().connectDeviceName,
|
|
|
|
|
userID: await Storage.getUid(),
|
2024-06-11 14:03:48 +08:00
|
|
|
utcTimeStamp: 0,
|
|
|
|
|
unixTimeStamp: 0,
|
2024-04-25 17:45:00 +08:00
|
|
|
isBeforeAddUser: false,
|
2024-01-18 11:25:56 +08:00
|
|
|
privateKey: getPrivateKeyList,
|
|
|
|
|
);
|
2024-06-05 17:36:46 +08:00
|
|
|
} else if (deviceConnectionState ==
|
|
|
|
|
BluetoothConnectionState.disconnected) {
|
2024-01-18 11:25:56 +08:00
|
|
|
dismissEasyLoading();
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
state.sureBtnState.value = 0;
|
2024-02-28 15:27:10 +08:00
|
|
|
if (state.ifCurrentScreen.value == true) {
|
2024-01-18 11:25:56 +08:00
|
|
|
showBlueConnetctToast();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取解析后的数据
|
|
|
|
|
late StreamSubscription<Reply> _replySubscription;
|
2024-06-05 17:36:46 +08:00
|
|
|
|
2024-01-18 11:25:56 +08:00
|
|
|
void _initReplySubscription() {
|
2024-02-28 15:27:10 +08:00
|
|
|
_replySubscription =
|
2024-06-04 09:12:41 +08:00
|
|
|
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
|
2024-01-18 11:25:56 +08:00
|
|
|
// 获取锁状态信息
|
|
|
|
|
if (reply is GetStarLockStatuInfoReply) {
|
|
|
|
|
_replyGetStarLockStatusInfo(reply);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取星锁状态
|
|
|
|
|
Future<void> _replyGetStarLockStatusInfo(Reply reply) async {
|
2024-06-03 13:50:07 +08:00
|
|
|
final int status = reply.data[2];
|
2024-01-18 11:25:56 +08:00
|
|
|
switch (status) {
|
|
|
|
|
case 0x00:
|
2024-02-28 15:27:10 +08:00
|
|
|
//成功
|
2024-01-18 11:25:56 +08:00
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
|
|
|
|
|
// 电池剩余电量
|
2024-06-04 09:12:41 +08:00
|
|
|
final int battRemCap = reply.data[132];
|
2024-08-09 14:00:36 +08:00
|
|
|
state.battRemCap.value = battRemCap>100?100:battRemCap;
|
|
|
|
|
state.lockSetInfoData.value.lockBasicInfo!.electricQuantity = state.battRemCap.value;
|
2024-04-30 16:16:06 +08:00
|
|
|
|
|
|
|
|
// 备用电池剩余电量
|
2024-06-04 09:12:41 +08:00
|
|
|
final int battRemCapStandby = reply.data[133];
|
2024-08-09 14:00:36 +08:00
|
|
|
state.battRemCapStandby.value = battRemCapStandby>100?100:battRemCapStandby;
|
|
|
|
|
state.lockSetInfoData.value.lockBasicInfo!.electricQuantityStandby = state.battRemCapStandby.value;
|
2024-06-05 17:36:46 +08:00
|
|
|
|
|
|
|
|
uploadElectricQuantityRequest(
|
|
|
|
|
battRemCap.toString(), battRemCapStandby.toString());
|
2024-01-18 11:25:56 +08:00
|
|
|
break;
|
|
|
|
|
case 0x06:
|
2024-02-28 15:27:10 +08:00
|
|
|
//无权限
|
2024-06-05 17:36:46 +08:00
|
|
|
final List<String>? privateKey =
|
|
|
|
|
await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
final List<int> getPrivateKeyList =
|
|
|
|
|
changeStringListToIntList(privateKey!);
|
2024-04-19 18:13:34 +08:00
|
|
|
IoSenderManage.senderGetStarLockStatuInfo(
|
|
|
|
|
lockID: BlueManage().connectDeviceName,
|
|
|
|
|
userID: await Storage.getUid(),
|
2024-06-11 14:03:48 +08:00
|
|
|
utcTimeStamp: 0,
|
|
|
|
|
unixTimeStamp: 0,
|
2024-04-25 17:45:00 +08:00
|
|
|
isBeforeAddUser: false,
|
2024-04-19 18:13:34 +08:00
|
|
|
privateKey: getPrivateKeyList,
|
|
|
|
|
);
|
2024-06-04 09:12:41 +08:00
|
|
|
|
2024-01-18 11:25:56 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
2024-02-28 15:27:10 +08:00
|
|
|
//失败
|
2024-01-18 11:25:56 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 14:03:48 +08:00
|
|
|
// // 从服务器获取锁的时间 开锁时传入
|
|
|
|
|
// 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;
|
|
|
|
|
// }
|
2024-05-09 10:40:30 +08:00
|
|
|
|
2024-01-18 11:25:56 +08:00
|
|
|
@override
|
|
|
|
|
void onReady() {
|
|
|
|
|
super.onReady();
|
|
|
|
|
|
|
|
|
|
_initReplySubscription();
|
2024-06-07 10:53:24 +08:00
|
|
|
// getServerDatetime();
|
2024-01-18 11:25:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onClose() {
|
|
|
|
|
super.onClose();
|
|
|
|
|
_replySubscription.cancel();
|
|
|
|
|
}
|
2024-02-28 15:27:10 +08:00
|
|
|
}
|