--下面列出了常用的回调函数
--更多功能请阅读<<物联型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_tcp        = 5

local select_ssid = ""
local select_psw  = ""

local server_ip   = '192.168.155.1'
local server_port = '8080'

--扫描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_sendToserver(sendData)
	
	local send_buff = {}	
	local send_len  = string.len(sendData)
	
	for i = 1, send_len 
	do 
		send_buff[i - 1] = tonumber(string.byte(sendData, i, i))
	end
	
	client_send_data(send_buff)
end

function on_client_recv_data(packet)
	
	local client_rec = string.format("%02X",packet[0])

	for i= 1,#(packet) do
		client_rec = client_rec .." ".. string.format("%02X",packet[i])
	end

	set_text(sc_tcp,5,client_rec)
	
	return 1
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
		play_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
	
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_tcp
	then
		if control == 3
		then
			local sendData = get_text(sc_tcp, 3)
			my_sendToserver(sendData)
			
		elseif control == 7
		then
			server_ip = get_text(sc_tcp, 7)
			local wificom = 0
			local mode    = 1
			set_network_service_cfg(wificom, mode, server_port, server_ip)
			save_network_cfg()
			
		elseif control == 9 and value == 0
		then
			local wificom, mode, port, server_addr = get_network_service_cfg()
			server_ip   = server_addr
			server_port = port
			
			set_text(sc_tcp,  7, server_ip)
			set_text(sc_tcp, 13, server_port)
			
		elseif control == 13
		then
			local wificom = 0
			local mode    = 1
			server_port   = get_text(sc_tcp, 13)
			set_network_service_cfg(wificom, mode, server_port, server_ip)
			save_network_cfg() 
		end
	end
end

