From 4936478c86b503338d62edfe25ab27ab1c45c6f6 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Fri, 22 Dec 2023 09:06:47 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=8A=8Atest=E7=9B=AE=E5=BD=95=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E6=8F=90=E4=BA=A4=E4=B8=80=E4=B8=8B,=E5=A5=BD?= =?UTF-8?q?=E4=B9=85=E6=B2=A1=E6=8F=90=E4=BA=A4=E4=BA=86,=E7=B4=AF?= =?UTF-8?q?=E8=AE=A1=E5=88=B021=E4=B8=AA=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/001.helloworld/main.lua | 23 ++- test/002.tcp/main.lua | 4 +- test/004.http/gzip | Bin 0 -> 199 bytes test/004.http/main.lua | 47 ++++- test/004.http/wifi.json | 1 + test/005.mqtt/main.lua | 10 +- test/009.ftp/main.lua | 101 ++++++++++ test/011.onenet_coap/main.lua | 16 +- test/012.sqlite3/main.lua | 12 +- test/013.libgnss/main.lua | 41 ++++ test/014.ntp/main.lua | 16 ++ test/015.lvgl_switch_event/main.lua | 23 +++ test/016.lvgl_chinese/main.lua | 29 +++ test/017.libgnss_hour0/main.lua | 11 ++ test/018.sntp/main.lua | 18 ++ test/019.httplong/main.lua | 288 ++++++++++++++++++++++++++++ test/020.u8g2_buff/main.lua | 33 ++++ test/021.xxtea/main.lua | 32 ++++ 18 files changed, 674 insertions(+), 31 deletions(-) create mode 100644 test/004.http/gzip create mode 100644 test/004.http/wifi.json create mode 100644 test/009.ftp/main.lua create mode 100644 test/013.libgnss/main.lua create mode 100644 test/014.ntp/main.lua create mode 100644 test/015.lvgl_switch_event/main.lua create mode 100644 test/016.lvgl_chinese/main.lua create mode 100644 test/017.libgnss_hour0/main.lua create mode 100644 test/018.sntp/main.lua create mode 100644 test/019.httplong/main.lua create mode 100644 test/020.u8g2_buff/main.lua create mode 100644 test/021.xxtea/main.lua diff --git a/test/001.helloworld/main.lua b/test/001.helloworld/main.lua index 485778d..1e29d7c 100644 --- a/test/001.helloworld/main.lua +++ b/test/001.helloworld/main.lua @@ -1,14 +1,23 @@ _G.sys = require("sys") -print('Go') +-- print('Go') -sys.timerStart(function() - log.info("timer", "timeout once") -end, 1000) +-- sys.timerStart(function() +-- log.info("timer", "timeout once") +-- end, 1000) -sys.timerLoopStart(function() - log.info("timer", "3s repeat") -end, 3000) +-- sys.timerLoopStart(function() +-- log.info("timer", "3s repeat") +-- end, 3000) + +sys.taskInit(function() + while 1 do + sys.wait(1000) + log.info("lua", rtos.meminfo()) + log.info("sys", rtos.meminfo("sys")) + end + +end) sys.run() diff --git a/test/002.tcp/main.lua b/test/002.tcp/main.lua index 26056d4..0d418d1 100644 --- a/test/002.tcp/main.lua +++ b/test/002.tcp/main.lua @@ -1,6 +1,8 @@ _G.sys = require("sys") require "sysplus" +log.info("socket.ip", socket.localIP()) + sys.taskInit(function() sys.wait(100) while 1 do @@ -23,7 +25,7 @@ sys.taskInit(function() log.info("netc", netc) socket.config(netc) socket.debug(netc, true) - socket.connect(netc, "112.125.89.8", 46631) + socket.connect(netc, "112.125.89.8", 41506) while running do sys.wait(100) diff --git a/test/004.http/gzip b/test/004.http/gzip new file mode 100644 index 0000000000000000000000000000000000000000..21304c3c8b135e9acef2d7bbf99b7b8a4dd8f8c0 GIT binary patch literal 199 zcmV;&06702iwFSfK4)bD|Amh|3&JoEhWGx8lCjwI!-C+}q3W)pYfO&x29t7)ikAL& zlNO4b+%5Oqa|iDb#WB+#>^e|v8#{WyDh{j#p@4eN20waGO9`FxGfT3BP0~Ws#z?Es z=&(iw#-V#J&cXK6UF%$TL&F8qy5!OECz1 z@)76>6!d(fs!CM30H4&0 1 order by id desc limit 2") log.info("查询结果", ret, data) if ret then for k, v in pairs(data) do diff --git a/test/013.libgnss/main.lua b/test/013.libgnss/main.lua new file mode 100644 index 0000000..b461f37 --- /dev/null +++ b/test/013.libgnss/main.lua @@ -0,0 +1,41 @@ + +_G.sys = require("sys") + +PROJECT = "logtest" +VERSION = "1.0.0" + +sys.taskInit(function() + local tm = { + year = 2023, + month = 11, + mon = 11, + day = 22, + hour = 15, + min = 30, + sec = 44 + } + local lla = { + lat = "113.5", + lng = "022.5" + } + log.info(">>", json.encode(os.date("!*t"))) + local aid = libgnss.casic_aid(tm, lla) + log.info("AID", aid:toHex()) + -- BACE38000B010000000000605C400000000000803640000000000000000000000000D43A1341 + -- 0000000000000000000000000000000000000000F108 00 23 FD 23 B1 E5 + -- BACE38000B010000000000605C400000000000803640000000000000000000000000D43A1341 + -- 0000000000000000000000000000000060FD0601F108 00 23 5D 21 B8 E6 + -- BACE38000B010000000000605C400000000000803640000000000000000000000000D43A1341 + -- 0000000000000000000000000000000000000000F108 00 23 FD 23 B1 E5 + + local str = "$GNRMC,000625.00,A,3557.35652,N,13854.27058,E,1.681,73.98,111223,,,A,V*32\r\n" + -- libgnss.init() + libgnss.parse(str) + log.info("GNSS", libgnss.getIntLocation()) + log.info("GNSS", 1.681 * 1852) + log.info("GNSS", libgnss.getIntLocation(1)) + log.info("GNSS", libgnss.getIntLocation(2)) + log.info("GNSS", libgnss.getIntLocation(3)) +end) + +sys.run() diff --git a/test/014.ntp/main.lua b/test/014.ntp/main.lua new file mode 100644 index 0000000..effa64a --- /dev/null +++ b/test/014.ntp/main.lua @@ -0,0 +1,16 @@ + +_G.sys = require("sys") + +sys.taskInit(function() + sys.waitUntil("IP_READY") + while 1 do + socket.sntp() + sys.waitUntil("NTP_UPDATE", 10000) + local tm = socket.ntptm() + log.info("tm数据", json.encode(tm)) + log.info("时间戳", string.format("%u.%03d", tm.tsec, tm.tms)) + sys.wait(5000) + end +end) + +sys.run() diff --git a/test/015.lvgl_switch_event/main.lua b/test/015.lvgl_switch_event/main.lua new file mode 100644 index 0000000..ca6d289 --- /dev/null +++ b/test/015.lvgl_switch_event/main.lua @@ -0,0 +1,23 @@ + +sys = require "sys" +log.info("lvgl", lvgl.init()) + +local function event_handler(obj, event) + log.info("event", event) +end + +sys.taskInit(function() + local sw1 = lvgl.switch_create(lvgl.scr_act(), nil); + lvgl.obj_align(sw1, nil, lvgl.ALIGN_CENTER, 0, -50); + lvgl.obj_set_event_cb(sw1, event_handler); + + while 1 do + sys.wait(1000) + lvgl.switch_on(sw1, lvgl.ANIM_ON) + sys.wait(1000) + lvgl.switch_off(sw1, lvgl.ANIM_OFF) + end +end) + +sys.run() + diff --git a/test/016.lvgl_chinese/main.lua b/test/016.lvgl_chinese/main.lua new file mode 100644 index 0000000..d77110b --- /dev/null +++ b/test/016.lvgl_chinese/main.lua @@ -0,0 +1,29 @@ + +sys = require "sys" +log.info("lvgl", lvgl.init()) + +local function event_handler(obj, event) + log.info("event", event) +end + +sys.taskInit(function() + local scr = lvgl.obj_create(nil, nil) + local btn = lvgl.btn_create(scr) + local btn2 = lvgl.btn_create(scr) + lvgl.obj_align(btn, lvgl.scr_act(), lvgl.ALIGN_CENTER, 0, 0) + lvgl.obj_align(btn2, lvgl.scr_act(), lvgl.ALIGN_CENTER, 0, 50) + local label = lvgl.label_create(btn) + local label2 = lvgl.label_create(btn2) + lvgl.label_set_text(label, "LuatOS!") + lvgl.label_set_text(label2, "共和国") + local font = lvgl.font_get("opposans_m_10") + lvgl.obj_set_style_local_text_font(label2, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, font); + + lvgl.scr_load(scr) + + sys.wait(1000) + -- lvgl.obj_set_style_local_text_font(label2, lvgl.LABEL_PART_MAIN, lvgl.STATE_DEFAULT, lvgl.font_get("opposans_m_102")); +end) + +sys.run() + diff --git a/test/017.libgnss_hour0/main.lua b/test/017.libgnss_hour0/main.lua new file mode 100644 index 0000000..001c233 --- /dev/null +++ b/test/017.libgnss_hour0/main.lua @@ -0,0 +1,11 @@ + +sys = require "sys" + +sys.taskInit(function() + local str = "$GNRMC,005814.000,V,,,,,,,251123,,,M,V*24\r\n" + libgnss.parse(str) + log.info("rmc", json.encode(libgnss.getRmc(), "7f")) +end) + +sys.run() + diff --git a/test/018.sntp/main.lua b/test/018.sntp/main.lua new file mode 100644 index 0000000..3350f68 --- /dev/null +++ b/test/018.sntp/main.lua @@ -0,0 +1,18 @@ + +_G.sys = require("sys") + +sys.taskInit(function() + sys.waitUntil("IP_READY") + while 1 do + sys.wait(1000) + socket.sntp({ + "ntp.aliyun.com", + "ntp2.aliyun.com", + "ntp3.aliyun.com", + "ntp4.aliyun.com", + }) + end + +end) + +sys.run() diff --git a/test/019.httplong/main.lua b/test/019.httplong/main.lua new file mode 100644 index 0000000..9fc14c3 --- /dev/null +++ b/test/019.httplong/main.lua @@ -0,0 +1,288 @@ + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "httpdemo" +VERSION = "1.0.0" + +--[[ +本demo需要http库, 大部分能联网的设备都具有这个库 +http也是内置库, 无需require +]] + +-- sys库是标配 +_G.sys = require("sys") +--[[特别注意, 使用http库需要下列语句]] +_G.sysplus = require("sysplus") + + +-- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦 +if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then + pm.power(pm.PWK_MODE, false) +end + + +sys.taskInit(function() + ----------------------------- + -- 统一联网函数, 可自行删减 + ---------------------------- + if wlan and wlan.connect then + -- wifi 联网, ESP32系列均支持 + local ssid = "HJ" + local password = "HJ@88888" + log.info("wifi", ssid, password) + -- TODO 改成esptouch配网 + -- LED = gpio.setup(12, 0, gpio.PULLUP) + wlan.init() + wlan.setMode(wlan.STATION) + wlan.connect(ssid, password, 1) + local result, data = sys.waitUntil("IP_READY", 30000) + log.info("wlan", "IP_READY", result, data) + device_id = wlan.getMac() + elseif rtos.bsp() == "AIR105" then + -- w5500 以太网, 当前仅Air105支持 + w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00) + w5500.config() --默认是DHCP模式 + w5500.bind(socket.ETH0) + -- LED = gpio.setup(62, 0, gpio.PULLUP) + sys.wait(1000) + -- TODO 获取mac地址作为device_id + elseif mobile then + -- Air780E/Air600E系列 + --mobile.simid(2) + -- LED = gpio.setup(27, 0, gpio.PULLUP) + device_id = mobile.imei() + log.info("ipv6", mobile.ipv6(true)) + sys.waitUntil("IP_READY", 30000) + elseif http then + sys.waitUntil("IP_READY") + else + while 1 do + sys.wait(1000) + log.info("http", "当前固件未包含http库") + end + end + log.info("已联网") + sys.publish("net_ready") +end) + +function demo_http_get() + -- 最普通的Http GET请求 + local code, headers, body = http.request("POST", "http://ql.betterforyou.com.cn:9090/ck/app/appUpgrade/findMiniUpgrade?equId=QL10000001&equType=1").wait() + log.info("http.get", code, headers, body) + -- local code, headers, body = http.request("GET", "https://mirrors6.tuna.tsinghua.edu.cn/", nil, nil, {ipv6=true}).wait() + -- log.info("http.get", code, headers, body) + -- sys.wait(100) + -- local code, headers, body = http.request("GET", "https://www.luatos.com/").wait() + -- log.info("http.get", code, headers, body) + + -- 按需打印 + -- code 响应值, 若大于等于 100 为服务器响应, 小于的均为错误代码 + -- headers是个table, 一般作为调试数据存在 + -- body是字符串. 注意lua的字符串是带长度的byte[]/char*, 是可以包含不可见字符的 + -- log.info("http", code, json.encode(headers or {}), #body > 512 and #body or body) +end + +-- function demo_http_post_json() +-- -- POST request 演示 +-- local req_headers = {} +-- req_headers["Content-Type"] = "application/json" +-- local body = json.encode({equId="QL10000001",equType="1"}) +-- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", +-- req_headers, +-- body -- POST请求所需要的body, string, zbuff, file均可 +-- ).wait() +-- log.info("http.post", code, headers, body) +-- end + +-- function demo_http_post_form() +-- -- POST request 演示 +-- local req_headers = {} +-- req_headers["Content-Type"] = "application/x-www-form-urlencoded" +-- local params = { +-- ABC = "123", +-- DEF = 345 +-- } +-- local body = "" +-- for k, v in pairs(params) do +-- body = body .. tostring(k) .. "=" .. tostring(v):urlEncode() .. "&" +-- end +-- local code, headers, body = http.request("POST","http://echohttp.wendal.cn/post", +-- req_headers, +-- body -- POST请求所需要的body, string, zbuff, file均可 +-- ).wait() +-- log.info("http.post.form", code, headers, body) +-- end + +-- -- local function http_download_callback(content_len,body_len,userdata) +-- -- print("http_download_callback",content_len,body_len,userdata) +-- -- end + +-- -- local http_userdata = "123456789" + +-- function demo_http_download() + +-- -- POST and download, task内的同步操作 +-- local opts = {} -- 额外的配置项 +-- opts["dst"] = "/data.bin" -- 下载路径,可选 +-- opts["timeout"] = 30000 -- 超时时长,单位ms,可选 +-- -- opts["adapter"] = socket.ETH0 -- 使用哪个网卡,可选 +-- -- opts["callback"] = http_download_callback +-- -- opts["userdata"] = http_userdata + +-- for k, v in pairs(opts) do +-- print("opts",k,v) +-- end + +-- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date", +-- {}, -- 请求所添加的 headers, 可以是nil +-- "", +-- opts +-- ).wait() +-- log.info("http.post", code, headers, body) -- 只返回code和headers + +-- -- local f = io.open("/data.bin", "rb") +-- -- if f then +-- -- local data = f:read("*a") +-- -- log.info("fs", "data", data, data:toHex()) +-- -- end + +-- -- GET request, 开个task让它自行执行去吧, 不管执行结果了 +-- sys.taskInit(http.request("GET","http://site0.cn/api/httptest/simple/time").wait) +-- end + +-- function demo_http_post_file() +-- -- -- POST multipart/form-data模式 上传文件---手动拼接 +-- local boundary = "----WebKitFormBoundary"..os.time() +-- local req_headers = { +-- ["Content-Type"] = "multipart/form-data; boundary="..boundary, +-- } +-- local body = "--"..boundary.."\r\n".. +-- "Content-Disposition: form-data; name=\"uploadFile\"; filename=\"luatos_uploadFile_TEST01.txt\"".. +-- "\r\nContent-Type: text/plain\r\n\r\n".. +-- "1111http_测试一二三四654zacc\r\n".. +-- "--"..boundary + +-- log.info("headers: ", "\r\n"..json.encode(req_headers)) +-- log.info("body: ", "\r\n"..body) +-- local code, headers, body = http.request("POST","http://airtest.openluat.com:2900/uploadFileToStatic", +-- req_headers, +-- body -- POST请求所需要的body, string, zbuff, file均可 +-- ).wait() +-- log.info("http.post", code, headers, body) + +-- -- 也可用postMultipartFormData(url, params) 上传文件 +-- postMultipartFormData( +-- "http://airtest.openluat.com:2900/uploadFileToStatic", +-- { +-- -- texts = +-- -- { +-- -- ["imei"] = "862991234567890", +-- -- ["time"] = "20180802180345" +-- -- }, + +-- files = +-- { +-- ["uploadFile"] = "/luadb/luatos_uploadFile.txt", +-- } +-- } +-- ) +-- end + + + +sys.taskInit(function() + sys.wait(100) + -- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件 + -- if crypto.cipher_suites then + -- log.info("cipher", "suites", json.encode(crypto.cipher_suites())) + -- end + + ------------------------------------- + -------- HTTP 演示代码 -------------- + ------------------------------------- + sys.waitUntil("net_ready") -- 等联网 + + while 1 do + -- 演示GET请求 + demo_http_get() + -- 表单提交 + -- demo_http_post_form() + -- POST一个json字符串 + -- demo_http_post_json() + -- 上传文件, mulitform形式 + -- demo_http_post_file() + -- 文件下载 + -- demo_http_download() + + sys.wait(1000) + -- 打印一下内存状态 + log.info("sys", rtos.meminfo("sys")) + log.info("lua", rtos.meminfo("lua")) + sys.wait(6000) + end +end) + +---- MultipartForm上传文件 +-- url string 请求URL地址 +-- req_headers table 请求头 +-- params table 需要传输的数据参数 +-- function postMultipartFormData(url, params) +-- local boundary = "----WebKitFormBoundary"..os.time() +-- local req_headers = { +-- ["Content-Type"] = "multipart/form-data; boundary="..boundary, +-- } +-- local body = {} + +-- -- 解析拼接 body +-- for k,v in pairs(params) do +-- if k=="texts" then +-- local bodyText = "" +-- for kk,vv in pairs(v) do +-- print(kk,vv) +-- bodyText = bodyText.."--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"\r\n\r\n"..vv.."\r\n" +-- end +-- table.insert(body, bodyText) +-- elseif k=="files" then +-- local contentType = +-- { +-- txt = "text/plain", -- 文本 +-- jpg = "image/jpeg", -- JPG 格式图片 +-- jpeg = "image/jpeg", -- JPEG 格式图片 +-- png = "image/png", -- PNG 格式图片 +-- gif = "image/gif", -- GIF 格式图片 +-- html = "image/html", -- HTML +-- json = "application/json" -- JSON +-- } + +-- for kk,vv in pairs(v) do +-- if type(vv) == "table" then +-- for i=1, #vv do +-- print(kk,vv[i]) +-- table.insert(body, "--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"; filename=\""..vv[i]:match("[^%/]+%w$").."\"\r\nContent-Type: "..contentType[vv[i]:match("%.(%w+)$")].."\r\n\r\n") +-- table.insert(body, io.readFile(vv[i])) +-- table.insert(body, "\r\n") +-- end +-- else +-- print(kk,vv) +-- table.insert(body, "--"..boundary.."\r\nContent-Disposition: form-data; name=\""..kk.."\"; filename=\""..vv:match("[^%/]+%w$").."\"\r\nContent-Type: "..contentType[vv:match("%.(%w+)$")].."\r\n\r\n") +-- table.insert(body, io.readFile(vv)) +-- table.insert(body, "\r\n") +-- end +-- end +-- end +-- end +-- table.insert(body, "--"..boundary.."--\r\n") +-- body = table.concat(body) +-- log.info("headers: ", "\r\n" .. json.encode(req_headers), type(body)) +-- log.info("body: " .. body:len() .. "\r\n" .. body) +-- local code, headers, body = http.request("POST",url, +-- req_headers, +-- body +-- ).wait() +-- log.info("http.post", code, headers, body) +-- end + + +-- 用户代码已结束--------------------------------------------- +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!! diff --git a/test/020.u8g2_buff/main.lua b/test/020.u8g2_buff/main.lua new file mode 100644 index 0000000..4b655fe --- /dev/null +++ b/test/020.u8g2_buff/main.lua @@ -0,0 +1,33 @@ + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "httpdemo" +VERSION = "1.0.0" + +--[[ +本demo需要http库, 大部分能联网的设备都具有这个库 +http也是内置库, 无需require +]] + +-- sys库是标配 +_G.sys = require("sys") +--[[特别注意, 使用http库需要下列语句]] +_G.sysplus = require("sysplus") + + +sys.taskInit(function() + sys.wait(100) + u8g2.begin({ic = "ssd1306",direction = 0,mode="i2c_hw",i2c_id=1,i2c_speed = i2c.FAST}) + u8g2.DrawUTF8("U8g2+LuatOS", 4, 4) + u8g2.SendBuffer() + local len = u8g2.CopyBuffer() + log.info("缓冲区大小", len) + local buff = zbuff.create(len) + log.info("zbuff", buff) + u8g2.CopyBuffer(buff) + log.info("buff数据", buff:toStr(0, 256):toHex()) +end) + +-- 用户代码已结束--------------------------------------------- +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!! diff --git a/test/021.xxtea/main.lua b/test/021.xxtea/main.lua new file mode 100644 index 0000000..665762e --- /dev/null +++ b/test/021.xxtea/main.lua @@ -0,0 +1,32 @@ + +-- LuaTools需要PROJECT和VERSION这两个信息 +PROJECT = "httpdemo" +VERSION = "1.0.0" + +--[[ +本demo需要http库, 大部分能联网的设备都具有这个库 +http也是内置库, 无需require +]] + +-- sys库是标配 +_G.sys = require("sys") +--[[特别注意, 使用http库需要下列语句]] +_G.sysplus = require("sysplus") + + +sys.taskInit(function() + local text = "Hello World!" + local key = "07946" + local encrypt_data = xxtea.encrypt(text, key) + log.info("testCrypto.xxteaTest","xxtea_encrypt:", encrypt_data:toHex()) + + print("========================================================") + + local decrypt_data = xxtea.decrypt(encrypt_data, key) + log.info("testCrypto.xxteaTest","decrypt_data:", decrypt_data:toHex()) +end) + +-- 用户代码已结束--------------------------------------------- +-- 结尾总是这一句 +sys.run() +-- sys.run()之后后面不要加任何语句!!!!!