httpsrv_start.lua 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, "AirPHY_1000-以太网"
  53. elseif netdrv and netdrv.ready(socket.LWIP_USER1) then
  54. return socket.LWIP_USER1, "AirETH_1000-以太网"
  55. end
  56. end
  57. local function start_http_server()
  58. -- 等待网络就绪事件
  59. sys.waitUntil("CREATE_OK")
  60. -- 获取当前使用的网卡适配器
  61. local adapter, adapter_name = get_current_adapter()
  62. local local_ip = socket.localIP(adapter)
  63. -- 启动HTTP服务器
  64. httpsrv.start(80, handle_http_request, adapter)
  65. log.info("HTTP", "文件服务器已启动,使用" .. adapter_name .. "模式")
  66. if adapter == socket.LWIP_AP then
  67. log.info("HTTP", "请连接WiFi: luatos8888 密码: 12345678")
  68. end
  69. log.info("HTTP", "访问地址: http://" .. (local_ip or "192.168.4.1"))
  70. end
  71. local function scan_done_handle()
  72. local result = wlan.scanResult()
  73. scan_result = {}
  74. for k, v in pairs(result) do
  75. log.info("scan", (v["ssid"] and #v["ssid"] > 0) and v["ssid"] or "[隐藏SSID]", v["rssi"], (v["bssid"]:toHex()))
  76. if v["ssid"] and #v["ssid"] > 0 then
  77. table.insert(scan_result, {
  78. ssid = v["ssid"],
  79. rssi = v["rssi"]
  80. })
  81. end
  82. end
  83. log.info("scan", "aplist count:", #scan_result)
  84. end
  85. -- 订阅WiFi扫描完成事件
  86. sys.subscribe("WLAN_SCAN_DONE", scan_done_handle)
  87. sys.taskInit(start_http_server)