221 lines
6.3 KiB
Dart
221 lines
6.3 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
import 'package:date_format/date_format.dart';
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
|
|
import '../../../../blue/blue_manage.dart';
|
|
import '../../../../blue/io_protocol/io_getLockStatu.dart';
|
|
import '../../../../blue/io_protocol/io_timing.dart';
|
|
import '../../../../blue/io_reply.dart';
|
|
import '../../../../blue/io_tool/io_manager.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 '../../../../tools/toast.dart';
|
|
import 'lockTime_state.dart';
|
|
|
|
class LockTimeLogic extends BaseGetXController{
|
|
final LockTimeState state = LockTimeState();
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if(reply is TimingReply) {
|
|
_replyTiming(reply);
|
|
}
|
|
|
|
// 获取锁状态
|
|
if(reply is GetLockStatuReply) {
|
|
_replyGetLockStatus(reply);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 获取锁状态数据解析
|
|
Future<void> _replyGetLockStatus(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
|
|
// 锁当前时间
|
|
var lockTime = reply.data.sublist(60, 64);
|
|
|
|
int value = (
|
|
(0xff & lockTime[(0)]) << 24 |
|
|
(0xff & lockTime[1]) << 16 |
|
|
(0xff & lockTime[2]) << 8 |
|
|
(0xFF & lockTime[3]));
|
|
|
|
String dataEime = DateTool().dateToYMDHNSString("$value");
|
|
state.dateTime.value = dataEime;
|
|
|
|
// String dataEime = DateTool().dateToYMDHNSString("${value}");
|
|
// state.dateTime.value = dataEime;
|
|
print("lockTime:$lockTime value:$value dataEime:$dataEime");
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("${reply.commandType}需要鉴权");
|
|
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("${reply.commandType}用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("${reply.commandType}权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("${reply.commandType}失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 校时数据解析
|
|
Future<void> _replyTiming(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
// var lockTime = reply.data.sublist(4, 8);
|
|
//
|
|
// int value = (
|
|
// (0xff & lockTime[(0)]) << 24 |
|
|
// (0xff & lockTime[1]) << 16 |
|
|
// (0xff & lockTime[2]) << 8 |
|
|
// (0xFF & lockTime[3]));
|
|
//
|
|
// String dataEime = DateTool().dateToYMDHNSString("$value");
|
|
// state.dateTime.value = dataEime;
|
|
|
|
// _getLockStatus();
|
|
Toast.show(msg:"锁时间更新成功");
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("${reply.commandType}需要鉴权");
|
|
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("${reply.commandType}用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("${reply.commandType}权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("${reply.commandType}失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 获取锁状态
|
|
Future<void> _getLockStatus() async {
|
|
// 进来之后首先连接
|
|
BlueManage().judgeReconnect(BlueManage().connectDeviceMacAddress, BlueManage().connectDeviceName, (DeviceConnectionState state) async {
|
|
if (state == DeviceConnectionState.connected) {
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
IoSenderManage.senderGetLockStatu(
|
|
lockID:BlueManage().connectDeviceName,
|
|
userID:await Storage.getUid(),
|
|
privateKey:getPrivateKeyList,
|
|
);
|
|
}
|
|
}, isShowLoading: false);
|
|
}
|
|
|
|
// 校验时间
|
|
Future<void> sendTiming() async {
|
|
BlueManage().judgeReconnect(BlueManage().connectDeviceMacAddress, BlueManage().connectDeviceName, (DeviceConnectionState state) async {
|
|
if (state == DeviceConnectionState.connected) {
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> getTokenList = changeStringListToIntList(token!);
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> getSignKeyList = changeStringListToIntList(signKey!);
|
|
|
|
IoSenderManage.senderTimingCommand(
|
|
lockID:BlueManage().connectDeviceName,
|
|
userID:await Storage.getUid(),
|
|
nowTime:DateTime.now().millisecondsSinceEpoch~/1000,
|
|
token:getTokenList,
|
|
needAuthor:1,
|
|
signKey:getSignKeyList,
|
|
privateKey:getPrivateKeyList,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 从网关获取时间
|
|
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(
|
|
lockId: state.lockSetInfoData.value.lockId.toString(),
|
|
);
|
|
if(entity.errorCode!.codeIsSuccessful){
|
|
String dataEime = DateTool().dateToYMDHNSString("${entity.data!.date!}");
|
|
state.dateTime.value = dataEime;
|
|
sendTiming();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_initReplySubscription();
|
|
// getLockTimeFromGateway();
|
|
getServerDatetime();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
// _getLockStatus();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
} |