Better error handling
This commit is contained in:
		@@ -1,3 +1,22 @@
 | 
			
		||||
local lastMessage = 0
 | 
			
		||||
 | 
			
		||||
function love.draw2()
 | 
			
		||||
	love.graphics.setBackgroundColor(0, 1, 0)
 | 
			
		||||
	love.graphics.setBackgroundColor(0, 0, 0)
 | 
			
		||||
 | 
			
		||||
	local time
 | 
			
		||||
	if lastMessage == 0 then
 | 
			
		||||
		time = "Never"
 | 
			
		||||
	else
 | 
			
		||||
		time = love.timer.getTime() - time "s ago"
 | 
			
		||||
	end
 | 
			
		||||
	love.graphics.print("Last message received: " .. time, 5, 5)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function love.load()
 | 
			
		||||
	love.graphics.setFont(love.graphics.newFont(15))
 | 
			
		||||
	love.window.setFullscreen(true)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function love.gamepadpressed(joystick, button)
 | 
			
		||||
	print("Pressed gamepad button " .. button .. " on joystick " .. joystick:getName())
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -2,4 +2,5 @@ function love.conf(t)
 | 
			
		||||
	t.version = "11.4"
 | 
			
		||||
	t.window.title = "Spider Controller"
 | 
			
		||||
	t.window.resizable = true
 | 
			
		||||
	t.window.fullscreen = true
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -19,9 +19,16 @@ function printTable(table, indentation)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function safeCall(target, ...)
 | 
			
		||||
	local success, msg = pcall(target, ...)
 | 
			
		||||
	if not success then
 | 
			
		||||
		errorMessage = msg
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function love.draw(...)
 | 
			
		||||
	if love.draw2 then
 | 
			
		||||
		love.draw2(...)
 | 
			
		||||
	if love.draw2 and not errorMessage then
 | 
			
		||||
		safeCall(love.draw2, ...)
 | 
			
		||||
	else
 | 
			
		||||
		local text = "Awaiting payload..."
 | 
			
		||||
		local font = love.graphics.getFont()
 | 
			
		||||
@@ -58,11 +65,11 @@ end
 | 
			
		||||
function love.update(...)
 | 
			
		||||
	local message = mqttEventChannel:pop()
 | 
			
		||||
	if message then
 | 
			
		||||
		love.mqtt[message.target](unpack(message.args))
 | 
			
		||||
		safeCall(love.mqtt[message.target], unpack(message.args))
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	if love.update2 then
 | 
			
		||||
		love.update2(...)
 | 
			
		||||
	if love.update2 and not errorMessage then
 | 
			
		||||
		safeCall(love.update2, ...)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@@ -78,6 +85,7 @@ end
 | 
			
		||||
 | 
			
		||||
function love.mqtt.message2(topic, payload)
 | 
			
		||||
	if topic == "controller/payload" then
 | 
			
		||||
		errorMessage = nil
 | 
			
		||||
		local success = love.filesystem.unmount("client.zip")
 | 
			
		||||
		if not success then
 | 
			
		||||
			print("Could not unmount client.zip")
 | 
			
		||||
@@ -98,7 +106,7 @@ function love.mqtt.message2(topic, payload)
 | 
			
		||||
		love.load = nil
 | 
			
		||||
		chunk()
 | 
			
		||||
		if love.load then
 | 
			
		||||
			love.load()
 | 
			
		||||
			safeCall(love.load)
 | 
			
		||||
		end
 | 
			
		||||
	elseif love.mqtt.message then
 | 
			
		||||
		love.mqtt.message(topic, payload)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										54
									
								
								sim.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								sim.lua
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
package.path = package.path .. ';./controller-host/?/init.lua;./controller-host/?.lua'
 | 
			
		||||
local mqtt = require("mqtt")
 | 
			
		||||
local client
 | 
			
		||||
 | 
			
		||||
local file = ...
 | 
			
		||||
local fh = io.open(file, "rb")
 | 
			
		||||
local contents = fh:read("a")
 | 
			
		||||
fh:close()
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local function onConnect(connack)
 | 
			
		||||
	if connack.rc ~= 0 then
 | 
			
		||||
		print("Connection to broker failed:", connack:reason_string())
 | 
			
		||||
		os.exit(1)
 | 
			
		||||
	end
 | 
			
		||||
	print("Connected to MQTT")
 | 
			
		||||
 | 
			
		||||
	assert(client:subscribe{
 | 
			
		||||
		topic = "spider/controller/stdout"
 | 
			
		||||
	})
 | 
			
		||||
	
 | 
			
		||||
	io.write("Sending payload...")
 | 
			
		||||
	assert(client:publish {
 | 
			
		||||
		topic = "spider/controller/payload",
 | 
			
		||||
		payload = contents,
 | 
			
		||||
		qos = 0
 | 
			
		||||
	})
 | 
			
		||||
	print(" DONE!")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
client = mqtt.client {
 | 
			
		||||
	uri = "mqtt.seeseepuff.be",
 | 
			
		||||
	username = "mqtt_controller",
 | 
			
		||||
	clean = true,
 | 
			
		||||
	reconnect = 5,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
client:on {
 | 
			
		||||
	connect = onConnect,
 | 
			
		||||
	message = onMessage,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print("Connecting")
 | 
			
		||||
mqtt.run_ioloop(client)
 | 
			
		||||
		Reference in New Issue
	
	Block a user