package.path = package.path .. ';./controller-host/?/init.lua;./controller-host/?.lua'
local mqtt = require("mqtt")
local client

local host = ...

function printTable(table, indentation)
	indentation = indentation or ""
	for name, value in pairs(table) do
		print(indentation .. tostring(name) .. ": " .. tostring(value))
	end
end

local function onMessage(data)
	print(data.payload)
	os.exit()
end

local function onConnect(connack)
	if connack.rc ~= 0 then
		print("Connection to broker failed:", connack:reason_string())
		os.exit(1)
	end

	assert(client:subscribe{
		topic = "host/" .. host
	})
end

client = mqtt.client {
	uri = "mqtt.seeseepuff.be",
	username = "tools",
	clean = true,
	reconnect = 5,
}

client:on {
	connect = onConnect,
	message = onMessage,
}

mqtt.run_ioloop(client)