8.5 Publisher Platform

IoTBoX supports custom push protocols to cloud platforms

8.5.1 Built-in Variables

  • g: Global table, can be called and modified in any function
  • publisher: Current publisher platform, type is Publisher

8.5.2 Basic Architecture

Minimal code as follows

--@return number 初始化返回 固定为0
---@description Called when the program starts for the first time
---@usage Used to initialize content required for publishing, including push protocol, protocol configuration, etc.
function Init()
    return 0
end

---@return number 推送状态 0为OK,非零为推送失败
---@description Called internally in a loop for pushing data
---@usage Push data after Format
function Push()
    return 0
end

---@return number 格式化返回 固定返回0
---@description Called internally in a loop for data formatting
---@usage The system calls this function for data formatting before pushing data
function Format()
    return 0
end

---@return number 连接状态 0表示连接成功,其他值表示未连接
---@description The system will call the connection function when connection fails
---@usage This function will be called automatically
function Connect()
    return 0
end

---@return number 连接状态 0表示连接成功,其他值表示未连接
---@description Called internally in a loop to check MQTT connection status
---@usage Repeatedly call this function in scenarios where MQTT connection needs to be ensured to monitor connection status
function CheckConnect()
    return 0
end
  1. IoTBoX will call the Init function during initialization to complete the initialization of the publisher platform
  2. After initialization, the Connect function will be called to connect to the cloud platform. Returns 0 if connection succeeds, non-zero if it fails
  3. IoTBoX internally calls the CheckConnect function in a loop. If it returns non-zero, it will call the Connect function to reconnect
  4. IoTBoX will execute the Format function in each push cycle to format the collected data, returns 0
  5. After formatting, the Push function will be called to push data to the cloud platform. Returns 0 if push succeeds, non-zero if it fails

8.5.3 Example MQTT Platform Connection

function on_connect()
    -- Handle after connection
    box.log("info","mqtt服务器已连接")
    g.mqtt:Subscribe(g.control_topic,0)
    init={};
    init["device_id"] = publisher.gateway_id;
    init["device_type"] = "gateway";
    init["online"] = true;
    g.mqtt:PushMessage(cjson.encode(init), g.data_topic,0,0);
end

function on_disconnect()
    -- Handle after disconnection
    box.log("warn","mqtt服务器离线")
end

function on_message(topic,message)
    -- Handle received data
    box.log("debug",topic,message)
end

function Init()
    -- Initialize push
    g.control_topic="control_topic"
    g.data_topic="data_topic"
    g.mqtt=MqttClient()
    g.mqtt.address=config.address;
    if config.username ~=nil and #config.username~=0 then
        g.mqtt.username=config.username
    end
    if config.password ~=nil and #config.password~=0 then
        g.mqtt.password=config.password
    end
    g.mqtt.client_id=box.fingerprint;
    g.mqtt.on_connect=on_connect
    g.mqtt.on_disconnect=on_disconnect
    g.mqtt.on_message=on_message
    will={}
    will["device_id"] = publisher.gateway_id
    will["device_type"] = "gateway";
    will["online"] = false;
    g.mqtt.will_msg=cjson.encode(will)
    g.mqtt.will_topic=g.data_topic
    return 0
end

function Push()
    box.log("debug","aaaaa  push")
    return g.mqtt:PushMessage(cjson.encode(publisher:GetMessage()),g.data_topic,0,0)
end


function Format()
    box.log("debug","aaaaa  format")
    local origin_messages=publisher:GetOriginMessage()
    local result_messages={}
    for i, origin_message in ipairs(origin_messages) do
        local one_message={}
        one_message.device_id=origin_message.device_id
        one_message.device_name=origin_message.device_name
        one_message.device_state=origin_message.device_state
        one_message.device_type=origin_message.device_type
        one_message.ts=origin_message.ts
        one_message.values={}
        local index=1
        for name, value in pairs(origin_message.values) do
            value.name = name;
            value.id = nil;
            one_message.values[index]=value
            index=index+1
        end
        result_messages[i]=one_message
    end
    publisher:SetMessage(result_messages)
    return 0
end
--- 2
function Connect()
    return g.mqtt:Connect()
end
function CheckConnect()
    local r= g.mqtt:CheckConnect()
    return r
end