Advertisement
enderpro100

RednetClient

Dec 3rd, 2020 (edited)
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | Gaming | 0 0
  1. --- Program : Rednet client
  2. --- Author : LightKnight51
  3. --- Last modification : 18/04/2023
  4. --- Dependency for work : Rednet server (DqARQRRe)
  5.  
  6. --- Utils API
  7. --os.loadAPI("MarquitoLuaUtils")
  8.  
  9. --- Variables
  10.  
  11. -- The programe to execute
  12. local programName = ""
  13.  
  14. local sSeparator = "|"
  15. local turtleId = os.getComputerID()
  16. local libraryName = "MarquitoLuaUtils"
  17. local libraryCode = "mVTSwvw1"
  18.  
  19. local deviceType = "COMPUTER"
  20. local turtleType = "NONE"
  21.  
  22. --- Functions
  23.  
  24. -- Wait to receive data from server
  25. function ReceiveData()
  26.     -- Set the device type
  27.     deviceType = MarquitoLuaUtils.GetDeviceType()
  28.     -- Set the turtle type if this device is a turtle
  29.     if deviceType == MarquitoLuaUtils.DeviceType.TURTLE then
  30.         turtleType = MarquitoLuaUtils.GetTurtleType()
  31.     end
  32.     term.clear()
  33.     while true do
  34.         if MarquitoLuaUtils.OpenWirelessRednetModem() then
  35.             local protocol = "TurtleProgram." .. turtleType
  36.  
  37.             print("Id of the turtle : " .. tostring(turtleId))
  38.             print("Waiting connection ...")
  39.  
  40.             local data = MarquitoLuaUtils.ReceiveDataFromRednet(protocol)
  41.             if data ~= nil then
  42.                 if data == "exit" then
  43.                     break
  44.                 else
  45.                     pType, param1, param2 = ""
  46.                     i = 0
  47.                     for value in (data .. sSeparator):gmatch("([^|]*)|") do
  48.                         if i == 0 then
  49.                             pType = value
  50.                         elseif i == 1 then
  51.                             param1 = value
  52.                         elseif i == 2 then
  53.                             param2 = value
  54.                         end
  55.                         i = i + 1
  56.                     end
  57.                     if pType == "download" then
  58.                         io.output(param1)
  59.                         io.write(param2)
  60.                         io.close()
  61.                         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Download and install program : " .. param1)
  62.                     elseif pType == "execute" then
  63.                         print("Execute program : " .. param1)
  64.                         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Execute programe : " .. param1)
  65.                         programName = param1
  66.                     elseif pType == "delete" then
  67.                         print("Delete program : " .. param1)
  68.                         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Delete programe : " .. param1)
  69.                         shell.run("delete " .. param1)
  70.                     elseif pType == "coordinates" then
  71.                         x, y, z = gps.locate()
  72.                         returnMessage = x .. sSeparator .. y .. sSeparator .. z .. sSeparator
  73.                         rednet.send(senderId, returnMessage, "turtleSendCoords")
  74.                         print("Send coordinates to  : " .. senderId .. " (" .. returnMessage .. ")")
  75.                         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Send coordinates to  : " .. senderId .. " (" .. returnMessage .. ")")
  76.                     end
  77.                 end
  78.             end
  79.             sleep(0.5)
  80.             term.clear()
  81.         end
  82.     end
  83. end
  84.  
  85. -- Download program
  86. function DownloadProgram(pastebinCode, programName)
  87.     if fs.exists(programName) then
  88.         fs.delete(programName)
  89.     end
  90.     shell.run("pastebin get " .. pastebinCode .. " " .. programName)
  91. end
  92.  
  93. -- Exit the program
  94. function ExitProgram()
  95.     print("End of program")
  96.     os.sleep(1)
  97.     MarquitoLuaUtils.CloseWirelessRednetModem()
  98.     MarquitoLuaUtils.EndLog()
  99.     os.shutdown()
  100. end
  101.  
  102. -- The main program, if received execute order, this function execute the program
  103. function MainProgram()
  104.     while true do
  105.         if programName ~= "" then
  106.             shell.run(param1)
  107.             programName = ""
  108.         end
  109.         sleep(0.5)
  110.     end
  111. end
  112.  
  113. -- We need this library for other programs work
  114. DownloadProgram(libraryCode, libraryName)
  115. os.loadAPI("MarquitoLuaUtils")
  116. -- Run the rednet client
  117. parallel.waitForAny(MainProgram, ReceiveData)
  118. ExitProgram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement