client.lua
49 lines · 1.2 KB
Remote eDig listener
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/eDig/client.lua
| 1 | -- client.lua - Remote eDig listener |
| 2 | local function findModem() |
| 3 | for _, side in pairs(rs.getSides()) do |
| 4 | if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then |
| 5 | return side |
| 6 | end |
| 7 | end |
| 8 | error("No modem found!") |
| 9 | end |
| 10 | |
| 11 | rednet.open(findModem()) |
| 12 | local id = os.getComputerID() |
| 13 | |
| 14 | print("eDig client ready (ID: " .. id .. ")") |
| 15 | print("Waiting for jobs...") |
| 16 | |
| 17 | while true do |
| 18 | local sender, msg, protocol = rednet.receive() |
| 19 | |
| 20 | local cmd = "" |
| 21 | if type(msg) == "table" and msg.command == "RUN" then |
| 22 | cmd = msg.args or "" |
| 23 | elseif type(msg) == "string" then |
| 24 | cmd = msg |
| 25 | end |
| 26 | |
| 27 | if cmd ~= "" then |
| 28 | print("Running: eDig/edig dig " .. cmd) |
| 29 | local ok, err = pcall(function() |
| 30 | -- Try different paths to find the edig program |
| 31 | if fs.exists("eDig/edig") then |
| 32 | shell.run("eDig/edig dig " .. cmd) |
| 33 | elseif fs.exists("edig") then |
| 34 | shell.run("edig dig " .. cmd) |
| 35 | else |
| 36 | error("Could not find edig program") |
| 37 | end |
| 38 | end) |
| 39 | |
| 40 | if ok then |
| 41 | rednet.send(sender, {status = "done", id = id}) |
| 42 | print("Job completed") |
| 43 | else |
| 44 | rednet.send(sender, {status = "error", id = id, error = err}) |
| 45 | print("Job failed: " .. tostring(err)) |
| 46 | end |
| 47 | end |
| 48 | end |
| 49 |