Advertisement
tobast

[AT_switch] server

Jun 29th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. ------ PARAMS ------
  2. MODEM_SIDE='bottom'
  3. ENGINE_SIDE='top'
  4. CHECK_PERIOD = 5 -- (seconds)
  5. ------ END PARAMS --
  6.  
  7. timerId = nil
  8. remainToCheck = 0
  9. processing = false
  10.  
  11. function mainScreen(isProcessing)
  12.     if(not isProcessing) then
  13.         term.setBackgroundColor(colors.lime)
  14.         term.setCursorPos(18, 6)
  15.         term.write("                  ")
  16.         term.setCursorPos(18, 7)
  17.         term.write(" Initiate process ")
  18.         term.setCursorPos(18, 8)
  19.         term.write("                  ")
  20.     else
  21.         term.setBackgroundColor(colors.gray)
  22.         term.setCursorPos(18, 6)
  23.         term.write("                  ")
  24.         term.setCursorPos(18, 7)
  25.         term.write("   Processing...  ")
  26.         term.setCursorPos(18, 8)
  27.         term.write("                  ")
  28.     end
  29. end
  30.  
  31. function main()
  32.     rednet.open(MODEM_SIDE)
  33.     checkers = { rednet.lookup('switch_check','checker') }
  34.     remainToCheck = #checkers
  35.  
  36.     term.clear()
  37.     mainScreen(false)
  38.  
  39.     timerId = os.startTimer(CHECK_PERIOD)
  40.  
  41.     while true do
  42.         data = { os.pullEvent() }
  43.         evt = data[1]
  44.         if(evt == 'rednet_message') then
  45.             mess = data[3]
  46.             if(mess.type == 'status' and mess.status == false) then
  47.                 remainToCheck = remainToCheck - 1
  48.                 if(remainToCheck == 0) then
  49.                     processing = false
  50.                     mainScreen(false)
  51.                     redstone.setOutput(ENGINE_SIDE, false)
  52.                 end
  53.             end
  54.  
  55.         elseif(evt == 'timer' and data[2] == timerId) then
  56.             if(processing) then
  57.                 remainToCheck = #checkers
  58.                 for _,checker in pairs(checkers) do
  59.                     rednet.send(checker, {type = 'check'})
  60.                 end
  61.                 timerId = os.startTimer(CHECK_PERIOD)
  62.             end
  63.         elseif(evt == 'mouse_click' and processing == false) then
  64.             x = data[3]
  65.             y = data[4]
  66.             if(x >= 18 and x <= 36 and y >= 6 and y <= 8) then
  67.                 mainScreen(true)
  68.                 redstone.setOutput(ENGINE_SIDE, true)
  69.                 processing = true
  70.                 timerId = os.startTimer(CHECK_PERIOD)
  71.             end
  72.         end
  73.     end
  74. end
  75. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement