404 lines
15 KiB
Dart
404 lines
15 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
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_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/eventBusEventManage.dart';
|
|
import '../../../tools/storage.dart';
|
|
import 'saveLock_state.dart';
|
|
|
|
class SaveLockLogic extends BaseGetXController {
|
|
final SaveLockState state = SaveLockState();
|
|
|
|
// 获取解析后的数据
|
|
late StreamSubscription<Reply> _replySubscription;
|
|
void _initReplySubscription() {
|
|
_replySubscription = EventBusManager().eventBus!.on<Reply>().listen((reply) {
|
|
if(reply is AddUserReply) {
|
|
_replyAddUserKey(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 reply.data:${reply.data}");
|
|
|
|
print("status:$status");
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("添加用户数据解析成功");
|
|
state.lockUserNo = reply.data[47];
|
|
|
|
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> _replyGetStarLockStatusInfo(Reply reply) async {
|
|
int status = reply.data[2];
|
|
switch(status){
|
|
case 0x00:
|
|
//成功
|
|
print("${reply.commandType}数据解析成功");
|
|
|
|
// 厂商名称
|
|
var vendor = reply.data.sublist(3, 23);
|
|
print("vendor:$vendor reply.data:${reply.data}");
|
|
var vendorStr = utf8String(vendor);
|
|
state.lockInfo["vendor"] = vendorStr;
|
|
// print("vendor:$vendor vendorStr:$vendorStr vendorStr.length${vendorStr.length}");
|
|
|
|
// 锁设备类型
|
|
var product = reply.data[23];
|
|
state.lockInfo["product"] = product;
|
|
// print("product:$product");
|
|
|
|
// 产品名称
|
|
var model = reply.data.sublist(24, 44);
|
|
var modelStr = utf8String(model);
|
|
state.lockInfo["model"] = modelStr;
|
|
// print("model:$model modelStr:$modelStr modelStr:${modelStr.length}");
|
|
|
|
// 软件版本
|
|
var fwVersion = reply.data.sublist(44, 64);
|
|
var fwVersionStr = utf8String(fwVersion);
|
|
state.lockInfo["fwVersion"] = fwVersionStr;
|
|
// print("fwVersion:$fwVersion fwVersionStr:$fwVersionStr fwVersionStr:${fwVersionStr.length}");
|
|
|
|
// 硬件版本
|
|
var hwVersion = reply.data.sublist(64, 84);
|
|
var hwVersionStr = utf8String(hwVersion);
|
|
state.lockInfo["hwVersion"] = hwVersionStr;
|
|
// print("hwVersion:$hwVersion hwVersionStr:${hwVersionStr.length}");
|
|
|
|
// 厂商序列号
|
|
var serialNum0 = reply.data.sublist(84, 100);
|
|
var serialNum0Str = utf8String(serialNum0);
|
|
// state.lockInfo["serialNum0"] = serialNum0Str;
|
|
state.lockInfo["serialNum0"] = "${DateTime.now().millisecondsSinceEpoch~/10}";
|
|
// print("serialNum0Str:$serialNum0Str serialNum0Str:${serialNum0Str.length}");
|
|
|
|
// 成品商序列号
|
|
var serialNum1 = reply.data.sublist(100, 116);
|
|
var serialNum1Str = utf8String(serialNum1);
|
|
state.lockInfo["serialNum1"] = serialNum1Str;
|
|
// print("serialNum1Str:$serialNum1Str serialNum1Str:${serialNum1Str.length}");
|
|
|
|
// 蓝牙名称
|
|
var btDeviceName = reply.data.sublist(116, 132);
|
|
var btDeviceNameStr = utf8String(btDeviceName);
|
|
state.lockInfo["btDeviceName"] = btDeviceNameStr;
|
|
// print("btDeviceName:$btDeviceName btDeviceNameStr:$btDeviceNameStr btDeviceNameStr:${btDeviceNameStr.length}");
|
|
|
|
// 电池剩余电量
|
|
var battRemCap = reply.data[132];
|
|
state.lockInfo["electricQuantity"] = battRemCap;
|
|
// print("battRemCap:$battRemCap");
|
|
|
|
// 重置次数
|
|
var restoreCounter = reply.data.sublist(133, 135);
|
|
state.lockInfo["restoreCount"] = restoreCounter[0] * 256 + restoreCounter[1];
|
|
// 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());
|
|
state.lockInfo["restoreDate"] = restoreDateValue*1000;
|
|
// print("restoreDate:$restoreDate restoreDateValue:$restoreDateValue");
|
|
|
|
// 主控芯片型号
|
|
var icPartNo = reply.data.sublist(139, 149);
|
|
var icPartNoStr = utf8String(icPartNo);
|
|
state.lockInfo["icPartNo"] = icPartNoStr;
|
|
// print("icPartNo:$icPartNo icPartNoStr:$icPartNoStr");
|
|
|
|
// 有效时间
|
|
var indate = reply.data.sublist(149, 153);
|
|
int indateValue = ((0xff & indate[(0)]) << 24 | (0xff & indate[1]) << 16 | (0xff & indate[2]) << 8 | (0xFF & indate[3]));
|
|
// String indateStr = DateTool().dateToYMDHNSString("$indateValue");
|
|
state.lockInfo["indate"] = indateValue*1000;
|
|
// print("indate:$indate indateValue:$indateValue");
|
|
|
|
// mac地址
|
|
var macAddress = reply.data.sublist(153, 173);
|
|
var macAddressStr = utf8String(macAddress);
|
|
state.lockInfo["mac"] = macAddressStr;
|
|
print("macAddress:$macAddress macAddressStr:$macAddressStr");
|
|
|
|
var index = 173;
|
|
// 锁特征值字符串长度
|
|
var featureValueLength = reply.data[173];
|
|
// 锁特征值说明(本机能支持的功能)
|
|
// 获取到锁给的字符数组
|
|
var featureValue = reply.data.sublist(index+1, index + featureValueLength+1);
|
|
String featureValueStr = asciiString(featureValue);
|
|
state.featureValue = featureValueStr;
|
|
// List allFeatureValueTwoList = charListChangeIntList(featureValue);
|
|
// print("featureValueLength:$featureValueLength featureValue:$featureValue \n featureValueStr:$featureValueStr");
|
|
index = index + featureValueLength + 1;
|
|
|
|
// 使能特征值字符串长度
|
|
var featureEnValLength = reply.data[index];
|
|
// 使能锁特征值说明(本机启用的功能)
|
|
var featureEnVal = reply.data.sublist(index+1, index + featureEnValLength+1);
|
|
String featureEnValStr = asciiString(featureValue);
|
|
state.featureSettingValue = featureEnValStr;
|
|
// List allFeatureEnValTwoList = charListChangeIntList(featureEnVal);
|
|
// print("featureEnValLength:$featureEnValLength featureEnVal:$featureEnVal \n featureEnValStr:$featureEnValStr");
|
|
index = index + featureEnValLength + 1;
|
|
|
|
// 支持的带参数特征值的总条目数
|
|
// var featureParaTotal = reply.data[index];
|
|
|
|
var featureParaTotalList = reply.data.sublist(index);
|
|
state.featureSettingParams = featureParaTotalList;
|
|
// print("featureParaTotalList:$featureParaTotalList");
|
|
|
|
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);
|
|
// print("featureValueStr:$featureValueStr featureValueTwoStr:$featureValueTwoStr featureValueTwoList:$featureValueTwoList allFeatureValueTwoList:$allFeatureValueTwoList");
|
|
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{
|
|
print("state.lockInfo:${state.lockInfo}");
|
|
|
|
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(
|
|
lockAlias:state.aliName.value,
|
|
position:positionMap,
|
|
bluetooth:bluetooth,
|
|
lockInfo: state.lockInfo,
|
|
lockUserNo:state.lockUserNo.toString(),
|
|
pwdTimestamp:state.pwdTimestamp.value.toString(),
|
|
featureValue:state.featureValue,
|
|
featureSettingValue:state.featureSettingValue,
|
|
featureSettingParams:state.featureSettingParams,
|
|
);
|
|
if(entity.errorCode!.codeIsSuccessful){
|
|
eventBus.fire(RefreshLockListInfoDataEvent());
|
|
Get.offAllNamed(Routers.starLockMain);
|
|
}
|
|
}
|
|
|
|
// 获取锁状态
|
|
Future<void> _getStarLockStatus() 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();
|
|
|
|
_getStarLockStatus();
|
|
}
|
|
|
|
@override
|
|
void onInit() {
|
|
// TODO: implement onInit
|
|
super.onInit();
|
|
print("onInit()");
|
|
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
// TODO: implement onClose
|
|
super.onClose();
|
|
_replySubscription.cancel();
|
|
}
|
|
|
|
} |