Better error handling
This commit is contained in:
		@@ -1,3 +1,22 @@
 | 
				
			|||||||
 | 
					local lastMessage = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function love.draw2()
 | 
					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
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,4 +2,5 @@ function love.conf(t)
 | 
				
			|||||||
	t.version = "11.4"
 | 
						t.version = "11.4"
 | 
				
			||||||
	t.window.title = "Spider Controller"
 | 
						t.window.title = "Spider Controller"
 | 
				
			||||||
	t.window.resizable = true
 | 
						t.window.resizable = true
 | 
				
			||||||
 | 
						t.window.fullscreen = true
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,9 +19,16 @@ function printTable(table, indentation)
 | 
				
			|||||||
	end
 | 
						end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function safeCall(target, ...)
 | 
				
			||||||
 | 
						local success, msg = pcall(target, ...)
 | 
				
			||||||
 | 
						if not success then
 | 
				
			||||||
 | 
							errorMessage = msg
 | 
				
			||||||
 | 
						end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function love.draw(...)
 | 
					function love.draw(...)
 | 
				
			||||||
	if love.draw2 then
 | 
						if love.draw2 and not errorMessage then
 | 
				
			||||||
		love.draw2(...)
 | 
							safeCall(love.draw2, ...)
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		local text = "Awaiting payload..."
 | 
							local text = "Awaiting payload..."
 | 
				
			||||||
		local font = love.graphics.getFont()
 | 
							local font = love.graphics.getFont()
 | 
				
			||||||
@@ -58,11 +65,11 @@ end
 | 
				
			|||||||
function love.update(...)
 | 
					function love.update(...)
 | 
				
			||||||
	local message = mqttEventChannel:pop()
 | 
						local message = mqttEventChannel:pop()
 | 
				
			||||||
	if message then
 | 
						if message then
 | 
				
			||||||
		love.mqtt[message.target](unpack(message.args))
 | 
							safeCall(love.mqtt[message.target], unpack(message.args))
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if love.update2 then
 | 
						if love.update2 and not errorMessage then
 | 
				
			||||||
		love.update2(...)
 | 
							safeCall(love.update2, ...)
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -78,6 +85,7 @@ end
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
function love.mqtt.message2(topic, payload)
 | 
					function love.mqtt.message2(topic, payload)
 | 
				
			||||||
	if topic == "controller/payload" then
 | 
						if topic == "controller/payload" then
 | 
				
			||||||
 | 
							errorMessage = nil
 | 
				
			||||||
		local success = love.filesystem.unmount("client.zip")
 | 
							local success = love.filesystem.unmount("client.zip")
 | 
				
			||||||
		if not success then
 | 
							if not success then
 | 
				
			||||||
			print("Could not unmount client.zip")
 | 
								print("Could not unmount client.zip")
 | 
				
			||||||
@@ -98,7 +106,7 @@ function love.mqtt.message2(topic, payload)
 | 
				
			|||||||
		love.load = nil
 | 
							love.load = nil
 | 
				
			||||||
		chunk()
 | 
							chunk()
 | 
				
			||||||
		if love.load then
 | 
							if love.load then
 | 
				
			||||||
			love.load()
 | 
								safeCall(love.load)
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
	elseif love.mqtt.message then
 | 
						elseif love.mqtt.message then
 | 
				
			||||||
		love.mqtt.message(topic, payload)
 | 
							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