示例-定时发控制
举例说明
- 定时每个月1日,00:05 执行
local datestr = os.date("%d %H:%M")
if datestr == "01 00:05" then -- 定时每个月1日,00:05 执行
--todo xxx
end
- 定时10:00 执行
local datestr = os.date("%H:%M")
if datestr == "10:00" then -- 定时10:05 执行
--todo xxx
end
- 定时5分钟执行一次
if os.time() % 300 == 0 then -- 定时5分钟(300s)执行一次
--todo xxx
end
完整实例
local log = require("log")
local json = require("json")
local http = require("http")
-------------------------------------------------------------------------------------
--[[
示例,定时发控制
]]
-------------------------------------------------------------------------------------
local prog_name = "下发控制" --程序名称,日志
local object_id = "OBJ3014355407xx" --设备ID(需要设定)
function run()
while true do
local datestr = os.date("%d %H:%M")
if datestr == "01 00:05" then -- 定时每个月1日,00:05 执行
publish_command(object_id, control_id, "1")
end
os.execute("sleep 60s")
end
end
---发控制,经过平台控制码
function publish_command(object_id, control_id, value)
local apidata = {
block_mapping = "pusher",
action = "publish_command",
rule = "never",
object_id = object_id,
command = control_id,
value = value,
}
response, error_message = http.get("http://consul:8500/v1/health/service/mapping")
register = json.decode(response["body"])
url = "http://" .. register[1]["Service"]["Meta"]["api_mode_http"] .. "/api"
response, error_message = http.post(url, {
body=json.encode(apidata)
})
end
function show(msg, info)
local str_info = ""
if type(info) == "table" then
str_info = json.encode(info)
elseif type(info) == "nil" then
str_info = ""
else
str_info = tostring(info)
end
msg = "程序:"..prog_name..";设备ID:"..object_id .. ";[" .. os.date("%Y-%m-%d %H:%M:%S") .. "];" .. msg
log.info(msg ..";" .. str_info)
--log.info(json_info)
end
-------------------------------------------------------------------------------------
run()
文档更新时间: 2024-05-28 11:14 作者:技术支持