Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- opening a rednet Port. Modem Must be attached
- peripheral.find("modem", rednet.open)
- -- setting the owner computer id from where messages comes
- owner=21
- -- checking after reboot the train schedule state
- function check()
- if not fs.exists("trains") then
- file=fs.open("trains","w")
- file.writeLine("go")
- file.close()
- end
- file=fs.open("trains","r")
- trains=file.readLine()
- file.close()
- if trains == "go" then
- rs.setOutput("top", true)
- elseif trains == "stop" then
- rs.setOutput("top", false)
- end
- end
- -- checking for new messages
- -- checking if the owner is the sender
- -- checking for the command "go"/"stop"
- -- if message == "pastebin" it installs the program
- function receive()
- while not message do
- id, message = rednet.receive()
- end
- if message == "getName" then
- print("request to broadcast Station Name")
- if peripheral.find("Create_Station") then
- trainstation=peripheral.find("Create_Station")
- stationname=trainstation.getStationName()
- cid=os.getComputerID()
- print("Actual Station Name: "..stationname)
- text="ID: "..cid..", Station-Name: "..stationname
- sleepTime=tonumber(cid)/10
- sleep(sleepTime)
- rednet.broadcast(text)
- else
- print("no Station found")
- end
- elseif message == "getTrains" then
- print("request to broadcast stopped trains")
- if peripheral.find("Create_Station") then
- trainstation=peripheral.find("Create_Station")
- if trainstation.isTrainPresent() == true then
- trainname=trainstation.getTrainName()
- cid=os.getComputerID()
- print("Stopped Train: "..trainname)
- text="ID: "..cid..", Train-Name: "..trainname
- sleepTime=tonumber(cid)/10
- sleep(sleepTime)
- rednet.broadcast(text)
- else
- print("no Train found")
- end
- else
- print("no Station found")
- end
- elseif id == owner then
- if message == "go" then
- print("received message from owner. switch to go")
- rs.setOutput("top", true)
- file=fs.open("trains","w")
- file.writeLine("go")
- file.close()
- elseif message == "stop" then
- print("received message from owner. switch to stop")
- rs.setOutput("top", false)
- file=fs.open("trains","w")
- file.writeLine("stop")
- file.close()
- elseif message == "pastebin" then
- print("awaiting name for program")
- message = false
- while not message do
- id, message = rednet.receive()
- end
- if id == owner then
- name=message
- else
- print("got another message from id "..id)
- print("message: "..message)
- print("action aborted")
- return
- end
- print("awaiting pastebin code")
- message = false
- while not message do
- id, message = rednet.receive()
- end
- if id == owner then
- if fs.exists(name) then
- fs.delete(name)
- end
- shell.run("pastebin get "..message.." "..name)
- else
- print("got another message from id "..id)
- print("message: "..message)
- print("action aborted")
- return
- end
- elseif message == "command" then
- print("awating command")
- message = false
- while not message do
- id, message = rednet.receive()
- end
- if id == owner then
- shell.run(message)
- else
- print("got another message from id "..id)
- print("message: "..message)
- print("action aborted")
- return
- end
- else
- print(message)
- end
- else
- print("ID: "..id..", Message: "..message)
- end
- message=false
- end
- -- executing the program
- while true do
- check()
- receive()
- check()
- end
Advertisement
Add Comment
Please, Sign In to add comment