Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Controls the APCs through network
- ------------------- CONFIGURATION ------------------------
- MODEM_SIDE = 'back'
- MONITOR_SIDE = 'left'
- REFRESH_RATE = 10 -- in seconds
- ------------------- END CONFIG ---------------------------
- os.loadAPI('/lib/slot_sig')
- os.loadAPI('/lib/button')
- apcList = {}
- idPosition = {}
- buttons = {}
- nextTimer = nil
- STATE_ON = 0
- STATE_OFF = 1
- STATE_FORCEOFF = 2
- STATE_BTN = {
- [0] = {text = 'ON ', color = colors.lime},
- [1] = {text = 'OFF', color = colors.orange},
- [2] = {text = 'OFF', color = colors.red}
- }
- function onRednet(data)
- sender = data[1]
- message = data[2]
- if(message.type == 'EU_levels') then
- displayUpdate(sender,message)
- end
- end
- function onTimer(data)
- id=data[1]
- if(id ~= nextTimer) then
- return
- end
- updateData()
- nextTimer = os.startTimer(REFRESH_RATE)
- end
- function formatPercent(perc)
- if(perc >= 100) then
- return '100%'
- else
- return perc..'%'
- end
- end
- function displayUpdate(sender,data)
- mon = peripheral.wrap(MONITOR_SIDE)
- pos = idPosition[sender]
- mon.setCursorPos(pos.x,pos.y)
- mon.setTextColor(colors.lightBlue)
- mon.write(data.area)
- mon.setTextColor(colors.white)
- mon.setCursorPos(pos.x+1, pos.y+1)
- mon.write('Lights')
- mon.setCursorPos(pos.x+12, pos.y+1)
- mon.write(formatPercent(data.lightsPercent))
- buttons[sender].lights:setBgColor(STATE_BTN[data.lightsState].color)
- buttons[sender].lights:setText(STATE_BTN[data.lightsState].text)
- mon.setCursorPos(pos.x+1, pos.y+2)
- mon.write('Others')
- mon.setCursorPos(pos.x+12, pos.y+2)
- mon.write(formatPercent(data.othersPercent))
- buttons[sender].others:setBgColor(STATE_BTN[data.othersState].color)
- buttons[sender].others:setText(STATE_BTN[data.othersState].text)
- end
- function updateData()
- for _,id in pairs(apcList) do
- rednet.send(id, {type = 'levels_query'})
- end
- end
- function buttonClicked(btnType,id)
- curLights = (buttons[sender].lights.text == 'ON ')
- curOthers = (buttons[sender].others.text == 'ON ')
- if(btnType == 'lights') then
- curLights = not curLights
- else
- curOthers = not curOthers
- end
- message = {
- type = 'set_state',
- lightsState = curLights,
- forceLights = false,
- othersState = curOthers,
- forceOthers = false }
- rednet.send(id,message)
- rednet.send(id,{type = 'levels_query'})
- end
- function mapIDs()
- wid,hei = peripheral.call(MONITOR_SIDE,'getSize')
- curCol = 1
- curRow = 1
- for _,id in pairs(apcList) do
- if(curCol > wid) then
- error('screen is too small')
- end
- idPosition[id] = {x=curCol, y=curRow}
- buttons[id] = {
- lights = button.Button(MONITOR_SIDE, curCol+8,curRow+1,'ON ',
- function() buttonClicked('lights',id) end,
- colors.lime, colors.white),
- others = button.Button(MONITOR_SIDE, curCol+8,curRow+2,'ON ',
- function() buttonClicked('others',id) end,
- colors.lime, colors.white) }
- curRow = curRow + 3
- if(curRow+2 > hei) then
- curRow = 1
- curCol = curCol + 18
- end
- end
- end
- function initMonitor()
- monitor = peripheral.wrap(MONITOR_SIDE)
- monitor.clear()
- monitor.setTextScale(0.5)
- end
- function main()
- rednet.open(MODEM_SIDE)
- rednet.host('energy_controller', 'controller')
- apcList = { rednet.lookup('apc') }
- mapIDs()
- initMonitor()
- slot_sig.connectSlot('rednet_message', onRednet)
- slot_sig.connectSlot('timer', onTimer)
- updateData()
- nextTimer = os.startTimer(REFRESH_RATE)
- slot_sig.run()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement