Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Please download it by executing "pastebin get 4nRg9CHU json"
- -- in the same directory as this file.
- os.loadAPI("json")
- --Delay after each loop (seconds)
- -- Note: Currently the AE system scanning is very slow. This is an arbitrary number.
- updateInterval = 10
- --Server auth and connection data
- username = "<YOUR USERNAME HERE>"
- password = "<YOUR PASSWORD HERE>"
- APIUrl = "http://aeviewer-rhbvkleef.rhcloud.com/updateAESystem"
- --AE system
- ae = peripheral.wrap("left")
- --Information monitor
- mon = peripheral.wrap("top")
- --The state of the AE system since the last check
- previousAEState = ""
- --Should retry next tick?
- re = false
- --Available log levels, used for determining the severity of the logging action
- LogLevel = {
- deb="debug",
- info="info",
- warn="warning",
- err="error"
- }
- --Used for clearing a line on a monitor
- whitespace = " "
- --Store current state of AE system in memory
- function updateChangeCatalog()
- local state = getCurrentAECatalog()
- if state then
- previousAEState = state
- end
- end
- function getCurrentAECatalog()
- local itemList = {}
- local items = ae.getAvailableItems()
- for i = 1, #items do
- --There is still something wrong here.
- if(items[i] ~= nil) then
- if(ae.getItemDetail(items[i].fingerprint) ~= nil and ae.getItemDetail(items[i].fingerprint).basic ~= nil) then
- items[i].display_name = textutils.urlEncode(
- ae.getItemDetail(
- items[i].fingerprint
- )
- .basic()
- .display_name
- )
- else
- items[i].display_name = "Unknown"
- end
- end
- end
- return items
- end
- --Compare current state of AE system with
- -- the stored state
- function checkAEHasChanged()
- if re then
- re = false
- return true
- end
- if not ae.getAvailableItems()[1] then
- log("no items found! Is there power?", LogLevel.warn)
- return false
- end
- return not deepcompare(previousAEState, getCurrentAECatalog(), true);
- end
- --Sends an update packet to the database server
- -- TODO: implement this method
- function sendAEUpdatePacket()
- --Send packet
- local handle = http.post(
- APIUrl,
- "aeSystem=" .. textutils.urlEncode(json.encode(previousAEState)) .. "&" ..
- "username=" .. textutils.urlEncode(username) .. "&" ..
- "password=" .. textutils.urlEncode(password)
- )
- --Check for success response
- if not handle then
- log("failed to connect to database", LogLevel.err)
- re = true
- elseif not handle.getResponseCode() == 200 then
- local body = json.decode(handle.readAll())
- if body.status then
- log("received response code " .. handle.getResponseCode() .. " from server with status"..body.status, LogLevel.warn)
- else
- log("received response code " .. handle.getResponseCode() .. " from server", LogLevel.warn)
- end
- else
- log("Successfully sent update!", LogLevel.info)
- mon.setCursorPos(1,1)
- mon.write("Last update packet: " .. gtime() .. whitespace)
- end
- end
- --Compare two tables/objects
- function deepcompare(t1,t2,ignore_mt)
- local ty1 = type(t1)
- local ty2 = type(t2)
- if ty1 ~= ty2 then
- return false
- end
- -- non-table types can be directly compared
- if ty1 ~= 'table' and ty2 ~= 'table' then
- return t1 == t2
- end
- -- as well as tables which have the metamethod __eq
- local mt = getmetatable(t1)
- if not ignore_mt and mt and mt.__eq then
- return t1 == t2
- end
- for k1,v1 in pairs(t1) do
- local v2 = t2[k1]
- if v2 == nil or not deepcompare(v1,v2) then
- return false
- end
- end
- for k2,v2 in pairs(t2) do
- local v1 = t1[k2]
- if v1 == nil or not deepcompare(v1,v2) then
- return false
- end
- end
- return true
- end
- -- Get the time
- function gtime()
- local handle = http.get("http://www.timeapi.org/west/now?format=%20%25l:%25M%20%25p")
- if handle then
- t = string.sub(handle.readAll(), 2, -1)
- if string.sub(t, 1, 1) == " " then
- return string.sub(t, 2, -1)
- else
- return t
- end
- else
- mon.setCursorPos(1,2)
- mon.write("Last critical message: " .. LogLevel.warn .. " [unknown]" .. whitespace)
- mon.setCursorPos(1,3)
- mon.write("Could not request time from internet!" .. whitespace)
- return "NOINTERNET"
- end
- end
- --Logs the message to the appropriate places
- function log(message, logLvl)
- local theTime = gtime()
- if not logLvl then
- logLvl = LogLevel.info
- end
- print("["..theTime.."] "..logLvl..": "..message)
- if logLvl == LogLevel.warn or logLvl == LogLevel.err then
- mon.setCursorPos(1,2)
- mon.write("Last critical message: " .. logLvl .. " [" .. theTime .. "]" .. whitespace)
- mon.setCursorPos(1,3)
- mon.write(message .. whitespace)
- end
- mon.setCursorPos(1,4)
- mon.write("Last debug message: " .. logLvl .. " [" .. theTime .. "]" .. whitespace)
- mon.setCursorPos(1,5)
- mon.write(message .. whitespace)
- end
- --Initialize display
- mon.setCursorPos(1,1)
- mon.write("Last update packet:")
- mon.setCursorPos(1,2)
- mon.write("Last critical message:")
- mon.setCursorPos(1,4)
- mon.write("Last debug message:")
- --Store initial AE state and upload to database
- updateChangeCatalog()
- sendAEUpdatePacket()
- while true do
- os.sleep(updateInterval)
- if checkAEHasChanged() then
- updateChangeCatalog()
- sendAEUpdatePacket()
- else
- --log("AE system has not changed", "info")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement