Files
pitts-touchscreen/3B/archive/root_11.bak/run_mqtt.lua

131 lines
3.2 KiB
Lua

-- LUA default run file
-- LUA default run file
require "env"
MQTT = require("mqtt")
Scheduler = require('scheduler')
LOGING = 1
local function errLog(msg)
io.stderr:write(msg)
end
function logPrint(msg,aux)
if ( LOGING ) then
if aux then
print(msg.." , "..aux)
else
print(msg);
end
end
end
function dbgPrint(msg,aux)
if ( DEBUG ) then
if aux then
print(msg.." , "..aux)
else
print(msg);
end
end
end
MQTT.onConnect (
function()
errLog("MQTT:\t Connect\n");
MQTT.subscribe("+/#")
end
)
MQTT.onMessage (
function(mid, topic, payload)
logPrint("MQTT:\t Message <"..topic..">", payload)
--posY,posX,strData,attrData = string.match(payload,"(%d+)|(%d+)|([^|])|([^|])")
posY,posX,rawData = string.match(payload,"(%d+)|(%d+)|(.+)")
strData=string.sub(rawData,1,string.len(rawData)/2)
attrData=string.sub(rawData,string.len(rawData)/2+2)
print(posY.."|"..posX.."|"..strData.."|"..attrData.."\n")
if posY and posX and strData and attrData then
local len = string.len(strData)
for j=1, len, 1 do
local label = line[posY+1][posX+j]
if attrData:sub(j,j) == 'b' then
label:setTextColor(HMI.Color.black);
label:setBackground(HMI.Color.green);
else
label:setTextColor(HMI.Color.green);
label:setBackground(HMI.Color.black);
end
label:setText(strData:sub(j,j))
label:draw()
end
end
end
)
function setup()
if app.mqttBroker then
MQTT.begin(app.mqttBroker[1])
end
MQTT.loop()
instance = HMI.App.instance;
layout = HMI.Layout(instance,200,48);
mono = HMI.Font("/root/font/mono.ttf",34)
line = {}
for i=1, 6, 1 do
line[i] = {}
for j=1, 40, 1 do
line[i][j] = HMI.Label(layout,5*(j-1),8+4*(i-1),5,4,j%10);
-- line[i] = HMI.Label(layout,0,8+4*(i-1),200,4,"1234567890123456789012345678901234567890");
line[i][j]:setTextColor(HMI.Color.green);
line[i][j]:setBackground(HMI.Color.black);
line[i][j]:setFont(mono)
line[1][j]:draw()
end
end
number = {}
for i=1, 10, 1 do
number[i] = HMI.Cell(layout,1+20*(i-1),0,20,7,i-1);
number[i]:setTextColor(HMI.Color.black);
number[i]:setBackground(HMI.Color.grey);
number[i]:setFont(mono)
end
control = {}
for j=0, 1, 1 do
for i=1, 10, 1 do
control[i+j] = HMI.Cell(layout,1+20*(i-1),34+j*7,20,7,i-1);
control[i+j]:setTextColor(HMI.Color.black);
control[i+j]:setBackground(HMI.Color.grey);
control[i+j]:setFont(mono)
end
end
updateTimer = Scheduler.Timer(500)
cnt = 0;
end
function loop()
--[[
if updateTimer.elapsed() then
line[1]:setText(string.format("%5.2f",cnt))
line[1]:draw()
cnt = cnt + 1
updateTimer.restart()
end
--]]
MQTT.loop()
end
io.stderr:write("Run LUA\n");
--------------------------------------------------