2023-09-04 15:00:42 +08:00
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
2024-06-04 09:12:41 +08:00
|
|
|
import 'package:date_format/date_format.dart';
|
2024-03-18 16:16:51 +08:00
|
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
2024-04-16 17:44:38 +08:00
|
|
|
import 'package:get/get.dart';
|
2024-06-04 09:12:41 +08:00
|
|
|
import 'package:star_lock/app_settings/app_settings.dart';
|
|
|
|
|
import 'package:star_lock/main/lockDetail/lockSet/lockTime/getServerDatetime_entity.dart';
|
2023-09-04 15:00:42 +08:00
|
|
|
|
|
|
|
|
import '../../../../blue/blue_manage.dart';
|
2024-03-19 18:04:51 +08:00
|
|
|
import '../../../../blue/io_protocol/io_getStarLockStatusInfo.dart';
|
2023-09-04 15:00:42 +08:00
|
|
|
import '../../../../blue/io_protocol/io_timing.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';
|
2023-09-07 18:36:16 +08:00
|
|
|
import '../../../../network/api_repository.dart';
|
2023-09-04 15:00:42 +08:00
|
|
|
import '../../../../tools/baseGetXController.dart';
|
|
|
|
|
import '../../../../tools/dateTool.dart';
|
|
|
|
|
import '../../../../tools/storage.dart';
|
|
|
|
|
import 'lockTime_state.dart';
|
|
|
|
|
|
|
|
|
|
class LockTimeLogic extends BaseGetXController{
|
|
|
|
|
final LockTimeState state = LockTimeState();
|
|
|
|
|
|
|
|
|
|
// 获取解析后的数据
|
|
|
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
|
|
|
void _initReplySubscription() {
|
2024-06-04 09:12:41 +08:00
|
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
|
2023-09-04 15:00:42 +08:00
|
|
|
if(reply is TimingReply) {
|
|
|
|
|
_replyTiming(reply);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取锁状态
|
2024-06-04 09:12:41 +08:00
|
|
|
if(reply is GetStarLockStatuInfoReply && state.ifCurrentScreen.value == true) {
|
|
|
|
|
_replyGetStarLockStatusInfo(reply);
|
|
|
|
|
}
|
2023-09-04 15:00:42 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:12:41 +08:00
|
|
|
// 获取锁状态数据解析
|
|
|
|
|
Future<void> _replyGetStarLockStatusInfo(Reply reply) async {
|
|
|
|
|
final int status = reply.data[2];
|
|
|
|
|
switch (status) {
|
|
|
|
|
case 0x00:
|
|
|
|
|
//成功
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
|
|
|
|
|
// 有效时间
|
|
|
|
|
final List<int> indate = reply.data.sublist(150, 154);
|
|
|
|
|
final int indateValue = (0xff & indate[0]) << 24 |
|
|
|
|
|
(0xff & indate[1]) << 16 |
|
|
|
|
|
(0xff & indate[2]) << 8 |
|
|
|
|
|
(0xFF & indate[3]);
|
|
|
|
|
AppLog.log('indate:$indate indateValue:$indateValue');
|
|
|
|
|
state.dateTime.value = DateTool().dateToYMDHNString('$indateValue');
|
|
|
|
|
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(),
|
2024-06-11 14:03:48 +08:00
|
|
|
utcTimeStamp: state.serverTime,
|
|
|
|
|
unixTimeStamp: getLocalTime(),
|
2024-06-04 09:12:41 +08:00
|
|
|
isBeforeAddUser: false,
|
|
|
|
|
privateKey: getPrivateKeyList,
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
//失败
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-04 15:00:42 +08:00
|
|
|
|
|
|
|
|
// 校时数据解析
|
|
|
|
|
Future<void> _replyTiming(Reply reply) async {
|
2024-06-04 09:12:41 +08:00
|
|
|
final int status = reply.data[2];
|
2023-09-04 15:00:42 +08:00
|
|
|
switch(status){
|
|
|
|
|
case 0x00:
|
2024-04-16 17:44:38 +08:00
|
|
|
//成功
|
2024-06-11 14:03:48 +08:00
|
|
|
final String dataEime = DateTool().dateToYMDHNString('${state.serverTime}');
|
2024-03-19 18:04:51 +08:00
|
|
|
state.dateTime.value = dataEime;
|
|
|
|
|
|
2024-01-09 18:02:02 +08:00
|
|
|
state.sureBtnState.value = 0;
|
2024-01-18 11:25:56 +08:00
|
|
|
cancelBlueConnetctToastTimer();
|
2024-01-09 18:02:02 +08:00
|
|
|
dismissEasyLoading();
|
2024-06-04 09:12:41 +08:00
|
|
|
showToast('锁时间更新成功'.tr);
|
2023-09-04 15:00:42 +08:00
|
|
|
break;
|
|
|
|
|
case 0x06:
|
2024-04-16 17:44:38 +08:00
|
|
|
//无权限
|
2024-06-11 14:03:48 +08:00
|
|
|
// final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
// final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
|
//
|
|
|
|
|
// final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
|
|
|
// final List<int> getTokenList = changeStringListToIntList(token!);
|
|
|
|
|
//
|
|
|
|
|
// final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
|
|
|
|
|
// final List<int> getSignKeyList = changeStringListToIntList(signKey!);
|
|
|
|
|
//
|
|
|
|
|
// IoSenderManage.senderTimingCommand(
|
|
|
|
|
// lockID:BlueManage().connectDeviceName,
|
|
|
|
|
// userID:await Storage.getUid(),
|
|
|
|
|
// nowTime: state.serverTime,
|
|
|
|
|
// token:getTokenList,
|
|
|
|
|
// needAuthor:1,
|
|
|
|
|
// signKey:getSignKeyList,
|
|
|
|
|
// privateKey:getPrivateKeyList,
|
|
|
|
|
// );
|
2023-09-04 15:00:42 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验时间
|
|
|
|
|
Future<void> sendTiming() async {
|
2024-01-09 18:02:02 +08:00
|
|
|
if(state.sureBtnState.value == 1){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
state.sureBtnState.value = 1;
|
|
|
|
|
|
2024-01-02 18:03:50 +08:00
|
|
|
showEasyLoading();
|
2024-01-09 18:02:02 +08:00
|
|
|
showBlueConnetctToastTimer(action: (){
|
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
});
|
2024-05-21 15:33:06 +08:00
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async {
|
2024-03-18 16:16:51 +08:00
|
|
|
if (connectionState == BluetoothConnectionState.connected) {
|
2024-06-04 09:12:41 +08:00
|
|
|
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
2023-09-04 15:00:42 +08:00
|
|
|
|
2024-06-04 09:12:41 +08:00
|
|
|
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
|
|
|
|
final List<int> getTokenList = changeStringListToIntList(token!);
|
2023-09-04 15:00:42 +08:00
|
|
|
|
2024-06-04 09:12:41 +08:00
|
|
|
final List<String>? signKey = await Storage.getStringList(saveBlueSignKey);
|
|
|
|
|
final List<int> getSignKeyList = changeStringListToIntList(signKey!);
|
2023-09-04 15:00:42 +08:00
|
|
|
|
|
|
|
|
IoSenderManage.senderTimingCommand(
|
|
|
|
|
lockID:BlueManage().connectDeviceName,
|
|
|
|
|
userID:await Storage.getUid(),
|
2024-06-11 14:03:48 +08:00
|
|
|
nowTime: state.serverTime,
|
2023-09-04 15:00:42 +08:00
|
|
|
token:getTokenList,
|
|
|
|
|
needAuthor:1,
|
|
|
|
|
signKey:getSignKeyList,
|
|
|
|
|
privateKey:getPrivateKeyList,
|
|
|
|
|
);
|
2024-03-18 16:16:51 +08:00
|
|
|
} else if (connectionState == BluetoothConnectionState.disconnected) {
|
2024-01-02 18:03:50 +08:00
|
|
|
dismissEasyLoading();
|
2024-01-09 18:02:02 +08:00
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
state.sureBtnState.value = 0;
|
|
|
|
|
if(state.ifCurrentScreen.value == true){
|
|
|
|
|
showBlueConnetctToast();
|
|
|
|
|
}
|
2023-09-04 15:00:42 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:12:41 +08:00
|
|
|
// 获取锁状态 更新时间
|
|
|
|
|
Future<void> getStarLockStatus() async {
|
|
|
|
|
showEasyLoading();
|
|
|
|
|
showBlueConnetctToastTimer(action: () {
|
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
});
|
|
|
|
|
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
|
|
|
|
(BluetoothConnectionState deviceConnectionState) async {
|
|
|
|
|
if (deviceConnectionState == BluetoothConnectionState.connected) {
|
|
|
|
|
final List<String>? privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
|
|
|
final List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
|
|
|
|
|
|
IoSenderManage.senderGetStarLockStatuInfo(
|
|
|
|
|
lockID: BlueManage().connectDeviceName,
|
|
|
|
|
userID: await Storage.getUid(),
|
2024-06-11 14:03:48 +08:00
|
|
|
utcTimeStamp: state.serverTime,
|
|
|
|
|
unixTimeStamp: getLocalTime(),
|
2024-06-04 09:12:41 +08:00
|
|
|
isBeforeAddUser: false,
|
|
|
|
|
privateKey: getPrivateKeyList,
|
|
|
|
|
);
|
|
|
|
|
} else if (deviceConnectionState == BluetoothConnectionState.disconnected) {
|
|
|
|
|
dismissEasyLoading();
|
|
|
|
|
cancelBlueConnetctToastTimer();
|
|
|
|
|
if (state.ifCurrentScreen.value == true) {
|
|
|
|
|
showBlueConnetctToast();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-19 18:04:51 +08:00
|
|
|
|
2023-10-12 14:00:49 +08:00
|
|
|
// 从网关获取时间
|
2024-06-04 09:12:41 +08:00
|
|
|
Future<void> getLockTimeFromGateway() async{
|
|
|
|
|
final GetServerDatetimeEntity entity = await ApiRepository.to.getLockTimeFromGateway(
|
2023-11-01 17:28:59 +08:00
|
|
|
lockId: state.lockSetInfoData.value.lockId.toString(),
|
2023-09-07 18:36:16 +08:00
|
|
|
);
|
|
|
|
|
if(entity.errorCode!.codeIsSuccessful){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 10:40:30 +08:00
|
|
|
// 从服务器获取锁的时间 开锁时传入
|
2024-06-11 14:03:48 +08:00
|
|
|
Future<void> getServerDatetime(bool isSendTime) async{
|
2024-06-07 10:53:24 +08:00
|
|
|
final GetServerDatetimeEntity entity = await ApiRepository.to.getServerDatetimeData(isUnShowLoading:false);
|
2023-09-07 18:36:16 +08:00
|
|
|
if(entity.errorCode!.codeIsSuccessful){
|
2024-06-11 14:03:48 +08:00
|
|
|
state.serverTime = entity.data!.date! ~/ 1000;
|
2024-05-09 10:40:30 +08:00
|
|
|
// AppLog.log("entity.data!.date! ~/ 1000:${entity.data!.date! ~/ 1000} DateTime.now().millisecondsSinceEpoch ~/ 1000:${DateTime.now().millisecondsSinceEpoch ~/ 1000} 服务器时间差:${state.differentialTime}");
|
2024-06-11 14:03:48 +08:00
|
|
|
if(isSendTime == false){
|
|
|
|
|
getStarLockStatus();
|
|
|
|
|
}else{
|
|
|
|
|
sendTiming();
|
|
|
|
|
}
|
2023-09-07 18:36:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 14:03:48 +08:00
|
|
|
int getLocalTime(){
|
|
|
|
|
final DateTime now = DateTime.now();
|
|
|
|
|
final Duration timeZoneOffset = now.timeZoneOffset;
|
2024-08-21 14:12:15 +08:00
|
|
|
// AppLog.log('timeZoneOffset.inSeconds:$timeZoneOffset.inSeconds');
|
2024-06-11 14:03:48 +08:00
|
|
|
return state.serverTime + timeZoneOffset.inSeconds;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 15:00:42 +08:00
|
|
|
@override
|
|
|
|
|
void onReady() {
|
|
|
|
|
super.onReady();
|
|
|
|
|
|
|
|
|
|
_initReplySubscription();
|
2024-03-19 18:04:51 +08:00
|
|
|
|
2023-10-12 14:00:49 +08:00
|
|
|
// getLockTimeFromGateway();
|
2024-06-07 10:53:24 +08:00
|
|
|
|
2024-06-11 14:03:48 +08:00
|
|
|
getServerDatetime(false);
|
|
|
|
|
|
2023-09-04 15:00:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onInit() {
|
|
|
|
|
super.onInit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void onClose() {
|
|
|
|
|
super.onClose();
|
|
|
|
|
_replySubscription.cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|