Advertisement
tobast

[energy_monitor] standalone APC

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