优化代码

This commit is contained in:
ling 2024-04-16 15:48:39 +08:00
parent 1145bf688f
commit 74b22dbc21

View File

@ -26,15 +26,15 @@ function index()
--模块调试 --模块调试
entry({"admin", "network", "modem", "modem_debug"},template("modem/modem_debug"),translate("Modem Debug"),30).leaf = true entry({"admin", "network", "modem", "modem_debug"},template("modem/modem_debug"),translate("Modem Debug"),30).leaf = true
entry({"admin", "network", "modem", "get_quick_commands"}, call("getQuickCommands"), nil).leaf = true entry({"admin", "network", "modem", "quick_commands_config"}, cbi("modem/quick_commands_config")).leaf = true
entry({"admin", "network", "modem", "send_at_command"}, call("sendATCommand"), nil).leaf = true
entry({"admin", "network", "modem", "get_modem_debug_info"}, call("getModemDebugInfo"), nil).leaf = true
entry({"admin", "network", "modem", "get_mode_info"}, call("getModeInfo"), nil).leaf = true entry({"admin", "network", "modem", "get_mode_info"}, call("getModeInfo"), nil).leaf = true
entry({"admin", "network", "modem", "set_mode"}, call("setMode"), nil).leaf = true entry({"admin", "network", "modem", "set_mode"}, call("setMode"), nil).leaf = true
entry({"admin", "network", "modem", "get_network_prefer_info"}, call("getNetworkPreferInfo"), nil).leaf = true entry({"admin", "network", "modem", "get_network_prefer_info"}, call("getNetworkPreferInfo"), nil).leaf = true
entry({"admin", "network", "modem", "set_network_prefer"}, call("setNetworkPrefer"), nil).leaf = true entry({"admin", "network", "modem", "set_network_prefer"}, call("setNetworkPrefer"), nil).leaf = true
entry({"admin", "network", "modem", "get_self_test_info"}, call("getSelfTestInfo"), nil).leaf = true entry({"admin", "network", "modem", "get_self_test_info"}, call("getSelfTestInfo"), nil).leaf = true
entry({"admin", "network", "modem", "quick_commands_config"}, cbi("modem/quick_commands_config")).leaf = true entry({"admin", "network", "modem", "get_quick_commands"}, call("getQuickCommands"), nil).leaf = true
entry({"admin", "network", "modem", "send_at_command"}, call("sendATCommand"), nil).leaf = true
-- entry({"admin", "network", "modem", "get_modem_debug_info"}, call("getModemDebugInfo"), nil).leaf = true
--AT命令旧界面 --AT命令旧界面
entry({"admin", "network", "modem", "at_command_old"},template("modem/at_command_old")).leaf = true entry({"admin", "network", "modem", "at_command_old"},template("modem/at_command_old")).leaf = true
@ -50,6 +50,18 @@ function hasLetters(str)
return string.find(str, pattern) ~= nil return string.find(str, pattern) ~= nil
end end
--[[
@Description Shell脚本
@Params
command sh命令
]]
function shell(command)
local odpall = io.popen(command)
local odp = odpall:read("*a")
odpall:close()
return odp
end
--[[ --[[
@Description AT命令 @Description AT命令
@Params @Params
@ -57,11 +69,29 @@ end
at_command AT命令 at_command AT命令
]] ]]
function at(at_port,at_command) function at(at_port,at_command)
local odpall = io.popen("source "..script_path.."modem_debug.sh && at "..at_port.." "..at_command) local command="source "..script_path.."modem_debug.sh && at "..at_port.." "..at_command
local odp = odpall:read("*a") local result=shell(command)
odpall:close() result=string.gsub(result, "\r", "")
odp=string.gsub(odp, "\r", "") return result
return odp end
--[[
@Description
@Params
at_port AT串口
]]
function getManufacturer(at_port)
local manufacturer
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
manufacturer=modem_device["manufacturer"]
return true --跳出循环
end
end)
return manufacturer
end end
--[[ --[[
@ -75,15 +105,33 @@ function getMode(at_port,manufacturer,platform)
local mode="unknown" local mode="unknown"
if at_port and manufacturer~="unknown" then if at_port and manufacturer~="unknown" then
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_mode "..at_port.." "..platform) local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_get_mode "..at_port.." "..platform
opd = odpall:read("*a") local result=shell(command)
odpall:close() mode=string.gsub(result, "\n", "")
mode = string.gsub(opd, "\n", "")
end end
return mode return mode
end end
--[[
@Description
@Params
at_port AT串口
]]
function getModes(at_port)
local modes
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
modes=modem_device["modes"]
return true --跳出循环
end
end)
return modes
end
--[[ --[[
@Description @Description
@Params @Params
@ -96,10 +144,9 @@ function getModemConnectStatus(at_port,manufacturer,define_connect)
local connect_status="unknown" local connect_status="unknown"
if at_port and manufacturer~="unknown" then if at_port and manufacturer~="unknown" then
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_connect_status "..at_port.." "..define_connect) local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_get_connect_status "..at_port.." "..define_connect
opd = odpall:read("*a") local result=shell(command)
odpall:close() connect_status=string.gsub(result, "\n", "")
connect_status = string.gsub(opd, "\n", "")
end end
return connect_status return connect_status
@ -140,12 +187,11 @@ end
function getModemMoreInfo(at_port,manufacturer,define_connect) function getModemMoreInfo(at_port,manufacturer,define_connect)
--获取模组信息 --获取模组信息
local odpall = io.popen("sh "..script_path.."modem_info.sh".." "..at_port.." "..manufacturer.." "..define_connect) local command="sh "..script_path.."modem_info.sh".." "..at_port.." "..manufacturer.." "..define_connect
local opd = odpall:read("*a") local result=shell(command)
odpall:close()
--设置值 --设置值
local modem_more_info=json.parse(opd) local modem_more_info=json.parse(result)
return modem_more_info return modem_more_info
end end
@ -380,6 +426,160 @@ function getATPort()
luci.http.write_json(data) luci.http.write_json(data)
end end
--[[
@Description
]]
function getModeInfo()
local at_port = http.formvalue("port")
--获取值
local mode_info={}
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
--获取制造商
local manufacturer=modem_device["manufacturer"]
if manufacturer=="unknown" then
return true --跳出循环
end
--获取支持的拨号模式
local modes=modem_device["modes"]
--获取模组拨号模式
local mode=getMode(at_port,manufacturer,modem_device["platform"])
--设置模式信息
mode_info["mode"]=mode
mode_info["modes"]=modes
return true --跳出循环
end
end)
--设置值
local modem_debug_info={}
modem_debug_info["mode_info"]=mode_info
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(modem_debug_info)
end
--[[
@Description
]]
function setMode()
local at_port = http.formvalue("port")
local mode_config = http.formvalue("mode_config")
--获取制造商
local manufacturer=getManufacturer(at_port)
--设置模组拨号模式
local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_set_mode "..at_port.." "..mode_config
shell(command)
--获取设置好后的模组拨号模式
local mode
if at_port and manufacturer and manufacturer~="unknown" then
local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_get_mode "..at_port
local result=shell(command)
mode=string.gsub(result, "\n", "")
end
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(mode)
end
--[[
@Description
]]
function getNetworkPreferInfo()
local at_port = http.formvalue("port")
--获取制造商
local manufacturer=getManufacturer(at_port)
--获取值
local network_prefer_info
if manufacturer~="unknown" then
--获取模组网络偏好
local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_get_network_prefer "..at_port
local result=shell(command)
network_prefer_info=json.parse(result)
end
--设置值
local modem_debug_info={}
modem_debug_info["network_prefer_info"]=network_prefer_info
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(modem_debug_info)
end
--[[
@Description
]]
function setNetworkPrefer()
local at_port = http.formvalue("port")
local network_prefer_config = json.stringify(http.formvalue("prefer_config"))
--获取制造商
local manufacturer=getManufacturer(at_port)
--设置模组网络偏好
local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_set_network_prefer "..at_port.." "..network_prefer_config
shell(command)
--获取设置好后的模组网络偏好
local network_prefer={}
if at_port and manufacturer and manufacturer~="unknown" then
local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_get_network_prefer "..at_port
local result=shell(command)
network_prefer=json.parse(result)
end
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(network_prefer)
end
--[[
@Description
]]
function getSelfTestInfo()
local at_port = http.formvalue("port")
--获取制造商
local manufacturer=getManufacturer(at_port)
--获取值
local self_test_info={}
if manufacturer~="unknown" then
--获取模组电压
local command="source "..script_path..manufacturer..".sh && "..manufacturer.."_get_voltage "..at_port
local result=shell(command)
self_test_info["voltage"]=json.parse(result)
--获取模组温度
command="source "..script_path..manufacturer..".sh && "..manufacturer.."_get_temperature "..at_port
result=shell(command)
self_test_info["temperature"]=json.parse(result)
end
--设置值
local modem_debug_info={}
modem_debug_info["self_test_info"]=self_test_info
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(modem_debug_info)
end
--[[ --[[
@Description @Description
]] ]]
@ -391,15 +591,7 @@ function getQuickCommands()
local at_port = http.formvalue("port") local at_port = http.formvalue("port")
--获取制造商 --获取制造商
local manufacturer local manufacturer=getManufacturer(at_port)
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
--获取制造商
manufacturer=modem_device["manufacturer"]
return true --跳出循环
end
end)
--未适配模组时,快捷命令选项为自定义 --未适配模组时,快捷命令选项为自定义
if manufacturer=="unknown" then if manufacturer=="unknown" then
@ -410,11 +602,11 @@ function getQuickCommands()
local commands={} local commands={}
if quick_option=="auto" then if quick_option=="auto" then
--获取模组AT命令 --获取模组AT命令
-- local odpall = io.popen(source "..script_path.."modem_debug.sh && get_quick_commands "..quick_option.." "..manufacturer) -- local command="source "..script_path.."modem_debug.sh && get_quick_commands "..quick_option.." "..manufacturer
local odpall = io.popen("cat "..script_path..manufacturer.."_at_commands.json") local command="cat "..script_path..manufacturer.."_at_commands.json"
local opd = odpall:read("*a") local result=shell(command)
odpall:close() quick_commands=json.parse(result)
quick_commands=json.parse(opd)
else else
uci:foreach("modem", "custom-commands", function (custom_commands) uci:foreach("modem", "custom-commands", function (custom_commands)
local command={} local command={}
@ -447,207 +639,6 @@ function sendATCommand()
luci.http.write_json(response) luci.http.write_json(response)
end end
--[[
@Description
]]
function setNetworkPrefer()
local at_port = http.formvalue("port")
local network_prefer_config = json.stringify(http.formvalue("prefer_config"))
--获取制造商
local manufacturer
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
--获取制造商
manufacturer=modem_device["manufacturer"]
return true --跳出循环
end
end)
--设置模组网络偏好
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_set_network_prefer "..at_port.." "..network_prefer_config)
odpall:close()
--获取设置好后的模组网络偏好
local network_prefer={}
if at_port and manufacturer and manufacturer~="unknown" then
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_network_prefer "..at_port)
local opd = odpall:read("*a")
network_prefer=json.parse(opd)
odpall:close()
end
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(network_prefer)
end
--[[
@Description
]]
function setMode()
local at_port = http.formvalue("port")
local mode_config = http.formvalue("mode_config")
--获取制造商
local manufacturer
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
--获取制造商
manufacturer=modem_device["manufacturer"]
return true --跳出循环
end
end)
--设置模组拨号模式
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_set_mode "..at_port.." "..mode_config)
odpall:close()
--获取设置好后的模组拨号模式
local mode
if at_port and manufacturer and manufacturer~="unknown" then
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_mode "..at_port)
mode = odpall:read("*a")
mode=string.gsub(mode, "\n", "")
odpall:close()
end
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(mode)
end
--[[
@Description
]]
function getModeInfo()
local at_port = http.formvalue("port")
--获取制造商
local manufacturer
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
--获取制造商
manufacturer=modem_device["manufacturer"]
return true --跳出循环
end
end)
--获取值
local mode_info={}
if manufacturer~="unknown" then
--获取支持的拨号模式
local modes
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
modes=modem_device["modes"]
return true --跳出循环
end
end)
--获取模组拨号模式
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_mode "..at_port)
local opd = odpall:read("*a")
odpall:close()
local mode=string.gsub(opd, "\n", "")
--设置模式信息
mode_info["mode"]=mode
mode_info["modes"]=modes
end
--设置值
local modem_debug_info={}
modem_debug_info["mode_info"]=mode_info
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(modem_debug_info)
end
--[[
@Description
]]
function getNetworkPreferInfo()
local at_port = http.formvalue("port")
--获取制造商
local manufacturer
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
--获取制造商
manufacturer=modem_device["manufacturer"]
return true --跳出循环
end
end)
--获取值
local network_prefer_info
if manufacturer~="unknown" then
--获取模组网络偏好
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_network_prefer "..at_port)
local opd = odpall:read("*a")
odpall:close()
network_prefer_info=json.parse(opd)
end
--设置值
local modem_debug_info={}
modem_debug_info["network_prefer_info"]=network_prefer_info
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(modem_debug_info)
end
--[[
@Description
]]
function getSelfTestInfo()
local at_port = http.formvalue("port")
--获取制造商
local manufacturer
uci:foreach("modem", "modem-device", function (modem_device)
--设置模组AT串口
if at_port == modem_device["at_port"] then
--获取制造商
manufacturer=modem_device["manufacturer"]
return true --跳出循环
end
end)
--获取值
local self_test_info={}
if manufacturer~="unknown" then
--获取模组电压
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_voltage "..at_port)
local opd = odpall:read("*a")
odpall:close()
self_test_info["voltage"]=json.parse(opd)
--获取模组温度
local odpall = io.popen("source "..script_path..manufacturer..".sh && "..manufacturer.."_get_temperature "..at_port)
local opd = odpall:read("*a")
odpall:close()
self_test_info["temperature"]=json.parse(opd)
end
--设置值
local modem_debug_info={}
modem_debug_info["self_test_info"]=self_test_info
-- 写入Web界面
luci.http.prepare_content("application/json")
luci.http.write_json(modem_debug_info)
end
--[[ --[[
@Description @Description
]] ]]
@ -655,15 +646,7 @@ end
-- local at_port = http.formvalue("port") -- local at_port = http.formvalue("port")
-- --获取制造商 -- --获取制造商
-- local manufacturer -- local manufacturer=getManufacturer(at_port)
-- uci:foreach("modem", "modem-device", function (modem_device)
-- --设置模组AT串口
-- if at_port == modem_device["at_port"] then
-- --获取制造商
-- manufacturer=modem_device["manufacturer"]
-- return true --跳出循环
-- end
-- end)
-- --获取值 -- --获取值
-- local mode_info={} -- local mode_info={}