Crunchy_Nut

Miner Update Manager

Jun 1st, 2020
667
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args = {...}
  2. local sides = rs.getSides()
  3.  
  4. function clearTerm()
  5.     term.clear()
  6.     term.setCursorPos(1, 1)
  7. end
  8.  
  9. function getChannel(t)
  10.     if t == "motor" then
  11.         return "coms_turtles_motor"
  12.     elseif t == "miner" then
  13.         return "coms_turtles_miner"
  14.     elseif t == "master" then
  15.         return "coms_turtles_master"
  16.     end
  17.  
  18.     return nil
  19. end
  20.  
  21. for i = 1, #sides do
  22.     if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == "modem" then
  23.         rednet.open(sides[i])
  24.         break
  25.     end
  26. end
  27.  
  28. if #args < 3 then
  29.     print("Usage: update [turtle-type] [program-name] [paste-code]")
  30. else
  31.     clearTerm()
  32.  
  33.     turtle_channel = getChannel(args[1])
  34.     programName = args[2]
  35.     pasteCode = args[3]
  36.    
  37.     if not turtle_channel then
  38.         print("Invalid turtle type. Use motor|miner")
  39.         return
  40.     end
  41.  
  42.     print("Updating over protocol: " .. turtle_channel)
  43.     shell.run("pastebin get " .. pasteCode .. " program")
  44.     print("Program downloaded.")
  45.     sleep(1.5)
  46.  
  47.     rednet.broadcast("UPDATE", turtle_channel)
  48.    
  49.     while(true) do
  50.         print("Waiting for ready response from turtles..")
  51.         id, msg, proto = rednet.receive(turtle_channel, 2)
  52.         if not proto then
  53.             clearTerm()
  54.             term.setTextColor(colors.red)
  55.             print("No turtle was listening to update request.")
  56.             break
  57.         end
  58.  
  59.         if msg == "UPDATE_READY" then
  60.             f = fs.open("program", "r")
  61.             packet = textutils.serialize(
  62.             {
  63.                 name = programName,
  64.                 content = f.readAll()
  65.             })
  66.             f.close()
  67.  
  68.             print("Sending program packet.")
  69.             rednet.broadcast(packet, "coms_turtles_payload")
  70.  
  71.             sleep(1.5)
  72.             clearTerm()
  73.             term.setTextColor(colors.green)
  74.             print("Update complete.")
  75.             break
  76.         end
  77.     end
  78.  
  79.     fs.delete("program")
  80. end
RAW Paste Data