require "internal_api" local json = require "cjson" require "sys" require "utils" dyutils = require("dyutils") siganl = require 'posix.signal' syswait = require 'posix.sys.wait' unistd = require 'posix.unistd' --[[ /app/config/RUIJIE.json { "baseurl":"http://113.137.24.86:22222", "user":"admin", "password":"ruijie.com" } /etc/init.d/ruijie #!/bin/sh /etc/rc.common START=99 USE_PROCD=1 PROG="lua" start_service () { procd_open_instance procd_set_param respawn 3600 10 0 procd_set_param command "$PROG" /app/fetch_ruijie_wifi_status.lua procd_set_param pidfile /var/run/ruijie.pid procd_set_param limits core="unlimited" procd_close_instance } restart() { stop sleep 1 start } reload_service() { stop sleep 1 start } ]] AP_CONFIG_FILE = "/app/config/RUIJIE.json" ap_conf = nil gw_sn = nil local_mqtt_client = nil function exec_host_cmd(cmd) local result = io.popen(cmd):read("*a") return result end function get_data(base_url, user, passwd, mac) local mac_arr = dyutils.split(mac, ":") local mac_str = string.format("%s%s.%s%s.%s%s", mac_arr[1], mac_arr[2], mac_arr[3], mac_arr[4], mac_arr[5], mac_arr[6]) mac_str = string.lower(mac_str) exec_host_cmd("rm /tmp/cookie.txt") local login = string.format( "curl -s -X POST -d 'username=%s&password=%s' -c /tmp/cookie.txt %s/login.php", user, passwd, base_url) exec_host_cmd(login) local get = string.format( "curl -s -X POST -o /tmp/output.txt -d 'command=show+web-api+apmg+ap-config+permit&mode_url=exec&charset=gbk&mode_url=exec&charset=gbk' -b /tmp/cookie.txt %s/newcli.php", base_url) exec_host_cmd(get) path = "/tmp/output.txt" str = io.readFile(path) local ap_info = {} if str ~= nil then ap_info = json.decode(str) if ap_info == nil then print("json format error from file:" .. path) end else print("Got nil from file:" .. path) end path = "/tmp/output.txt" local get = string.format( "curl -s -X POST -o %s -d 'command=show+ac-config+debug+client&mode_url=exec&charset=gbk' -b /tmp/cookie.txt %s/newcli.php", path, base_url) exec_host_cmd(get) local ap_list = ap_info['data']['list'] local ap_name = nil for key, value in pairs(ap_list) do if string.lower(mac_str) == string.lower(value['mac']) then ap_name = value['apname'] end end if ap_name == nil then return nil end -- 读取文件 -- 以只读方式打开文件 local file_input = io.open(path, "r") io.input(file_input) -- 逐行读取文件 sta_table = {} local str = file_input:read("*l") while (str ~= nil) do if string.find(str, ap_name) ~= nil then sta_mac, ip, ap = string.match(str, "(%w+)%s(%w+)%s%w+%s(%w+)") local index = 1 local sta = {} for word in string.gmatch(str, "[%w,:,.,/]+") do -- print(index, word) if index == 1 then sta['sta_mac'] = word elseif index == 2 then sta['sta_ip'] = word elseif index == 4 then sta['apname'] = word elseif index == 5 then sta['radio'] = tonumber(word) elseif index == 14 then sta['rx_rate'] = tonumber(word) elseif index == 15 then sta['tx_rate'] = tonumber(word) end index = index + 1 end table.insert(sta_table, sta) end str = file_input:read("*l") end io.close(file_input) local result = {} for key, value in pairs(ap_list) do if string.lower(mac_str) == string.lower(value['mac']) then result['mac'] = mac sta_info = nil local sta_2g_cnt = 0 local sta_5g_cnt = 0 local rate_2g_tx = 0 local rate_5g_tx = 0 local rate_2g_rx = 0 local rate_5g_rx = 0 for k, sta in pairs(sta_table) do if sta['apname'] == value['apname'] then if sta['radio'] == 1 then sta_2g_cnt = sta_2g_cnt + 1 rate_2g_tx = rate_2g_tx + sta['tx_rate'] rate_2g_rx = rate_2g_rx + sta['rx_rate'] elseif sta['radio'] == 2 then sta_5g_cnt = sta_5g_cnt + 1 rate_5g_tx = rate_5g_tx + sta['tx_rate'] rate_5g_rx = rate_5g_rx + sta['rx_rate'] end end end result['client_number'] = value['stanum'] result['client_number_2g'] = sta_2g_cnt result['client_number_5g'] = sta_5g_cnt result['rx_rate_2g'] = rate_2g_rx result['tx_rate_2g'] = rate_2g_tx result['rx_rate_5g'] = rate_5g_rx result['tx_rate_5g'] = rate_5g_tx end end print(cjson.encode(result)) return result end function load_ap_conf() local str = io.readFile(AP_CONFIG_FILE) if str ~= nil then ap_conf = json.decode(str) end end function get_mac(mac) if ap_conf ~= nil then return get_data(ap_conf['baseurl'], ap_conf['user'], ap_conf['password'], mac) end return nil end local function handler() local pid, status, code = syswait.wait(-1, syswait.WNOHANG) log.error('system exit', pid, status, code) -- 打印不出来 os.exit(0) end local function ruijie_wifi_info(mi, mac) result = get_mac(mac) result['mi'] = mi local topic = string.format( "G/%s/gateway/device/%s/service/Get_RuiJieWiFiStatus", gw_sn, gw_sn) local json_str = cjson.encode(result) internal_api.mqtt_send(local_mqtt_client, topic, json_str) end local function on_mqtt_msg(topic, payload) if topic == subscribe[1] then -- Ruijie WiFi local obj = cjson.decode(payload) ruijie_wifi_info(obj['mi'], obj['mac']) local topic = string.format( "G/%s/gateway/device/%s/service/Get_RuiJieWiFiStatus", gw_sn, gw_sn) end end function main() load_ap_conf() gw_sn = exec_host_cmd( "cat /app/config/fac.ini | grep -i ^sn | awk -F = '{print $2}' | awk '{gsub(/^\\s+|\\s+$/, \"\");print}' | tr '\\n' '\\0'") sub_str = string.format( "M/%s/gateway/device/%s/service/Get_RuiJieWiFiStatus", gw_sn, gw_sn) subscribe = {} table.insert(subscribe, sub_str) local_mqtt_client = internal_api.mqtt_session("localhost", 1883, nil, nil, subscribe, on_mqtt_msg) siganl.signal(siganl.SIGKILL, handler) siganl.signal(siganl.SIGHUP, handler) sys.taskInit(function() while true do sys.wait(2000) end end) -- 启动系统框架 sys.init(0, 0) sys.run() end main()