Chuyển tới nội dung chính

API khởi chạy kịch bản từ xa

  • Mô tả giao diện

    POST /spawn HTTP/1.1
    spawn_args: {"đối số khởi chạy kịch bản": ...}

    [nội dung kịch bản]
  • Phản hồi có thể có

    {"code":1,"message":"操作失败"}
    {"code":2,"message":"脚本有语法错误","detail":"chi tiết lỗi cụ thể"}
    {"code":3,"message":"已经有脚本正在运行中"}
    {"code":233,"message":"未知错误"}
  • Lưu ý

    • spawn_args là chuỗi có thể in và không chứa ký tự xuống dòng
    • [nội dung kịch bản] có thể là văn bản thuần hoặc dữ liệu nhị phân đã mã hóa
    • Kịch bản có thể đọc đối số khởi chạy qua proc_get("spawn_args") hoặc proc_take("spawn_args")
    • Kịch bản không được lưu thành tệp trên thiết bị mà chỉ chạy trong bộ nhớ
    • Ngoại lệ: khi bật chế độ tiến trình nền cho kịch bản, thiết bị có thể lưu đệm dữ liệu kịch bản gắn với thiết bị đó
  • Ví dụ

    Ví dụ về yêu cầu HTTP gửi đến thiết bị:

    POST /spawn HTTP/1.1
    spawn_args: {"server_ip":"192.168.31.13","port":55555}
    Content-Length: 123

    local args = proc_get("spawn_args")
    args = json.decode(args)
    sys.alert("IP máy chủ: "..args.server_ip.."\nCổng: "..args.port)

    Thiết bị sẽ hiển thị cảnh báo:

    IP máy chủ: 192.168.31.13
    Cổng: 55555

    Ví dụ Python 3.x:

    # -*- coding: utf-8 -*-

    import http.client
    import json

    spawn_args = {
    "server_ip": "192.168.31.13",
    "port": 55555,
    }

    data = r'''
    local args = proc_get("spawn_args")
    args = json.decode(args)
    sys.alert("IP máy chủ: "..args.server_ip.."\nCổng: "..args.port)
    '''

    headers = {
    "Content-type": "text/lua",
    "spawn_args": json.dumps(spawn_args),
    }

    conn = http.client.HTTPConnection("192.168.31.72:46952")
    conn.request("POST", "/spawn", data.encode('utf-8'), headers)

    response = conn.getresponse()
    print(response.status, response.reason)

    data = response.read()
    print(data.decode('utf-8'))

    conn.close()