6.2 内置命名空间

6.2.1 box

网关内置工具

6.2.1.1 获取当前时间

  • 定义: now() -> int64

  • 分类: 函数

  • 描述: 获取毫秒级的时间戳

  • 示例:

    -- 获取当前时间
    local now = box.now()

6.2.1.2 时间格式化

  • 定义: tsformat(time, format) -> string

  • 分类: 函数

  • 参数:

    • time: int64 毫秒级时间戳
    • format: string 时间格式
      • 格式对照表:

        格式 描述 示例
        %a 缩写的星期几名称 Thu
        %A 完整的星期几名称 Thursday
        %b 缩写的月份名称 Aug
        %B 完整的月份名称 August
        %c 如示例 Thu Aug 23 14:55:02 2001
        %C 年份除以100并截断为整数(00-99) 20
        %d 日(01-31) 23
        %D 短日期 MM/DD/YY,等同于 %m/%d/%y 08/23/01
        %e 日,以空格填充(1-31) 23
        %F 短日期 YYYY-MM-DD,等同于 %Y-%m-%d 2001-08-23
        %g 基于周的年份,最后两位数字(00-99) 01
        %G 基于周的年份 2001
        %h 缩写的月份名称 Aug
        %H 24小时制的时 14
        %I 12小时制的时 02
        %j 每年的第几天 235
        %m 月份 08
        %M 分钟 55
        %n 换行
        %p AM/PM PM
        %r 如示例 12小时制时间 02:55:02 pm
        %R 如示例 24小时制时间 14:55
        %S 秒 (00-61) 02
        %t tab字符
        %T 等于 %H:%M:%S 14:55:02
        %u 每的第几天,周一为1 (1-7) 4
        %U 以周天为第一天的每年的第几周 (00-53) 33
        %V ISO 8601 每年的第几周 (01-53) 34
        %w 每周的第几天,周天为0 (0-6) 4
        %W 以周一为第一天的每年的第几周 (00-53) 34
        %x 如示例 月/年/天 08/23/01
        %X 如示例 14:55:02
        %y 年的最后两位 01
        %Y Y年 2001
        %z ISO 8601 基于utc时间偏差 (1 minute=1, 1 hour=100) +100
        %Z 时区名称 CDT
        %% 转义为% %
  • 返回: string 格式化后的时间字符串

  • 描述: 格式化传入的时间戳

  • 示例:

    -- 格式化当前时间
    local now = box.now()
    --将当前时间格式化为"2024-05-27 13:04:05" 这种格式
    local time_str=tsformat(now,"%Y-%m-%d %H:%M:%S")

6.2.1.3 请求网关内置接口

  • 定义: request(url, method, data) -> table

  • 分类: 函数

  • 参数:

    • url: string HTTP 请求的 URL,不包括 host 和 schema 部分
    • method: string HTTP 请求方法,例如 PUT/GET/POST
    • data: table 请求传入的内容,没有则传入 nil
  • 返回: table 返回的状态码和内容

  • 描述: 请求网关内置的所有接口,具体接口详见 HTTP API

  • 示例:

    -- 获取当前网关的设备信息
    local gateway_info = request("/api/gateway","GET","")
    box.log(gateway_info.hardware)

6.2.1.4 获取随机数

  • 定义: random(start, end) -> int

  • 分类: 函数

  • 参数:

    • start: int 起始
    • end: int 结束
  • 返回: int 返回的随机数

  • 描述: 返回大于等于 start 并且小于等于 end 的随机整数

  • 示例:

    -- 获取0-100之间的随机数
    local r=random(0,100)

6.2.1.5 打印日志

  • 定义: log(level, any...)

  • 分类: 函数

  • 参数:

    • level: string 日志级别,包括 trace, debug, info, warn (warning), err (error), critical
    • any: any 任意数量的内容参数,顺序打印
  • 描述: 打印日志

  • 示例:

    -- 打印到日志
    log("info",1,"11",true)
    log("debug","this is a debug info")

6.2.1.6 网关 ID

  • 定义: gateway_id

  • 分类: 属性

  • 类型: string

  • 描述: 网关的 ID,在推送界面配置的

  • 示例:

    -- 返回当前网关的id
    local gatway_id=box.gateway_id

6.2.1.7 网关指纹

  • 定义: fingerprint

  • 分类: 属性

  • 类型: string

  • 描述: 网关的指纹

  • 示例:

    -- 返回当前网关的指纹
    local fingerprint=box.fingerprint

6.2.2 redis

操作 redis 缓存,所有 key 的前缀自动追加 edge_calc_ * 这里的 key 是全网关内唯一的,如果多个设备使用边缘计算,请注意 key 的重复问题。 * 此操作在重启网关后可能会清空,如需持久存储请使用 store。

6.2.2.1 读 key

  • 定义: get(key) -> table

  • 分类: 函数

  • 参数:

    • key: string 需要读取的 key
  • 返回: table 返回获取到的内容,如果为空则返回 nil

  • 描述: 读取 redis 缓存

  • 示例:

    -- 读取redis缓存
    local cache=redis.get("cached_value")

6.2.2.2 写 key

  • 定义: set(key, value)

  • 分类: 函数

  • 参数:

    • key: string 需要写入的 key
    • value: table 需要写入的内容
  • 描述: 写入 redis 缓存

  • 示例:

    -- 写入redis缓存
    redis.set("cached_value",1)

6.2.2.3 删除 key

  • 定义: del(key)

  • 分类: 函数

  • 参数:

    • key: string 需要删除的 key
  • 描述: 删除 redis 缓存

  • 示例:

    -- 删除redis缓存
    redis.del("cached_value")

6.2.3 store

操作网关内置 db 变量,所有 key 的前缀自动追加 edge_calc_ * 这里的 key 是全网关内唯一的,如果多个设备使用边缘计算,请注意 key 的重复问题。 * 此 key 也可以在设备配置页面里操作,实现配置与边缘计算的联动。 * 此操作会持久存储,但性能较差,因为读写硬盘。如无需持久化,请使用 redis。

6.2.3.1 读 key

  • 定义: get(key) -> table

  • 分类: 函数

  • 参数:

    • key: string 需要读取的 key
  • 返回: table 返回获取到的内容,如果为空则返回 nil

  • 描述: 读取 store 缓存

  • 示例:

    -- 写入store缓存
    local cache=store.get("cached_value",1)

6.2.3.2 写 key

  • 定义: set(key, value)

  • 分类: 函数

  • 参数:

    • key: string 需要写入的 key
    • value: table 需要写入的内容
  • 描述: 写入 store 缓存

  • 示例:

    -- 写入store缓存
    store.set("cached_value",1)

6.2.3.3 删除 key

  • 定义: del(key)

  • 分类: 函数

  • 参数:

    • key: string 需要删除的 key
  • 描述: 删除 store 缓存

  • 示例:

    -- 任何地方均可使用
    -- 删除store缓存
    store.del("cached_value")

6.2.4 http

请求HTTP接口

6.2.4.1 公共参数

  • 参数:
    • request: table 请求参数
      • url : string 请求的url
      • params : table 请求的query参数(可为空)
      • headers : table 请求http头(可为空)
      • cookies : table 请求cookie(可为空)
      • form : table 请求的form表单(可为空,仅post可用)
      • body : table/string post的内容(可为空,仅post和put可用)
  • ** 返回**
    • status_code : int 返回的http_code
    • text : string 返回的内容
    • headers : table 返回http头
    • cookies : array[table] 返回cookie
      • name : string cookie名称
      • value : string cookie的内容
      • domain : string 域名
      • path : string 路劲
      • include_subdomain : bool 是否包含子域名
      • http_only : bool 是否http only
      • expires : int 过期时间
    {
        "status_code": 200,
        "text": "hello",
        "headers": {
            "X-Foo": "bar"
        },
        "cookies": {
            [
                "name":"user_id",
                "value":"1"
            ]
        }
    }

6.2.4.2 get

  • 定义: get(request) -> table

  • 分类: 函数

  • 参数: 见公共参数

  • 返回: 见公共参数

  • 描述: HTTP GET 请求

  • 示例:

    local r=http.get({
        url="http://127.0.0.1",
        params={
            foo="bar"
        },
        headers={
            ["X-Data"]="aaaa"
        },
        cookie={
            user_id=1
        }
    })
    box.log("debug",r)

6.2.4.3 post

  • 定义: post(request) -> table

  • 分类: 函数

  • 参数: 见公共参数

  • 返回: 见公共参数

  • 描述: HTTP POST 请求

  • 示例:

    local r1=http.post({
        url="http://127.0.0.1",
        form={
            foo="bar"
        },
    })
    box.log("debug",r1)
    local r2=http.post({
        url="http://127.0.0.1",
        body={
            foo="bar"
        },
    })
    box.log("debug",r2)

6.2.4.4 put

  • 定义: put(request) -> table

  • 分类: 函数

  • 参数: 见公共参数

  • 返回: 见公共参数

  • 描述: HTTP PUT 请求

  • 示例:

    local r=http.put({
        url="http://127.0.0.1",
        body={
            foo="bar"
        },
    })
    box.log("debug",r)

6.2.4.5 delete

  • 定义: delete(request) -> table

  • 分类: 函数

  • 参数: 见公共参数

  • 返回: 见公共参数

  • 描述: HTTP DELETE 请求

  • 示例:

    local r=http.delete({
        url="http://127.0.0.1",
    })
    box.log("debug",r)