Advertisement
hbar

mote

Mar 25th, 2014
2,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. -- Client for Mote - The Turtle Remote
  2. -- http://hbar.kapsi.fi/ccserver/mote/
  3. -- 2014 Matti Vapa
  4. -- Feel free to modify and redistribute
  5. -- this program. Attribution is appreciated,
  6. -- but not required.
  7.  
  8. -- I take no responsibility on any
  9. -- problems this program might cause
  10. -- to anyone ever. Not that it will.
  11.  
  12. -- version 2.0
  13. -- updated to asynchronous connection to the
  14. -- server for better responsivenes
  15.  
  16. if not http then
  17.   print("HTTP required!")
  18.   return
  19. end
  20.  
  21. print("Checking fuel...")
  22. if turtle.getFuelLevel() == 0 then
  23.   print("Refuel me!")
  24.   return
  25. end
  26. print("Ok!")
  27.  
  28. -- Query this address for commands.
  29. url = "http://hbar.kapsi.fi/ccserver/mote/get/"
  30.  
  31. -- Map the commands returned by the server to functions.
  32. -- These are all of the commands available at the time of
  33. -- writing. If you want more functions, you could use
  34. -- for example "menu" to change between sets of functions.
  35. -- Also note that this works with computers too, so you
  36. -- could use this to control your base doors and machines
  37. -- or anything. The session stays live as long as there
  38. -- was at least one command or query during the last hour
  39. -- (might change if there's need for it).
  40. cmds = {}
  41. cmds.arrowUp = turtle.forward
  42. cmds.arrowDown = turtle.back
  43. cmds.arrowLeft = turtle.turnLeft
  44. cmds.arrowRight = turtle.turnRight
  45. cmds.button1 = turtle.up
  46. cmds.button2 = turtle.dig
  47. cmds.button3 = turtle.place
  48. cmds.button4 = turtle.down
  49. cmds.menu = turtle.attack
  50.  
  51. sID = {...}
  52. if #sID < 1 then
  53.   print("Usage: mote <session ID>")
  54.   print("Parameter session ID is the random numbers and letters")
  55.   print("following /mote/ in the controller URL.")
  56. end
  57. sID = sID[1]
  58. -- We need to send this data to get the commands from server.
  59. data = "sessionId="..sID
  60.  
  61. print("Listening for commands...")
  62.  
  63. -- Initialize the command value
  64. cmd = "null"
  65.  
  66. -- We use this function to asynchronously wait for the command from the server.
  67. -- The server will keep the connection open until there is a command to
  68. -- send or it has been open for 60 seconds.
  69.  
  70. function waitForCmd()
  71.   -- http.request will raise an "http_success" or "http_failure"
  72.   -- event after completion. We'll wait for that and act accordingly.
  73.   http.request(url,data)
  74.   e, u, r = os.pullEvent()
  75.   while e ~= "http_success" and e ~= "http_failure" do
  76.     e, u, r = os.pullEvent()
  77.   end
  78.   if e == "http_failure" then
  79.     error("Could not connect to server.")
  80.   elseif r.getResponseCode() ~= 200 then
  81.     print("Something went wrong!")
  82.     print("Server response code: "..tostring(r.getResponseCode()))
  83.     print("Reason: "..r.readAll())
  84.     error("Bad request. Check your session ID.")
  85.   else
  86.     cmd = r.readAll()
  87.   end
  88. end
  89.  
  90. -- Main loop. If we receive a proper command, we simultaneously execute it
  91. -- and request the next one. This way the program is more responsive compared
  92. -- to continuously polling the server.
  93. while true do
  94.   if cmds[cmd] then
  95.     parallel.waitForAll(cmds[cmd],waitForCmd)
  96.   else
  97.     waitForCmd()
  98.   end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement