httpsrv_start.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. --[[
  2. @module httpsrv_start
  3. @summary Air8101 HTTP服务器应用模块
  4. @version 1.0
  5. @date 2025.10.24
  6. @author 拓毅恒
  7. @usage
  8. 本文件为Air8101核心板演示 HTTP 服务器功能的应用模块,核心业务逻辑为:
  9. 1. 初始化HTTP服务器
  10. 2. 配置HTTP服务器参数
  11. 3. 启动HTTP服务器服务
  12. 4. 处理HTTP请求和响应
  13. ]]
  14. local function handle_http_request(fd, method, uri, headers, body)
  15. log.info("httpsrv", method, uri, body or "")
  16. if uri == "/send/text" then
  17. log.info("Text Request", "Method:", method, "Body Length:", body and #body or 0)
  18. if method == "POST" and body and #body > 0 then
  19. -- 直接打印接收到的文本内容
  20. log.info("Received Text:", body)
  21. return 200, {}, "ok"
  22. end
  23. return 400, {}, "Invalid request"
  24. elseif uri == "/scan/go" then
  25. wlan.scan()
  26. return 200, {}, "ok"
  27. elseif uri == "/scan/list" then
  28. return 200, {["Content-Type"]="application/json"}, (json.encode({data=scan_result, ok=true}))
  29. elseif uri == "/connect" then
  30. if method == "POST" and body and #body > 2 then
  31. local jdata = json.decode(body)
  32. if jdata and jdata.ssid then
  33. sys.timerStart(wlan.connect, 500, jdata.ssid, jdata.passwd)
  34. return 200, {}, "ok"
  35. end
  36. end
  37. return 400, {}, "ok"
  38. elseif uri == "/connok" then
  39. log.info("connok", json.encode({ip=socket.localIP(2)}))
  40. return 200, {["Content-Type"]="application/json"}, json.encode({ip=socket.localIP(2)})
  41. end
  42. return 404, {}, "Not Found" .. uri
  43. end
  44. -- 检测当前使用的网卡适配器
  45. local function get_current_adapter()
  46. -- 检查各个网卡是否就绪
  47. if netdrv and netdrv.ready(socket.LWIP_AP) then
  48. return socket.LWIP_AP, "AP"
  49. elseif netdrv and netdrv.ready(socket.LWIP_STA) then
  50. return socket.LWIP_STA, "STA"
  51. elseif netdrv and netdrv.ready(socket.LWIP_ETH) then
  52. return socket.LWIP_ETH, "ETH"
  53. end
  54. end
  55. local function start_http_server()
  56. -- 等待网络就绪事件
  57. sys.waitUntil("CREATE_OK")
  58. -- 获取当前使用的网卡适配器
  59. local adapter, adapter_name = get_current_adapter()
  60. local local_ip = socket.localIP(adapter)
  61. -- 启动HTTP服务器
  62. httpsrv.start(80, handle_http_request, adapter)
  63. log.info("HTTP", "文件服务器已启动,使用" .. adapter_name .. "模式")
  64. if adapter == socket.LWIP_AP then
  65. log.info("HTTP", "请连接WiFi: luatos8888 密码: 12345678")
  66. end
  67. log.info("HTTP", "访问地址: http://" .. (local_ip or "192.168.4.1"))
  68. end
  69. local function scan_done_handle()
  70. local result = wlan.scanResult()
  71. scan_result = {}
  72. for k, v in pairs(result) do
  73. log.info("scan", (v["ssid"] and #v["ssid"] > 0) and v["ssid"] or "[隐藏SSID]", v["rssi"], (v["bssid"]:toHex()))
  74. if v["ssid"] and #v["ssid"] > 0 then
  75. table.insert(scan_result, {
  76. ssid = v["ssid"],
  77. rssi = v["rssi"]
  78. })
  79. end
  80. end
  81. log.info("scan", "aplist count:", #scan_result)
  82. end
  83. -- 订阅WiFi扫描完成事件
  84. sys.subscribe("WLAN_SCAN_DONE", scan_done_handle)
  85. sys.taskInit(start_http_server)