113 lines
3.8 KiB
Dart
113 lines
3.8 KiB
Dart
|
||
|
||
import 'io_protocol/io_addUser.dart';
|
||
import 'io_protocol/io_getPrivateKey.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';
|
||
import 'sm4Encipher/sm4.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位用来指示数据加密前的原长度
|
||
print("dataLen:$dataLen oriLen:$oriLen");
|
||
List<int> dataList = [];
|
||
List<int> oriDataList = [];
|
||
switch(tmpType){
|
||
case 0: //不加密
|
||
// for (var i = 0; i < oriLen ; i++) {
|
||
// oriDataList.add(data[12 + i]);
|
||
// }
|
||
oriDataList = data.sublist(12, 12 + dataLen);
|
||
print("不加密 oriDataList:$oriDataList");
|
||
break;
|
||
case 1:
|
||
//AES128
|
||
break;
|
||
case 2:
|
||
// SM4(事先约定密钥)
|
||
// 获取的加密数组
|
||
var getDataList = data.sublist(12, 12 + dataLen);
|
||
String key = SM4.createHexKey(key: 'TMH_c3570480da8d');
|
||
// 解密
|
||
oriDataList = SM4.decrypt(getDataList, key: key, mode: SM4CryptoMode.ECB);
|
||
oriDataList = oriDataList.sublist(0, oriLen);
|
||
print("SM4 oriDataList:$oriDataList");
|
||
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;
|
||
|
||
}
|
||
parseData(oriDataList).then((value) {
|
||
EventBusManager().eventBusFir(value);
|
||
});
|
||
}
|
||
// 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 cmd:$cmd commandType:$commandType data:$data");
|
||
var reply;
|
||
switch(commandType) {
|
||
case CommandType.getLockPublicKey:
|
||
{
|
||
reply = GetPublicKeyReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.getLockPrivateKey:
|
||
{
|
||
reply = GetPrivateKeyReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.addUser:
|
||
{
|
||
reply = AddUserReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
case CommandType.openDoor:
|
||
{
|
||
reply = OpenDoorReply.parseData(commandType, data);
|
||
}
|
||
break;
|
||
}
|
||
return reply;
|
||
}
|
||
}
|
||
} |