Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Program : Rednet client
- --- Author : LightKnight51
- --- Last modification : 18/04/2023
- --- Dependency for work : Rednet server (DqARQRRe)
- --- Utils API
- --os.loadAPI("MarquitoLuaUtils")
- --- Variables
- -- The programe to execute
- local programName = ""
- local sSeparator = "|"
- local turtleId = os.getComputerID()
- local libraryName = "MarquitoLuaUtils"
- local libraryCode = "mVTSwvw1"
- local deviceType = "COMPUTER"
- local turtleType = "NONE"
- --- Functions
- -- Wait to receive data from server
- function ReceiveData()
- -- Set the device type
- deviceType = MarquitoLuaUtils.GetDeviceType()
- -- Set the turtle type if this device is a turtle
- if deviceType == MarquitoLuaUtils.DeviceType.TURTLE then
- turtleType = MarquitoLuaUtils.GetTurtleType()
- end
- term.clear()
- while true do
- if MarquitoLuaUtils.OpenWirelessRednetModem() then
- local protocol = "TurtleProgram." .. turtleType
- print("Id of the turtle : " .. tostring(turtleId))
- print("Waiting connection ...")
- local data = MarquitoLuaUtils.ReceiveDataFromRednet(protocol)
- if data ~= nil then
- if data == "exit" then
- break
- else
- pType, param1, param2 = ""
- i = 0
- for value in (data .. sSeparator):gmatch("([^|]*)|") do
- if i == 0 then
- pType = value
- elseif i == 1 then
- param1 = value
- elseif i == 2 then
- param2 = value
- end
- i = i + 1
- end
- if pType == "download" then
- io.output(param1)
- io.write(param2)
- io.close()
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Download and install program : " .. param1)
- elseif pType == "execute" then
- print("Execute program : " .. param1)
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Execute programe : " .. param1)
- programName = param1
- elseif pType == "delete" then
- print("Delete program : " .. param1)
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Delete programe : " .. param1)
- shell.run("delete " .. param1)
- elseif pType == "coordinates" then
- x, y, z = gps.locate()
- returnMessage = x .. sSeparator .. y .. sSeparator .. z .. sSeparator
- rednet.send(senderId, returnMessage, "turtleSendCoords")
- print("Send coordinates to : " .. senderId .. " (" .. returnMessage .. ")")
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Send coordinates to : " .. senderId .. " (" .. returnMessage .. ")")
- end
- end
- end
- sleep(0.5)
- term.clear()
- end
- end
- end
- -- Download program
- function DownloadProgram(pastebinCode, programName)
- if fs.exists(programName) then
- fs.delete(programName)
- end
- shell.run("pastebin get " .. pastebinCode .. " " .. programName)
- end
- -- Exit the program
- function ExitProgram()
- print("End of program")
- os.sleep(1)
- MarquitoLuaUtils.CloseWirelessRednetModem()
- MarquitoLuaUtils.EndLog()
- os.shutdown()
- end
- -- The main program, if received execute order, this function execute the program
- function MainProgram()
- while true do
- if programName ~= "" then
- shell.run(param1)
- programName = ""
- end
- sleep(0.5)
- end
- end
- -- We need this library for other programs work
- DownloadProgram(libraryCode, libraryName)
- os.loadAPI("MarquitoLuaUtils")
- -- Run the rednet client
- parallel.waitForAny(MainProgram, ReceiveData)
- ExitProgram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement