Advertisement
tobast

[energy_monitor] apc

Jun 26th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. -- APC code by Tobast. Intended for setup (from front) :
  2. --         [ screen ]
  3. -- [batbox][computer][batbox]
  4.  
  5. ------------------ CONFIG --------------------
  6. AREA_NAME=''
  7. BATBOX_LIGHTS_SIDE='left'
  8. BATBOX_OTHERS_SIDE='right'
  9. SCREEN_SIDE='top'
  10. MODEM_SIDE='bottom'
  11. ----------------- END CONFIG -----------------
  12.  
  13. os.loadAPI('/lib/slot_sig')
  14. os.loadAPI('/lib/button')
  15.  
  16. STATE_ON = 0
  17. STATE_OFF = 1
  18. STATE_FORCEOFF = 2
  19.  
  20. state_lights = 0
  21. state_others = 0
  22.  
  23. nextUpdateTimer = nil
  24.  
  25. function onBtnLightsClicked()
  26.     if(state_lights == STATE_ON) then
  27.         setBatboxState(false, BATBOX_LIGHTS_SIDE, false)
  28.     else
  29.         setBatboxState(true, BATBOX_LIGHTS_SIDE, false)
  30.     end
  31. end
  32.  
  33. function onBtnOthersClicked()
  34.     if(state_others == STATE_ON) then
  35.         setBatboxState(false, BATBOX_OTHERS_SIDE, false)
  36.     else
  37.         setBatboxState(true, BATBOX_OTHERS_SIDE, false)
  38.     end
  39. end
  40.  
  41. btn_lights = button.Button(SCREEN_SIDE, 8, 1, '', onBtnLightsClicked)
  42. btn_others = button.Button(SCREEN_SIDE, 8, 6, '', onBtnOthersClicked)
  43.  
  44. function getEUStored(side)
  45.     periph = peripheral.wrap(side)
  46.     return periph.getEUStored()
  47. end
  48.  
  49. function getPercentage(side)
  50.     periph = peripheral.wrap(side)
  51.     return math.floor(periph.getEUStored() / periph.getEUCapacity() * 100)
  52. end
  53.  
  54. function onLevelsQuery(requester)
  55.     data = {}
  56.     data.type='EU_levels'
  57.     data.area=AREA_NAME
  58.     data.lights = getEUStored(BATBOX_LIGHTS_SIDE)
  59.     data.lightsPercent = getPercentage(BATBOX_LIGHTS_SIDE)
  60.     data.others = getEUStored(BATBOX_OTHERS_SIDE)
  61.     data.othersPercent = getPercentage(BATBOX_OTHERS_SIDE)
  62.     data.lightsState = state_lights
  63.     data.othersState = state_others
  64.  
  65.     rednet.send(requester, data, 'apc_eu_reply')
  66. end
  67.  
  68. function setBatboxState(nState, side, isForceShutdown)
  69.     redstone.setOutput(side, not nState)
  70.  
  71.     setButtonState(side, nState, isForceShutdown)
  72. end
  73.    
  74. function setButtonState(side, nState, isForceShutdown)
  75.     color=nil
  76.     text=''
  77.     if nState==true then
  78.         color = colors.lime
  79.         text=' ON  '
  80.         if(side == BATBOX_LIGHTS_SIDE) then
  81.             state_lights = STATE_ON
  82.         else
  83.             state_others = STATE_ON
  84.         end
  85.     elseif isForceShutdown then
  86.         color = colors.red
  87.         text=' OFF '
  88.         if(side == BATBOX_LIGHTS_SIDE) then
  89.             state_lights = STATE_FORCEOFF
  90.         else
  91.             state_others = STATE_FORCEOFF
  92.         end
  93.     else
  94.         color = colors.orange
  95.         text=' OFF '
  96.  
  97.         if(side == BATBOX_LIGHTS_SIDE) then
  98.             state_lights = STATE_OFF
  99.         else
  100.             state_others = STATE_OFF
  101.         end
  102.     end
  103.  
  104.     if(side == BATBOX_LIGHTS_SIDE) then
  105.         btn_lights:setBgColor(color)
  106.         btn_lights:setText(text)
  107.     else
  108.         btn_others:setBgColor(color)
  109.         btn_others:setText(text)
  110.     end
  111. end
  112.  
  113. function setStates(data)
  114.     setBatboxState(data.lightsState, BATBOX_LIGHTS_SIDE, data.forceLights)
  115.     setBatboxState(data.othersState, BATBOX_OTHERS_SIDE, data.forceOthers)
  116. end
  117.  
  118. function onRednet(input)
  119.     sender = input[1]
  120.     data = input[2]
  121.    
  122.     if(type(data) == 'string') then
  123.         data = textutils.unserialize(data)
  124.     end
  125.    
  126.     if(data.type == 'levels_query') then
  127.         onLevelsQuery(sender)
  128.     elseif(data.type == 'set_state') then
  129.         setStates(data)
  130.     end
  131. end
  132.  
  133. function initStates()
  134.     setButtonState(BATBOX_LIGHTS_SIDE,
  135.         not redstone.getOutput(BATBOX_LIGHTS_SIDE), false)
  136.     setButtonState(BATBOX_OTHERS_SIDE,
  137.         not redstone.getOutput(BATBOX_OTHERS_SIDE), false)
  138. end
  139.  
  140. function periodicUpdate()
  141.     mainScreen()
  142. end
  143.  
  144. function onTimer(data)
  145.     id = data[1]
  146.     if(id == nextUpdateTimer) then
  147.         periodicUpdate()
  148.         nextUpdateTimer = os.startTimer(5)
  149.     end
  150. end
  151.  
  152. function mainScreen()
  153.     scr = peripheral.wrap(SCREEN_SIDE)
  154.    
  155.     scr.setCursorPos(1,1)
  156.     scr.setTextColor(colors.lightBlue)
  157.     scr.write('LIGHTS')
  158.     scr.setTextColor(colors.white)
  159.     scr.setCursorPos(3,2)
  160.     scr.write(getPercentage(BATBOX_LIGHTS_SIDE))
  161.     scr.write('%  ')
  162.     scr.setCursorPos(3,3)
  163.     scr.write(getEUStored(BATBOX_LIGHTS_SIDE))
  164.     scr.write('EU')
  165.  
  166.     scr.setCursorPos(1,6)
  167.     scr.setTextColor(colors.lightBlue)
  168.     scr.write('OTHERS')
  169.     scr.setTextColor(colors.white)
  170.     scr.setCursorPos(3,7)
  171.     scr.write(getPercentage(BATBOX_OTHERS_SIDE))
  172.     scr.write('%  ')
  173.     scr.setCursorPos(3,8)
  174.     scr.write(getEUStored(BATBOX_OTHERS_SIDE))
  175.     scr.write('EU')
  176. end
  177.  
  178. function main()
  179.     rednet.open(MODEM_SIDE)
  180.     rednet.host('apc',AREA_NAME)
  181.  
  182.     peripheral.call(SCREEN_SIDE, 'clear')
  183.     peripheral.call(SCREEN_SIDE, 'setTextScale', 0.5)
  184.    
  185.     initStates()
  186.     mainScreen()
  187.  
  188.     nextUpdateTimer = os.startTimer(5)
  189.  
  190.     slot_sig.connectSlot('rednet_message',onRednet)
  191.     slot_sig.connectSlot('timer', onTimer)
  192.  
  193.     slot_sig.run()
  194. end
  195. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement