View difference between Paste ID: 9Pz7P9zU and 14guQppF
SHOW: | | - or go back to the newest paste.
1
-- Override the terminate event
2
local oldPullEvent = os.pullEvent
3
os.pullEvent = os.pullEventRaw
4
5
base_url = "http://Gateburn.asuscomm.com/"
6
local get_command_url = base_url .. "api/LOOK_OVER_HERE"
7
local cc_id = os.getComputerLabel()
8
9
assistant_name = "core"
10
11
-- lib
12
function Split(s, delimiter)
13
  result = {};
14
  for match in (s..delimiter):gmatch("(.-)"..delimiter) do
15
      table.insert(result, match);
16
  end
17
  return result;
18
end
19
20
local TIMEOUT_SECONDS = 2 -- Adjust this to the desired timeout duration
21
22
function get_files_from_server()
23
  local function downloadFiles()
24
    repeat res = http.get(base_url .. "api/coreFileNames", {["Content-Type"] = "application/json"})
25
	local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to connect to API..." }), { ["Content-Type"] = "application/json" })
26
    until res
27
    dl_files = res.readAll()
28
    dl_files = dl_files:sub(2, #dl_files - 1):gsub("\"", "")
29
	res.close()
30
	--print(dl_files)
31
32
    term.write("autoupdate...")
33
    for _, name in pairs(Split(dl_files, ",")) do
34
      local res = http.get(base_url .. assistant_name .. "/" .. name)
35
	  local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to obtain file... " .. name }), { ["Content-Type"] = "application/json" })
36
      if not res then
37
	  local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Error on file request" }), { ["Content-Type"] = "application/json" })
38
        print("Error on GET " .. base_url .. assistant_name .. "/" .. name)
39
40
      else
41
        local f = fs.open(name, "w")
42
        f.write(res.readAll())
43
        f.close()
44
        if name == "startup" then
45
          fs.makeDir("backup")
46
          fs.delete("backup/startup")
47
          fs.copy("startup", "backup/startup")
48
        end
49
		res.close()
50
      end
51
    end
52
    term.write("complete\n")
53
	local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to obtain files... COMPLETE?" }), { ["Content-Type"] = "application/json" })
54
  end
55
  
56
local function downloadAPI()
57
    -- Attempt to connect to the API (First API request)
58
    local res_api = nil
59
    repeat
60
        res_api = http.get(base_url .. "api/api", {["Content-Type"] = "application/json"})
61
        local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to connect to API..." }), { ["Content-Type"] = "application/json" })
62
    until res_api -- Continue retrying until we successfully get a response
63
64
    -- Try to download the Lua file (Second request)
65
    local res_api_lua = http.get(base_url .. "api/api.lua")
66
    local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Attempt to obtain api.lua file..." }), { ["Content-Type"] = "application/json" })
67
68
    -- Handle errors if the Lua file download fails
69
    if not res_api_lua then
70
        local response = http.post(get_command_url, textutils.serializeJSON({ id = tostring(cc_id), message = "Error on file request for api.lua" }), { ["Content-Type"] = "application/json" })
71
        print("Error on GET " .. base_url .. "api/api.lua")
72
    else
73
        -- Successfully received the Lua file, save it locally
74
        local f = fs.open("api.lua", "w")
75
        f.write(res_api_lua.readAll())
76
        f.close()
77
        print("Successfully downloaded api.lua")
78
    end
79
80
    -- Close both response objects
81
    if res_api then res_api.close() end
82
    if res_api_lua then res_api_lua.close() end
83
end
84
85
86
  local function timeoutTimer()
87
    sleep(TIMEOUT_SECONDS) -- Wait for the timeout duration
88
  end
89
90
  if parallel.waitForAny(downloadFiles, downloadAPI, timeoutTimer) == 2 then
91
    -- If the timeoutTimer finishes first, we know the downloadFiles function took too long.
92
    print("Operation timed out. Moving on...")
93
  end
94
end
95
96
-- autoupdate
97
get_files_from_server()
98
sleep(0.5)
99
shell.run("main.lua")