tsb_wlan_page.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. --[[
  2. @module tsb_wlan_page
  3. @summary 网络配置页面
  4. @version 1.0
  5. @date 2026.03.17
  6. @author 李一玮
  7. @usage
  8. 本文件是网络配置页面,展示网络配置的各种用法。
  9. ]]
  10. local tsb_wlan_page = {}
  11. -- 页面UI元素
  12. local main_container = nil
  13. local common_ui = require("tsb_common_page")
  14. local net_manager = require("net_manager") -- 引入网络管理器
  15. -- 网络状态UI元素(需要在事件回调中访问)
  16. local network4g_status = nil
  17. local network4g_switch = nil
  18. local wifi_status = nil
  19. local wifi_switch = nil -- WiFi开关按钮
  20. local ip_address_label = nil -- IP地址标签
  21. local signal_strength_label = nil -- 信号强度标签
  22. -- 创建UI
  23. function tsb_wlan_page.create_ui()
  24. main_container = airui.container({
  25. x = 0,
  26. y = 0,
  27. w = 480,
  28. h = 320,
  29. color = 0xFFFFFF,
  30. })
  31. --------------------- 标题栏 ------------------------
  32. local title_bar = airui.container({
  33. parent = main_container,
  34. x = 0,
  35. y = 0,
  36. w = 480,
  37. h = 30,
  38. color = 0xCE93D8,
  39. })
  40. airui.label({
  41. parent = title_bar,
  42. text = "网络设置",
  43. x = 200,
  44. y = 10,
  45. w = 80,
  46. h = 20,
  47. font_size = 16,
  48. color = 0xFFFFFF,
  49. })
  50. -- 标题栏公共信息展示
  51. common_ui.add_battery_display(title_bar)
  52. common_ui.create_back_button(title_bar, tsb_wlan_page.cleanup)
  53. ---------------------------------------------------
  54. -- 滚动容器
  55. local scroll_container = airui.container({
  56. parent = main_container,
  57. x = 0,
  58. y = 30,
  59. w = 480,
  60. h = 260,
  61. color = 0xFFFFFF,
  62. })
  63. local keyboard1 = airui.keyboard({
  64. x = 0,
  65. y = 0,
  66. w = 480,
  67. h = 160, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算
  68. mode = "lower", -- 键盘模式,可选 "text"/"upper"/"lower"/"numeric"
  69. auto_hide = true, -- 自动隐藏键盘
  70. preview = true,
  71. preview_height = 35,
  72. bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明
  73. on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
  74. log.info("keyboard", "commit")
  75. end
  76. })
  77. --------------------- 4G网络配置 ------------------------
  78. -- 4G网络容器
  79. local network4g_container = airui.container({
  80. parent = scroll_container,
  81. x = 20,
  82. y = 10,
  83. w = 440,
  84. h = 80,
  85. color = 0xF5F5F5,
  86. radius = 8,
  87. })
  88. -- 4G网络标题
  89. airui.label({
  90. parent = network4g_container,
  91. text = "4G网络配置",
  92. x = 10,
  93. y = 10,
  94. w = 120,
  95. h = 30,
  96. font_size = 16,
  97. color = 0xCE93D8,
  98. })
  99. -- 4G开关标签
  100. airui.label({
  101. parent = network4g_container,
  102. text = "4G网络:",
  103. x = 10,
  104. y = 40,
  105. w = 80,
  106. h = 30,
  107. font_size = 14,
  108. })
  109. -- 4G开关状态
  110. network4g_status = airui.label({
  111. parent = network4g_container,
  112. text = "开启",
  113. x = 80,
  114. y = 40,
  115. w = 60,
  116. h = 30,
  117. font_size = 14,
  118. color = 0x4CAF50,
  119. })
  120. -- 4G开关按钮
  121. network4g_switch = airui.button({
  122. parent = network4g_container,
  123. x = 350,
  124. y = 40,
  125. w = 80,
  126. h = 30,
  127. text = "关闭",
  128. on_click = function(self)
  129. local current_status = network4g_status:get_text()
  130. if current_status == "开启" then
  131. network4g_status:set_text("关闭")
  132. network4g_status:set_color(0xF44336)
  133. self:set_text("开启")
  134. log.info("set_4g", "关闭4G网络")
  135. net_manager.disable_mobile() -- 调用net_manager关闭4G
  136. else
  137. network4g_status:set_text("开启")
  138. network4g_status:set_color(0x4CAF50)
  139. self:set_text("关闭")
  140. log.info("set_4g", "开启4G网络")
  141. net_manager.enable_mobile() -- 调用net_manager开启4G
  142. end
  143. end
  144. })
  145. ---------------------------------------------------
  146. --------------------- WIFI配置 ------------------------
  147. -- WIFI配置容器
  148. local wifi_container = airui.container({
  149. parent = scroll_container,
  150. x = 20,
  151. y = 100,
  152. w = 440,
  153. h = 160,
  154. color = 0xF5F5F5,
  155. radius = 8,
  156. })
  157. -- WIFI配置标题
  158. airui.label({
  159. parent = wifi_container,
  160. text = "WIFI配置",
  161. x = 10,
  162. y = 10,
  163. w = 100,
  164. h = 30,
  165. font_size = 16,
  166. color = 0xCE93D8,
  167. })
  168. -- WIFI开关标签
  169. airui.label({
  170. parent = wifi_container,
  171. text = "WIFI:",
  172. x = 10,
  173. y = 40,
  174. w = 60,
  175. h = 30,
  176. font_size = 14,
  177. })
  178. -- WIFI开关状态
  179. wifi_status = airui.label({
  180. parent = wifi_container,
  181. text = "关闭",
  182. x = 80,
  183. y = 40,
  184. w = 60,
  185. h = 30,
  186. font_size = 14,
  187. color = 0xF44336,
  188. })
  189. -- WIFI开关按钮
  190. wifi_switch = airui.button({
  191. parent = wifi_container,
  192. x = 350,
  193. y = 40,
  194. w = 80,
  195. h = 30,
  196. text = "开启",
  197. on_click = function(self)
  198. local current_status = wifi_status:get_text()
  199. if current_status == "开启" then
  200. wifi_status:set_text("关闭")
  201. wifi_status:set_color(0xF44336)
  202. self:set_text("开启")
  203. log.info("set_wifi", "关闭WIFI")
  204. net_manager.disconnect_wifi() -- 调用net_manager断开WiFi
  205. else
  206. wifi_status:set_text("开启")
  207. wifi_status:set_color(0x4CAF50)
  208. self:set_text("关闭")
  209. log.info("set_wifi", "开启WIFI")
  210. -- 这里可以添加开启WIFI的逻辑
  211. end
  212. end
  213. })
  214. -- WIFI名称标签
  215. airui.label({
  216. parent = wifi_container,
  217. text = "WIFI名称:",
  218. x = 10,
  219. y = 80,
  220. w = 100,
  221. h = 30,
  222. font_size = 14,
  223. })
  224. -- WIFI名称输入框
  225. local wifi_name_input = airui.textarea({
  226. parent = wifi_container,
  227. x = 110,
  228. y = 80,
  229. w = 200,
  230. h = 30,
  231. text = "",
  232. keyboard = keyboard1
  233. })
  234. -- WIFI密码标签
  235. airui.label({
  236. parent = wifi_container,
  237. text = "WIFI密码:",
  238. x = 10,
  239. y = 120,
  240. w = 100,
  241. h = 30,
  242. font_size = 14,
  243. })
  244. -- WIFI密码输入框
  245. local wifi_password_input = airui.textarea({
  246. parent = wifi_container,
  247. x = 110,
  248. y = 120,
  249. w = 200,
  250. h = 30,
  251. text = "",
  252. keyboard = keyboard1
  253. })
  254. -- 连接按钮
  255. local connect_wifi_btn = airui.button({
  256. parent = wifi_container,
  257. x = 320,
  258. y = 90,
  259. w = 100,
  260. h = 40,
  261. text = "连接",
  262. on_click = function(self)
  263. local current_wifi_status = wifi_status:get_text()
  264. if current_wifi_status ~= "开启" then
  265. log.info("set_wifi", "请先开启WiFi")
  266. return
  267. end
  268. local wifi_name = wifi_name_input:get_text()
  269. local wifi_password = wifi_password_input:get_text()
  270. if not wifi_name or wifi_name == "" then
  271. log.info("set_wifi", "请输入WiFi名称")
  272. return
  273. end
  274. log.info("set_wifi", "连接WIFI:" .. wifi_name)
  275. net_manager.connect_wifi(wifi_name, wifi_password) -- 调用net_manager连接WiFi
  276. end
  277. })
  278. ---------------------------------------------------
  279. --------------------- 网络状态 ------------------------
  280. -- 网络状态容器
  281. local network_status_container = airui.container({
  282. parent = scroll_container,
  283. x = 20,
  284. y = 280,
  285. w = 440,
  286. h = 100,
  287. color = 0xF5F5F5,
  288. radius = 8,
  289. })
  290. -- 网络状态标题
  291. airui.label({
  292. parent = network_status_container,
  293. text = "网络状态",
  294. x = 10,
  295. y = 10,
  296. w = 100,
  297. h = 30,
  298. font_size = 16,
  299. color = 0xCE93D8,
  300. })
  301. -- IP地址标签
  302. airui.label({
  303. parent = network_status_container,
  304. text = "IP地址:",
  305. x = 10,
  306. y = 40,
  307. w = 80,
  308. h = 30,
  309. font_size = 14,
  310. })
  311. -- IP地址值
  312. ip_address_label = airui.label({
  313. parent = network_status_container,
  314. text = "0.0.0.0",
  315. x = 100,
  316. y = 40,
  317. w = 200,
  318. h = 30,
  319. font_size = 14,
  320. })
  321. -- 信号强度标签
  322. airui.label({
  323. parent = network_status_container,
  324. text = "信号强度:",
  325. x = 10,
  326. y = 70,
  327. w = 80,
  328. h = 30,
  329. font_size = 14,
  330. })
  331. -- 信号强度值
  332. signal_strength_label = airui.label({
  333. parent = network_status_container,
  334. text = "0",
  335. x = 100,
  336. y = 70,
  337. w = 50,
  338. h = 30,
  339. font_size = 14,
  340. })
  341. ---------------------------------------------------
  342. -- 底部信息
  343. common_ui.create_status_bar(main_container)
  344. end
  345. ----- 初始化页面
  346. function tsb_wlan_page.init(params)
  347. tsb_wlan_page.create_ui()
  348. -- 初始化网络管理器(默认启动4G)
  349. net_manager.init()
  350. -- 更新WiFi信息显示
  351. local function update_wifi_info()
  352. local wlan_info = wlan.getInfo()
  353. if wlan_info then
  354. -- 更新IP地址(使用网关作为IP地址显示)
  355. if ip_address_label then
  356. ip_address_label:set_text(wlan_info.gw or "0.0.0.0")
  357. end
  358. -- 更新信号强度
  359. if signal_strength_label then
  360. local rssi = wlan_info.rssi
  361. if rssi then
  362. signal_strength_label:set_text(rssi .. " dBm")
  363. -- 根据信号强度设置颜色
  364. if rssi > -60 then
  365. signal_strength_label:set_color(0x4CAF50) -- 绿色,信号强
  366. elseif rssi >= -80 then
  367. signal_strength_label:set_color(0xFF9800) -- 橙色,信号中等
  368. else
  369. signal_strength_label:set_color(0xF44336) -- 红色,信号差
  370. end
  371. else
  372. signal_strength_label:set_text("0")
  373. end
  374. end
  375. log.info("wlan_page", "WiFi信息更新", json.encode(wlan_info))
  376. end
  377. end
  378. -- 监听网络状态变化,更新UI
  379. sys.subscribe("NET_STATE_CHANGED", function(state)
  380. log.info("wlan_page", "网络状态变化:", state)
  381. if state == net_manager.STATE.WIFI_CONNECTED then
  382. -- WiFi连接成功,更新4G状态显示为关闭
  383. network4g_status:set_text("关闭")
  384. network4g_status:set_color(0xF44336)
  385. network4g_switch:set_text("开启")
  386. -- 更新WiFi信息
  387. update_wifi_info()
  388. elseif state == net_manager.STATE.MOBILE_CONNECTED then
  389. -- 4G连接成功
  390. network4g_status:set_text("开启")
  391. network4g_status:set_color(0x4CAF50)
  392. network4g_switch:set_text("关闭")
  393. -- 关闭WiFi UI状态
  394. if wifi_status then
  395. wifi_status:set_text("关闭")
  396. wifi_status:set_color(0xF44336)
  397. end
  398. if wifi_switch then
  399. wifi_switch:set_text("开启")
  400. end
  401. -- 重置WiFi信息显示
  402. if ip_address_label then
  403. ip_address_label:set_text("0.0.0.0")
  404. end
  405. if signal_strength_label then
  406. signal_strength_label:set_text("0")
  407. signal_strength_label:set_color(0x000000)
  408. end
  409. end
  410. end)
  411. end
  412. -- 清理页面
  413. function tsb_wlan_page.cleanup()
  414. if main_container then
  415. main_container:destroy()
  416. main_container = nil
  417. end
  418. end
  419. return tsb_wlan_page