Advertisement
quenti77

sensor_control

Apr 21st, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. -- Init program
  2. os.loadAPI("ocs/apis/graph")
  3. os.loadAPI("ocs/apis/sensor")
  4.  
  5. rednetSide = "right"
  6. sensorSide = "back"
  7. idScreenPC = 20
  8.  
  9. dataSend = {}
  10. mfsuList = {}
  11.  
  12. function getMFSU()
  13.     mfsuList = {}
  14.     targetNameSelect = {}
  15.     targetNames = sensor.call(sensorSide, "getTargets")
  16.    
  17.     if targetNames then
  18.         -- Adding all targets in the temp list
  19.         for k, value in pairs(targetNames) do
  20.             table.insert(targetNameSelect, k)
  21.         end
  22.         table.sort(targetNameSelect)
  23.        
  24.         -- Adding a target in the mfsu list when the name is "MFSU"
  25.         infoTable = {}
  26.         for i = 1, #targetNameSelect do
  27.             infoTable = sensor.call(sensorSide, "getTargetDetails", targetNameSelect[i])
  28.            
  29.             if infoTable ~= nil then
  30.                 for k, v in pairs(infoTable) do
  31.                     if k == "Name" and v == "MFSU" then
  32.                         table.insert(mfsuList, infoTable)
  33.                     end
  34.                 end
  35.             end
  36.         end
  37.     end
  38. end
  39.  
  40. rednet.open(rednetSide)
  41.  
  42. -- Main loop
  43. while true do
  44.     term.clear()
  45.  
  46.  id, msg, d = rednet.receive(0.5)
  47.  
  48.  if id ~= nil and id == idScreenPC then
  49.   if msg ~= nil then
  50.    action = textutils.unserialize(msg)
  51.  
  52.    if action["button"] ~= nil then
  53.     if action.button == "on_lava" then
  54.      rs.setBundledOutput("left", 1)
  55.      print("1")
  56.     elseif action.button == "off_lava" then
  57.      rs.setBundledOutput("left", 0)
  58.      print("0")
  59.     end
  60.    end
  61.    rednet.send(id, "ok")
  62.   end
  63.  end
  64.    
  65.     -- Getting information sensor
  66.     getMFSU()
  67.    
  68.     for i = 0, (#mfsuList - 1) do
  69.         dataSend[i] = {}
  70.   j = i + 1
  71.         dataSend[i]["p"] = tonumber(mfsuList[j]["StoredPercentage"])
  72.         dataSend[i]["e"] = tonumber(mfsuList[j]["EnergyEmitted"])
  73.     end
  74.    
  75.     -- Message sending
  76.     msgSend = textutils.serialize(dataSend)
  77.     rednet.send(idScreenPC, msgSend)
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement