fy36
2025-06-24 a3f50e5bd0d5ecbd0c5992da042fe4292dbb6911
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-- 这个是常量设置为目前我们要调试的 mobox3 服务器的IP+PORT
MOBOX3 = "http://124.71.2.100:5103"
 
function call_mobox_epi( api_url, post_data )
  -- 将 POST 数据编码为 JSON 格式
  local post_data_json = json.encode(post_data)
  -- 创建一个表来存储响应数据
  local response_body = {}
 
  local chunks = {}
  local sink = ltn12.sink.table(chunks)
 
  -- 发送 HTTP POST 请求
  local result, code, headers, status_line = http.request{
      url = api_url,
      method = "POST",
      headers = {
          ["Content-Type"] = "application/json",
          ["Content-Length"] = #post_data_json
      },
      source = ltn12.source.string(post_data_json),
      sink = sink
  }
  local response_body = table.concat(chunks)
  
  -- 检查请求是否成功
  local err_msg
  if code == 200 then
      local response = json.decode( response_body )
      if response.err_code == 0 then
        if ( response.result.ret_parameter_num == 1 ) then
            return response.result.code, response.result.value
        else
            return response.result.code, response.result.value, response.result.value2
        end
      end
      err_msg = "mobox3 MOBOX-Lua EPI script error!"..response.err_msg
  else
      err_msg = "Failed to post data. HTTP code: " .. code
  end
  return 1, err_msg    
end