Advertisement
EnterYourName

StorageSensor

Nov 4th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2.  
  3. local channel
  4. local label = os.getComputerLabel()
  5.  
  6. local sensor = sensor.wrap("top")
  7. local modem = peripheral.wrap("right")
  8.  
  9. function getMinecraftTimestamp()
  10.     return string.format("%d:%d:%d", os.day(), os.time(), os.clock())
  11. end
  12.  
  13. if label == nil or label == "" then
  14.     label = getMinecraftTimestamp()
  15.     os.setComputerLabel(label)
  16. end
  17.  
  18. function setupEventHandler()
  19.  
  20.     local _, _, senderChannel, _, message, _ = os.pullEvent("modem_message")
  21.     local table
  22.  
  23.     print("-- Received Event --")
  24.     print("SenderChannel: "..(senderChannel or "nil"))
  25.     print("Message: "..(message or "nil"))
  26.  
  27.     if pcall(function() table = textutils.unserialize(message) end) and senderChannel == 999 and table.id == label then
  28.  
  29.         channel = table.channel
  30.  
  31.     end
  32.  
  33. end
  34.  
  35. function timeout()
  36.     sleep(5)
  37. end
  38.  
  39. function setupChannel()
  40.  
  41.     local attempt = 0
  42.     channel = 0
  43.  
  44.     while channel == 0 do
  45.         modem.open(999)
  46.         attempt = attempt+1
  47.         term.clear()
  48.         term.setCursorPos(1,1)
  49.         print("Setting up communication channel.")
  50.         print("Attempt #"..attempt)
  51.  
  52.         modem.transmit(2,999,label)
  53.  
  54.         parallel.waitForAny(timeout, setupEventHandler)
  55.  
  56.         sleep(0.1)
  57.     end
  58.  
  59.     modem.close(999)
  60.     modem.open(channel)
  61.     print("Received channel: "..channel)
  62.  
  63. end
  64.  
  65. setupChannel()
  66.  
  67.  
  68. local target
  69.  
  70. for k,_ in pairs(sensor.getTargets()) do
  71.  
  72.     target = k
  73.     break
  74.  
  75. end
  76.  
  77. local heartBeat = false
  78.  
  79. function eventHandler()
  80.  
  81.     while true do
  82.  
  83.         local _, _, _, _, message, _ = os.pullEvent("modem_message")
  84.  
  85.         if message == "Ok" then
  86.             heartBeat = true
  87.             break
  88.         end
  89.  
  90.     end
  91.  
  92.  
  93. end
  94.  
  95. while true do
  96.  
  97.     local toSend = {}
  98.  
  99.     local targetDetails = sensor.getTargetDetails(target)
  100.  
  101.     local items = targetDetails.Items
  102.  
  103.     for _,v in pairs(items) do
  104.  
  105.         local rawname = v.RawName
  106.         local count = v.Size
  107.  
  108.         if toSend[rawname] ~= nil then
  109.             toSend[rawname] = toSend[rawname] + count
  110.         else
  111.             toSend[rawname] = count
  112.         end
  113.  
  114.     end
  115.  
  116.     local str = textutils.serialize(toSend)
  117.  
  118.     modem.transmit(channel, channel, str)
  119.  
  120.     heartBeat = false
  121.     parallel.waitForAny(timeout,eventHandler)
  122.     if not heartBeat then
  123.         modem.close(channel)
  124.         setupChannel()
  125.     end
  126.  
  127.     sleep(2)
  128.  
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement