Abstract out request processing functionality

This commit is contained in:
osmarks 2018-05-06 08:28:22 +01:00
parent 01bbdc3a92
commit 84ce642973
4 changed files with 20 additions and 14 deletions

View File

@ -36,7 +36,7 @@ Commands:
w [name] - withdraw all items whose names contain [name] w [name] - withdraw all items whose names contain [name]
w [qty] [name] - withdraw [qty] items whose names contain [name] w [qty] [name] - withdraw [qty] items whose names contain [name]
c - Craft item, using the turtle's inventory as a grid (turtle.craft) c - Craft item, using the turtle's inventory as a grid (turtle.craft)
ac - Atempt to craft item, using Dragon autocrafting. Takes an internal name. ac [item] - Atempt to craft item, using Dragon autocrafting. Takes an internal name.
d - Dump all items into storage d - Dump all items into storage
d [slot] - Dump items in [slot] into storage d [slot] - Dump items in [slot] into storage
r - Force connected storage server to reindex r - Force connected storage server to reindex

View File

@ -68,15 +68,16 @@ local function craft(i)
for _, tsk in pairs(tsks) do for _, tsk in pairs(tsks) do
craftOne(tsk) craftOne(tsk)
end end
return "OK"
else else
return "ERROR" return "ERROR"
end end
end end
while true do while true do
local id, msg = rednet.receive "dragon" util.processMessage(function(m)
if msg and msg.cmd and msg.cmd == "craft" and msg.item then if m.cmd and m.item and m.cmd == "craft" then
craft(msg.item) return craft(m.item)
rednet.send(id, "OK", "dragon") end
end end)
end end

View File

@ -133,15 +133,12 @@ end
function processRequests() function processRequests()
while true do while true do
local id, msg = rednet.receive "dragon" util.processMessage(function(msg)
if msg and msg.cmd and msg.uid then
local ok, r = pcall(processRequest, msg) local ok, r = pcall(processRequest, msg)
if not ok then r = "ERROR" end if not ok then r = "ERROR" end
rednet.send(id, { msg = r, uid = msg.uid }, "dragon") return r
end end)
end
end end
updateIndex() updateIndex()

View File

@ -103,4 +103,12 @@ local function split(str, sSeparator, nMax, bRegexp)
return aRecord return aRecord
end end
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split } local function processMessage(f)
local id, msg = rednet.receive "dragon"
if msg and msg.uid then
local r = f(msg)
rednet.send(id, { uid = msg.uid, msg = r }, "dragon")
end
end
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split, process = process }