#!/bin/sh

. ../common/ajax_common

CONFDIR=${PGETC}/App/sac
roam_conf="${CONFDIR}/roam.conf"


load_apuser_list()
{
	[ "${CGI_statype}" = "" ] && CGI_statype="all"
	[ "${CGI_grpid}"   = "" ] && CGI_grpid="-1"

	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100

	args="grpid=${CGI_grpid}"
	
	[ "${CGI_iprange}" != "" ] && args="${args} -ip ${CGI_iprange}"
	[ "${CGI_wire}" = "0" ] && args="${args} ethsta=0"
	[ "${CGI_wire}" = "1" ] && args="${args} wlsta=1"
	
	dot=""
	count=0
	end_num=$((${CGI_page} * ${CGI_limit}))
	beg_num=$((${end_num} - ${CGI_limit}))
	
	CGI_keyword_utf8=`gb2312toutf8 ${CGI_keyword}`

	if [ "${CGI_statype}" = "rssi" ]; then
		sort_type="shell"
		sort_col="-rk 16"
	elif [ "${CGI_statype}" = "onlinetm" ]; then
		sort_type="shell"
		sort_col="-rk 3"
	elif [ "${CGI_statype}" = "apname" ]; then
		sort_type="shell"
		sort_col="-k 13"
	fi

	if [ "${sort_type}" = "shell" ]; then
		result=`${FLOWEYE} sacsta list liststa=1 ${args} | sort ${sort_col} | grep -iE "${CGI_keyword}|${CGI_keyword_utf8}"`
	else
		result=`${FLOWEYE} sacsta list liststa=1 ${args} -s ${CGI_statype} | grep -iE "${CGI_keyword}|${CGI_keyword_utf8}"`
	fi
	
	if [ "${result}" = "" ]; then
		echo "{\"count\":0,\"data\":[]}"
		return 0
	fi

	printf "{\"data\":["
	while read tag ip onlinetm rdidx ttl flowcnt mac inbytes outbytes inbps outbps ipname apname ssid oui rssi jump rate pmode
	do
		apname=`utf8togb2312 ${apname}`

		[ "${CGI_apname}" != "" -a "${CGI_apname}" != "${apname}" ] && continue
		[ "${CGI_rdidx}" != "" -a "${rdidx}" != "${CGI_rdidx}" ] && continue

		count=$((${count}+1))
		[ ${count} -le ${beg_num} -o ${count} -gt ${end_num} ] && continue

		printf "${dot}"
		printf "{"
		printf "\"ipaddr\":\"%s\"," ${ip}
		printf "\"onlinetm\":\"%s\"," ${onlinetm}
		printf "\"rdidx\":\"%s\"," ${rdidx}
		printf "\"ttl\":\"%s\"," ${ttl}
		printf "\"flowcnt\":\"%s\"," ${flowcnt}
		printf "\"mac\":\"%s\"," ${mac}
		printf "\"inbytes\":%d," ${inbytes}
		printf "\"outbytes\":%d," ${outbytes}
		printf "\"inbps\":%d," ${inbps}
		printf "\"outbps\":%d," ${outbps}
		printf "\"ipname\":\"%s\"," ${ipname}
		printf "\"apname\":\"%s\"," ${apname}
		printf "\"ssid\":\"%s\"," "`utf8togb2312 ${ssid} | sed 's/#/ /g'`"
		printf "\"oui\":\"%s\"," ${oui}
		printf "\"rssi\":%d," ${rssi}
		printf "\"jump\":\"%s\"," ${jump}
		printf "\"rate\":\"%s\"," ${rate}
		printf "\"pmode\":\"%s\"" ${pmode}
		printf "}"

		[ "${dot}" = "" ] && dot=","
	done  << EOF
	${result}
EOF
	printf "],\"count\":%d}" ${count};
}


load_roam_conf()
{
	enable=""
	rssi=70
	rate=3
	keep=10
	sleeps=10

	[ -f "${roam_conf}" ] && . ${roam_conf}

	printf "{"
	printf "\"enable\":\"${enable}\","
	printf "\"rssi\":\"${rssi}\","
	printf "\"rate\":\"${rate}\","
	printf "\"keep\":\"${keep}\","
	printf "\"sleeps\":\"${sleeps}\""
	printf "}"
}


set_roam_conf()
{
	mkdir -p ${CONFDIR}
	roam_conf="${CONFDIR}/roam.conf"
	
	echo "enable=${CGI_enable}" > ${roam_conf}
	echo "rssi=${CGI_rssi}" >> ${roam_conf}
	echo "rate=${CGI_rate}" >> ${roam_conf}
	echo "keep=${CGI_keep}" >> ${roam_conf}
	echo "sleeps=${CGI_sleeps}" >> ${roam_conf}
	
	${WEBUICTL} start sac
	
	retjson 0 "ɹ"
}


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

    "load_roam_conf")
        retjson 0 "OK" "`load_roam_conf`"
        ;;

    "set_roam_conf")
        action_check
        set_roam_conf
        ;;
		
    *)
        retjson 1 "UNKNOW_ACTION!"
        ;;
esac
