#!/bin/sh

. ../common/ajax_common


sac_ap_list()
{
    [ "${CGI_grpid}"  != "" ] && args="${args} grpid=${CGI_grpid}"
    [ "${CGI_apname}" != "" ] && args="${args} apname=${CGI_apname}"

    printf "["

    ${FLOWEYE} sac list json=1 ${args}

    printf "]"
}


sac_devtype_list()
{
    ${FLOWEYE} sac list listdevtype=1 | awk \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


load_ap_list()
{
    . ./ajax_sac_object_lib

    printf "{"
    printf "\"newver\":`saclib_apnew_ver`"
    printf ",\"devtype\":`sac_devtype_list`"
    printf ",\"group\":`saclib_group_dict`"
    printf ",\"tmpl\":`saclib_tmpl_dict`"
    printf ",\"ap\":`sac_ap_list`"
    printf "}"
}


load_offap_list()
{
    total=0
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	result=`${FLOWEYE} sacoffap list | grep -i "${CGI_keyword}" | sort -rk9`

	if [ "${result}" = "" ]; then
		echo "{\"total\":0,\"data\":[]}"
		return 0
	fi
    
	printf "{\"data\":["
	
	while read id cfgmagic uptime apname group aptype mac ipv4 birth ltime
	do
		total=$((${total}+1))
		[ ${total} -le ${start} -o ${total} -gt ${end} ] && continue

		printf "${dot}"
		printf "{"
		printf "\"id\":\"${id}\","
		printf "\"cfgmagic\":\"${cfgmagic}\","
		printf "\"uptime\":\"${uptime}\","
		printf "\"apname\":\"`utf8togb2312 ${apname}`\","
		printf "\"group\":\"${group}\","
		printf "\"aptype\":\"${aptype}\","
		printf "\"mac\":\"${mac}\","
		printf "\"ipv4\":\"${ipv4}\","
		printf "\"birth\":\"${birth}\","
		printf "\"ltime\":\"${ltime}\""
		printf "}"
		
		[ "${dot}" = "" ] && dot=","
		
	done << EOF
	${result}
EOF

	printf "],\"total\":%d}" ${total};
}


remove_offline_ap()
{
    for apid in ${CGI_id}
    do
        errmsg=`${FLOWEYE} sacoffap remove apid=${apid}`
        [ $? -ne 0 ] && retjson 1 "${errmsg}"
    done
    
    WEB_LOGGER "ɾPA¼"
    retjson 0 "${LANG_001:=ɹ}"
}



load_ap_group()
{
    . ./ajax_sac_object_lib

    printf "{"
    printf "\"group\":`saclib_group_list`"
    printf ",\"ap\":`sac_ap_list`"
    printf "}"
}


get_ap_chart()
{
    now=$(( (`date +%s`) * 1000))
    rd0=`${FLOWEYE} sac get objtype=radio apid=${CGI_apid} radioid=0 | grep rd_chanuse | cut -d"=" -f2`
    rd1=`${FLOWEYE} sac get objtype=radio apid=${CGI_apid} radioid=1 | grep rd_chanuse | cut -d"=" -f2`

    [ "${rd0}" = "" ] && rd0=0
    [ "${rd1}" = "" ] && rd1=0

    printf "{"
    printf "\"2g\":{\"x\":${now},\"y\":${rd0}},"
    printf "\"5g\":{\"x\":${now},\"y\":${rd1}}"
    printf "}"
}


get_ap_newversion()
{
    VERDIR="/usr/ramdisk/tmp/sac/new/"

	AP_RELEASE=""
	DEV_TYPE=""
	FILE_MD5=""
	FILE_NAME=""
	FILE_SIZE=""
	SFT_DATE=""
	SFT_VER=""
	VER_DESC=""

	VERFILE=`ls ${VERDIR} | grep "${CGI_devtype}.txt"`
	[ "${VERFILE}" != "" -a -f ${VERDIR}/${VERFILE} ] && . ${VERDIR}/${VERFILE}

	printf "{"
	printf "\"AP_RELEASE\":\"${AP_RELEASE}\","
	printf "\"DEV_TYPE\":\"${DEV_TYPE}\","
	printf "\"FILE_MD5\":\"${FILE_MD5}\","
	printf "\"FILE_NAME\":\"${FILE_NAME}\","
	printf "\"FILE_SIZE\":\"${FILE_SIZE}\","
	printf "\"SFT_DATE\":\"${SFT_DATE}\","
	printf "\"SFT_VER\":\"${SFT_VER}\","
	printf "\"VER_DESC\":\"%s\"" `echo ${VER_DESC} | tr " " "/"`
	printf "}"

    ${WEBUICTL} start sac >/dev/null 2>&1 &     #°汾
}


get_ap_detail()
{	
    printf "{"

    for line in `${FLOWEYE} sac get objtype=ap apmac=${CGI_mac}`
    do
        key=`echo ${line} | cut -d"=" -f1`
        val=`echo ${line} | cut -d"=" -f2`

        [ "${key}" = "name" ] && val=`utf8togb2312 ${val}`
        
        printf "${dot}"
        printf "\"${key}\":\"${val}\""

        [ "${dot}" = "" ] && dot=","
    done

    printf "}"
}


get_ap_radio()
{
    ${FLOWEYE} sac get objtype=radio apid=${CGI_apid} radioid=0 | awk -F"=" \
    'BEGIN{
        printf "[{";
        printf "\"name\":\"2G\"";
    }{
        printf ",\"%s\":\"%s\"", $1, $2;
    }END{
        printf "}";
    }'

    ${FLOWEYE} sac get objtype=radio apid=${CGI_apid} radioid=1 | awk -F"=" \
    'BEGIN{
        printf ",{";
        printf "\"name\":\"5G\"";
    }{
        printf ",\"%s\":\"%s\"", $1, $2;
    }END{
        printf "}]";
    }'
}


get_ap_ssid()
{	
    printf "["

    result=`${FLOWEYE} sac list listssid=1 apid=${CGI_apid} rdid=0`

    if [ ${#result} -ge 1 ]; then
        have2g=","
        while read vlan wlanid bssid ssid authmode stanum intf_tx intf_rx mcast_tx mcast_rx sta_tx sta_rx kick reject isolate
        do
            printf "${dot}"
            printf "{"
            printf "\"rdid\":\"2G\","
            printf "\"wlanid\":\"%s\"," ${wlanid}
            printf "\"bssid\":\"%s\"," ${bssid}
            printf "\"ssid\":\"%s\"," "`utf8togb2312 ${ssid} | sed 's/#/ /g'`"
            printf "\"authmode\":\"%s\"," ${authmode}
            printf "\"vlan\":%d," ${vlan}
            printf "\"stanum\":%d," ${stanum}
            printf "\"isolate\":%d," ${isolate}
            printf "\"intf_tx\":%d," ${intf_tx}
            printf "\"intf_rx\":%d," ${intf_rx}
            printf "\"mcast_tx\":%d," ${mcast_tx}
            printf "\"mcast_rx\":%d," ${mcast_rx}
            printf "\"sta_tx\":%d," ${sta_tx}
            printf "\"sta_rx\":%d," ${sta_rx}
            printf "\"kick\":%d," ${kick}
            printf "\"reject\":%d," ${reject}
            printf "\"isolate\":%d" ${isolate}
            printf "}"
            [ "${dot}" = "" ] && dot=","
        done << EOF
        ${result}
EOF
    fi

    result=`${FLOWEYE} sac list listssid=1 apid=${CGI_apid} rdid=1`

    if [ ${#result} -ge 1 ]; then
        while read vlan wlanid bssid ssid authmode stanum intf_tx intf_rx mcast_tx mcast_rx sta_tx sta_rx kick reject isolate
        do
            printf "${have2g}{"
            printf "\"rdid\":\"5G\","
            printf "\"wlanid\":\"%s\"," ${wlanid}
            printf "\"bssid\":\"%s\"," ${bssid}
            printf "\"ssid\":\"%s\"," "`utf8togb2312 ${ssid} | sed 's/#/ /g'`"
            printf "\"authmode\":\"%s\"," ${authmode}
            printf "\"vlan\":%d," ${vlan}
            printf "\"stanum\":%d," ${stanum}
            printf "\"isolate\":%d," ${isolate}
            printf "\"intf_tx\":%d," ${intf_tx}
            printf "\"intf_rx\":%d," ${intf_rx}
            printf "\"mcast_tx\":%d," ${mcast_tx}
            printf "\"mcast_rx\":%d," ${mcast_rx}
            printf "\"sta_tx\":%d," ${sta_tx}
            printf "\"sta_rx\":%d," ${sta_rx}
            printf "\"kick\":%d," ${kick}
            printf "\"reject\":%d," ${reject}
            printf "\"isolate\":%d" ${isolate}
            printf "}"

            [ "${have2g}" = "" ] && have2g=","
        done << EOF
        ${result}
EOF
    fi

    printf "]"
}


load_ap_info()
{
    if [ "${CGI_apid}" != "" -a "${CGI_mac}" = "" ]; then
        CGI_mac=`${FLOWEYE} sac get apid=${CGI_apid} objtype=ap | grep "ap_mac" | cut -d"=" -f2`
    fi

    printf "{"
    printf "\"detail\":`get_ap_detail`"

    if [ "${CGI_apid}" != "" ]; then
        printf ",\"radio\":`get_ap_radio`"
        printf ",\"ssid\":`get_ap_ssid`"
    fi

    printf "}"
}


set_ap_info()
{
    [ "${CGI_password}" != "" ] && cmdstr="pappiw&mgmtpass&user=admin&pass=${CGI_password}"

    if [ "${CGI_vlan}" != "" ]; then
        if [ "${cmdstr}" = "" ]; then
            cmdstr="pappiw&lanvlan&vlan=${CGI_vlan}"
        else
            cmdstr="${cmdstr};pappiw&lanvlan&vlan=${CGI_vlan}"
        fi
    fi

    if [ "${CGI_uprate}" != "" -o "${CGI_downrate}" != "" ]; then
        [ "${CGI_uprate}"  = "" ] && retjson 1 "δ"
        [ "${CGI_downrate}" = "" ] && retjson 1 "δ"

        if [ "${cmdstr}" = "" ]; then
            cmdstr="pappiw&setclntrate&uprate=${CGI_uprate}&downrate=${CGI_downrate}"
        else
            cmdstr="${cmdstr};pappiw&setclntrate&uprate=${CGI_uprate}&downrate=${CGI_downrate}"
        fi
    fi

    if [ "${CGI_mca_tx}" != "" -o "${CGI_mca_rx}" != "" ]; then
        [ "${CGI_mca_tx}"  = "" ] && retjson 1 "δ"
        [ "${CGI_mca_rx}" = "" ] && retjson 1 "δ"

        if [ "${cmdstr}" = "" ]; then
            cmdstr="pappiw&setifrate&src=eth&type=WAN&uppps=${CGI_mca_tx}&dwpps=${CGI_mca_rx}"
        else
            cmdstr="${cmdstr};pappiw&setifrate&src=eth&type=WAN&uppps=${CGI_mca_tx}&dwpps=${CGI_mca_rx}"
        fi
    fi

    if [ "${cmdstr}" != "" ]; then
        ${FLOWEYE} sactask add apidstr=",${CGI_apid}" debugstr="${cmdstr};pappiw&reload;"
    fi

    apname=`gb2312toutf8 ${CGI_name}`
	errmsg=`${FLOWEYE} sac set apid=${CGI_apid} name=${apname} grpid=${CGI_grpid}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        WEB_LOGGER "AP" "apid=${CGI_apid} name=${CGI_name}"
        retjson 0 "ɹ"
    fi
}


set_aps_info()
{
    [ "${CGI_password}" != "" ] && cmdstr="pappiw&mgmtpass&user=admin&pass=${CGI_password}"

    if [ "${CGI_vlan}" != "" ]; then
        if [ "${cmdstr}" = "" ]; then
            cmdstr="pappiw&lanvlan&vlan=${CGI_vlan}"
        else
            cmdstr="${cmdstr};pappiw&lanvlan&vlan=${CGI_vlan}"
        fi
    fi
    
    if [ "${CGI_uprate}" != "" -o "${CGI_downrate}" != "" ]; then
        [ "${CGI_uprate}"  = "" ] && retjson 1 "δ"
        [ "${CGI_downrate}" = "" ] && retjson 1 "δ"

        if [ "${cmdstr}" = "" ]; then
            cmdstr="pappiw&setclntrate&uprate=${CGI_uprate}&downrate=${CGI_downrate}"
        else
            cmdstr="${cmdstr};pappiw&setclntrate&uprate=${CGI_uprate}&downrate=${CGI_downrate}"
        fi
    fi

    if [ "${CGI_mca_tx}" != "" -o "${CGI_mca_rx}" != "" ]; then
        [ "${CGI_mca_tx}"  = "" ] && retjson 1 "δ"
        [ "${CGI_mca_rx}" = "" ] && retjson 1 "δ"

        if [ "${cmdstr}" = "" ]; then
            cmdstr="pappiw&setifrate&src=eth&type=WAN&uppps=${CGI_mca_tx}&dwpps=${CGI_mca_rx}"
        else
            cmdstr="${cmdstr};pappiw&setifrate&src=eth&type=WAN&uppps=${CGI_mca_tx}&dwpps=${CGI_mca_rx}"
        fi
    fi

    if [ "${cmdstr}" != "" ]; then
        ${FLOWEYE} sactask add apidstr=",${CGI_apid}" debugstr="${cmdstr};pappiw&reload;"
    fi

    if [ "${CGI_name}" != "" ]; then
        apname=`gb2312toutf8 ${CGI_name}`
        prefix=1
    fi

    for apid in `echo ${CGI_apid} | tr "," " "`
    do
        cmdargs="apid=${apid}"

        if [ "${CGI_grpid}" != "" ]; then
            cmdargs="${cmdargs} grpid=${CGI_grpid}"
        fi

        if [ "${CGI_name}" != "" ]; then
            name=`awk -v apname=${apname} -v prefix=${prefix} 'BEGIN{printf("%s-%04d", apname, prefix)}'`
            cmdargs="${cmdargs} name=${name}"
            prefix=$((${prefix} + 1))
        fi

        [ "${cmdargs}" = "apid=${apid}" ] && continue
        errmsg=`${FLOWEYE} sac set ${cmdargs}`
    done

    WEB_LOGGER "AP"
    retjson 0 "ɹ"
}


set_ap_base()
{
    #ledon/ledoff/reboot/reset
    cmdstr="op=${CGI_op}"

    . ./ajax_sac_object_lib

    WEB_LOGGER "AP" "action=${CGI_op}"
	sac_task_commit "${cmdstr}"
}


set_ap_ledblink()
{
	[ "${CGI_apidstr}" = "" ] && retjson 1 "INV_APID"
	[ "${CGI_sleep}" = "" ] && retjson 1 "INV_SLEEP"

	LEDBLINK="/usr/ramdisk/app/webui/bin/sac_ledblink"
	sh ${LEDBLINK} ${CGI_apidstr} ${CGI_sleep} >/dev/null 2>&1 &
	
    WEB_LOGGER "AP˸"
	retjson 0 "ɹ"
}


set_ap_speedup()
{
    . ./ajax_sac_object_lib
    
    WEB_LOGGER "APӲ"

    sac_debug_commit "pappiw&setspeedup&state=${CGI_speedup}"
}


set_ap_radio()
{
    cmdstr="op=modrd"

    #Ƶãϰ汾ãҳûõĵط
    if [ "${CGI_rd0_chan_modified}" = "1" -a "${CGI_rd0_chan}" != "" ]; then
        cmdstr="${cmdstr}&rd0_chan=${CGI_rd0_chan}"
    fi
    if [ "${CGI_rd1_chan_modified}" = "1" -a "${CGI_rd1_chan}" != "" ]; then
        cmdstr="${cmdstr}&rd1_chan=${CGI_rd1_chan}"
    fi

    #Ƶ
    if [ "${CGI_rd0_rdcfg}" != "" ]; then
        cmdstr="${cmdstr}&rd0_txpwr=${CGI_rd0_txpwr}&rd0_phymode=${CGI_rd0_phymode}&rd0_enable=${CGI_rd0_enable}"
    fi
    if [ "${CGI_rd1_rdcfg}" != "" ]; then
        cmdstr="${cmdstr}&rd1_txpwr=${CGI_rd1_txpwr}&rd1_phymode=${CGI_rd1_phymode}&rd1_enable=${CGI_rd1_enable}"
    fi

    #ŵ [AP]
    [ "${CGI_rd0_channel}" != "" || "${CGI_rd0_channel}" != "0" ] && cmdstr="${cmdstr}&rd0_chan=${CGI_rd0_channel}"
    [ "${CGI_rd1_channel}" != "" || "${CGI_rd1_channel}" != "0" ] && cmdstr="${cmdstr}&rd1_chan=${CGI_rd1_channel}"

    . ./ajax_sac_object_lib

    WEB_LOGGER "APƵ"
    sac_task_commit "${cmdstr}"
}


set_ap_channel()
{
	#ŵ []
	
    if [ "${CGI_chanstr}" = "" ]; then
        retjson 1 "CMD_ERR"
    else
        cmdstr="op=modrd&chanstr=${CGI_chanstr}"
    fi
	
    . ./ajax_sac_object_lib

    WEB_LOGGER "APŵ"
    sac_task_commit "${cmdstr}"
}


import_ap_channel()
{
    [ ! -f "${CGI_file}" ] && retjson 1 "Ҫϴļ"

    data=`awk -F"," \
    -v apidstr=${CGI_apidstr} \
    'BEGIN{
        i = 1;
        printf "{";
        printf "\"_\":\"_\"";
        sub(/^,/, "", apidstr);
        split(apidstr, apid, ",");
    }{
        if( NR > length(apid) )
            next;
            
        gsub(/\r/, "", $2);
        gsub(/\n/, "", $2);
        printf ",\"2gchain_%d\":\"%s\"", apid[i], $1;
        printf ",\"5gchain_%d\":\"%s\"", apid[i], $2;
        i++;
    }END{
        printf "}";
    }' ${CGI_file}`
    
    rm -rf ${CGI_file}
    retjson 0 "ɹ" "${data}"
}


rmv_ap_ssid()
{
    . ./ajax_sac_object_lib

    CGI_ssidstr=`echo ${CGI_ssidstr} | sed 's/ /#/g'`
    CGI_ssidstr=`gb2312toutf8 ${CGI_ssidstr}`

    WEB_LOGGER "ɾSSID"
    sac_task_commit "op=delwlan&ssidstr=${CGI_ssidstr}"
}


set_ap_upgrade()
{
    cloudip=`nslookup_url "download.panabit.com"`
    CLOUD="https://${cloudip}:8443/appbags/PanabitAP/"

    if [ "${CGI_ver_source}" = "custom" ]; then
        [ "${CGI_ver_link}" = "" ] && retjson 1 "δύԶ汾Ϣ"
        cmdstr="op=upgrade&custom=1&newverstr=${CGI_ver_link}"
    else
        [ "${CGI_newverstr}" = "" ] && retjson 1 "δύƶ˰汾Ϣ"
        cmdstr="op=upgrade&newverstr=${CGI_newverstr}&cloud=${CLOUD}"
    fi

    . ./ajax_sac_object_lib

    WEB_LOGGER "AP豸"
    sac_task_commit "${cmdstr}"
}


ap_moveto_cloud()
{
    mkdir -p ${TMPDIR}/sac/
    retfile="/usr/ramdisk/tmp/sac/cloud.ret"

    #Ƿƽ̨
    if [ "${CGI_enable}" = "" ]; then
        ${FLOWEYE} sactask add apidstr=-1, debugstr="pappiw&cloudsw&sw=0" >/dev/null 2>&1
        retjson 200 "ɹ"
    else
        ${FLOWEYE} sactask add apidstr=-1, debugstr="pappiw&cloudsw&sw=1" >/dev/null 2>&1
    fi

    # Ǩƽ̨
    API="https://newcloud.panabit.com/Device/sacap2cloud.php?user=${CGI_account}&pwd=${CGI_password}&pjname=${CGI_name}"

    data=""
    num=0

    while read line
    do
        num=$((${num}+1))
        if [ ${num} -eq 100 ]; then
            URL="${API}&apstr=${data}"
            ${CURL} --connect-timeout 20 -sk "${URL}" -o /dev/null
            num=0
            data=""
        fi

        mac=`echo ${line} | cut -d" " -f18 | sed 's/-//g'`
        dev=`echo ${line} | cut -d" " -f2`
        sn=`echo ${line} | cut -d" " -f10`
        rn=`echo ${line} | cut -d" " -f11`
        name=`echo ${line} | cut -d" " -f4 | sed 's/#/%23/g'`
        group=`gb2312toutf8 ${line} | cut -d" " -f5 | sed 's/#/%23/g'`

        data="${data}${mac},${dev},${sn},${rn},${name},${group}%0a"
    done << EOF
`${FLOWEYE} sac list json=0`
EOF

    URL="${API}&apstr=${data}"
    ${CURL} --connect-timeout 20 -sk "${URL}" -o ${retfile}

    [ $? -ne 0 ] && retjson 100 "ʧ"

    if [ -f ${retfile} ] ; then
        print_json_head
        cat ${retfile}
    else
        retjson 100 "ʧ"
    fi
}


ap_moveto_saas()
{
    [ "${CGI_user}" = "" ] && retjson 1 "INV_USER"
    [ "${CGI_pwd}"  = "" ] && retjson 1 "INV_PWD"
    [ "${CGI_phone}" = "" ] && retjson 1 "INV_PHONE"
    [ "${CGI_domain}" = "" ] && retjson 1 "INV_DOMAIN"
    
	mkdir -p ${TMPDIR}/sac/
	retfile="/usr/ramdisk/tmp/sac/saas.ret"

    # ǨSAAS
    API="https://saas.panabit.com/Ap/sacap2saas"

    data=""
    num=0

    post_args="user=${CGI_user}&pwd=${CGI_pwd}&phone=${CGI_phone}&domain=${CGI_domain}"
    
    while read line
    do
        num=$((${num}+1))
        if [ ${num} -eq 100 ]; then
            args="${post_args}&apstr=${data}"
            ${CURL} -sk -d "${args}" -X POST ${API} -o /dev/null
            num=0
            data=""
        fi

        mac=`echo ${line} | cut -d" " -f18 | grep -E "[0-9a-fA-F]{2}(-[0-9a-fA-F]{2}){5}" | sed 's/-/:/g'`
		[ "${mac}" = "" ] && continue

        model=`echo ${line} | cut -d" " -f2 | grep "PAP-"`
		"${model}" = "" -o ${#model} -ne 9 ] && continue

        sn=`echo ${line} | cut -d" " -f10`
		[ ${#sn} -ne 12 ] && continue

        rn=`echo ${line} | cut -d" " -f11`
		[ "${rn}" != "" -a ${#rn} -ne 18 ] && continue

        name=`echo ${line} | cut -d" " -f4`
        name=`gb2312toutf8 ${name} | sed 's/#/%23/g'`

		group=`echo ${line} | cut -d" " -f5`
        group=`gb2312toutf8 ${group} | sed 's/#/%23/g'`

        data="${data}${mac},${model},${sn},${rn},${name},${group}%0a"
    done << EOF
`${FLOWEYE} sac list json=0`
EOF

    args="${post_args}&apstr=${data}"
    ${CURL} -sk -d "${args}" -X POST "${API}" -o ${retfile}

    [ $? -ne 0 ] && retjson 100 ${errmsg}

    if [ -f ${retfile} ] ; then
        print_json_head
        cat ${retfile}
    else
        retjson 100 "ʧ"
    fi
}


export_aplist()
{
    file_name="Panabit_APб.csv"
    file_path="${WEB_DOWNLOAD}/${file_name}"

    mkdir -p ${WEB_DOWNLOAD}

    echo "AP MAC,AP,AP汾,APͺ,AP,APID,к(S/N),(R/N)" > ${file_path}.bak
    ${FLOWEYE} sac list json=0  | cut -d" " -f1,2,4,5,6,10,11,18 | while read apver apdev apname grpname grpid sn rn mac
    do
        #apname=`gb2312toutf8 ${apname}`
        echo "${mac},${apname},${apver},${apdev},${grpname},${grpid},${sn},${rn}" >> ${file_path}.bak
    done

    mv ${file_path}.bak ${file_path}

    WEB_LOGGER "APע"
    retjson 0 "OK" "\"${file_name}\""
}


import_ap_desc()
{
    [ "${CGI_file}" = "" ] && retjson 1 "INV_FILE"

    if [ "${CGI_type}" = "txt" ]; then
        cat ${CGI_file} | tr -ds '\r' ''  | while read name grp mac
        do
            mac=`echo ${mac} | cut -d"=" -f2`
            apid=`${FLOWEYE} sac list | awk -v mac=${mac} '{if($18 == mac) print $3}'`
            [ "${apid}" = "" ] && continue
            errmsg=`${FLOWEYE} sac set apid=${apid} ${name} ${grp}`
            [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        done
    else
        while IFS=',' read -r mac name ver model grpName grp sn rn
        do
            mac=`echo ${mac} | cut -d"=" -f2`
            apid=`${FLOWEYE} sac list | awk -v mac=${mac} '{if($18 == mac) print $3}'`
            [ "${apid}" = "" ] && continue
            errmsg=`${FLOWEYE} sac set apid=${apid} name=${name} grpid=${grp}`
            [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        done << EOF
`cat "${CGI_file}" | tail -n +2 `
EOF
    fi

    WEB_LOGGER "APע"
    retjson 0 "ɹ"
}


import_upgrde()
{
    
    mkdir -p ${DATAPATH}/sac

    # жĿ¼Ƿиð汾ļ
    for file in `find ${DATAPATH}/sac -name "*$CGI_model*"`
    do
        rm -f $file
    done

    mv ${CGI_file} ${DATAPATH}/sac/${CGI_filename}
    
    # ȷ
    ${WEBUICTL} start sac_upgrade_server 2>&1 >/dev/null &

    . /${PGETC}/ifadmin.conf
    link="http://${ADMIN_IP}:65480/${CGI_filename}"
    cmdstr="op=upgrade&custom=1&newverstr=${link}"
    . ./ajax_sac_object_lib

    WEB_LOGGER "AP豸"
    sac_task_commit "${cmdstr}"

    retjson 0 "ɹ"
}


load_upgrade_list()
{
    printf "["
    
    ls ${DATAPATH}/sac | while read file
    do

        size=`ls -lh ${DATAPATH}/sac/$file | awk '{print $5}'`

        printf "${dot}{"
        printf "\"name\":\"${file}\","
        printf "\"size\":\"${size}\""
        printf "}"

        [ "${dot}" = "" ] && dot=","
    done

    printf "]"
}


remove_file()
{
    file_path="${DATAPATH}/sac/${CGI_name}"

    [ `dirname "${file_path}"` != "${DATAPATH}/sac" ] && retjson 1 "INV_FILE"
    [ ! -f ${file_path} ] && retjson 1 "INV_FILE"

    rm -f ${DATAPATH}/sac/${CGI_name}

    retjson 0 "ɹ"
}


load_upgrade_server()
{
    enable=0

    if [ -f "${PGETC}/web/sac_upgrade_server.conf" ]; then
        . ${PGETC}/web/sac_upgrade_server.conf
    fi

    printf "{"
    printf "\"enable\":\"${enable}\""
    printf "}"
}


set_upgrade_server()
{
    mkdir -p "${PGETC}/web/"

    echo "enable=${CGI_enable}" > ${PGETC}/web/sac_upgrade_server.conf

    ${WEBUICTL} start sac_upgrade_server 2>&1 >/dev/null &
    WEB_LOGGER "SAC" "enable=${CGI_enable}"
    retjson 0 "ɹ"
}


case "${CGI_action}" in
    "sac_ap_list")
        retjson 0 "OK" "`sac_ap_list`"
        ;;
        
    "load_ap_list")
        retjson 0 "OK" "`load_ap_list`"
        ;;

    "get_ap_chart")
        retjson 0 "OK" "`get_ap_chart`"
        ;;

    "get_ap_newversion")
        retjson 0 "OK" "`get_ap_newversion`"
        ;;

    "load_offap_list")
        retjson 0 "OK" "`load_offap_list`"
        ;;

    "remove_offline_ap")
        action_check
        remove_offline_ap
        ;;

    "load_ap_group")
        retjson 0 "OK" "`load_ap_group`"
        ;;

    "load_ap_info")
        retjson 0 "OK" "`load_ap_info`"
        ;;

    "set_ap_info")
        action_check
        set_ap_info
        ;;

    "set_aps_info")
        action_check
        set_aps_info
        ;;

    "set_ap_base")
        action_check
        set_ap_base
        ;;

    "set_ap_ledblink")
        action_check
        set_ap_ledblink
        ;;

    "set_ap_speedup")
        action_check
        set_ap_speedup
        ;;

    "set_ap_radio")
        action_check
        set_ap_radio
        ;;

    "set_ap_channel")
        action_check
        set_ap_channel
        ;;

    "import_ap_channel")
        action_check
        import_ap_channel
        ;;

    "rmv_ap_ssid")
        action_check
        rmv_ap_ssid
        ;;

    "set_ap_upgrade")
        action_check
        set_ap_upgrade
        ;;
        
    "ap_moveto_cloud")
        action_check
        ap_moveto_cloud
        ;;

    "ap_moveto_saas")
        action_check
        ap_moveto_saas
        ;;
        
    "export_aplist")
        action_check
        export_aplist
        ;;

    "import_ap_desc")
        action_check
        import_ap_desc
        ;;

    "import_upgrde")
        action_check
        import_upgrde
        ;;

    "load_upgrade_list")
        action_check
        retjson 0 "OK" "`load_upgrade_list`"
        ;;

    "remove_file")
        action_check
        remove_file
        ;;

    "load_upgrade_server")
        retjson 0 "OK" "`load_upgrade_server`"
        ;;

    "set_upgrade_server")
        action_check
        set_upgrade_server
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
