Advertisement
rhbvkleef

updateAEViewer

Mar 26th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.24 KB | None | 0 0
  1. --Please download it by executing "pastebin get 4nRg9CHU json"
  2. -- in the same directory as this file.
  3. os.loadAPI("json")
  4.  
  5. --Delay after each loop (seconds)
  6. -- Note: Currently the AE system scanning is very slow. This is an arbitrary number.
  7. updateInterval = 10
  8.  
  9. --Server auth and connection data
  10. username = "<YOUR USERNAME HERE>"
  11. password = "<YOUR PASSWORD HERE>"
  12. APIUrl = "http://aeviewer-rhbvkleef.rhcloud.com/updateAESystem"
  13.  
  14. --AE system
  15. ae = peripheral.wrap("left")
  16.  
  17. --Information monitor
  18. mon = peripheral.wrap("top")
  19.  
  20. --The state of the AE system since the last check
  21. previousAEState = ""
  22.  
  23. --Should retry next tick?
  24. re = false
  25.  
  26. --Available log levels, used for determining the severity of the logging action
  27. LogLevel = {
  28.   deb="debug",
  29.   info="info",
  30.   warn="warning",
  31.   err="error"
  32. }
  33.  
  34. --Used for clearing a line on a monitor
  35. whitespace = "                                                                                                                   "
  36.  
  37. --Store current state of AE system in memory
  38. function updateChangeCatalog()
  39.     local state = getCurrentAECatalog()
  40.     if state then
  41.         previousAEState = state
  42.     end
  43. end
  44.  
  45. function getCurrentAECatalog()
  46.     local itemList = {}
  47.     local items = ae.getAvailableItems()
  48.     for i = 1, #items do
  49.         --There is still something wrong here.
  50.         if(items[i] ~= nil) then
  51.             if(ae.getItemDetail(items[i].fingerprint) ~= nil and ae.getItemDetail(items[i].fingerprint).basic ~= nil) then
  52.                 items[i].display_name = textutils.urlEncode(
  53.                     ae.getItemDetail(
  54.                         items[i].fingerprint
  55.                     )
  56.                     .basic()
  57.                     .display_name
  58.                 )
  59.             else
  60.                 items[i].display_name = "Unknown"
  61.             end
  62.         end
  63.     end
  64.     return items
  65. end
  66.  
  67. --Compare current state of AE system with
  68. -- the stored state
  69. function checkAEHasChanged()
  70.     if re then
  71.         re = false
  72.         return true
  73.     end
  74.     if not ae.getAvailableItems()[1] then
  75.         log("no items found! Is there power?", LogLevel.warn)
  76.         return false
  77.     end
  78.     return not deepcompare(previousAEState, getCurrentAECatalog(), true);
  79. end
  80.  
  81. --Sends an update packet to the database server
  82. -- TODO: implement this method
  83. function sendAEUpdatePacket()
  84.     --Send packet
  85.     local handle = http.post(
  86.     APIUrl,
  87.         "aeSystem=" .. textutils.urlEncode(json.encode(previousAEState)) .. "&" ..
  88.         "username=" .. textutils.urlEncode(username) .. "&" ..
  89.         "password=" .. textutils.urlEncode(password)
  90.     )
  91.    
  92.     --Check for success response
  93.     if not handle then
  94.         log("failed to connect to database", LogLevel.err)
  95.         re = true
  96.     elseif not handle.getResponseCode() == 200 then
  97.         local body = json.decode(handle.readAll())
  98.         if body.status then
  99.             log("received response code " .. handle.getResponseCode() .. " from server with status"..body.status, LogLevel.warn)
  100.         else
  101.             log("received response code " .. handle.getResponseCode() .. " from server", LogLevel.warn)
  102.         end
  103.     else
  104.         log("Successfully sent update!", LogLevel.info)
  105.         mon.setCursorPos(1,1)
  106.         mon.write("Last update packet: " .. gtime() .. whitespace)
  107.     end
  108. end
  109.  
  110. --Compare two tables/objects
  111. function deepcompare(t1,t2,ignore_mt)
  112.     local ty1 = type(t1)
  113.     local ty2 = type(t2)
  114.     if ty1 ~= ty2 then
  115.         return false
  116.     end
  117.     -- non-table types can be directly compared
  118.     if ty1 ~= 'table' and ty2 ~= 'table' then
  119.         return t1 == t2
  120.     end
  121.     -- as well as tables which have the metamethod __eq
  122.     local mt = getmetatable(t1)
  123.     if not ignore_mt and mt and mt.__eq then
  124.         return t1 == t2
  125.     end
  126.     for k1,v1 in pairs(t1) do
  127.         local v2 = t2[k1]
  128.         if v2 == nil or not deepcompare(v1,v2) then
  129.             return false
  130.         end
  131.     end
  132.     for k2,v2 in pairs(t2) do
  133.         local v1 = t1[k2]
  134.         if v1 == nil or not deepcompare(v1,v2) then
  135.             return false
  136.         end
  137.     end
  138.     return true
  139. end
  140.  
  141. -- Get the time
  142. function gtime()
  143.     local handle = http.get("http://www.timeapi.org/west/now?format=%20%25l:%25M%20%25p")
  144.     if handle then
  145.         t = string.sub(handle.readAll(), 2, -1)
  146.         if string.sub(t, 1, 1) == " " then
  147.             return string.sub(t, 2, -1)
  148.         else
  149.             return t
  150.         end
  151.     else
  152.         mon.setCursorPos(1,2)
  153.         mon.write("Last critical message: " .. LogLevel.warn .. " [unknown]" .. whitespace)
  154.         mon.setCursorPos(1,3)
  155.         mon.write("Could not request time from internet!" .. whitespace)
  156.         return "NOINTERNET"
  157.     end
  158. end
  159.  
  160. --Logs the message to the appropriate places
  161. function log(message, logLvl)
  162.     local theTime = gtime()
  163.     if not logLvl then
  164.         logLvl = LogLevel.info
  165.     end
  166.     print("["..theTime.."] "..logLvl..": "..message)
  167.     if logLvl == LogLevel.warn or logLvl == LogLevel.err then
  168.         mon.setCursorPos(1,2)
  169.         mon.write("Last critical message: " .. logLvl .. " [" .. theTime .. "]" .. whitespace)
  170.         mon.setCursorPos(1,3)
  171.         mon.write(message .. whitespace)
  172.     end
  173.     mon.setCursorPos(1,4)
  174.     mon.write("Last debug message: " .. logLvl .. " [" .. theTime .. "]" .. whitespace)
  175.     mon.setCursorPos(1,5)
  176.     mon.write(message .. whitespace)
  177. end
  178.  
  179. --Initialize display
  180. mon.setCursorPos(1,1)
  181. mon.write("Last update packet:")
  182. mon.setCursorPos(1,2)
  183. mon.write("Last critical message:")
  184. mon.setCursorPos(1,4)
  185. mon.write("Last debug message:")
  186.  
  187. --Store initial AE state and upload to database
  188. updateChangeCatalog()
  189. sendAEUpdatePacket()
  190. while true do
  191.     os.sleep(updateInterval)
  192.     if checkAEHasChanged() then
  193.         updateChangeCatalog()
  194.         sendAEUpdatePacket()
  195.     else
  196.         --log("AE system has not changed", "info")
  197.     end
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement