Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ocs/apis/sensor")
- local channel
- local label = os.getComputerLabel()
- local sensor = sensor.wrap("top")
- local modem = peripheral.wrap("right")
- function getMinecraftTimestamp()
- return string.format("%d:%d:%d", os.day(), os.time(), os.clock())
- end
- if label == nil or label == "" then
- label = getMinecraftTimestamp()
- os.setComputerLabel(label)
- end
- function setupEventHandler()
- local _, _, senderChannel, _, message, _ = os.pullEvent("modem_message")
- local table
- print("-- Received Event --")
- print("SenderChannel: "..(senderChannel or "nil"))
- print("Message: "..(message or "nil"))
- if pcall(function() table = textutils.unserialize(message) end) and senderChannel == 999 and table.id == label then
- channel = table.channel
- end
- end
- function timeout()
- sleep(5)
- end
- function setupChannel()
- local attempt = 0
- channel = 0
- while channel == 0 do
- modem.open(999)
- attempt = attempt+1
- term.clear()
- term.setCursorPos(1,1)
- print("Setting up communication channel.")
- print("Attempt #"..attempt)
- modem.transmit(2,999,label)
- parallel.waitForAny(timeout, setupEventHandler)
- sleep(0.1)
- end
- modem.close(999)
- modem.open(channel)
- print("Received channel: "..channel)
- end
- setupChannel()
- local target
- for k,_ in pairs(sensor.getTargets()) do
- target = k
- break
- end
- local heartBeat = false
- function eventHandler()
- while true do
- local _, _, _, _, message, _ = os.pullEvent("modem_message")
- if message == "Ok" then
- heartBeat = true
- break
- end
- end
- end
- while true do
- local toSend = {}
- local targetDetails = sensor.getTargetDetails(target)
- local items = targetDetails.Items
- for _,v in pairs(items) do
- local rawname = v.RawName
- local count = v.Size
- if toSend[rawname] ~= nil then
- toSend[rawname] = toSend[rawname] + count
- else
- toSend[rawname] = count
- end
- end
- local str = textutils.serialize(toSend)
- modem.transmit(channel, channel, str)
- heartBeat = false
- parallel.waitForAny(timeout,eventHandler)
- if not heartBeat then
- modem.close(channel)
- setupChannel()
- end
- sleep(2)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement