Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("/liststore")
- --Find wireless modem
- print("Configuring networking")
- rednet.open("left")
- print("Done!")
- -- Setup database
- print ("Configuring database")
- db = liststore.TodoDB:new()
- db:setDbPath("db.txt")
- print ("Done!")
- function defaultResponse(succ, ret)
- if succ then
- if ret == true then
- return {status = "Success"}
- else
- return {status = "Error", msg = ret}
- end
- else
- return {status = "Error", msg = "Catastrophic runtime error", err = ret}
- end
- end
- handlers = {}
- function handlers.ping(message)
- return {status = "Success", msg = "pong"}
- end
- function handlers.addItem(message)
- local succ, ret = pcall(db.addItem, db, message.name, message.priority)
- return defaultResponse(succ, ret)
- end
- function handlers.renameItem(message)
- local succ, ret = pcall(db.addItem, db, message.from, message.to)
- return defaultResponse(succ, ret)
- end
- function handlers.modifyAttribute(message)
- local succ, ret = pcall(db.addItemAttribute, db, message.attribute, message.value)
- return defaultResponse(succ, ret)
- end
- function handlers.removeItem(message)
- local succ, ret = pcall(db.removeItem, db, message.name)
- return defaultResponse(succ, ret)
- end
- function handlers.listItems(message)
- local succ, ret = pcall(db.getItemNames, db, message.opts)
- if succ then
- return {status = "Success", list = ret}
- else
- return {status = "Error", msg = "Catastrophic runtime error"}
- end
- end
- function handlers.getItem(message)
- local succ, ret = pcall(db.removeItem, db, message.name)
- if succ then
- if ret == true then
- return {status = "Success", msg = ret}
- else
- return {status = "Error", msg = ret}
- end
- else
- return {status = "Error", msg = "Catastrophic runtime error"}
- end
- end
- function handleMessage(message)
- if handlers[message.command] == nil then
- return {status = "Error", msg = "Command \"" .. "\" is unknown" }
- end
- return handlers[message.command](message)
- end
- while true do
- -- Main loop
- local senderId, messageRaw, protocol = rednet.receive("todo")
- local message = textutils.unserialise(messageRaw)
- response = handleMessage(message)
- rednet.send(senderId, textutils.serialise(response))
- print("Handled message")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement