Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- FarmBot Administrator Script
- =======================
- Functions:
- > Converts an Advanced Computer (with modem) into an Admin Control Panel for FarmBots.
- > Listens for crops from FarmBots
- > Sends FarmBots the most updated crop list.
- ]]--
- local admin = {
- input = ""
- }
- local adminSendData = {
- data = {},
- request = ""
- }
- -- HANDLE PERIPHERALS --
- function findModem()
- local periList = peripheral.getNames()
- for i = 1, #periList do
- if peripheral.getType(periList[i]) == "modem" then
- return periList[i]
- end
- end
- return nil
- end
- function findMonitor()
- local periList = peripheral.getNames()
- for i = 1, #periList do
- if peripheral.getType(periList[i]) == "monitor" then
- return periList[i]
- end
- end
- return nil
- end
- function findPrinter()
- local periList = peripheral.getNames()
- for i = 1, #periList do
- if peripheral.getType(periList[i]) == "printer" then
- return periList[i]
- end
- end
- return nil
- end
- local modem = findModem()
- local monitor = findMonitor()
- local printer = findPrinter()
- mon = peripheral.wrap(monitor)
- function newLine()
- local _,cY= mon.getCursorPos()
- mon.setCursorPos(1,cY+1)
- end
- -- MAIN MENU FUNCTIONALITY --
- function mainMenu()
- term.clear()
- print(" FarmBot Admin PC")
- print("---------------------------------------------------")
- print("This is a FarmBot Administrator Computer. Please ensure you know what you're doing before you mess with things.")
- print("The following are the commands available for you:")
- print(" ")
- print("(v) - View crop list")
- print("(g) - Get bot starting location")
- print("(f) - Force update system")
- print("(x) - Terminate program")
- print(" ")
- print(" ")
- term.write("Your Answer: ")
- admin.input = read()
- if admin.input == "v" then
- viewCropList()
- elseif admin.input == "f" then
- forceUpdate()
- elseif admin.input == "g" then
- getBotStartLocation()
- end
- -- Reset the send data.
- adminSendData.request = ""
- adminSendData.data = {}
- end
- -- END MAIN MENU --
- -- VIEW CROP LIST --
- function viewCropList()
- term.clear()
- print(" View Crop List")
- print("---------------------------------------------------")
- 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.")
- print(" ")
- print(" ")
- term.write("FarmBotDB ID: ")
- local id = tonumber(read())
- adminSendData.request = "latest"
- -- Send Request to FBDB
- rednet.send(id, textutils.serialize(adminSendData), "fbtpa")
- -- Wait for the DB to respond.
- local senderId, response, protocol = rednet.receive("fbtpa")
- local data = textutils.unserialize(response)
- local crops = data.crops
- --Grab the monitor to the right
- mon.clear()
- mon.setCursorPos(1,1)
- for k,v in pairs(crops) do
- mon.write("Crop: " .. v.name)
- newLine()
- mon.write("Seed:" .. v.seed)
- newLine()
- end
- end
- -- END VIEW CROP LIST --
- -- FORCE UPDATE --
- function forceUpdate()
- print("Forcing update to newest script...")
- fs.delete("startup")
- shell.run("pastebin get XHrCpCVV startup")
- print("Done forcing update.")
- os.sleep(10)
- end
- -- END FORCE UPDATE
- -- GET BOT START LOCATION --
- function getBotStartLocation()
- -- Step 1: Ask the user what ID the database is.
- term.clear()
- print(" Bot Start Location (1/2)")
- print("---------------------------------------------------")
- print("All FarmBot start locations are stored in the DB. Therefore, you must enter in a valid FarmBotDB ID.")
- print(" ")
- print(" ")
- term.write("FarmBotDB ID: ")
- local dbId = tonumber(read())
- -- Step 2: Ask what the FarmBot ID is.
- term.clear()
- print(" Bot Start Location (2/2)")
- print("---------------------------------------------------")
- 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.")
- print(" ")
- print(" ")
- term.write("FarmBot ID: ")
- local id = tonumber(read())
- term.clear()
- -- Step 3: Fill in the information to the table.
- adminSendData.data = { id = id }
- adminSendData.request = "gps_start"
- -- Step 4: Send this information to the DB so it can respond back.
- rednet.send(dbId, textutils.serialize(adminSendData), "fbtpa")
- -- Step 5: Receive the response from the DB.
- local senderId, response, protocol = rednet.receive("fbtpa")
- response = textutils.unserialize(response)
- -- Step 6: Display on console.
- mon.clear()
- mon.write(" Bot #" .. id .. " Start Location")
- mon.write("---------------------------------------------------")
- mon.write("X: ", response.gps.x)
- mon.write("Y: ", response.gps.y)
- mon.write("Z: ", response.gps.z)
- end
- -- END GET BOT START LOCATION --
- --[[
- This is where the script starts... Do not edit this.
- ]]--
- if modem ~= nil then
- if rednet.isOpen(modem) == false then
- rednet.open(modem)
- end
- -- The default protocol for admin control terminals is fbtpa
- rednet.host("fbtpa", os.getComputerLabel())
- rednet.host("fbtp", os.getComputerLabel())
- if monitor ~= nil then
- while admin.input ~= "x" do
- mainMenu()
- end
- -- The user has decided to shut down.
- term.clear()
- print("TERMINATED: To startup the script again, please do 'ctrl + s'.")
- else
- print("FATAL ERROR: You need to connect at least 1 monitor to this terminal.")
- end
- else
- print("FATAL ERROR: You need to have a modem connected to your terminal...")
- end
Add Comment
Please, Sign In to add comment