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_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'; import '../../../../network/api_repository.dart'; 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 _replySubscription; void _initReplySubscription() { _replySubscription = EventBusManager().eventBus!.on().listen((reply) { if(reply is TimingReply) { _replyTiming(reply); } // 获取锁状态 // if(reply is GetStarLockStatuInfoReply && state.ifCurrentScreen.value == true) { // _replyGetStarLockStatusInfo(reply); // } }); } // 获取锁状态数据解析 // Future _replyGetStarLockStatusInfo(Reply reply) async { // int status = reply.data[2]; // switch (status) { // case 0x00: // //成功 // cancelBlueConnetctToastTimer(); // dismissEasyLoading(); // // // 有效时间 // var indate = reply.data.sublist(149, 153); // int indateValue = ((0xff & indate[(0)]) << 24 | // (0xff & indate[1]) << 16 | // (0xff & indate[2]) << 8 | // (0xFF & indate[3])); // state.dateTime.value = DateTool().dateToYMDHNString("$indateValue"); // break; // case 0x06: // //需要鉴权 // var privateKey = await Storage.getStringList(saveBluePrivateKey); // List getPrivateKeyList = changeStringListToIntList(privateKey!); // IoSenderManage.senderGetStarLockStatuInfo( // lockID: BlueManage().connectDeviceName, // userID: await Storage.getUid(), // isBeforeAddUser: false, // privateKey: getPrivateKeyList, // ); // break; // default: // //失败 // break; // } // } // 校时数据解析 Future _replyTiming(Reply reply) async { int status = reply.data[2]; switch(status){ case 0x00: //成功 String dataEime = DateTool().dateToYMDHNString("${getUTCNetTime()}"); state.dateTime.value = dataEime; state.sureBtnState.value = 0; cancelBlueConnetctToastTimer(); dismissEasyLoading(); showToast("锁时间更新成功".tr); break; case 0x06: //无权限 break; default: break; } } // 校验时间 Future sendTiming() async { if(state.sureBtnState.value == 1){ return; } state.sureBtnState.value = 1; showEasyLoading(); showBlueConnetctToastTimer(action: (){ dismissEasyLoading(); state.sureBtnState.value = 0; }); BlueManage().blueSendData(BlueManage().connectDeviceName, (BluetoothConnectionState connectionState) async { if (connectionState == BluetoothConnectionState.connected) { var privateKey = await Storage.getStringList(saveBluePrivateKey); List getPrivateKeyList = changeStringListToIntList(privateKey!); var token = await Storage.getStringList(saveBlueToken); List getTokenList = changeStringListToIntList(token!); var signKey = await Storage.getStringList(saveBlueSignKey); List getSignKeyList = changeStringListToIntList(signKey!); IoSenderManage.senderTimingCommand( lockID:BlueManage().connectDeviceName, userID:await Storage.getUid(), // nowTime:DateTime.now().millisecondsSinceEpoch~/1000, nowTime: getUTCNetTime(), token:getTokenList, needAuthor:1, signKey:getSignKeyList, privateKey:getPrivateKeyList, ); } else if (connectionState == BluetoothConnectionState.disconnected) { dismissEasyLoading(); cancelBlueConnetctToastTimer(); state.sureBtnState.value = 0; if(state.ifCurrentScreen.value == true){ showBlueConnetctToast(); } } }); } // 获取锁状态 更新电量 // Future getStarLockStatus() async { // showEasyLoading(); // showBlueConnetctToastTimer(action: () { // dismissEasyLoading(); // }); // BlueManage().bludSendData(BlueManage().connectDeviceName, // (BluetoothConnectionState deviceConnectionState) async { // if (deviceConnectionState == BluetoothConnectionState.connected) { // dismissEasyLoading(); // var privateKey = await Storage.getStringList(saveBluePrivateKey); // List getPrivateKeyList = changeStringListToIntList(privateKey!); // IoSenderManage.senderGetStarLockStatuInfo( // lockID: BlueManage().connectDeviceName, // userID: await Storage.getUid(), // isBeforeAddUser: false, // privateKey: getPrivateKeyList, // ); // } else if (deviceConnectionState == BluetoothConnectionState.disconnected) { // dismissEasyLoading(); // cancelBlueConnetctToastTimer(); // if (state.ifCurrentScreen.value == true) { // showBlueConnetctToast(); // } // } // }); // } // 从网关获取时间 void getLockTimeFromGateway() async{ var entity = await ApiRepository.to.getLockTimeFromGateway( lockId: state.lockSetInfoData.value.lockId.toString(), ); if(entity.errorCode!.codeIsSuccessful){ } } // 从服务器获取锁的时间 开锁时传入 void getServerDatetime() async{ var 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 getUTCNetTime(){ return DateTime.now().millisecondsSinceEpoch ~/ 1000 + state.differentialTime; } @override void onReady() { // TODO: implement onReady super.onReady(); _initReplySubscription(); // getStarLockStatus(); // getLockTimeFromGateway(); getServerDatetime(); } @override void onInit() { // TODO: implement onInit super.onInit(); // _getLockStatus(); } @override void onClose() { // TODO: implement onClose super.onClose(); _replySubscription.cancel(); } }