195 lines
6.6 KiB
Dart
Executable File
195 lines
6.6 KiB
Dart
Executable File
import 'dart:async';
|
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:star_lock/main/lockDetail/lockSet/lockTime/getServerDatetime_entity.dart';
|
|
|
|
import '../../../../../app_settings/app_settings.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';
|
|
import '../../../../../network/api_repository.dart';
|
|
import '../../../../../tools/baseGetXController.dart';
|
|
import '../../../../../tools/dateTool.dart';
|
|
import '../../../../../tools/eventBusEventManage.dart';
|
|
import '../../../../../tools/storage.dart';
|
|
import '../../../lockOperatingRecord/keyOperationRecord_entity.dart';
|
|
import 'uploadElectricQuantity_state.dart';
|
|
|
|
class UploadElectricQuantityLogic extends BaseGetXController {
|
|
final UploadElectricQuantityState state = UploadElectricQuantityState();
|
|
|
|
//电量更新请求
|
|
Future<void> uploadElectricQuantityRequest(
|
|
String electricQuantity, String electricQuantityStandby) async {
|
|
final KeyOperationRecordEntity entity = await ApiRepository.to
|
|
.uploadElectricQuantity(
|
|
electricQuantity: electricQuantity,
|
|
electricQuantityStandby: electricQuantityStandby,
|
|
lockId: state.lockSetInfoData.value.lockId.toString(),
|
|
isUnShowLoading: false);
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
showToast('锁电量更新成功'.tr, something: () {
|
|
eventBus
|
|
.fire(PassCurrentLockInformationEvent(state.lockSetInfoData.value));
|
|
eventBus.fire(LockSetChangeSetRefreshLockDetailWithType(4, <String,dynamic>{
|
|
'electricQuantity': electricQuantity,
|
|
'uploadElectricQuantityDate': state.uploadElectricQuantityDate.value,
|
|
}));
|
|
eventBus.fire(RefreshLockListInfoDataEvent());
|
|
});
|
|
}
|
|
}
|
|
|
|
// 获取锁状态
|
|
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: getUTCTime(),
|
|
unixTimeStamp: getLocalTime(),
|
|
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.lockSetInfoData.value.lockBasicInfo!.electricQuantity =
|
|
battRemCap;
|
|
|
|
// 备用电池剩余电量
|
|
final int battRemCapStandby = reply.data[133];
|
|
state.lockSetInfoData.value.lockBasicInfo!.electricQuantityStandby =
|
|
battRemCapStandby;
|
|
|
|
state.uploadElectricQuantityDate.value =
|
|
DateTime.now().millisecondsSinceEpoch;
|
|
state.lockSetInfoData.value.lockBasicInfo!.electricQuantityDate =
|
|
DateTime.now().millisecondsSinceEpoch;
|
|
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: getUTCTime(),
|
|
unixTimeStamp: getLocalTime(),
|
|
isBeforeAddUser: false,
|
|
privateKey: getPrivateKeyList,
|
|
);
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 从服务器获取锁的时间 开锁时传入
|
|
Future<void> getServerDatetime() async {
|
|
final GetServerDatetimeEntity entity =
|
|
await ApiRepository.to.getServerDatetimeData();
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
state.differentialTime = entity.data!.date! ~/ 1000 -
|
|
DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
// AppLog.log("entity.data!.date! ~/ 1000:${entity.data!.date! ~/ 1000} DateTime.now().millisecondsSinceEpoch ~/ 1000:${DateTime.now().millisecondsSinceEpoch ~/ 1000} 服务器时间差:${state.differentialTime}");
|
|
}
|
|
}
|
|
|
|
int getLocalTime() {
|
|
return DateTime.now().millisecondsSinceEpoch ~/ 1000 +
|
|
state.differentialTime;
|
|
}
|
|
|
|
int getUTCTime() {
|
|
final DateTime utcTime =
|
|
DateTime.fromMillisecondsSinceEpoch(getLocalTime() * 1000, isUtc: true);
|
|
|
|
final String appointmentDate =
|
|
DateTool().getYMDHNDateStringWithDateTime(utcTime, 1);
|
|
final int utcTimeValue = DateTool().dateToTimestamp(appointmentDate, 1);
|
|
AppLog.log('appointmentDate: $appointmentDate utcTimeValue:$utcTimeValue');
|
|
return utcTimeValue ~/ 1000;
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
super.onReady();
|
|
|
|
_initReplySubscription();
|
|
getServerDatetime();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
}
|