buffsovernexus

FarmBot Administrator Script

May 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | None | 0 0
  1. --[[
  2.  
  3. FarmBot Administrator Script
  4. =======================
  5.  
  6. Functions:
  7. > Converts an Advanced Computer (with modem) into an Admin Control Panel for FarmBots.
  8. > Listens for crops from FarmBots
  9. > Sends FarmBots the most updated crop list.
  10.  
  11. ]]--
  12.  
  13.  
  14. local admin = {
  15.     input = ""
  16. }
  17.  
  18. local adminSendData = {
  19.     data = {},
  20.     request = ""
  21. }
  22.  
  23.  
  24. -- HANDLE PERIPHERALS --
  25. function findModem()
  26.     local periList = peripheral.getNames()
  27.  
  28.     for i = 1, #periList do
  29.         if peripheral.getType(periList[i]) == "modem" then
  30.             return periList[i]
  31.         end
  32.     end
  33.     return nil
  34. end
  35. function findMonitor()
  36.     local periList = peripheral.getNames()
  37.  
  38.     for i = 1, #periList do
  39.         if peripheral.getType(periList[i]) == "monitor" then
  40.             return periList[i]
  41.         end
  42.     end
  43.     return nil
  44. end
  45. function findPrinter()
  46.     local periList = peripheral.getNames()
  47.  
  48.     for i = 1, #periList do
  49.         if peripheral.getType(periList[i]) == "printer" then
  50.             return periList[i]
  51.         end
  52.     end
  53.     return nil
  54. end
  55.  
  56. local modem = findModem()
  57. local monitor = findMonitor()
  58. local printer = findPrinter()
  59. mon = peripheral.wrap(monitor)
  60.  
  61.  
  62. function newLine()
  63.   local _,cY= mon.getCursorPos()
  64.   mon.setCursorPos(1,cY+1)
  65. end
  66.  
  67. -- MAIN MENU FUNCTIONALITY --
  68. function mainMenu()
  69.     term.clear()
  70.     print("               FarmBot Admin PC")
  71.     print("---------------------------------------------------")
  72.     print("This is a FarmBot Administrator Computer. Please ensure you know what you're doing before you mess with things.")
  73.     print("The following are the commands available for you:")
  74.     print(" ")
  75.     print("(v) - View crop list")
  76.     print("(g) - Get bot starting location")
  77.     print("(f) - Force update system")
  78.     print("(x) - Terminate program")
  79.     print(" ")
  80.     print(" ")
  81.     term.write("Your Answer: ")
  82.     admin.input = read()
  83.    
  84.     if admin.input == "v" then
  85.         viewCropList()
  86.     elseif admin.input == "f" then
  87.         forceUpdate()
  88.     elseif admin.input == "g" then
  89.         getBotStartLocation()
  90.     end
  91.    
  92.     -- Reset the send data.
  93.     adminSendData.request = ""
  94.     adminSendData.data = {}
  95. end
  96. -- END MAIN MENU --
  97.  
  98. -- VIEW CROP LIST --
  99. function viewCropList()
  100.     term.clear()
  101.     print("               View Crop List")
  102.     print("---------------------------------------------------")
  103.     print("In order to see the list of crops, please enter in the ID of the FarmBotDB terminal. You can easily see the ID of the machine by right-clicking the terminal and look for 'ID - #' on the screen.")
  104.     print(" ")
  105.     print(" ")
  106.     term.write("FarmBotDB ID: ")
  107.     local id = tonumber(read())
  108.     adminSendData.request = "latest"
  109.     -- Send Request to FBDB
  110.     rednet.send(id, textutils.serialize(adminSendData), "fbtpa")
  111.    
  112.     -- Wait for the DB to respond.
  113.     local senderId, response, protocol = rednet.receive("fbtpa")
  114.  
  115.     local data = textutils.unserialize(response)
  116.     local crops = data.crops
  117.    
  118.     --Grab the monitor to the right
  119.     mon.clear()
  120.     mon.setCursorPos(1,1)
  121.    
  122.     for k,v in pairs(crops) do
  123.         mon.write("Crop: " .. v.name)
  124.         newLine()
  125.         mon.write("Seed:" .. v.seed)
  126.         newLine()
  127.     end
  128. end
  129. -- END VIEW CROP LIST --
  130.  
  131. -- FORCE UPDATE --
  132. function forceUpdate()
  133.     print("Forcing update to newest script...")
  134.     fs.delete("startup")
  135.     shell.run("pastebin get XHrCpCVV startup")
  136.     print("Done forcing update.")
  137.     os.sleep(10)
  138. end
  139. -- END FORCE UPDATE
  140.  
  141. -- GET BOT START LOCATION --
  142. function getBotStartLocation()
  143.     -- Step 1: Ask the user what ID the database is.
  144.     term.clear()
  145.     print("               Bot Start Location (1/2)")
  146.     print("---------------------------------------------------")
  147.     print("All FarmBot start locations are stored in the DB. Therefore, you must enter in a valid FarmBotDB ID.")
  148.     print(" ")
  149.     print(" ")
  150.     term.write("FarmBotDB ID: ")
  151.     local dbId = tonumber(read())
  152.    
  153.     -- Step 2: Ask what the FarmBot ID is.
  154.     term.clear()
  155.     print("               Bot Start Location (2/2)")
  156.     print("---------------------------------------------------")
  157.     print("To differentiate between different FarmBots, please enter in the FarmBot's ID. You can find this by running the command 'id' in the console command.")
  158.     print(" ")
  159.     print(" ")
  160.     term.write("FarmBot ID: ")
  161.     local id = tonumber(read())
  162.     term.clear()
  163.    
  164.     -- Step 3: Fill in the information to the table.
  165.     adminSendData.data = { id = id }
  166.     adminSendData.request = "gps_start"
  167.    
  168.     -- Step 4: Send this information to the DB so it can respond back.
  169.     rednet.send(dbId, textutils.serialize(adminSendData), "fbtpa")
  170.    
  171.     -- Step 5: Receive the response from the DB.
  172.     local senderId, response, protocol = rednet.receive("fbtpa")
  173.     response = textutils.unserialize(response)
  174.    
  175.     -- Step 6: Display on console.
  176.     mon.clear()
  177.    
  178.     mon.write("               Bot #" .. id .. " Start Location")
  179.     mon.write("---------------------------------------------------")
  180.     mon.write("X: ", response.gps.x)
  181.     mon.write("Y: ", response.gps.y)
  182.     mon.write("Z: ", response.gps.z)
  183. end
  184. -- END GET BOT START LOCATION --
  185.  
  186. --[[
  187. This is where the script starts... Do not edit this.
  188. ]]--
  189. if modem ~= nil then
  190.     if rednet.isOpen(modem) == false then
  191.         rednet.open(modem)
  192.     end
  193.     -- The default protocol for admin control terminals is fbtpa
  194.     rednet.host("fbtpa", os.getComputerLabel())
  195.     rednet.host("fbtp", os.getComputerLabel())
  196.     if monitor ~= nil then
  197.         while admin.input ~= "x" do
  198.             mainMenu()
  199.         end
  200.         -- The user has decided to shut down.
  201.         term.clear()
  202.         print("TERMINATED: To startup the script again, please do 'ctrl + s'.")
  203.     else
  204.         print("FATAL ERROR: You need to connect at least 1 monitor to this terminal.")
  205.     end
  206. else
  207.     print("FATAL ERROR: You need to have a modem connected to your terminal...")
  208. end
Add Comment
Please, Sign In to add comment