Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- door controller
- local component=require"component"
- local modem=component.modem
- local shell=require"shell"
- local thread=require"thread"
- local event=require"event"
- local computer=require"computer"
- function splitString(input, delimiter)
- if delimiter == nil then
- delimiter = "%s"
- end
- local result = {}
- for match in (input .. delimiter):gmatch("(.-)" .. delimiter) do
- table.insert(result, match)
- end
- return result
- end
- local timeoutTime=5
- function actionThread()
- io.write("Input: ")
- local mes=io.read()
- local command=splitString(mes," ")[1]
- local args=splitString(mes," ")
- if(command=="help")then
- print("im cumming")
- actionThread()
- return
- elseif(command=="ping")then
- if(#args==1)then
- print("Command \"ping\" requires atleast 1 argument (Port)")
- actionThread()
- return
- end
- if(tonumber(args[2])==nil)then
- print("Command \"ping\" (Port) argument must be a number")
- actionThread()
- return
- end
- local portNum=tonumber(args[2])
- local ctime=computer.uptime()
- print("Pinging...")
- modem.broadcast(portNum,"DNET_ping")
- print("Opening Listening Thread...")
- local responded=false
- local listeningThread=thread.create(function()
- print("{Listening Thread}: Starting listener for modem events...")
- local _, _, from, port, _, message = event.pull("modem_message")
- if(port==portNum)then
- print("{Listening Thread}: Controller responded!")
- print("{Timeout Thread}: Ending Timeout Thread...")
- responded=true
- actionThread()
- return
- end
- end)
- print("Opening Timeout Thread...")
- local timeoutThread=thread.create(function()
- while true do
- if(computer.uptime()>=ctime+timeoutTime)then
- print("{Timeout Thread}: Timeout time reached")
- print("{Timeout Thread}: Ending Listening Thread...")
- listeningThread:kill()
- actionThread()
- return
- end
- if responded then
- return
- end
- os.sleep(.5)
- end
- end)
- else
- print("\nUnknown Command: "..mes)
- actionThread()
- return
- end
- end
- actionThread()
Add Comment
Please, Sign In to add comment