重构代码

用字典的方式更优雅得判断数据
This commit is contained in:
CSSC-WORK\murmur 2024-07-05 17:31:06 +08:00
parent 8e98a9f26d
commit cf9007247a

66
UUV.lua
View File

@ -1,36 +1,35 @@
local n1 = {"漏水", "温度", "CDB", "S-TT", "BD", "SJL", "电源", "预留"}
local n2 = {"S-TT", "BD", "CDB", "SJL", "FB", "预留2", "预留1", "预留0"}
local bx = {"0.6kbps", "1.2kbps", "2.4kbps", "4.8kbps"}
local sttSpeed = {"2.4bps", "4.8kbps", "9.6kbps"}
local cdbSpeed = {"未知","未知","2.4kbps", "4.8kbps", "38.4kbps"}
local s6Info = {"漏水", "温度", "CDB", "S-TT", "BD", "SJL", "电源", "预留"}
local s7Info = {"S-TT", "BD", "CDB", "SJL", "FB", "预留2", "预留1", "预留0"}
local a23Info = {nil,"2.4kbps", "4.8kbps", "38.4kbps"}
local a24Info = {"一线通天线<----->榄杆天线","二线通天线<----->FB天线"}
local a25Info = {"点波束","海模式"}
local a26Info = {"2.4bps", "4.8kbps", "9.6kbps"}
local a27Info = {"一线通天线<----->榄杆天线","二线通天线<----->FB天线"}
local a28Info = {"北三","芯片","北二"}
local a70Info = {"AM-老SJL专用保留","FM"}
local a71Info = {[0xc1]="X02模式",[0xc2]="X03模式"}
local a73Info = {[0x01]="主站",[0x04]="从站"}
local a75Info = {"0.6kbps", "1.2kbps", "2.4kbps", "4.8kbps"}
-- 创建一个自定义的遍历函数
function orderedPairs(t)
local keys = {}
-- 将table中的键存储到一个数组中
for k, _ in pairs(t) do
table.insert(keys, k)
local function safeGet(array, index)
return array[index] ~= nil and array[index] or "错误:"..string.format("0x%02X",index)
end
-- 对数组进行排序
table.sort(keys)
-- 遍历排序后的数组,并打印每个元素的值
function printHead(t)
local tmp=""
for _, k in ipairs(keys) do
tmp = tmp..t[k].."\t"
for i = 1, #t do
tmp = tmp..t[i].."\t"
end
return tmp
end
function getBattInfo(str)
function parseInfo(str)
local a1,a3,a5,a6,a7,a8,a9,a10,a18,a19,a23,a24,a25,a26,a27,a28,a29,a30= string.unpack("<c2Hc1BBBBc8BI4BBBBBBBB",str)
local a31,a33,a34,a35,a36,a37,a38,a40,a48,a56,a57,a58,a62,a66,a70,a71,a73,a75,a76,a78,a80,a82,a84,a86,a88,a90,a92,a94,a96,a97= string.unpack("<HBBBBBHddBBfffBHHBHHHHHHHHHHBH",str:sub(31,-1))
local s1="-----------------------------------------------------------------------\r\n"
s1=s1..orderedPairs(n1).."\r\n"
s1=s1..printHead(s6Info).."\r\n"
-- Print the corresponding statuses
for i = 7,0,-1 do
@ -40,7 +39,7 @@ end
s1=s1.."\r\n-----------------------------------------------------------------------"
local s2="-----------------------------------------------------------------------\r\n"
s2=s2..orderedPairs(n2).."\r\n"
s2=s2..printHead(s7Info).."\r\n"
-- Print the corresponding statuses
for i = 7,0,-1 do
@ -62,12 +61,12 @@ s2.."\r\n"..
"a10 "..a10.."\r\n"..
"a18 "..a18.."\r\n"..
"a19 ".."CDB频率\t\t"..a19.." kHz\r\n"..
"a23 ".."CDB速率\t\t"..(a23 < 0x05 and bx[a23] or "错误:"..string.format("0x%02X",a23)).."\r\n"..
"a24 ".."S天线模式\t\t"..(a24 == 0x01 and "一线通天线<----->榄杆天线" or (a24 == 0x02 and "二线通天线<----->FB天线" or "错误:"..string.format("0x%02X",a24))).."\r\n"..
"a25 ".."TT模式\t\t"..(a25 == 0x00 and "点波束" or (a28 == 0x01 and "海模式" or "错误:"..string.format("0x%02X",a25))).."\r\n"..
"a26 ".."TT速率\t\t"..(a26 < 0x04 and sttSpeed[a26] or "错误:"..string.format("0x%02X",a26)).."\r\n"..
"a27 ".."BD天线模式\t"..(a27 == 0x01 and "一线通天线<----->榄杆天线" or (a27 == 0x02 and "二线通天线<----->FB天线" or "错误:"..string.format("0x%02X",a27))).."\r\n"..
"a28 ".."卡模式\t\t"..(a28 == 0x01 and "民卡模式" or (a28 == 0x02 and "芯片模式" or "错误:"..string.format("0x%02X",a28))).."\r\n"..
"a23 ".."CDB速率\t\t"..safeGet(a23Info,a23).."\r\n"..
"a24 ".."S天线模式\t\t"..safeGet(a24Info,a24).."\r\n"..
"a25 ".."STT模式\t\t"..safeGet(a25Info,a25+1).."\r\n"..
"a26 ".."STT速率\t\t"..safeGet(a26Info,a26+1).."\r\n"..
"a27 ".."BD天线模式\t"..safeGet(a27Info,a27).."\r\n"..
"a28 ".."卡模式\t\t"..safeGet(a28Info,a28+1).."\r\n"..
"a29 "..a29.."\r\n"..
"a30 "..a30.."\r\n"..
"a31 ".."时间\t\t"..string.format("%d年%d月%d日 %d:%d:%d.%d",a31,a33,a34,a35,a36,a37,a38).."\r\n"..
@ -78,10 +77,10 @@ s2.."\r\n"..
"a58 ".."HDOP\t\t"..a58.."\r\n"..
"a62 ".."海拔\t\t"..a62.."\r\n"..
"a66 ".."航速\t\t"..a66.."\r\n"..
"a70 ".."调制方式\t\t"..(a70 == 1 and "FM" or "AM").."\r\n"..
"a71 ".."信道类型\t\t"..(a71 == 0xc1 and "X02" or (a71 == 0xc2 and "X03" or "错误:"..string.format("0x%02X",a71))).."\r\n"..
"a73 ".."站方式\t\t"..(a73 == 0x01 and "主站" or (a73 == 0x04 and "从站" or "错误:"..string.format("0x%02X",a73))).."\r\n"..
"a75 ".."波形速率\t\t"..(a75 < 0x04 and bx[a75] or "错误").."\r\n"..
"a70 ".."调制方式\t\t"..safeGet(a70Info,a70+1).."\r\n"..
"a71 ".."信道类型\t\t"..safeGet(a71Info,a71).."\r\n"..
"a73 ".."站方式\t\t"..safeGet(a73Info,a73).."\r\n"..
"a75 ".."波形速率\t\t"..safeGet(a75Info,a75+1).."\r\n"..
--"a76 "..a76.."\r\n"..
--"a78 "..a78.."\r\n"..
--"a80 "..a80.."\r\n"..
@ -106,9 +105,8 @@ function getinfo(data)
--每包都处理一遍
for i=1,#udata do
if #udata[i]>10 then
-- local rst = parseInfo(string.char(0x54)..string.char(0x58)..udata[i])
local rst = getBattInfo(string.char(0x54)..string.char(0x58)..udata[i])
if #udata[i]>100 then
local rst = parseInfo(string.char(0x54)..string.char(0x58)..udata[i])
print(rst)
end
end