MadManMarkAu

[CC] Switchboard Endpoint

May 28th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.57 KB | None | 0 0
  1. -- Implementation of a redstone switchboard endpoint.
  2. -- The client controls the actual device that is being turned on or off.
  3. -- The switchboard controller is responsible for setting and querying the client's state.
  4.  
  5. -- Configuration variables.
  6. local sModemSide = "right"
  7. local sRedstoneSide = "top"
  8. local sName = "Spawners"
  9.  
  10. -- State variables.
  11. local bOutputState = false
  12.  
  13. -- Local variables.
  14. local nSenderID
  15. local tMessage
  16.  
  17. print("Switchbarod endpoint initializing...")
  18.  
  19. -- Initialize.
  20. rednet.open(sModemSide)
  21.  
  22. -- Do an initial broadcast, to announce we're here.
  23. rednet.broadcast({Name=sName, State=bOutputState}, "switchboard")
  24.  
  25. print("Ready.")
  26.  
  27. -- Message loop.
  28. while true do
  29.     nSenderID, tMessage = rednet.receive("switchboard")
  30.    
  31.     if tMessage.Command == "set" then
  32.         -- Turn output on or off.
  33.         if tMessage.State == true then
  34.             print("Turning output on.")
  35.             redstone.setAnalogOutput(sRedstoneSide, 15)
  36.             bOutputState = true
  37.         else
  38.             print("Turning output off.")
  39.             redstone.setAnalogOutput(sRedstoneSide, 0)
  40.             bOutputState = false
  41.         end
  42.  
  43.         -- Update the controller's state.
  44.         rednet.send(nSenderID, {Name=sName, State=bOutputState}, "switchboard")
  45.     elseif tMessage.Command == "get" then
  46.         -- Get the current output state.
  47.         rednet.send(nSenderID, {Name=sName, State=bOutputState}, "switchboard")
  48.     elseif tMessage.Command == "kill" then
  49.         -- Kills the endpoint, leaving it in it's current output state.
  50.         print("Termination request!")
  51.         break
  52.     end
  53. end
  54.  
  55. -- Uninitialize.
  56. rednet.close(sModemSide)
  57.  
  58. print("Switchboard endpoint terminated.")
Add Comment
Please, Sign In to add comment