Guest User

monitor

a guest
May 9th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.45 KB | None | 0 0
  1. --Savage_Me55iah--
  2.  
  3. --local tArgs = { ... }
  4. --local first_run = tArgs[1] or false
  5.  
  6. --TABLES--
  7. print("tables")
  8.  
  9. names = {}
  10. ids = {}
  11. status = {}
  12.  
  13. function add_button(command, id, on_off)
  14.   num = table.maxn(names) + 1
  15.  
  16.   names[num] = command
  17.   ids[num] = id
  18.   status[num] = on_off
  19. end
  20.  
  21. add_button("online", 0, true)
  22.  
  23. local load_save_loc = {
  24.   ["load_save_loc"] = "load_save_loc",
  25.   ["settings"] = "settings",
  26.   ["names"] = "names",
  27.   ["ids"] = "ids",
  28.   ["status"] = "status",
  29.   ["eu"] = "eu",
  30.   ["mj"] = "mj",
  31. }
  32.  
  33. local settings = {
  34.   ["heading color"] = colors.yellow,
  35.   ["heading background"] = colors.black,
  36.   ["heading"] = "Hello I am Jarvis at your service Sir...",
  37.   ["active color"] = colors.green,
  38.   ["inactive color"] = colors.red,
  39.   ["id"] = os.getComputerID(),
  40.   ["label"] = "Server",
  41.   ["refresh_rate"] = 60,
  42.   ["text_scale"] = 1,
  43.   ["button_width"] = 5,
  44.   ["energy_stats"] = false,
  45.   ["open_peripherals"] = false,
  46.   ["direct_power"] = false,
  47.   ["energy_room"] = 0,
  48.   ["eu_id"] = 1000,
  49.   ["mj_id"] = 1001,
  50.   ["screen_saver"] = true,
  51.   ["timeout"] = 10,
  52.   ["image_x"] = 27,
  53.   ["image_y"] = 9,
  54. }
  55.  
  56. local button_x1 = {
  57.   [1] = 2
  58. }
  59.  
  60. local button_x2 = {
  61.   [1] = 12
  62. }
  63.  
  64. local button_y1 = {
  65.   [1] = 4
  66. }
  67.  
  68. local eu = {
  69.   ["max"] = 0,
  70.   ["current"] = 0,
  71.   ["percent"] = 0
  72. }
  73.  
  74. local mj = {
  75.   ["max"] = 0,
  76.   ["current"] = 0,
  77.   ["percent"] = 0
  78. }
  79.  
  80. if not fs.exists("settings") then print("Enter computer label: ") os.setComputerLabel(read() or settings["label"]) end
  81.  
  82. --LOAD & SAVE FUNCTIONS--
  83.  
  84. function load()
  85.   print("Loading...")
  86.   local fileHandle = fs.open (load_save_loc["load_save_loc"], 'r')
  87.   load_save_loc = textutils.unserialize (fileHandle.readAll())
  88.   fileHandle.close()
  89.  
  90.   local fileHandle = fs.open (load_save_loc["settings"], 'r')
  91.   settings = textutils.unserialize (fileHandle.readAll())
  92.   fileHandle.close()
  93.  
  94.   local fileHandle = fs.open (load_save_loc["names"], 'r')
  95.   names = textutils.unserialize (fileHandle.readAll())
  96.   fileHandle.close()
  97.  
  98.   local fileHandle = fs.open (load_save_loc["ids"], 'r')
  99.   ids = textutils.unserialize (fileHandle.readAll())
  100.   fileHandle.close()
  101.  
  102.   local fileHandle = fs.open (load_save_loc["status"], 'r')
  103.   status = textutils.unserialize (fileHandle.readAll())
  104.   fileHandle.close()
  105.  
  106.   local fileHandle = fs.open (load_save_loc["eu"], 'r')
  107.   eu = textutils.unserialize (fileHandle.readAll())
  108.   fileHandle.close()
  109.  
  110.   local fileHandle = fs.open (load_save_loc["mj"], 'r')
  111.   mj = textutils.unserialize (fileHandle.readAll())
  112.   fileHandle.close()
  113. end
  114.  
  115. function save()
  116.   print("Saving...")
  117.   local fileHandle = fs.open (load_save_loc["load_save_loc"], 'w')
  118.   fileHandle.write (textutils.serialize (load_save_loc))
  119.   fileHandle.close()
  120.  
  121.   local fileHandle = fs.open (load_save_loc["settings"], 'w')
  122.   fileHandle.write (textutils.serialize (settings))
  123.   fileHandle.close()
  124.  
  125.   local fileHandle = fs.open (load_save_loc["names"], 'w')
  126.   fileHandle.write (textutils.serialize (names))
  127.   fileHandle.close()
  128.  
  129.   local fileHandle = fs.open (load_save_loc["ids"], 'w')
  130.   fileHandle.write (textutils.serialize (ids))
  131.   fileHandle.close()
  132.  
  133.   local fileHandle = fs.open (load_save_loc["status"], 'w')
  134.   fileHandle.write (textutils.serialize (status))
  135.   fileHandle.close()
  136.  
  137.   local fileHandle = fs.open (load_save_loc["eu"], 'w')
  138.   fileHandle.write (textutils.serialize (eu))
  139.   fileHandle.close()
  140.  
  141.   local fileHandle = fs.open (load_save_loc["mj"], 'w')
  142.   fileHandle.write (textutils.serialize (mj))
  143.   fileHandle.close()
  144. end
  145.  
  146.  
  147. --PERLIMINARY WORKINGS--
  148.  
  149. function imagewrite(code)
  150.   outputFile:write("/n")
  151.   outputFile:write(code)
  152. end
  153.  
  154. image_table = {[1] = "eeeeeeeeeeeeeeeeeeeeeeeeeee",
  155.   [2] = "eeeeeffffffffeefffffefffffe",
  156.   [3] = "eeeeff0000f0feef000ffff00fe",
  157.   [4] = "eeeef0fffffffeef0f00f00f0fe",
  158.   [5] = "eeeeff00ffeeeeef0ff000ff0fe",
  159.   [6] = "efffffff0feeeeefffff0fffffe",
  160.   [7] = "ef0f0000ffeeeeef0fefffef0fe",
  161.   [8] = "efffffffffeeeeefffeeeeefffe",
  162.   [9] = "eeeeeeeeeeeeeeeeeeeeeeeeeee"
  163. }
  164.  
  165. if not fs.exists("image") then
  166.   save()
  167.  
  168. --  fs.delete("startup")
  169. --  outputFile = io.open("startup", "a")
  170. --  text = tostring(shell.getRunningProgram())
  171. --  outputFile:write("shell.run(" .. text .. ")")
  172. --  outputFile:close()
  173.  
  174.   fs.delete("image")
  175.   outputFile = io.open("image", "w")
  176.  
  177.  outputFile:write(image_table[1])
  178.   for num = 2,table.maxn(image_table) do
  179.     imagewrite(image_table[num])
  180.   end
  181.   num = nil
  182.   outputFile:close()
  183. else
  184.   load()
  185. end
  186.  
  187. function find_peripheral(t, e)
  188.   e = e or nil
  189.   sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
  190.   peripheral_found = false
  191.   for num=1, 6 do
  192.     type = peripheral.getType(sides[num])
  193.     if type == t then
  194.       if e == "wireless" then
  195.         if peripheral.call(sides[num], "isWireless") then
  196.           return sides[num]
  197.         end
  198.       elseif e == "remote" then
  199.         if not peripheral.call(sides[num], "isWireless") then
  200.           return sides[num]
  201.         end
  202.       elseif e == nil then
  203.         return sides[num]
  204.       end
  205.     end
  206.   end
  207.   if peripheral_found ~= true then
  208.     error("No " .. t .. " attached to the computer", 0)
  209.   end
  210. end
  211.  
  212. -- MODEM REMOTE CONNECTIONS --
  213.  
  214. mod = peripheral.wrap(find_peripheral("modem", "remote"))
  215. local remote_peripherals = mod.getNamesRemote()
  216. remote_types = {}
  217. remote_monitors = {[1] = peripheral.wrap(find_peripheral("monitor"))}
  218. remote_rec = {}
  219. remote_eu = {}
  220. a = 1
  221. if remote_monitors[1] == nil then b = 1 else b = 2 end
  222. c = 1
  223. d = 1
  224. while remote_peripherals[a] ~= nil do
  225.   remote_types[a] = mod.getTypeRemote(remote_peripherals[a])
  226.   if remote_types[a] == "monitor" then
  227.     remote_monitors[b] = peripheral.wrap(remote_peripherals[a])
  228.     b = b + 1
  229.   end
  230.   if settings["open_peripherals"] and settings["energy_stats"] and settings["direct_power"] then
  231.     if remote_types[a] == "cofh_thermalexpansion_energycell" then
  232.       remote_rec[c] = peripheral.wrap(remote_peripherals[a])
  233.       c = c + 1
  234.     end
  235.     if remote_types[a] == "mfsu" then
  236.       remote_eu[d] = peripheral.wrap(remote_peripherals[a])
  237.       d = d + 1
  238.     end
  239.   end
  240.   a = a + 1
  241. end
  242.  
  243. rednet.open(find_peripheral("modem", "wireless"))
  244. mon = remote_monitors[1]
  245. mon.setTextScale(settings["text_scale"])
  246. oa_x, oa_y = mon.getSize()
  247.  
  248. if settings["energy_stats"] and settings["open_peripherals"] then
  249. oa_x = oa_x - 10
  250. end
  251.  
  252. reps_x = math.floor(oa_x / (settings["button_width"] + 1))
  253. reps_y = math.floor((oa_y / 2) - 1)
  254. total_buttons = reps_x * reps_y
  255. print(oa_x .. "," .. oa_y .. " - " .. reps_x .. " x " .. reps_y .. " - " .. total_buttons)
  256.  
  257.  
  258. --FUNCTIONS DRAWING MONITOR SCREEN--
  259.  
  260. print("Drawing mon screen")
  261.  
  262. function reset_text()
  263.   mon.setTextColor(colors.white)
  264.   mon.setBackgroundColor(colors.black)
  265. end
  266.  
  267. function heading()
  268.   mon.setCursorPos(2,2)
  269.   mon.setTextColor(settings["heading color"])
  270.   mon.setBackgroundColor(settings["heading background"])
  271.   mon.write(settings["heading"])
  272.   reset_text()
  273. end
  274.  
  275. function button_draw(number, xpos, ypos)
  276.   mon.setCursorPos(xpos, ypos)
  277.   button_x1[number], button_y1[number] = mon.getCursorPos()
  278.   if names[number] ~= nil then
  279.     if status[number] then
  280.       mon.setBackgroundColor(settings["active color"])
  281.     else
  282.       mon.setBackgroundColor(settings["inactive color"])
  283.     end
  284.     text = names[number]:upper()
  285.     text_length = string.len(text) + 2
  286.     if text_length > settings["button_width"] then
  287.       settings["button_width"] = text_length
  288.     end
  289.     mon.write(" " .. text .. " ")
  290.     currpos_x, currpos_y = mon.getCursorPos()
  291.     while currpos_x ~= (settings["button_width"] + xpos) do
  292.       mon.write(" ")
  293.       currpos_x, currpos_y = mon.getCursorPos()
  294.     end
  295.     button_x2[z], y = mon.getCursorPos()
  296.  
  297.     reset_text()
  298.     print("Writing button " .. number .. ": " .. names[number] .. ": " .. tostring(status[number]))
  299.   end
  300. end
  301.  
  302. -- ENERGY FUNCTIONS --
  303.  
  304. function box(color, left, top, right, bot)
  305.   term.redirect(mon)
  306.   paintutils.drawLine(right, top, left, top, color)
  307.   paintutils.drawLine(left, top, left, bot, color)
  308.   paintutils.drawLine(right, bot, left, bot, color)
  309.   paintutils.drawLine(right, top, right, bot, color)
  310.   term.restore()
  311. end
  312.  
  313. function decimal(number)
  314.   text = nil
  315.   num = nil
  316.   if number > 999 then
  317. num = number / 1000
  318. text = "K"
  319.     if num > 999 then
  320. num = num / 1000
  321. text = "M"
  322.       if num > 999 then num = num / 1000
  323. text = "B"
  324.       end
  325.     end
  326.   num = math.floor(num)
  327.   end
  328.   a = num or 0
  329.   text = text or 0
  330.   text = a .. text
  331.   return text
  332. end
  333.  
  334. function energy_compile()
  335.   local a = 1
  336.   mj["max"] = 0
  337. mj["current"] = 0
  338. mj["percent"] = 0
  339.   while remote_rec[a] ~= nil do
  340.     mj["max"] = mj["max"] + remote_rec[a].getMaxEnergyStored("north")
  341.     mj["current"] = mj["current"] + remote_rec[a].getEnergyStored("north")
  342.     a = a + 1
  343.   end
  344.  
  345.   local a = 1
  346.   eu["max"] = 0
  347.   eu["current"] = 0
  348.   eu["percent"] = 0
  349.   while remote_eu[a] ~= nil do
  350.     eu["max"] = eu["max"] + remote_eu[a].getCapacity()
  351.     eu["current"] = eu["current"] + remote_eu[a].getStored()
  352.     a = a + 1
  353.   end
  354. end
  355.  
  356.  
  357. function percentage(type)
  358.   if type == "mj" then
  359.     mj["percent"] = mj["current"] / mj["max"] * 100
  360.     return math.floor(mj["percent"])
  361.   elseif type == "eu" then
  362.     eu["percent"] = eu["current"] / eu["max"] * 100
  363.     return math.floor(eu["percent"])
  364.   end
  365. end
  366.  
  367. function greaterx()
  368.   currx = mon.getCursorPos()
  369.   if currx > endx then
  370.     endx = currx
  371.   end
  372. end
  373.  
  374. function energy_stats(startx, starty)
  375.   if settings["direct_power"] then
  376.     energy_compile()
  377.   end
  378.  
  379.   x = startx + 2
  380.   y = starty + 2
  381.  
  382.   mon.setTextColor(colors.white)
  383.  
  384.   mon.setCursorPos(x,y)
  385.   mon.write("EU: " .. decimal(eu["current"]))
  386.   endx = mon.getCursorPos()
  387.   y = y + 1
  388.  
  389.   mon.setCursorPos(x,y)
  390.   mon.write("EU: " .. decimal(eu["max"]))
  391.   greaterx()
  392.   y = y + 1
  393.  
  394.  
  395.   mon.setCursorPos(x,y)
  396.   mon.write("EU: " .. percentage("eu") .. "%")
  397.   greaterx()
  398.   y = y + 2
  399.  
  400.  
  401.   mon.setCursorPos(x,y)
  402.   mon.write("MJ: " .. decimal(mj["current"]) )
  403.   greaterx()
  404.   y = y + 1
  405.  
  406.   mon.setCursorPos(x,y)
  407.   mon.write("MJ: " .. decimal(mj["max"]) )
  408.   greaterx()
  409.   y = y + 1
  410.  
  411.  
  412.   mon.setCursorPos(x,y)
  413.   mon.write("MJ: " .. percentage("mj") .. "%")
  414.   greaterx()
  415.   endy = y + 2
  416.   endx = endx + 1
  417.   box(colors.white, startx, starty, endx, endy)
  418. end
  419.  
  420. -- MONITOR SCREEN PROGRAM --
  421.  
  422. function mon_screen()
  423.  
  424.   screen = 1
  425.   while remote_monitors[screen] ~= nil do
  426.     mon = remote_monitors[screen]
  427. --    print("drawing monitor")
  428.     mon.clear()
  429.     heading()
  430.     x = 2
  431.     y = 4
  432.     if settings["energy_stats"] then
  433. energy_stats(x, y)
  434. settings["energy_room"] = endx
  435. x = endx + 1
  436. end
  437.  
  438.     z = 1
  439.  
  440.     for num=1,reps_x do
  441.       y = 4
  442.  
  443.       for num=1,reps_y do
  444.         button_draw(z, x, y)
  445.         y = y + 2
  446.         z = z + 1
  447.       end
  448.       x = x + settings["button_width"] + 1
  449.     end
  450.     save()
  451.     screen = screen + 1
  452.   end
  453.   sleep(0.1)
  454. end
  455.  
  456. --FUNCTION BUTTON DETECT--
  457.  
  458. function select_button()
  459.   z = 1
  460.   button_pressed = nil
  461.  
  462.   repeat
  463.     if names[z] ~= nil then
  464.       if x > button_x1[z] and x < button_x2[z] and y == button_y1[z] then
  465.         return z
  466.       end
  467.     end
  468.     z = z + 1
  469.   until z > total_buttons
  470. end
  471.  
  472. function message_send()
  473.   if status[button_pressed] then
  474.     text = "deactivate"
  475.     status[button_pressed] = not status[button_pressed]
  476.   else
  477.     text = "activate"
  478.     status[button_pressed] = not status[button_pressed]
  479.   end
  480.  
  481.   rednet.send(ids[button_pressed], text)
  482.   print(names[button_pressed] .. " " .. ids[button_pressed] .. " " .. text)
  483.  
  484.   mon_screen()
  485. end
  486.  
  487. function offline()
  488.   status[1] = false
  489.   a = 2
  490.   while names[a] ~= nil do
  491.     if status[a] then
  492.       status[a] = false
  493.       text = "deactivate"
  494.       rednet.send(ids[a], text)
  495.     end
  496.     a = a + 1
  497.   end
  498.   mon_screen()
  499. end
  500.  
  501. -- SCREEN SAVER --
  502.  
  503. function image_draw()
  504.   screen = 1
  505.   image = {}
  506.   image = paintutils.loadImage("image")
  507.   while remote_monitors[screen] ~= nil do
  508.     print("image: " .. screen)
  509.     mon = remote_monitors[screen]
  510.     mon.clear()
  511.     oa_x, oa_y = mon.getSize()
  512.     term.redirect(mon)
  513.     x = (oa_x / 2) - settings["image_x"]
  514.     y = (oa_y / 2) - settings["image_y"]
  515.     paintutils.drawImage(image, x, y)
  516.     term.restore()
  517.     reset_text()
  518.     screen = screen + 1
  519.   end
  520. end
  521.  
  522. function screen_saver()
  523.   print("screen Saver")
  524.  
  525.   image_draw()
  526.   os.pullEvent("monitor_touch")
  527. end
  528.  
  529.  
  530. -- PROGRAM --
  531.  
  532. mon_screen()
  533. while true do
  534.   if settings["direct_power"] or settings["screen_saver"] then
  535.     os.startTimer(settings["refresh_rate"])
  536.   end
  537.  
  538.   event, id, x, y  = os.pullEvent()
  539.  
  540.   if event == "monitor_touch" then
  541.     button_pressed = select_button()
  542.     e = 1
  543.     if names[button_pressed] ~= nil then
  544.       if button_pressed ~= 1 then
  545.         if status[1] then
  546.           message_send()
  547.         end
  548.       else
  549.  
  550.         if status[1] then
  551.           offline()
  552.         else
  553.           status[1] = true
  554. --          startup_buttons
  555.         end
  556.       end
  557.     end
  558.  
  559.     mon_screen()
  560.  
  561.   elseif event == "rednet_message" then
  562.  
  563.     if id == settings["eu_id"] then
  564.       eu = textutils.unserialize (x)
  565.     elseif id == settings["mj_id"] then
  566.       mj = textutils.unserialize (x)
  567.     end
  568.  
  569.     mon_screen()
  570.  
  571.   elseif event == "timer" then
  572.     print("timer")
  573.     if settings["screen_saver"] then
  574.       e = e + 1
  575.       print("Timeout: " .. e)
  576.       if e == settings["timeout"] then
  577.         screen_saver()
  578.         e = 1
  579.       end
  580.     end
  581.     mon_screen()
  582.   end
  583. end
Advertisement
Add Comment
Please, Sign In to add comment