Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local base_url = "http://73.181.45.168:5005/"
- os.loadAPI("squidlib/common.lua")
- function determine_tools()
- slot = common.findEmptySlot(true)
- if slot then
- turtle.equipRight()
- right_tool = nil
- if turtle.getItemCount() > 0 then
- right_tool = turtle.getItemDetail().name
- turtle.equipRight()
- end
- turtle.equipLeft()
- left_tool = nil
- if turtle.getItemCount() > 0 then
- left_tool = turtle.getItemDetail().name
- turtle.equipLeft()
- end
- return left_tool, right_tool
- end
- return false
- end
- function basic_post(postdata, endpoint)
- if not postdata then return end
- local postdata_pairs = {}
- for k,d in pairs(postdata) do
- local pairstring = k .. "=" .. d
- table.insert(postdata_pairs, pairstring)
- end
- local poststring = table.concat(postdata_pairs, "&")
- endpoint = endpoint or ""
- local result = http.post(base_url .. endpoint, poststring)
- local response = false
- if result then
- response = result.readAll()
- result.close()
- end
- return response
- end
- function basePostData()
- local post_data = {}
- post_data["computer_id"] = os.getComputerID()
- if turtle then
- post_data["fuel"] = turtle.getFuelLevel()
- end
- local exists, side = common.findModem()
- if exists then
- local x, y, z = gps.locate(.2)
- if z then
- post_data["x_position"] = x
- post_data["y_position"] = y
- post_data["z_position"] = z
- end
- end
- return post_data
- end
- function register()
- print("Registering With Python Server")
- local post_data = basePostData()
- post_data["label"] = os.getComputerLabel()
- post_data["computer_type"] = "computer"
- if turtle then
- post_data["computer_type"] = "turtle"
- local left_tool, right_tool = determine_tools()
- if right_tool then
- post_data["tool_right"] = right_tool
- end
- if left_tool then
- post_data["tool_left"] = left_tool
- end
- end
- local exists, side = common.findModem()
- if exists then
- post_data["modem_side"] = side
- end
- basic_post(post_data, "computers/register/")
- end
- function update()
- local post_data = basePostData()
- basic_post(post_data, "computers/update/")
- end
- function download_file(url, file, dir)
- dir = dir or ""
- local filepath = fs.combine(dir, file)
- local content = http.get(base_url .. url).readAll()
- if not content then
- error("Could not connect to website")
- end
- local f = fs.open(filepath, "w")
- f.write(content)
- f.close()
- end
- local function compile_items_data()
- if not turtle then
- return false
- end
- local inventory = {}
- for i=1, 16 do
- if turtle.getItemCount(i) > 0 then
- local detail = turtle.getItemDetail(i)
- table.insert(inventory, {
- name = detail.name,
- count = detail.count,
- slot = i
- })
- end
- end
- return inventory
- end
- function getWorkOrder()
- print("Asking For Work...")
- local post_data = basePostData()
- post_data['inventory'] = textutils.serializeJSON(compile_items_data())
- local work_order_json = basic_post(post_data, "computers/get_work/")
- if not work_order_json then
- return false
- end
- local work_order = textutils.unserializeJSON(work_order_json)
- if work_order.has_work then
- print("Got Work!")
- return work_order
- end
- print("No Work!")
- return false
- end
- function workCompleted(work_order)
- print('Notifying Server of Work Completion')
- local post_data = basePostData()
- post_data.work_id = work_order.id
- basic_post(post_data, "computers/work_completed/")
- end
- -- register()
- --update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement