Advertisement
joebodo

tankListener.lua

Jun 3rd, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. vos.loadApi('core.api')
  2. vos.loadApi('relay.api')
  3.  
  4. local config = Util.readTable('.tankListener')
  5.  
  6. Relay.find()
  7.  
  8. local active = false
  9.  
  10. local function setActive(active)
  11.   local iactive = active
  12.   if config.inverse then
  13.     iactive = not active
  14.   end
  15.   if iactive ~= rs.getOutput(config.side) then
  16.     print('setting output to ' .. tostring(active))
  17.     rs.setOutput(config.side, iactive)
  18.   end
  19.   Relay.send('panelUpdate', {
  20.     uid = config.uid,
  21.     text = config.text,
  22.     type = 'radio',
  23.     active = active,
  24.   })
  25. end
  26.  
  27. Message.addHandler('tankInfo', function(h, id, msg)
  28.   local tank = msg.contents
  29.   active = tank.amount / tank.capacity * 100 < config.limit
  30.   printf('%s: %d%%', config.tankName, math.floor(tank.amount / tank.capacity * 100))
  31.   setActive(active)
  32. end)
  33.  
  34. Event.addHandler('heartbeat', function()
  35.   Relay.send('getTankInfo', {
  36.     name = config.tankName
  37.   })
  38. end)
  39.  
  40. Logger.disable()
  41. os.queueEvent('heartbeat')
  42. Event.heartbeat(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement