Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- work in progress program to track turtle status remotely
- local function initNetwork()
- local bOpen, sFreeSide = false, nil
- for n,sSide in pairs(rs.getSides()) do
- if peripheral.getType(sSide) == "modem" then
- sFreeSide = sSide
- if rednet.isOpen(sSide) then
- bOpen = true
- break
- end
- end
- end
- if not bOpen then
- if sFreeSide then
- rednet.open(sFreeSide)
- return true
- else
- return false
- end
- end
- return true
- end
- -- API export
- function setStatus(task, currentIteration, totalIterations, receiverId)
- local isOnline = initNetwork()
- if not isOnline then return end
- info = {
- senderLabel = os.getComputerLabel(),
- task = task or "unknown",
- currentIteration = currentIteration,
- totalIterations = totalIterations
- }
- message = textutils.serialize(info)
- if receiverId == nil then
- rednet.broadcast(message)
- else
- rednet.send(receiverId, message)
- end
- end
- function startHost()
- local isOnline = initNetwork()
- if not isOnline then
- print("Can't establish network connection")
- return
- end
- while true do
- senderId, message, _ = rednet.receive()
- info = textutils.unserialize(message)
- if info.senderLabel ~= nil then
- print("Statusupdate from " .. info.senderLabel .. " (" .. senderId .. ")")
- print("\tTask: " .. info.task)
- if info.currentIteration ~= nil and info.totalIterations ~= nil then
- local progressPercent = math.floor((info.currentIteration / info.totalIterations) * 100)
- print("\tProgress: " .. progressPercent .. "% (" .. info.currentIteration .. " / " .. info.totalIterations .. ")")
- end
- end
- end
- end
- function startClient()
- print("TODO: implement client")
- end
- -- parse program args for non API calls
- local args={ ... }
- if #args >= 1 then
- if args[1] == "host" then
- startHost()
- elseif args[1] == "client" then
- startClient()
- else
- print("Usage: tracking <host|client>")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement