--下面列出了常用的回调函数
--更多功能请阅读<<物联型LUA脚本API.pdf>>
local cjson = require "cjson"

local sc_home       = 0
local sc_wifiset    = 1
local sc_scanwifi   = 2
local sc_wificonnet = 3
local sc_dhcpset    = 4
local sc_udp        = 5


local sockfd    = - 1                     -- UDP套接字
local prot      = 8080                    -- 端口号
local udp_ip    = '172.21.168.1'          -- UDP 的IP

local udp_send  = {}                      -- UDP 发送缓存区
local recv_str  = ''

--扫描wifi与显示
function my_scan_wifi()
	
	local ap_cnt = scan_ap()

	for i = 1, ap_cnt 
	do
		local ssid, security, quality = get_ap_info(i - 1)
		record_add(sc_scanwifi, 1, ssid..";"..quality..";")
	end
end

function my_udp_sendto(toudp_ip,toprot,toudp_send)
	
	local send_buff = {}	
	local send_len  = string.len(toudp_send)
	
	if sockfd == -1 
	then
		sockfd = udp_create(toprot)
		print("Lua_debugMsg: my_udp_sendto()  toprot ->"..toprot)
	end
	
	print("Lua_debugMsg: my_udp_sendto()  sockfd ->"..sockfd)
	
	if sockfd ~= -1 
	then
		
		for i = 1, send_len 
		do 
			send_buff[i - 1] = tonumber(string.byte(toudp_send, i, i))
		end
		
		udp_sendto(sockfd,toudp_ip,toprot,send_buff)
	end
end


function myudp_recvfrom(recvprot)
    
	
	if sockfd == -1 
	then
       sockfd = udp_create(recvprot)
	end

	if sockfd ~= -1
	then
		local recv_ret    = -1
		local recv_ip     = ''
		local recv_port   = 0
		local recv_packer = {}
		
		recv_ret,recv_ip,recv_port,recv_packer = udp_recvfrom(sockfd)
		
		if recv_ret == -1
		then
			set_text(sc_udp, 3, "recv data lenght -> "..recv_ret)
			set_text(sc_udp, 4, "xxx.xxx.xxx.xxx")
			set_text(sc_udp, 5, "xxxx")
			set_text(sc_udp, 6, "--")
		
		elseif recv_ret == 0
		then
			set_text(sc_udp, 3, "recv data lenght -> "..recv_ret)
			set_text(sc_udp, 4, "xxx.xxx.xxx.xxx")
			set_text(sc_udp, 5, "xxxx")
			set_text(sc_udp, 6, "--")
			
		else
			set_text(sc_udp, 3, "recv data lenght -> "..recv_ret)
			set_text(sc_udp, 4, recv_ip)
			set_text(sc_udp, 5, recv_port)
			
			for i = 0,#(recv_packer)
			do
				recv_str = recv_str..string.char(recv_packer[i])
				
			end
			
			print("Lua_debugMsg: UDP recv packer  ->"..recv_str)	
			set_text(sc_udp, 6, recv_str)
			recv_str = ''
		end
	end

	--udp_close(sockfd)
end

--初始化函数
function on_init()

	set_text(sc_wifiset, 2, "--")
	set_text(sc_wifiset, 5, "xxx.xxx.xxx.xxx")
	set_text(sc_wifiset, 7, "xxxxxxxxxxxx")
end

--系统回调函数，每秒执行一次
function on_systick()

	local text_secumode = ""
	local wifi_connect = get_network_state()
	local wifimode, secumode, ssid, password  = get_wifi_cfg()
	local dhcp, ipaddr, netmask, gateway, dns = get_network_cfg()
	
	--获取屏幕的MAC地址
	set_text(sc_wifiset, 7, get_wifi_mac())

	--有效WIFI名称长度，有设置WIFI连接
	if string.len(ssid) > 0
	then
		--已连接
		if wifi_connect ~= 0
		then
			if secumode == 0 
			then
				text_secumode = "(AUTO)"
			elseif secumode == 1 
			then
				text_secumode = "(WEP)"
			elseif secumode == 2
			then
				text_secumode = "(WPAPSK)"
			elseif secumode == 3
			then
				text_secumode = "(WPAPSK2)"
			end

			--停止播放动画，显示已连接的WIFI
			stop_animation(sc_home, 3)
			set_value(sc_home, 3, 4)
			set_text(sc_home, 4, ssid .. text_secumode)

			set_text(sc_wifiset, 2, ssid .. text_secumode)
			set_text(sc_wifiset, 5, ipaddr)
			set_text(sc_dhcpset, 4, ipaddr)
		
		--连接中
		else
			--播放动画，显示连接中
			play_animation(sc_home, 3)
			set_text(sc_home, 4, "连接 “" .. ssid .. "”中...")

			set_text(sc_wifiset, 2, "连接 “" .. ssid .. "”中...")
			set_text(sc_dhcpset, 4, "xxx.xxx.xxx.xxx")
			set_text(sc_wifiset, 5, "xxx.xxx.xxx.xxx")
		end
		
	--没设置WIFI连接
	else
		paly_animation(sc_home, 3)
		set_text(sc_home, 4, '未连接')

		set_text(sc_wifiset, 2, '未连接')
		set_text(sc_wifiset, 5, "xxx.xxx.xxx.xxx")
		set_text(sc_dhcpset, 4, "xxx.xxx.xxx.xxx")
	end

	myudp_recvfrom(prot)
	
	
end

--用户通过触摸修改控件后，执行此回调函数。
--点击按钮控件，修改文本控件、修改滑动条都会触发此事件。
function on_control_notify(screen,control,value)
	
	if screen == sc_wifiset
	then
		if control == 3 and value == 1
		then
			record_clear(sc_scanwifi, 1) 
			my_scan_wifi()
		end

	elseif screen == sc_scanwifi 
	then
		if control == 1 
		then
			
			local token = 0x3B
			local get_wifi_info = record_read(sc_scanwifi, control, value)
			
			local path_len = string.len(get_wifi_info)
			for char_index = 1, path_len 
			do
				if  token == tonumber(string.byte(get_wifi_info, char_index, char_index))
				then
					select_ssid = string.sub(get_wifi_info, 1, (char_index - 1))
					set_text(sc_wificonnet, 3, "请输入“"..select_ssid.."”的密码")
					set_text(sc_wificonnet, 4, "")
					break;
				end
			end
			
			change_child_screen(sc_wificonnet)
			
		elseif control == 2 and value == 1
		then
			record_clear(sc_scanwifi, 1) 
			my_scan_wifi()
		end
	
	
	elseif screen == sc_wificonnet
	then
		if control == 2
		then
			select_psw = get_text(sc_wificonnet, 4)
			set_wifi_cfg(1, 0, select_ssid, select_psw)
			save_network_cfg()                                                     --保存网络配置                   
			change_screen(sc_wifiset)
		end

	elseif screen == sc_dhcpset 
	then                                                       
		if control == 2 
		then
			local dhcp, ipaddr, netmask, gateway, dns = get_network_cfg()
			if value == 1     
			then
				set_text(sc_dhcpset, 4, ipaddr)  
				set_visiable(sc_dhcpset, 5, 1) 
				
				set_network_cfg(1, ipaddr, netmask, gateway, dns) 
				save_network_cfg()	
			else
				set_text(sc_dhcpset, 4, "xxx.xxx.xxx.xxx")
				set_text(sc_wifiset, 5, "xxx.xxx.xxx.xxx")
				set_visiable(sc_dhcpset, 5, 0)  
			end 
			
		elseif control == 5
		then
			return 1
		
		elseif control == 14  and value == 1                                        -- 保存配置                                     
		then
			local dhcp    = 0
			local ipaddr  = get_text(sc_dhcpset, 7)
			local netmask = get_text(sc_dhcpset, 9)
			local gateway = get_text(sc_dhcpset, 11)
			local dns     = get_text(sc_dhcpset, 13)
			
			set_network_cfg(dhcp, ipaddr, netmask, gateway, dns)
			save_network_cfg()	
		end
	
	elseif screen == sc_udp
	then
			
		if control == 1 and value == 0
		then
			--udp 发送
			local send_data = get_text(sc_udp, 2)
			my_udp_sendto(udp_ip,prot,send_data)  
			
		end
	end
end
