var str = "DIRM010121090003000000011223344049D4ABA22202210281634530ADB3B922202210281634531ADB3B922202210281634532ADB3B9222022102816345337E";//考勤记录
var command_runboot = {
"otaCmdType": "1",
"otaPackageNo": 0,
"otaTotalPackage": 152,
};
var command_cleandata = {
"otaCmdType": "2",
"otaCmdParam": 0x03,
"otaPackageNo": 0,
"otaTotalPackage": 152,
};
var command_upgradedata = {
"otaCmdType": "4",
"otaCmdParam": 0x05,
"otaPackageNo": 5,
"otaPackageData": [1, 2, 3, 4, 5, 6, 7, 8, 9],
"otaTotalPackage": 152,
};
var result = otaEncode({}, command_upgradedata, {}, {});
console.log(result);
function otaEncode(session, command, metadata, deviceConfig) {
function getCheckSum(bs, start, end) {
var checkSum = 0;
for (i = start; i < end; i++) {
checkSum = checkSum + bs[i];
}
var result = [];
result[0] = checkSum & 0xff;// 校验和取低两字节,低字节在前
result[1] = (checkSum >> 8) & 0xff; // 校验和取低两字节,低字节在前
return result;
};
var NoActionOTACmdType = "0";
var PreOTACmdType = "1";
var StartOTACmdType = "2";
var SendPackageDataOTACmdType = "3";
var UpgradingOTACmdType = "4";
var UpgradeCompleteOTACmdType = "5";
var OTASuccessOTACmdType = "6";
var cmd2Byte_run_app = 0x01;
var cmd2Byte_run_boot = 0x02;
var cmd2Byte_clean_data = 0x03;
var cmd2Byte_upgrade_finish = 0x04;
var cmd2Byte_upgrade_data = 0x05;
console.log(command);
var result = {};
var fullCmd = [];
for (let i = 0; i < 522; i++) {
fullCmd[i] = 0x00;
}
fullCmd[0] = 0xaa;// 固定开头
fullCmd[520] = 0x88;// 固定结尾
fullCmd[521] = 0x88;// 固定结尾
var otaCmdType = command.otaCmdType;
if (otaCmdType == PreOTACmdType) {
fullCmd[1] = cmd2Byte_run_boot;
var checksum = getCheckSum(fullCmd, 0, 518);
fullCmd[518] = checksum[0];
fullCmd[519] = checksum[1];
result.command = fullCmd;
result.session = {
"otaLastCmdType": PreOTACmdType,
"otaTtotalPackage": command.otaTotalPackage,
}
return result;
} else if (otaCmdType == StartOTACmdType) {
fullCmd[1] = cmd2Byte_clean_data;
var checksum = getCheckSum(fullCmd, 0, 518);
fullCmd[518] = checksum[0];
fullCmd[519] = checksum[1];
result.command = fullCmd;
result.session = {
"otaLastCmdType": StartOTACmdType,
"otaTotalPackage": command.otaTotalPackage,
}
return result;
} else if (otaCmdType == SendPackageDataOTACmdType) {
fullCmd[1] = cmd2Byte_upgrade_data;// 命令位,也可以是command.cmdParam
var packageNo = command.otaPackageNo;
var packageData = command.otaPackageData;
fullCmd[2] = packageNo & 0xff;
fullCmd[3] = (packageNo >> 8) & 0xff;
var validDataLength = packageData.length;
fullCmd[4] = validDataLength & 0xff;
fullCmd[5] = (validDataLength >> 8) & 0xff;
for (let i = 0; i < packageData.length; i++) {
fullCmd[i + 6] = packageData[i];
}
var checksum = getCheckSum(fullCmd, 0, 518);
fullCmd[518] = checksum[0];
fullCmd[519] = checksum[1];
result.command = fullCmd;
result.session = {
"otaLastCmdType": SendPackageDataOTACmdType,
"otaTotalPackage": command.otaTotalPackage,
}
return result;
} else if (otaCmdType == UpgradingOTACmdType) {
fullCmd[1] = cmd2Byte_upgrade_finish;// 命令位,也可以是command.cmdParam
var checksum = getCheckSum(fullCmd, 0, 518);
fullCmd[518] = checksum[0];
fullCmd[519] = checksum[1];
result.command = fullCmd;
result.session = {
"otaLastCmdType": UpgradingOTACmdType,
"otaTotalPackage": command.otaTotalPackage,
}
return result;
} else if (otaCmdType == UpgradeCompleteOTACmdType) {
fullCmd[1] = cmd2Byte_run_app;// 命令位,也可以是command.cmdParam
var checksum = getCheckSum(fullCmd, 0, 518);
fullCmd[518] = checksum[0];
fullCmd[519] = checksum[1];
result.command = fullCmd;
result.session = {
"otaLastCmdType": UpgradeCompleteOTACmdType,
"otaTotalPackage": command.otaTotalPackage,
}
return result;
}
return result;
}
console.log("*********************************************");
var payload = [0xbb, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99];
var session = {
"otaLastCmdType": "1",
"otaTotalPackage": 153,
}
var dresult = otaDecode(session, payload, {}, {});
console.log(dresult);
function otaDecode(session, payload, metadata, deviceConfig) {
function getCheckSum(bs, start, end) {
var checkSum = 0;
for (i = start; i < end; i++) {
checkSum = checkSum + bs[i];
}
var crc = [];
crc[0] = checkSum & 0xff;// 校验和取低两字节,低字节在前
crc[1] = (checkSum >> 8) & 0xff; // 校验和取低两字节,低字节在前
return crc;
};
// 应答包
// 包头(0xbb) 命令(1Byte) 包序(2Byte) 校验和(2Byte) 包尾(0x9999)
var cmd2Byte_run_app = 0x01;
var cmd2Byte_run_boot = 0x02;
var cmd2Byte_clean_data = 0x03;
var cmd2Byte_upgrade_finish = 0x04;
var cmd2Byte_upgrade_data = 0x05;
var NoActionOTACmdType = "0";
var PreOTACmdType = "1";
var StartOTACmdType = "2";
var SendPackageDataOTACmdType = "3";
var UpgradingOTACmdType = "4";
var UpgradeCompleteOTACmdType = "5";
var OTASuccessOTACmdType = "6";
var otaLastCmdType = "0";
var otaCmdType = "0";
var otaCmdParam = 0x00;
var otaPackageNo = 0;
var result = {};
if (!payload instanceof Array) {
console.log("payload is not array, return");
return result;
}
var len = payload.length;
if (len < 8) {
return result;
}
// 应答包 固定头0xbb,固定尾 0x9999
if (payload[0] != 0xbb || payload[len - 2] != 0x99 || payload[len - 1] != 0x99) {
return result;
}
result.sn = session.sn;
result.category = session.category;
result.otaCmdParam = otaCmdParam;
result.otaCmdType = otaCmdType;
result.otaLastCmdType = otaLastCmdType;
var cmd = payload[1]; // ack回复报文第二个字节是命令
if (cmd == cmd2Byte_run_boot) {
result.otaLastCmdType = session.otaLastCmdType;
result.otaCmdType = NoActionOTACmdType;
return result;
} else if (cmd == cmd2Byte_clean_data) {
result.otaLastCmdType = session.otaLastCmdType;
result.otaCmdType = SendPackageDataOTACmdType;
// 开始发送数据,业务逻辑根据packageNo+1拼装数据包,所以初始传入-1
result.otaPackageNo = -1;
result.otaCmdParam = cmd2Byte_upgrade_data;
return result;
} else if (cmd == cmd2Byte_upgrade_data) {
result.otaLastCmdType = session.otaLastCmdType;
result.otaPackageNo = payload[2] + (package[3] << 8);
if ((result.otaPackageNo + 1) < session.otaTotalPackage) {
// 收到发送数据包的ack,继续发送
result.otaCmdType = SendPackageDataOTACmdType;
result.otaCmdParam = cmd2Byte_upgrade_data;
} else {
// 收到最后一个数据包的ack,发送完毕
result.otaCmdType = UpgradingOTACmdType;
result.otaCmdParam = cmd2Byte_upgrade_finish;
}
return result;
} else if (cmd == cmd2Byte_upgrade_finish) {
result.otaLastCmdType = session.otaLastCmdType;
result.otaCmdType = UpgradeCompleteOTACmdType;
// 开始发送数据,业务逻辑根据packageNo+1拼装数据包,所以初始传入-1
result.otaPackageNo = -1;
result.otaCmdParam = cmd2Byte_run_app;
return result;
} else if (cmd == cmd2Byte_run_app) {
result.otaLastCmdType = session.otaLastCmdType;
result.otaCmdType = OTASuccessOTACmdType;
// 开始发送数据,业务逻辑根据packageNo+1拼装数据包,所以初始传入-1
result.otaPackageNo = -1;
return result;
}
return result;
}
console