1
Jianw
9 天以前 f6f5e6b632d6649386a380558d84003f3de7ec6c
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
43
44
45
46
47
48
49
-- 这个是常量设置为目前我们要调试的 mobox3 服务器的IP+PORT
MOBOX3 = "http://192.168.1.214: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)
  response_body = string.gsub(response_body,  "\13\n", "")
 
  -- 检查请求是否成功
  local err_msg
  if code == 200 then
    local response
    local success
    success, response = pcall( json.decode, response_body )
    if ( success == false ) then 
        return 200, "解析返回的字符串错误: --> "..response_body
    end
 
    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