HawkPB

Untitled

Aug 3rd, 2024 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 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 command=splitString(mes," ")[1]
  24. local args=splitString(mes," ")
  25. if(command=="help")then
  26. print("im cumming")
  27. actionThread()
  28. return
  29. elseif(command=="ping")then
  30. if(#args==1)then
  31. print("Command \"ping\" requires atleast 1 argument (Port)")
  32. actionThread()
  33. return
  34. end
  35.  
  36. if(tonumber(args[2])==nil)then
  37. print("Command \"ping\" (Port) argument must be a number")
  38. actionThread()
  39. return
  40. end
  41. local portNum=tonumber(args[2])
  42. local ctime=computer.uptime()
  43. print("Pinging...")
  44. modem.broadcast(portNum,"DNET_ping")
  45. print("Opening Listening Thread...")
  46. local responded=false
  47. local listeningThread=thread.create(function()
  48. print("{Listening Thread}: Starting listener for modem events...")
  49. local _, _, from, port, _, message = event.pull("modem_message")
  50. if(port==portNum)then
  51. print("{Listening Thread}: Controller responded!")
  52. print("{Timeout Thread}: Ending Timeout Thread...")
  53. responded=true
  54. actionThread()
  55. return
  56. end
  57. end)
  58. print("Opening Timeout Thread...")
  59. local timeoutThread=thread.create(function()
  60. while true do
  61. if(computer.uptime()>=ctime+timeoutTime)then
  62. print("{Timeout Thread}: Timeout time reached")
  63. print("{Timeout Thread}: Ending Listening Thread...")
  64. listeningThread:kill()
  65. actionThread()
  66. return
  67. end
  68. if responded then
  69. return
  70. end
  71. os.sleep(.5)
  72. end
  73. end)
  74. else
  75. print("\nUnknown Command: "..mes)
  76. actionThread()
  77. return
  78. end
  79. end
  80.  
  81. actionThread()
  82.  
  83.  
  84.  
  85.  
  86.  
Add Comment
Please, Sign In to add comment