|
-下面列出了常用的回调函数
--更多功能请阅读<<物联型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_test = 5
local select_ssid = ""
local select_psw = ""
local down_test_testinfo = ""
local down_test_task_id_1 = 1
local down_test_task_id_2 = 2
local public_net_task_id_1 = 1000
local down_test_url_1 = 'http://video.gz-dc.com/59a4d2393ff77.jpg'
local down_test_url_2 = 'http://video.gz-dc.com/qj7118866528.jpg'
local public_net_url = 'http://pv.sohu.com/cityjson?ie=utf-8'
local server_url = "http://106.12.59.66/api/qrcode/index"
local surface = 0
local down_dir = "b:/"
function my_test_network()
local show_x = 30
local show_y = 118
local show_w = 420
local show_h = 40
local font_size = 6
local draw_text_color = 0xF800
local align_mode = (0x01 << 2) | (0x00)
local draw_pic_x = 30
local draw_pic_y = 168
local draw_pic_w = 420
local draw_pic_h = 300
draw_text(down_test_testinfo, show_x, show_y, show_w , show_h, font_size, draw_text_color, align_mode)
draw_surface(surface, draw_pic_x, draw_pic_y, draw_pic_w, draw_pic_h)
end
--扫描wifi与显示
function scan_ap_fill_list()
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 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 wifi_connect = get_network_state()
local wifimode, secumode, ssid, password = get_wifi_cfg()
local dhcp, ipaddr, netmask, gateway, dns = get_network_cfg()
set_text(sc_wifiset, 7, get_wifi_mac())
local text_secumode = ""
if string.len(ssid) > 0
then
if wifi_connect ~= 0
then
if secumode == 0
then
text_secumode = "(WEP)"
elseif secumode == 1
then
text_secumode = "(WPA-PSK/WPA2-PSK)"
elseif secumode == 2
then
text_secumode = "(WPA/WPA2)"
end
set_text(sc_wifiset, 2, ssid .. text_secumode)
set_text(sc_wifiset, 5, ipaddr)
set_text(sc_home, 4, ssid .. text_secumode)
set_visiable(sc_home, 2, 0)
set_visiable(sc_home, 3, 1)
set_text(sc_dhcpset, 4, ipaddr)
else
set_text(sc_wifiset, 2, "连接“" .. ssid .. "”中...")
set_text(sc_wifiset, 5, "xxx.xxx.xxx.xxx")
set_text(sc_home, 4, "连接“" .. ssid .. "”中...")
set_visiable(sc_home, 2, 1)
set_visiable(sc_home, 3, 0)
set_text(sc_dhcpset, 4, "xxx.xxx.xxx.xxx")
end
else
set_text(sc_wifiset, 2, '未连接')
set_text(sc_wifiset, 5, "xxx.xxx.xxx.xxx")
set_text(sc_home, 4, '未连接')
set_visiable(sc_home, 2, 1)
set_visiable(sc_home, 3, 0)
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)
scan_ap_fill_list()
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)
scan_ap_fill_list()
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_test
then
if control == 1 and value == 1
then
http_download(down_test_task_id_1, down_test_url_1, down_dir.."down1.jpg")
down_test_testinfo = "Http download1, waiting!!!"
if surface ~= 0
then
destroy_surface(surface)
surface = 0
end
elseif control == 2 and value == 1
then
http_download(down_test_task_id_2, down_test_url_2, down_dir.."down2.jpg")
down_test_testinfo = "Http download2, waiting!!!"
if surface ~= 0 then
destroy_surface(surface)
surface = 0
end
elseif control == 3 and value == 0
then
public_net_url = server_url.."?qid="..read_flash_string(200)..'&time='..os.time()
http_request(public_net_task_id_1, public_net_url, 0)
set_text(sc_test, 4, 'waitting ...')
set_text(sc_test, 10, 'waitting ...')
end
end
end
function on_http_download (taskid, status)
if taskid == down_test_task_id_1
then
if status == 0
then
down_test_testinfo = "Download1 Msg:download failed !!!"
elseif status == 1
then
down_test_testinfo = "Download1 Msg:download success, but save error !!!"
elseif status == 2
then
down_test_testinfo = "Download1 Msg:download and save success"
surface = load_surface(down_dir.."down1.jpg")
redraw()
end
elseif taskid == down_test_task_id_2
then
if status == 0
then
down_test_testinfo = "Download2 Msg:download failed !!!"
elseif status == 1
then
down_test_testinfo = "Download2 Msg:download success, but save error !!!"
elseif status == 2
then
down_test_testinfo = "Download2 Msg:download and save success"
surface = load_surface(down_dir.."down2.jpg")
redraw()
end
end
end
function on_http_response(taskid,response)
print("response ->"..response)
if public_net_task_id_1 == taskid and response ~= ""
then
--[[local data_s = string.find(response, "{", 1)
local data_e = string.find(response, "}", 1)
local CityIpTb = cjson.decode(string.sub(response, data_s, data_e))
print("cip ->"..CityIpTb['cip'])
print("cname ->"..CityIpTb['cname'])
local ip = CityIpTb['cip']
local city = CityIpTb['cname']
set_text(sc_test, 4, city)
set_text(sc_test, 10, ip)--]]
set_text(sc_test,12,response)
local response = cjson.decode(response) --解析JSON数据
set_text(sc_test,11,response['data']['qrcode']..'&time='..os.time())
end
--返回-1时,响应数据会通过串口发送给用户MCU
--return -1
end
function on_draw(screen)
if screen == sc_test
then
my_test_network()
end
end
function on_sd_inserted(dir)
down_dir = dir
end
function on_sd_removed()
down_dir = "b:/"
end
|
|