diff --git a/luci-app-modem/luasrc/controller/modem.lua b/luci-app-modem/luasrc/controller/modem.lua index aa9ef30..a4bffb7 100644 --- a/luci-app-modem/luasrc/controller/modem.lua +++ b/luci-app-modem/luasrc/controller/modem.lua @@ -1,4 +1,4 @@ --- Copyright 2020 Lienol +-- Copyright 2024 Siriling module("luci.controller.modem", package.seeall) local http = require "luci.http" local fs = require "nixio.fs" @@ -23,30 +23,47 @@ function index() entry({"admin", "network", "modem", "get_modems"}, call("getModems"), nil).leaf = true entry({"admin", "network", "modem", "status"}, call("act_status")).leaf = true - --AT命令 - entry({"admin", "network", "modem", "at_commands"},template("modem/at_commands"),translate("AT Commands"),30).leaf = true - entry({"admin", "network", "modem", "mode_info"}, call("modeInfo"), nil).leaf = true + --模块调试 + entry({"admin", "network", "modem", "modem_debug"},template("modem/modem_debug"),translate("Modem Debug"),30).leaf = true + entry({"admin", "network", "modem", "get_at_port"}, call("getATPort"), nil).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", "user_at_command"}, call("userATCommand"), nil).leaf = true - entry({"admin", "network", "modem", "get_at_port"}, call("getATPort"), nil).leaf = true + entry({"admin", "network", "modem", "mode_info"}, call("modeInfo"), nil).leaf = true + + --AT命令旧界面 + entry({"admin", "network", "modem", "at_command_old"},template("modem/at_command_old")).leaf = true end --- 判断字符串是否含有字母 +--[[ +@Description 判断字符串是否含有字母 +@Params + str 字符串 +]] function hasLetters(str) local pattern = "%a" -- 匹配字母的正则表达式 return string.find(str, pattern) ~= nil end --- AT命令 +--[[ +@Description 执行AT命令 +@Params + at_port AT串口 + at_command AT命令 +]] function at(at_port,at_command) - -- local odpall = io.popen("sh modem_at.sh "..at_port.." '"..at_command.."'") - local odpall = io.popen("sms_tool -d " .. at_port .. " at " ..at_command:gsub("[$]", "\\\$"):gsub("\"", "\\\"").." 2>&1") + local odpall = io.popen("cd "..script_path.." && source "..script_path.."modem_debug.sh && at "..at_port.." "..at_command) local odp = odpall:read("*a") odpall:close() return odp end --- 获取模组连接状态 +--[[ +@Description 获取模组连接状态 +@Params + at_port AT串口 + manufacturer 制造商 +]] function getModemConnectStatus(at_port,manufacturer) local connect_status="unknown" @@ -61,7 +78,11 @@ function getModemConnectStatus(at_port,manufacturer) return connect_status end --- 获取模组基本信息 +--[[ +@Description 获取模组基本信息 +@Params + at_port AT串口 +]] function getModemDeviceInfo(at_port) local modem_device_info={} @@ -83,7 +104,12 @@ function getModemDeviceInfo(at_port) return modem_device_info end --- 获取模组更多信息 +--[[ +@Description 获取模组更多信息 +@Params + at_port AT串口 + manufacturer 制造商 +]] function getModemMoreInfo(at_port,manufacturer) --获取模组信息 @@ -96,7 +122,9 @@ function getModemMoreInfo(at_port,manufacturer) return modem_more_info end --- 模块状态获取 +--[[ +@Description 模块状态获取 +]] function getModemInfo() --获取AT串口 @@ -166,7 +194,9 @@ function getModemInfo() luci.http.write_json(data) end --- 获取模组信息 +--[[ +@Description 获取模组信息 +]] function getModems() -- 获取所有模组 @@ -197,7 +227,9 @@ function getModems() luci.http.write_json(data) end --- 模块列表状态函数 +--[[ +@Description 模块列表状态函数 +]] function act_status() local e = {} e.index = luci.http.formvalue("index") @@ -206,7 +238,9 @@ function act_status() luci.http.write_json(e) end --- 模式信息 +--[[ +@Description 模式信息 +]] function modeInfo() -- 设置默认值 local modes={"qmi","gobinet","ecm","mbim","rndis","ncm"} @@ -231,21 +265,27 @@ function modeInfo() luci.http.write_json(modes) end --- 发送AT命令 +--[[ +@Description 发送AT命令 +]] function sendATCommand() local at_port = http.formvalue("port") local at_command = http.formvalue("command") - local response + local response={} if at_port and at_command then - response=at(at_port,at_command) - http.write(tostring(response)) - else - http.write_json(http.formvalue()) + response["response"]=at(at_port,at_command) + response["time"]=os.date("%Y-%m-%d %H:%M:%S") end + + -- 写入Web界面 + luci.http.prepare_content("application/json") + luci.http.write_json(response) end --- 用户AT命令 +--[[ +@Description 用户AT命令 +]] function userATCommand() local at_commands={} -- 获取模块AT命令 @@ -273,8 +313,11 @@ function userATCommand() luci.http.write_json(at_commands) end --- 获取模组的备注 --- @Param network 移动网络 +--[[ +@Description 获取模组的备注 +@Params + network 移动网络 +]] function getModemRemarks(network) local remarks="" uci:foreach("modem", "config", function (config) @@ -289,7 +332,9 @@ function getModemRemarks(network) return remarks end --- 获取AT串口 +--[[ +@Description 获取AT串口 +]] function getATPort() local at_ports={} uci:foreach("modem", "modem-device", function (modem_device) @@ -314,3 +359,37 @@ function getATPort() luci.http.prepare_content("application/json") luci.http.write_json(at_ports) end + +--[[ +@Description 获取快捷命令 +]] +function getQuickCommands() + + --获取快捷命令选项 + local quick_option = http.formvalue("option") + --获取AT串口 + 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"] + end + end) + + --获取模组AT命令 + local quick_commands={} + if quick_option and manufacturer then + local odpall = io.popen("cd "..script_path.." && source "..script_path.."modem_debug.sh && get_quick_commands "..quick_option.." "..manufacturer) + local opd = odpall:read("*a") + quick_commands=json.parse(opd) + odpall:close() + end + + -- 写入Web界面 + luci.http.prepare_content("application/json") + luci.http.write_json(quick_commands) +end \ No newline at end of file diff --git a/luci-app-modem/luasrc/view/modem/at_commands.htm b/luci-app-modem/luasrc/view/modem/at_command_old.htm similarity index 78% rename from luci-app-modem/luasrc/view/modem/at_commands.htm rename to luci-app-modem/luasrc/view/modem/at_command_old.htm index 770a578..117ba52 100644 --- a/luci-app-modem/luasrc/view/modem/at_commands.htm +++ b/luci-app-modem/luasrc/view/modem/at_command_old.htm @@ -93,49 +93,63 @@ // 根据所选的模组改变AT命令 function afunction() { - // var node = document.getElementById('odp'); + // var node = document.getElementById('response'); // node.style.visibility = 'hidden'; // var x = document.getElementById("at_command").value; // document.getElementById("code").value = x; - // document.getElementById("odp").innerHTML = ""; + // document.getElementById("response").innerHTML = ""; } //自动填写到命令输入框 function copyToSend() { - var node = document.getElementById('odp'); + var node = document.getElementById('response'); node.style.visibility = 'hidden'; var command_choose = document.getElementById("command_choose").value; document.getElementById("at_command").value = command_choose; - document.getElementById("odp").innerHTML = ""; + document.getElementById("response").innerHTML = ""; } function postCommand(at_port,at_command) { - (new XHR()).post('<%=luci.dispatcher.build_url("admin", "network", "modem", "send_at_command")%>', {"port":at_port,"command":at_command}, function(x) { - console.log(x.response) - console.log(x) + // (new XHR()).post('<%=luci.dispatcher.build_url("admin", "network", "modem", "send_at_command")%>', {"port":at_port,"command":at_command}, function(x) { + // console.log(x.response) + // console.log(x) - var aStr = x.response; - var myre = /^[\s\t]*(\r\n|\n|\r)/gm; - var bStr = aStr.replace(myre,""); - document.getElementById("odp").innerHTML = bStr; - var el = document.getElementsByName("odp")[0]; - el.value.replace(/(\r\n|\n|\r)/gm, ""); - }); + // var aStr = x.response; + // var myre = /^[\s\t]*(\r\n|\n|\r)/gm; + // var bStr = aStr.replace(myre,""); + // document.getElementById("response").innerHTML = bStr; + // var el = document.getElementsByName("response")[0]; + // el.value.replace(/(\r\n|\n|\r)/gm, ""); + // }); + + XHR.post('<%=luci.dispatcher.build_url("admin", "network", "modem", "send_at_command")%>', {"port":at_port,"command":at_command}, + function(x, data) + { + responseElement=document.getElementById("response"); + if ("response" in data) { + //显示当前时间 + // responseElement.value+=data["time"]; + //显示返回值 + responseElement.innerHTML+=data["response"]; + } + } + ); return false; } - document.addEventListener('DOMContentLoaded', function (ev) {var button = document.getElementById("sendcmd"); + document.addEventListener('DOMContentLoaded', function (ev) { + var button = document.getElementById("sendcmd"); button.addEventListener("click", function () { //获取AT串口 var at_port = document.getElementById("modem_select").value; if ( at_port.length == 0 ) { - document.getElementById("odp").innerHTML = ""; + document.getElementById("response").innerHTML = ""; alert("<%:Please choose a Modem%>"); return false; } @@ -144,7 +158,7 @@ var at_command = document.getElementById("at_command").value; if ( at_command.length == 0 ) { - document.getElementById("odp").innerHTML = ""; + document.getElementById("response").innerHTML = ""; alert("<%:Please enter a AT Command%>"); return false; } @@ -153,7 +167,7 @@ postCommand(at_port,at_command); at_command = ""; - var node = document.getElementById('odp'); + var node = document.getElementById('response'); if (node.style.visibility=='visible') { node.style.visibility = 'hidden'; } @@ -216,8 +230,9 @@
+
<%:Response%>: - +
diff --git a/luci-app-modem/luasrc/view/modem/modem_debug.htm b/luci-app-modem/luasrc/view/modem/modem_debug.htm new file mode 100644 index 0000000..2d53163 --- /dev/null +++ b/luci-app-modem/luasrc/view/modem/modem_debug.htm @@ -0,0 +1,337 @@ +<%+header%> + + + +
+

<%:Modem Debug%>

+
<%:Debug Your Module%>
+ + + + +
+

<%:Message%>

+ + + + + + +
+
+ <%:Loading%> + <%:Loading modem information%>... +
+
+
+ + + +
+ +<%+footer%> diff --git a/luci-app-modem/luasrc/view/modem/modem_info.htm b/luci-app-modem/luasrc/view/modem/modem_info.htm index 41a1605..12db86c 100644 --- a/luci-app-modem/luasrc/view/modem/modem_info.htm +++ b/luci-app-modem/luasrc/view/modem/modem_info.htm @@ -28,17 +28,17 @@ end border: 0px; } - +