HawkPB

Untitled

Aug 3rd, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. -- door controller
  2. local component=require"component"
  3. local modem=component.modem
  4. local shell=require"shell"
  5. local thread=require"thread"
  6. local event=require"event"
  7. local computer=require"computer"
  8.  
  9. function splitString(input, delimiter)
  10. if delimiter == nil then
  11. delimiter = "%s"
  12. end
  13. local result = {}
  14. for match in (input .. delimiter):gmatch("(.-)" .. delimiter) do
  15. table.insert(result, match)
  16. end
  17. return result
  18. end
  19. local timeoutTime=5
  20. function actionThread()
  21. io.write("Input: ")
  22. local mes=io.read()
  23. local args=splitString(mes," ")
  24. if(mes=="help")then
  25. print("im cumming")
  26. actionThread()
  27. return
  28. elseif(mes=="ping")then
  29. if(#args==1)then
  30. print("Command \"ping\" requires atleast 1 argument (Port)")
  31. actionThread()
  32. return
  33. end
  34.  
  35. if(tonumber(args[1])==nil)then
  36. print("Command \"ping\" (Port) argument must be a number")
  37. actionThread()
  38. return
  39. end
  40. local portNum=tonumber(args[1])
  41. local ctime=computer.uptime()
  42. print("Pinging...")
  43. modem.broadcast(portNum,"DNET_ping")
  44. print("Opening Listening Thread...")
  45. local responded=false
  46. local listeningThread=thread.create(function()
  47. print("{Listening Thread}: Starting listener for modem events...")
  48. local _, _, from, port, _, message = event.pull("modem_message")
  49. if(port==portNum)then
  50. print("{Listening Thread}: Controller responded!")
  51. print("{Timeout Thread}: Ending Timeout Thread...")
  52. responded=true
  53. actionThread()
  54. return
  55. end
  56. end)
  57. print("Opening Timeout Thread...")
  58. local timeoutThread=thread.create(function()
  59. while true do
  60. if(computer.uptime()>=ctime+timeoutTime)then
  61. print("{Timeout Thread}: Timeout time reached")
  62. print("{Timeout Thread}: Ending Listening Thread...")
  63. listeningThread:kill()
  64. actionThread()
  65. return
  66. end
  67. if responded then
  68. return
  69. end
  70. os.sleep(.5)
  71. end
  72. end)
  73. else
  74. print("\nUnknown Command: "..mes)
  75. actionThread()
  76. return
  77. end
  78. end
  79.  
  80. actionThread()
  81.  
  82.  
  83.  
  84.  
  85.  
Add Comment
Please, Sign In to add comment