Ajustor

sgMonitor

May 7th, 2018
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. local component = require("component")
  2. local sg = component.stargate
  3. local event = require("event")
  4. local os = require("os")
  5. local gpu = component.gpu
  6.  
  7.  
  8. local colors = { blue = 0x4286F4, purple = 0xB673d6, red = 0xC14141, green = 0xDA841,
  9.   black = 0x000000, white = 0xFFFFFF, grey = 0x47494C, lightGrey = 0xBBBBBB, orange = 0xFFA500, alert = 0xFF0000}
  10. local selected = 0
  11. local w, h = gpu.getResolution()
  12. local addresseArray = {}
  13. local addresseLength = 0
  14. local entry = 0
  15. local tmpAdd = ""
  16. local secured = false
  17. local is_ended = false
  18.  
  19. gpu.setForeground(0xffffff)
  20.  
  21. function parseAddress(add)
  22.   return string.format("%s-%s-%s",string.sub(add,1,4),string.sub(add,5,7),string.sub(add,8))
  23. end
  24.  
  25. function alert(text)
  26.   gpu.fill((w/2+w/4), 1, w/4, 1, " ")
  27.   gpu.setForeground(colors.alert)
  28.   gpu.set((w/2+w/4), 1 , text)
  29.   gpu.setForeground(colors.white)
  30. end
  31.  
  32. function getAddress()
  33.   file = io.open("address", "r")
  34.   if file == nil then
  35.     file = io.open("address", "w")
  36.     file:close()
  37.     file = io.open("address", "r")
  38.   end
  39.   while file:read() ~= nil do
  40.     if addresseLength>0 then
  41.       file:seek("set",9*addresseLength+1)
  42.     else
  43.       file:seek("set",9*addresseLength)
  44.     end
  45.     addresseArray[addresseLength] = file:read()
  46.     addresseLength = addresseLength+1
  47.   end
  48.   file:close()
  49. end
  50.  
  51. function drawList()
  52. for i=5, h  do
  53.     if i%2==0 then
  54.       gpu.setBackground(colors.grey)
  55.     else
  56.       gpu.setBackground(colors.lightGrey)
  57.     end
  58.     gpu.fill(1,i,w/2,1," ")
  59.     if (i-5) < addresseLength then
  60.       gpu.set(1,i, parseAddress(addresseArray[i-5]))
  61.     end
  62.   end
  63. end
  64.  
  65. function drawControls()
  66.   gpu.setBackground(colors.green)
  67.   gpu.fill(w/2+w/4, 5, w, 10, " ")
  68.   gpu.set((w/2+w/4)+(w-(w/2+w/4))/2, 10, "CALL")
  69.  
  70.   gpu.setBackground(colors.grey)
  71.   gpu.fill(w/2+w/6, 20, w/4, 1, " ")
  72.   gpu.setBackground(colors.black)
  73.   gpu.set((w/2+w/4), 16, "ADD ADDRESS")
  74.  
  75.   gpu.setBackground(colors.blue)
  76.   gpu.set((w/2+w/4)+(w/4)/2, 23, " SAVE ")
  77.  
  78.   gpu.setBackground(colors.orange)
  79.   gpu.set((w/2+w/6), 23, "CANCEL")
  80.  
  81.   gpu.setBackground(colors.red)
  82.   gpu.fill(w/2+1, 5, w/4, 10, " ")
  83.   gpu.set((w/2+(w/4)/2), 10, "DISCONNECT")
  84.   if sg.irisState() ~= "Offline" then
  85.     if sg.irisState() == "Closing" then
  86.       gpu.setBackground(colors.red)
  87.     else
  88.       gpu.setBackground(colors.green)
  89.     end
  90.     gpu.fill(w/2+1, 28, w, 10, " ")
  91.     gpu.set((w/2+w/4)-11, 33, "TOGGLE IRIS")
  92.   end
  93.   gpu.setBackground(colors.black)
  94. end
  95.  
  96. function main()
  97.   gpu.setBackground(colors.black)
  98.   getAddress()
  99.   gpu.fill(1, 1, w, h, " ")
  100.   gpu.set(w/2-6,1, parseAddress(sg.localAddress()))
  101.   drawList()
  102.   drawControls()
  103. end
  104.  
  105. main()
  106.  
  107. function clickChecker(name, id, x, y)
  108.   if x < w/2 and y >= 5 then
  109.     drawList()
  110.     selected = y-5
  111.     gpu.setBackground(colors.blue)
  112.     gpu.fill(1,y,w/2,1, " ")
  113.     if selected < addresseLength then
  114.       gpu.set(1,y,parseAddress(addresseArray[selected]))
  115.     end
  116.   elseif x > w/2+w/4 and y>=5 and y<=10 then
  117.     if addresseArray[selected] == nil then
  118.       alert("PLEASE SELECT AN ADDRESS")
  119.     else
  120.       sg.dial(addresseArray[selected])
  121.       if sg.irisState() ~= "Closed" then
  122.         sg.closeIris()
  123.       end
  124.     end
  125.   elseif x >= ((w/2+w/4)+(w/4)/2) and x <= (((w/2+w/4)+(w/4)/2)+6) and y==23 then
  126.     if entry == 9 then
  127.       file = io.open("address", "a")
  128.       file:write(tmpAdd.."\n")
  129.       file:close()
  130.       tmpAdd = ""
  131.       entry = 0
  132.       gpu.setBackground(colors.grey)
  133.       gpu.fill(w/2+w/6, 20, w/4, 1, " ")
  134.       gpu.setBackground(colors.black)
  135.       main()
  136.     else
  137.       alert("PLEASE ENTER A COMPLETE ADDRESS")
  138.     end
  139.   elseif x >= (w/2+w/6) and x <= (w/2+w/6)+6 and y==23 then
  140.     tmpAdd = ""
  141.     entry = 0
  142.     gpu.setBackground(colors.grey)
  143.     gpu.fill(w/2+w/6, 20, w/4, 1, " ")
  144.     gpu.setBackground(colors.black)
  145.   elseif x > w/2 and x < w/2+w/4 and y>=5 and y<=15 then
  146.     sg.disconnect()
  147.   elseif x > w/2 and y >= 28 and y<=38 and sg.irisState() ~= "Offline" then
  148.     if sg.irisState() == "Closed" then
  149.       sg.openIris()
  150.     else
  151.       sg.closeIris()
  152.     end
  153.     drawControls()
  154.   end
  155.   gpu.setBackground(colors.black)
  156. end
  157.  
  158. function keyPress(e,add,key)
  159.   gpu.setBackground(colors.grey)
  160.   if ((key >=97 and key <= 122) or (key >= 48 and key <= 57)) and entry < 9 then
  161.     gpu.set(w/2+w/6+entry, 20, string.upper(string.char(key)))
  162.     tmpAdd = tmpAdd .. string.upper(string.char(key))
  163.     entry = entry+1
  164.   elseif key == 8 and entry > 0 then
  165.     entry = entry-1
  166.     gpu.set(w/2+w/6+entry, 20, " ")
  167.     tmpAdd = string.sub(tmpAdd,1,entry)
  168.   end
  169.   gpu.setBackground(colors.black)
  170. end
  171.  
  172. function isCalling(this, src, remote)
  173.   gpu.setBackground(colors.alert)
  174.   gpu.fill(w/4, h/4, w/2, h/2, " ")
  175.   gpu.set(w/4+w/6, h/2, remote.." is calling")
  176.   os.sleep(3)
  177.   if (secured and ifIn(remote) == false and sg.irisState() ~= "Closed") then
  178.         sg.closeIris()
  179.       end
  180.   main()
  181. end
  182.  
  183. function ifIn(remote)
  184.  local check = false
  185.  for i=0, addresseLength  do
  186.       check = (parseAddress(addresseArray[i]) == remote)
  187.   end
  188.   return check;
  189. end
  190.  
  191. function stop()
  192.     print("soft interrupt, closing")
  193.     event.ignore("key_up",keyPress)
  194.     event.ignore("sgDialIn", isCalling)
  195.     event.ignore("touch", clickChecker)
  196. is_ended = true
  197. end
  198.  
  199. event.listen("key_up", keyPress)
  200. event.listen("sgDialIn", isCalling)
  201. event.listen("touch", clickChecker)
  202. event.listen("interrupted", stop)
  203.  
  204. while not is_ended do
  205.   os.sleep(15)
  206. end
Advertisement
Add Comment
Please, Sign In to add comment