Advertisement
Justin8303

eu_sensor

Jul 18th, 2020 (edited)
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. local modem_Side = "front"
  2. local deployer_Side = "right"
  3. local pipe_Side = "bottom"
  4. local messure_speed = 10
  5.  
  6. print("Computer ID: " .. os.getComputerID())
  7. rednet.open(modem_Side)
  8.  
  9. function convertNBT(nbt)
  10.     local conv = {}
  11.     if (nbt == nil) then
  12.         return nil
  13.     elseif (nbt["type"] == "NBTTagCompound") or (nbt["type"] == "NBTTagList") then
  14.         for key, value in pairs(nbt["value"]) do
  15.             conv[key] = convertNBT(value)
  16.         end
  17.     else
  18.         conv = nbt["value"]
  19.     end
  20.     return conv
  21. end
  22.  
  23.  
  24. function getNBT(side)
  25.     local pipe = peripheral.wrap(side)
  26.     pipe.getAvailableItems()
  27.     local event, items = os.pullEvent("available_items_return")
  28.    
  29.     for k,v in pairs(items) do
  30.         id = pipe.getItemID(v[1])
  31.         if id == 30182 then
  32.             local nbt = convertNBT(pipe.getNBTTagCompound(v[1]))           
  33.             if (nbt ~= nil) then
  34.                 return nbt
  35.             end
  36.         end
  37.     end
  38.  
  39.     sleep(messure_speed)
  40.     return getNBT(side)
  41. end
  42.  
  43.  
  44. function messure()
  45.     rs.setOutput(deployer_Side,true)
  46.     sleep(.5)
  47.     rs.setOutput(deployer_Side,false)
  48.     sleep(.5)
  49.     return getNBT(pipe_Side)
  50. end
  51.  
  52. function calculate()
  53.     count = 0
  54.    
  55.     while true do
  56.         if count > 0 then
  57.             local nbt = messure()
  58.             i2 = nbt["lastTotalEnergyEmitted"]
  59.             t2 = nbt["lastMeasureTime"]
  60.             local i = (i2 - i1)
  61.             local t = (t2 - t1)
  62.             print("Energie: "..i/t.."eu/t")
  63.             pkg = {}
  64.             table.insert(pkg,i/t)
  65.             rednet.broadcast(textutils.serialize(pkg))
  66.             count = 0
  67.         else
  68.             sleep(messure_speed)
  69.             local nbt = messure()
  70.             i1 = nbt["lastTotalEnergyEmitted"]
  71.             t1 = nbt["lastMeasureTime"]
  72.             count = 1
  73.         end
  74.     end
  75. end
  76.  
  77. calculate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement