Advertisement
tobast

[energy_monitor] controller

Jun 27th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. -- Controls the APCs through network
  2.  
  3. ------------------- CONFIGURATION ------------------------
  4. MODEM_SIDE = 'back'
  5. MONITOR_SIDE = 'left'
  6. REFRESH_RATE = 10 -- in seconds
  7. ------------------- END CONFIG ---------------------------
  8.  
  9. os.loadAPI('/lib/slot_sig')
  10. os.loadAPI('/lib/button')
  11.  
  12. apcList = {}
  13. idPosition = {}
  14. buttons = {}
  15. nextTimer = nil
  16.  
  17. STATE_ON = 0
  18. STATE_OFF = 1
  19. STATE_FORCEOFF = 2
  20. STATE_BTN = {
  21.     [0] = {text = 'ON ', color = colors.lime},
  22.     [1] = {text = 'OFF', color = colors.orange},
  23.     [2] = {text = 'OFF', color = colors.red}
  24. }
  25.  
  26. function onRednet(data)
  27.     sender = data[1]
  28.     message = data[2]
  29.  
  30.     if(message.type == 'EU_levels') then
  31.         displayUpdate(sender,message)
  32.     end
  33. end
  34.  
  35. function onTimer(data)
  36.     id=data[1]
  37.     if(id ~= nextTimer) then
  38.         return
  39.     end
  40.    
  41.     updateData()
  42.  
  43.     nextTimer = os.startTimer(REFRESH_RATE)
  44. end
  45.  
  46. function formatPercent(perc)
  47.     if(perc >= 100) then
  48.         return '100%'
  49.     else
  50.         return perc..'%'
  51.     end
  52. end
  53. function displayUpdate(sender,data)
  54.     mon = peripheral.wrap(MONITOR_SIDE)
  55.     pos = idPosition[sender]
  56.    
  57.     mon.setCursorPos(pos.x,pos.y)
  58.     mon.setTextColor(colors.lightBlue)
  59.     mon.write(data.area)
  60.     mon.setTextColor(colors.white)
  61.  
  62.     mon.setCursorPos(pos.x+1, pos.y+1)
  63.     mon.write('Lights')
  64.     mon.setCursorPos(pos.x+12, pos.y+1)
  65.     mon.write(formatPercent(data.lightsPercent))
  66.     buttons[sender].lights:setBgColor(STATE_BTN[data.lightsState].color)
  67.     buttons[sender].lights:setText(STATE_BTN[data.lightsState].text)
  68.    
  69.     mon.setCursorPos(pos.x+1, pos.y+2)
  70.     mon.write('Others')
  71.     mon.setCursorPos(pos.x+12, pos.y+2)
  72.     mon.write(formatPercent(data.othersPercent))
  73.     buttons[sender].others:setBgColor(STATE_BTN[data.othersState].color)
  74.     buttons[sender].others:setText(STATE_BTN[data.othersState].text)
  75. end
  76.  
  77. function updateData()
  78.     for _,id in pairs(apcList) do
  79.         rednet.send(id, {type = 'levels_query'})
  80.     end
  81. end
  82.  
  83. function buttonClicked(btnType,id)
  84.     curLights = (buttons[sender].lights.text == 'ON ')
  85.     curOthers = (buttons[sender].others.text == 'ON ')
  86.  
  87.     if(btnType == 'lights') then
  88.         curLights = not curLights
  89.     else
  90.         curOthers = not curOthers
  91.     end
  92.  
  93.     message = {
  94.         type = 'set_state',
  95.         lightsState = curLights,
  96.         forceLights = false,
  97.         othersState = curOthers,
  98.         forceOthers = false }
  99.     rednet.send(id,message)
  100.     rednet.send(id,{type = 'levels_query'})
  101. end
  102.  
  103. function mapIDs()
  104.     wid,hei = peripheral.call(MONITOR_SIDE,'getSize')
  105.     curCol = 1
  106.     curRow = 1
  107.     for _,id in pairs(apcList) do
  108.         if(curCol > wid) then
  109.             error('screen is too small')
  110.         end
  111.  
  112.         idPosition[id] = {x=curCol, y=curRow}
  113.         buttons[id] = {
  114.             lights = button.Button(MONITOR_SIDE, curCol+8,curRow+1,'ON ',
  115.                 function() buttonClicked('lights',id) end,
  116.                 colors.lime, colors.white),
  117.             others = button.Button(MONITOR_SIDE, curCol+8,curRow+2,'ON ',
  118.                 function() buttonClicked('others',id) end,
  119.                 colors.lime, colors.white) }
  120.  
  121.         curRow = curRow + 3
  122.         if(curRow+2 > hei) then
  123.             curRow = 1
  124.             curCol = curCol + 18
  125.         end
  126.     end
  127. end
  128.  
  129. function initMonitor()
  130.     monitor = peripheral.wrap(MONITOR_SIDE)
  131.     monitor.clear()
  132.     monitor.setTextScale(0.5)
  133. end
  134. function main()
  135.     rednet.open(MODEM_SIDE)
  136.     rednet.host('energy_controller', 'controller')
  137.    
  138.     apcList = { rednet.lookup('apc') }
  139.     mapIDs()
  140.  
  141.     initMonitor()
  142.  
  143.     slot_sig.connectSlot('rednet_message', onRednet)
  144.     slot_sig.connectSlot('timer', onTimer)
  145.  
  146.     updateData()
  147.     nextTimer = os.startTimer(REFRESH_RATE)
  148.     slot_sig.run()
  149. end
  150.  
  151. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement