Решение простое, почему раньше не додумался — ума не приложу.
Исходная обстановка — делаем таблицу для отправки на брокер. Вариантов два:
data = {
t = 25.0,
hume = 45
}
Второй
data = {
{"t", 25.0},
{"hume", 45}
}
В чем смысл? Пишем обычную функцию отправки данных на брокер и вызываем в callback(е) рекурсивно саму себя. Данные из таблицы извлекаем путем удаления элементов:
tp = table.remove(topub) - Возвращает удаленный элемент.
Как это выглядит?
data = {
{"t", 25.0},
{"hume", 45}
}
-- Если доступен брокер
if mod.broker == true then
-- Функция публикации
local function punow()
-- Если есть данные
if #data ~= 0 then
-- Забираем
local tp = table.remove(data)
-- Топик
local top = tp[1].."/state"
-- Содержание
local dat = tp[2]
-- Публикуем
m:publish(top, dat, 0,0, function(cl)
-- В callback - новый вызов
punow()
end)
end
end
punow()
end
-- Или совсем коротко:
if mod.broker == true then
local function punow()
if #data ~= 0 then
local tp = table.remove(data)
m:publish(tp[1].."/state", tp[2], 0,0, function(cl)
punow()
end)
end
end
punow()
end
И еще вариант:
data = {
t = 25.0,
hume = 45
}
if mod.broker == true then
local topub = {}
for k,v in pairs(data) do
table.insert(topub, {k,v})
end
local function punow()
if #topub ~= 0 then
local tp = table.remove(topub)
m:publish(tp[1].."/state", tp[2], 0,0, function(cl)
punow()
end)
end
end
punow()
end