local sn_counter = 0

local c_dir = "C:/DCIOT.PKG"
local u_dir = "d:/"
local ssid = 0
flag = 0

local screen0_main    = 0
local screen1_setting = 1
local screen2_scan    = 2
local screen3_http    = 3
--初始化设备状态



function on_systick()
     
    wifi_connect = get_network_state()                                      --获取网络状态
	if wifi_connect~=0
	then
      set_text(screen1_setting,1,'连接成功')             
	end
	
	--显示当前连接的WIFI
	wifimode,secumode,ssid,password = get_wifi_cfg()                       --获取WIFI配置
	dhcp,ipaddr = get_network_cfg()                                        --获取网络配置
	 
	if wifi_connect>0
	then
	   set_text(screen1_setting,3,ssid)                                                 --显示WIFI信息
	   set_text(screen1_setting,2,ipaddr)
	else
       set_text(screen1_setting,3,' ')
	   set_text(screen1_setting,2,' ')	   
	end		
end

--初始化函数
function on_init()

end


function on_timer(timer_id)  
	if timer_id == 0 then
		sn_counter = sn_counter +1
		if sn_counter >3 then
			sn_counter = 1
		end
		set_value(screen3_http,3,sn_counter)
	end
end

--扫描wifi与显示
function scan_ap_fill_list()
    ap_cnt = scan_ap()                                                       --扫描可用热点
	  
	for i=1,ap_cnt do
	  ssid,security,quality = get_ap_info(i-1)                               --获取信息
	  set_text(screen2_scan,i,ssid)                                                     --显示id
	end
	
	for i=ap_cnt,10 do
	   set_text(screen2_scan,i,"")                                                     --清空后面的
	end
end



--控制控件
function on_control_notify(screen,control,value) 
  
	 --点击扫描热点
	 if screen==screen1_setting and control==7 or 
	    screen==screen2_scan and control==21
	  then
	    scan_ap_fill_list()
	  end
	  
	 if screen==screen1_setting and control==8   and value == 1                                             --保存设置
	 then
	   ssid = get_text(screen1_setting,4)
	   psw = get_text(screen1_setting,5)  
	   set_wifi_cfg(screen1_setting,0,ssid,psw)                                               --1网卡模式，0自动识别加密
	   save_network_cfg();
	   set_text(screen1_setting,1,'连接中...')
	 end  

	 --选取热点
	 if screen==screen2_scan and control>=11 and control<=20 and value == 1           
	 then
	    ssid = get_text(screen2_scan,(control-10))                                      --文本控件从1~10
		set_text(screen1_setting,4,ssid)
	 end

 if screen==screen3_http and control == 1 and value == 1                                                            
	then
		set_value(screen3_http,3,1)
		start_timer(0,1000,1,0)
		set_visiable(screen3_http,2,0)
		play_animation(screen3_http,4)	
		http_download(1,'http://video.gz-dc.com/DCIOT1.PKG',c_dir)       --替换正确的链接，例程的链接举例子
	end	
end
function on_http_download (taskid, status)
	if taskid == 1 then
		if status == 0 then				--下载失败
			stop_timer(0)
			set_value(3,3,4)
			set_visiable(screen3_http,2,1)
			stop_animation(screen3_http,4)
		elseif status == 1 then      --下载成功，存储失败
			stop_timer(0)
			set_value(3,3,5)
			set_visiable(screen3_http,2,1)
			stop_animation(screen3_http,4)
		elseif status == 2 then      --下载成功，存储成功
			set_value(4,3,6)
			os.exit()             --重启
			set_visiable(screen3_http,2,1)
			stop_animation(screen3_http,4)
		end
	end
end
function on_screen_change(screen)

end

