106 lines
3.5 KiB
Dart
106 lines
3.5 KiB
Dart
|
||
|
||
import 'io_protocol/io_addUser.dart';
|
||
import 'io_protocol/io_getPublicKey.dart';
|
||
import 'io_protocol/io_openDoor.dart';
|
||
import 'io_protocol/io_reply.dart';
|
||
import 'io_protocol/io_type.dart';
|
||
import 'io_tool/io_tool.dart';
|
||
import 'io_tool/manager_event_bus.dart';
|
||
|
||
class CommandReciverManager {
|
||
|
||
static void appDataReceive(List<int> data, String lockId) async {
|
||
///解析数据
|
||
if(data.isEmpty){
|
||
return;
|
||
}
|
||
int data_size = data.length;
|
||
// 当小于包头加起来13个字节
|
||
if(data_size < 13){
|
||
return;
|
||
}
|
||
print("appDataReceiveData:$data");
|
||
if((data[0] == 0xEF)&&(data[1] == 0x01)&&(data[2] == 0xEE)&&(data[3] == 0x02)&&(data[4] == 0x11)){
|
||
var tmpType = (data[7] & 0x0f);// 包标识
|
||
print("temType:$tmpType");
|
||
var dataLen = data[8] * 256 + data[9];// 高16位用来指示后面数据块内容的长度
|
||
var oriLen = data[10] * 256 + data[11];// 低16位用来指示数据加密前的原长度
|
||
List<int> dataList = [];
|
||
List<int> oriDataList = [];
|
||
switch(tmpType){
|
||
case 0: //不加密
|
||
for (var i = 0; i < oriLen ; i++) {
|
||
oriDataList.add(data[12 + i]);
|
||
}
|
||
parseData(oriDataList).then((value) {
|
||
EventBusManager().eventBusFir(value);
|
||
});
|
||
break;
|
||
case 1: //AES128
|
||
break;
|
||
case 2: //SM4(事先约定密钥)
|
||
// for (var i = 0; i < dataLen ; i++) {
|
||
// dataList.add(data[12 + i]);
|
||
// }
|
||
//console.log("currCommStru.pairedName = ", currCommStru.pairedName);
|
||
// var d_cbc = cbc.decrypt_ecb(dataView, currCommStru.pairLockID, true, "nobase64");
|
||
// console.log("d_cbc = ", d_cbc);
|
||
// console.log("oriLen = ", oriLen);
|
||
// for (var i = 0; i < oriLen ; i++) {
|
||
// oriDataView[i] = d_cbc[i];
|
||
// }
|
||
// console.log("oriDataView = ", oriDataView);
|
||
break;
|
||
case 3: //SM4(设备指定密钥)
|
||
// for (var i = 0; i < dataLen ; i++) {
|
||
// dataView[i] = uint8Recv[12 + i];
|
||
// }
|
||
// console.log("dataView.length = ", dataView.length);
|
||
// console.log("currCommStru.pairCommKey = ", currCommStru.pairCommKey);
|
||
// var commKey1 = base64js.toByteArray(currCommStru.pairCommKey);
|
||
// console.log("commKey1 = ", commKey1);
|
||
// var d_cbc = cbc.decrypt_ecb(dataView, commKey1, false, "nobase64");
|
||
// console.log("d_cbc = ", d_cbc);
|
||
// console.log("oriLen = ", oriLen);
|
||
// for (var i = 0; i < oriLen ; i++) {
|
||
// oriDataView[i] = d_cbc[i];
|
||
// }
|
||
// console.log("oriDataView = ", oriDataView);
|
||
break;
|
||
|
||
}
|
||
}
|
||
|
||
// print('✅ 执行结束 _buffer:${_buffer.length}');
|
||
}
|
||
|
||
static Future<Reply?> parseData(List<int> data) async {
|
||
if(data.isNotEmpty){
|
||
var cmd = data[0] * 256 + data[1];
|
||
CommandType commandType = ExtensionCommandType.getCommandType(cmd);
|
||
data.removeRange(0, 2);
|
||
print("111111data:$data");
|
||
var reply;
|
||
switch(commandType) {
|
||
case CommandType.getLockPublicKey:
|
||
{
|
||
reply = GetPublicKeyReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.addUser:
|
||
{
|
||
reply = AddUserReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.openDoor:
|
||
{
|
||
reply = OpenDoorReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
}
|
||
return reply;
|
||
}
|
||
|
||
}
|
||
} |