module("luci.controller.fscan", package.seeall)

function index()
    entry({"admin", "modem", "fscan"}, call("redirect_to_tab1"), _("基站扫描"), 80).dependent = true
    entry({"admin", "modem", "fscan", "fibocom"}, template("fscan/fscan_fibocom"), _("广和通"), 10).dependent = true
    entry({"admin", "modem", "fscan", "quectel"}, template("fscan/fscan_quectel"), _("移远"), 20).dependent = true
    entry({"admin", "modem", "fscan", "switch2"}, call("action_switch2"), nil)
    entry({"admin", "modem", "fscan", "switch3"}, call("action_switch3"), nil)
end

function redirect_to_tab1()
    luci.http.redirect(luci.dispatcher.build_url("admin", "modem", "fscan", "fibocom"))
end

function action_switch2()
    local sys = require "luci.sys"
    local http = require "luci.http"
    local confirm = http.formvalue("confirm")
    
    if confirm and confirm == "yes" then
        os.execute("rm -rf /tmp/cellinfo")
        os.execute("/usr/share/fscan/fscan.sh &")
        http.write_json({ status = "success", message = "Fibocom扫描已启动" })
    else
        http.write_json({ status = "error", message = "确认参数无效" })
    end
end

function action_switch3()
    local sys = require "luci.sys"
    local http = require "luci.http"
    local confirm = http.formvalue("confirm")
    
    if confirm and confirm == "yes" then
        os.execute("rm -rf /tmp/yycellinfo")
        os.execute("/usr/share/fscan/yyfscan.sh &")
        http.write_json({ status = "success", message = "Quectel扫描已启动" })
    else
        http.write_json({ status = "error", message = "确认参数无效" })
    end
end

function parse_results_fibocom()
    local results = {}
    local controller = {}
    -- Read and parse cellinfo file
    local cellinfo = io.open("/tmp/cellinfo", "r")

    if cellinfo then
        for line in cellinfo:lines() do
            local mode, operator, band, earfcn, pci, rsrp, rsrq, cellid = line:match('(%w+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)')
            if mode and operator and earfcn and pci and rsrp and rsrq and cellid then
                table.insert(controller, {
                    mode = mode,
                    operator = operator,
                    band = band,
                    earfcn = earfcn,
                    pci = pci,
                    rsrp = rsrp,
                    rsrq = rsrq,
                    cellid = cellid,
                    lockit = '<button onclick="lockit();" class="cbi-button cbi-button-save">锁定它</button>'
                })
            end
        end
        cellinfo:close()
    else
        table.insert(controller, {
            mode = "wait for ctrl...",
            operator = "",
            band = "",
            earfcn = "",
            pci = "",
            rsrp = "",
            rsrq = "",
            cellid = "",
            lockit = ""
        })
    end
    return controller
end

function parse_results_quectel()
    local results = {}
    local controller = {}
    -- Read and parse cellinfo file
    local cellinfo = io.open("/tmp/yycellinfo", "r")
    if cellinfo then
        for line in cellinfo:lines() do
            local mode, operator, band, earfcn, pci, rsrp, rsrq = line:match('+QSCAN: "(.-)",(.-),(.-),(.-),(.-),(.-),(.+)')
            if mode and operator and earfcn and pci and rsrp and rsrq then
                table.insert(controller, {
                    mode = mode,
                    operator = operator,
                    band = band,
                    earfcn = earfcn,
                    pci = pci,
                    rsrp = rsrp,
                    rsrq = rsrq,
                    lockit = '<button onclick="lockit();" class="cbi-button cbi-button-save">锁定它</button>'
                })
            end
        end
        cellinfo:close()
    else
        table.insert(controller, {
            mode = "wait for ctrl...",
            operator = "",
            band = "",
            earfcn = "",
            pci = "",
            rsrp = "",
            rsrq = "",
            lockit = ""
        })
    end
    return controller
end