472 lines
16 KiB
Dart
472 lines
16 KiB
Dart
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
import '../../../appRouters.dart';
|
|
import '../../../blue/blue_manage.dart';
|
|
import '../../../blue/io_protocol/io_addUser.dart';
|
|
import '../../../blue/io_protocol/io_getLockStatu.dart';
|
|
import '../../../blue/io_protocol/io_getStarLockStatusInfo.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/eventBusEventManage.dart';
|
|
import '../../../tools/storage.dart';
|
|
import 'saveLock_state.dart';
|
|
|
|
class SaveLockLogic extends BaseGetXController {
|
|
|
|
final SaveLockState state = SaveLockState();
|
|
int userNo = 0;
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if(reply is AddUserReply) {
|
|
_replyAddUserKey(reply);
|
|
}
|
|
|
|
// 获取锁状态
|
|
if(reply is GetLockStatuReply) {
|
|
_replyGetLockStatus(reply);
|
|
}
|
|
|
|
// 获取锁状态信息
|
|
if(reply is GetStarLockStatuInfoReply) {
|
|
_replyGetStarLockStatusInfo(reply);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
Future<void> _replyAddUserKey(Reply reply) async {
|
|
var lockId = reply.data.sublist(2, 42);
|
|
print("lockId:$lockId");
|
|
|
|
var token = reply.data.sublist(42, 46);
|
|
List<String> strTokenList = changeIntListToStringList(token);
|
|
Storage.setStringList(saveBlueToken, strTokenList);
|
|
print("token:$token");
|
|
|
|
int status = reply.data[46];
|
|
print("status:$status");
|
|
|
|
userNo = reply.data[46];
|
|
print("status:$status");
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("添加用户数据解析成功");
|
|
bindBlueAdmin();
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("需要鉴权");
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
|
|
IoSenderManage.senderAddUser(
|
|
lockID:BlueManage().connectDeviceName,
|
|
authUserID:await Storage.getUid(),
|
|
keyID:"1",
|
|
userID:await Storage.getUid(),
|
|
openMode:1,
|
|
keyType:1,
|
|
startDate:0x11223344,
|
|
expireDate:0x11223344,
|
|
role:255,
|
|
password:"123456",
|
|
needAuthor:1,
|
|
publicKey:publicKeyDataList,
|
|
privateKey:getPrivateKeyList,
|
|
token: token
|
|
);
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("添加用户权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("领锁失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 获取锁状态数据解析
|
|
Future<void> _replyGetLockStatus(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
var softVersion = reply.data.sublist(3, 7);
|
|
print("softVersion:$softVersion");
|
|
|
|
var power = reply.data[7];
|
|
print("power:$power");
|
|
|
|
// APP 用户数量
|
|
var appUserCount = reply.data.sublist(50, 53);
|
|
print("appUserCount:$appUserCount");
|
|
|
|
// 黑名单用户数量
|
|
var blacklistCount = reply.data[53];
|
|
print("blacklistCount:$blacklistCount");
|
|
|
|
// 蓝牙钥匙数量
|
|
var bleKeyCount = reply.data[54];
|
|
print("bleKeyCount:$bleKeyCount");
|
|
|
|
// 剩余可添加用户数量
|
|
var remainCount = reply.data.sublist(54, 56);
|
|
print("remainCount:$remainCount");
|
|
|
|
// 未上传开锁记录数量
|
|
var notUploadCount = reply.data.sublist(56, 58);
|
|
print("notUploadCount:$notUploadCount");
|
|
|
|
// 已设置开门密码数量
|
|
var pwdCount = reply.data[58];
|
|
print("pwdCount:$pwdCount");
|
|
|
|
// 已设置开门指纹数量
|
|
var fingerprintCount = reply.data[59];
|
|
print("fingerprintCount:$fingerprintCount");
|
|
|
|
// 锁当前时间
|
|
var lockTime = reply.data.sublist(60, 64);
|
|
print("lockTime:$lockTime");
|
|
|
|
// 硬件版本信息,为固件升级提供判断依据
|
|
var hardVersion = reply.data.sublist(64, 68);
|
|
print("hardVersion:$hardVersion");
|
|
|
|
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> _replyGetStarLockStatusInfo(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
// 厂商名称
|
|
var vendor = reply.data.sublist(3, 23);
|
|
var vendorStr = utf8String(vendor);
|
|
print("softVersion:$vendor vendorStr:$vendorStr");
|
|
|
|
// 锁设备类型
|
|
var product = reply.data[23];
|
|
print("product:$product");
|
|
|
|
// 产品名称
|
|
var model = reply.data.sublist(24, 44);
|
|
var modelStr = utf8String(model);
|
|
print("model:$model modelStr:$modelStr");
|
|
|
|
// 软件版本
|
|
var fwVersion = reply.data.sublist(44, 64);
|
|
var fwVersionStr = utf8String(fwVersion);
|
|
print("fwVersion:$fwVersion fwVersionStr:$fwVersionStr");
|
|
|
|
// 硬件版本
|
|
var hwVersion = reply.data.sublist(64, 84);
|
|
print("hwVersion:$hwVersion");
|
|
|
|
// 厂商序列号
|
|
var serialNum0 = reply.data.sublist(84, 100);
|
|
print("serialNum0:$serialNum0");
|
|
|
|
// 成品商序列号
|
|
var serialNum1 = reply.data.sublist(100, 116);
|
|
print("serialNum1:$serialNum1");
|
|
|
|
// 蓝牙名称
|
|
var btDeviceName = reply.data.sublist(116, 132);
|
|
var btDeviceNameStr = utf8String(btDeviceName);
|
|
print("btDeviceName:$btDeviceName btDeviceNameStr:$btDeviceNameStr");
|
|
|
|
// 电池剩余电量
|
|
var battRemCap = reply.data[132];
|
|
print("battRemCap:$battRemCap");
|
|
|
|
// 重置次数
|
|
var restoreCounter = reply.data.sublist(133, 135);
|
|
print("restoreCounter:$restoreCounter");
|
|
|
|
// 重置时间
|
|
var restoreDate = reply.data.sublist(135, 139);
|
|
int restoreDateValue = ((0xff & restoreDate[(0)]) << 24 | (0xff & restoreDate[1]) << 16 | (0xff & restoreDate[2]) << 8 | (0xFF & restoreDate[3]));
|
|
String restoreDateStr = DateTool().dateToYMDHNSString(restoreDateValue.toString());
|
|
print("restoreDate:$restoreDate restoreDateStr:$restoreDateStr");
|
|
|
|
// 主控芯片型号
|
|
var icPartNo = reply.data.sublist(139, 149);
|
|
var icPartNoStr = utf8String(icPartNo);
|
|
print("icPartNo:$icPartNo icPartNoStr:$icPartNoStr");
|
|
|
|
// 有效时间
|
|
var indate = reply.data.sublist(149, 153);
|
|
int indateValue = ((0xff & restoreDate[(0)]) << 24 | (0xff & restoreDate[1]) << 16 | (0xff & restoreDate[2]) << 8 | (0xFF & restoreDate[3]));
|
|
String indateStr = DateTool().dateToYMDHNSString("$indateValue");
|
|
print("indate:$indate indateStr:$indateStr");
|
|
|
|
// var index = 153;
|
|
// // 锁特征值字符串长度
|
|
// var featureValueLength = reply.data[153];
|
|
// // 锁特征值说明(本机能支持的功能)
|
|
// // 获取到锁给的字符数组
|
|
// var featureValue = reply.data.sublist(index+1, index + featureValueLength+1);
|
|
// List allFeatureValueTwoList = charListChangeIntList(featureValue);
|
|
// // print("featureValueLength:$featureValueLength featureValue:$featureValue \n allFeatureValueTwoList:$allFeatureValueTwoList");
|
|
// index = index + featureValueLength + 1;
|
|
|
|
// // 使能特征值字符串长度
|
|
// var featureEnValLength = reply.data[index];
|
|
// // 使能锁特征值说明(本机启用的功能)
|
|
// var featureEnVal = reply.data.sublist(index+1, index + featureEnValLength+1);
|
|
// List allFeatureEnValTwoList = charListChangeIntList(featureEnVal);
|
|
// // print("featureEnValLength:$featureEnValLength featureEnVal:$featureEnVal \n allFeatureEnValTwoList:$allFeatureEnValTwoList");
|
|
// index = index + featureEnValLength + 1;
|
|
|
|
// // 支持的带参数特征值的总条目数
|
|
// var featureParaTotal = reply.data[index];
|
|
|
|
break;
|
|
case 0x06:
|
|
//无权限
|
|
print("${reply.commandType}需要鉴权");
|
|
|
|
break;
|
|
case 0x07:
|
|
//无权限
|
|
print("${reply.commandType}用户无权限");
|
|
|
|
break;
|
|
case 0x09:
|
|
// 权限校验错误
|
|
print("${reply.commandType}权限校验错误");
|
|
|
|
break;
|
|
default:
|
|
//失败
|
|
print("${reply.commandType}失败");
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
List charListChangeIntList(List<int> featureValue){
|
|
// 字符数组转化为16进制字符串
|
|
String featureValueStr = asciiString(featureValue);
|
|
// 16进制字符串转化为2进制的字符串 获取的是逆序的需要倒序 前面有0会消失 需要自动补全 暂时定位57个功能 要补全60
|
|
String featureValueTwoStr = int.parse(featureValueStr,radix: 16).toRadixString(2);
|
|
List<int> featureValueTwoList = [];
|
|
for(int i = 0;i<featureValueTwoStr.length;i++){
|
|
featureValueTwoList.add(int.parse(featureValueTwoStr[i]));
|
|
}
|
|
featureValueTwoList = featureValueTwoList.reversed.toList();
|
|
List allFeatureValueTwoList = getFixedLengthList(featureValueTwoList, 60 - featureValueTwoList.length);
|
|
return allFeatureValueTwoList;
|
|
}
|
|
|
|
// 保存事件,先调用蓝牙,蓝牙调用成功后再调用后台接口
|
|
saveLockAction(){
|
|
addUserConnectBlue();
|
|
}
|
|
|
|
// 添加用户
|
|
Future<void> addUserConnectBlue() 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 publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
|
|
var token = await Storage.getStringList(saveBlueToken);
|
|
List<int> getTokenList = [0,0,0,0];
|
|
if(token != null){
|
|
getTokenList = changeStringListToIntList(token);
|
|
}
|
|
|
|
IoSenderManage.senderAddUser(
|
|
lockID: BlueManage().connectDeviceName,
|
|
authUserID:await Storage.getUid(),
|
|
keyID:"1",
|
|
userID:await Storage.getUid(),
|
|
openMode:1,
|
|
keyType:1,
|
|
startDate:DateTime.now().millisecondsSinceEpoch,
|
|
expireDate:0x11223344,
|
|
role:255,
|
|
password:"123456",
|
|
needAuthor:1,
|
|
publicKey:publicKeyDataList,
|
|
privateKey:getPrivateKeyList,
|
|
token: getTokenList
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
void bindBlueAdmin() async{
|
|
var lockDataMap = {};
|
|
lockDataMap['lockName'] = BlueManage().connectDeviceName;
|
|
lockDataMap['lockMac'] = getRandomNumber();
|
|
// lockDataMap['lockMac'] = BlueManage().connectDeviceMacAddress;
|
|
|
|
var positionMap = {};
|
|
positionMap['longitude'] = state.addressInfo["longitude"];
|
|
positionMap['latitude'] = state.addressInfo["latitude"];
|
|
positionMap['country'] = state.addressInfo["country"];
|
|
positionMap['province'] = state.addressInfo["province"];
|
|
positionMap['city'] = state.addressInfo["city"];
|
|
positionMap['district'] = state.addressInfo["district"];
|
|
positionMap['township'] = state.addressInfo["street"];
|
|
positionMap['address'] = state.addressInfo["address"];
|
|
|
|
var bluetooth = {};
|
|
bluetooth['bluetoothDeviceId'] = BlueManage().connectDeviceMacAddress;
|
|
bluetooth['bluetoothDeviceName'] = BlueManage().connectDeviceName;
|
|
|
|
var publicKey = await Storage.getStringList(saveBluePublicKey);
|
|
List<int> publicKeyDataList = changeStringListToIntList(publicKey!);
|
|
bluetooth['publicKey'] = publicKeyDataList;
|
|
|
|
var privateKey = await Storage.getStringList(saveBluePrivateKey);
|
|
List<int> getPrivateKeyList = changeStringListToIntList(privateKey!);
|
|
bluetooth['privateKey'] = getPrivateKeyList;
|
|
|
|
var signKey = await Storage.getStringList(saveBlueSignKey);
|
|
List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
|
bluetooth['signKey'] = signKeyDataList;
|
|
|
|
// print("addUser:publicKeyDataList$publicKeyDataList getPrivateKeyList:$getPrivateKeyList signKeyDataList:$signKeyDataList");
|
|
|
|
var entity = await ApiRepository.to.bindingBlueAdmin(
|
|
bindingDate:DateTime.now().millisecondsSinceEpoch.toString(),
|
|
hotelMode:"2",
|
|
lockAlias:state.aliName.value,
|
|
lockData:lockDataMap,
|
|
nbInitSuccess:"0",
|
|
position:positionMap,
|
|
bluetooth:bluetooth,
|
|
deviceNo:"123456",
|
|
// lockUserNo:userNo.toString(),
|
|
lockUserNo:"1234",
|
|
pwdTimestamp:state.pwdTimestamp.value.toString()
|
|
);
|
|
if(entity.errorCode!.codeIsSuccessful){
|
|
eventBus.fire(RefreshLockListInfoDataEvent());
|
|
Get.offAllNamed(Routers.starLockMain);
|
|
}
|
|
}
|
|
|
|
String getRandomNumber(){
|
|
String str = "";
|
|
for(int i = 0; i<6; i++){
|
|
var a = Random().nextInt(99);
|
|
if(a<10){
|
|
a = a*10;
|
|
}
|
|
str = "$str$a:";
|
|
}
|
|
String result = str.substring(0, str.length - 1);
|
|
print("result:$result");
|
|
return result;
|
|
}
|
|
|
|
// 获取锁状态
|
|
Future<void> _getLockStatus() async {
|
|
print("connectDeviceMacAddress:${BlueManage().connectDeviceMacAddress} connectDeviceName:${BlueManage().connectDeviceName}");
|
|
// 进来之后首先连接
|
|
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,
|
|
// );
|
|
IoSenderManage.senderGetStarLockStatuInfo(
|
|
lockID:BlueManage().connectDeviceName,
|
|
userID:await Storage.getUid(),
|
|
privateKey:getPrivateKeyList,
|
|
);
|
|
}
|
|
}, isShowLoading: false);
|
|
}
|
|
|
|
@override
|
|
void onReady() {
|
|
// TODO: implement onReady
|
|
super.onReady();
|
|
print("onReady()");
|
|
|
|
_initReplySubscription();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
_getLockStatus();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
|
|
} |