Savage_Me55iah

monitor v1.6 beta

May 9th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.56 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.   local 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.           peripheral_found = true
  198.         end
  199.       elseif e == "remote" then
  200.         if not peripheral.call(sides[num], "isWireless()") then
  201.           return sides[num]
  202.           peripheral_found = true
  203.         end
  204.       elseif e == nil
  205.         return sides[num]
  206.         peripheral_found = true
  207.       end
  208.     end
  209.   end
  210.   if peripheral_found ~= true then
  211.     error("No " .. t .. " attached to the computer", 0)
  212.   end
  213. end
  214.  
  215. -- MODEM REMOTE CONNECTIONS --
  216.  
  217. mod = find_peripheral("modem", "remote")
  218. local remote_peripherals = mod.getNamesRemote()
  219. remote_types = {}
  220. remote_monitors = {[1] = find_peripheral("monitor")}
  221. remote_rec = {}
  222. remote_eu = {}
  223. local a = 1
  224. if remote_monitors[1] == nil then local
  225. b = 1 else local b = 2 end
  226. local
  227. c = 1
  228. local
  229. d = 1
  230. while remote_peripherals[a] ~= nil do
  231.   remote_types[a] = mod.getTypeRemote(remote_peripherals[a])
  232.   if remote_types[a] == "monitor" then
  233.     remote_monitors[b] = peripheral.wrap(remote_peripherals[a])
  234.     b = b + 1
  235.   end
  236.   if settings["open_peripherals"] and settings["energy_stats"] and settings["direct_power"] then
  237.     if remote_types[a] == "cofh_thermalexpansion_energycell" then
  238.       remote_rec[c] = peripheral.wrap(remote_peripherals[a])
  239.       c = c + 1
  240.     end
  241.     if remote_types[a] == "mfsu" then
  242.       remote_eu[d] = peripheral.wrap(remote_peripherals[a])
  243.       d = d + 1
  244.     end
  245.   end
  246.   a = a + 1
  247. end
  248.  
  249. rednet.open(find_peripheral("modem", "wireless"))
  250. mon = remote_monitors[1]
  251. mon.setTextScale(settings["text_scale"])
  252. oa_x, oa_y = mon.getSize()
  253.  
  254. if settings["energy_stats"] and settings["open_peripherals"] then
  255. oa_x = oa_x - 10
  256. end
  257.  
  258. reps_x = math.floor(oa_x / (settings["button_width"] + 1))
  259. reps_y = math.floor((oa_y / 2) - 1)
  260. total_buttons = reps_x * reps_y
  261. print(oa_x .. "," .. oa_y .. " - " .. reps_x .. " x " .. reps_y .. " - " .. total_buttons)
  262.  
  263.  
  264. --FUNCTIONS DRAWING MONITOR SCREEN--
  265.  
  266. print("Drawing mon screen")
  267.  
  268. function reset_text()
  269.   mon.setTextColor(colors.white)
  270.   mon.setBackgroundColor(colors.black)
  271. end
  272.  
  273. function heading()
  274.   mon.setCursorPos(2,2)
  275.   mon.setTextColor(settings["heading color"])
  276.   mon.setBackgroundColor(settings["heading background"])
  277.   mon.write(settings["heading"])
  278.   reset_text()
  279. end
  280.  
  281. function button_draw(number, xpos, ypos)
  282.   mon.setCursorPos(xpos, ypos)
  283.   button_x1[number], button_y1[number] = mon.getCursorPos()
  284.   if names[number] ~= nil then
  285.     if status[number] then
  286.       mon.setBackgroundColor(settings["active color"])
  287.     else
  288.       mon.setBackgroundColor(settings["inactive color"])
  289.     end
  290.     text = names[number]:upper()
  291.     text_length = string.len(text) + 2
  292.     if text_length > settings["button_width"] then
  293.       settings["button_width"] = text_length
  294.     end
  295.     mon.write(" " .. text .. " ")
  296.     currpos_x, currpos_y = mon.getCursorPos()
  297.     while currpos_x ~= (settings["button_width"] + xpos) do
  298.       mon.write(" ")
  299.       currpos_x, currpos_y = mon.getCursorPos()
  300.     end
  301.     button_x2[z], y = mon.getCursorPos()
  302.  
  303.     reset_text()
  304.     print("Writing button " .. number .. ": " .. names[number] .. ": " .. status[number])
  305.   end
  306. end
  307.  
  308. -- ENERGY FUNCTIONS --
  309.  
  310. function box(color, left, top, right, bot)
  311.   term.redirect(mon)
  312.   paintutils.drawLine(right, top, left, top, color)
  313.   paintutils.drawLine(left, top, left, bot, color)
  314.   paintutils.drawLine(right, bot, left, bot, color)
  315.   paintutils.drawLine(right, top, right, bot, color)
  316.   term.restore()
  317. end
  318.  
  319. function decimal(number)
  320.   text = nil
  321.   num = nil
  322.   if number > 999 then
  323. num = number / 1000
  324. text = "K"
  325.     if num > 999 then
  326. num = num / 1000
  327. text = "M"
  328.       if num > 999 then num = num / 1000
  329. text = "B"
  330.       end
  331.     end
  332.   num = math.floor(num)
  333.   end
  334.   a = num or 0
  335.   text = text or 0
  336.   text = a .. text
  337.   return text
  338. end
  339.  
  340. function energy_compile()
  341.   local a = 1
  342.   mj["max"] = 0
  343. mj["current"] = 0
  344. mj["percent"] = 0
  345.   while remote_rec[a] ~= nil do
  346.     mj["max"] = mj["max"] + remote_rec[a].getMaxEnergyStored("north")
  347.     mj["current"] = mj["current"] + remote_rec[a].getEnergyStored("north")
  348.     a = a + 1
  349.   end
  350.  
  351.   local a = 1
  352.   eu["max"] = 0
  353.   eu["current"] = 0
  354.   eu["percent"] = 0
  355.   while remote_eu[a] ~= nil do
  356.     eu["max"] = eu["max"] + remote_eu[a].getCapacity()
  357.     eu["current"] = eu["current"] + remote_eu[a].getStored()
  358.     a = a + 1
  359.   end
  360. end
  361.  
  362.  
  363. function percentage(type)
  364.   if type == "mj" then
  365.     mj["percent"] = mj["current"] / mj["max"] * 100
  366.     return math.floor(mj["percent"])
  367.   elseif type == "eu" then
  368.     eu["percent"] = eu["current"] / eu["max"] * 100
  369.     return math.floor(eu["percent"])
  370.   end
  371. end
  372.  
  373. function greaterx()
  374.   currx = mon.getCursorPos()
  375.   if currx > endx then
  376.     endx = currx
  377.   end
  378. end
  379.  
  380. function energy_stats(startx, starty)
  381.   if settings["direct_power"] then
  382.     energy_compile()
  383.   end
  384.  
  385.   x = startx + 2
  386.   y = starty + 2
  387.  
  388.   mon.setTextColor(colors.white)
  389.  
  390.   mon.setCursorPos(x,y)
  391.   mon.write("EU: " .. decimal(eu["current"])
  392.   endx = mon.getCursorPos()
  393.   y = y + 1
  394.  
  395.   mon.setCursorPos(x,y)
  396.   mon.write("EU: " .. decimal(eu["max"])
  397.  )
  398.   greaterx()
  399.   y = y + 1
  400.  
  401.  
  402.   mon.setCursorPos(x,y)
  403.   mon.write("EU: " .. percentage("eu") .. "%")
  404.   greaterx()
  405.   y = y + 2
  406.  
  407.  
  408.   mon.setCursorPos(x,y)
  409.   mon.write("MJ: " .. decimal(mj["current"]) )
  410.   greaterx()
  411.   y = y + 1
  412.  
  413.   mon.setCursorPos(x,y)
  414.   mon.write("MJ: " .. decimal(mj["max"]) )
  415.   greaterx()
  416.   y = y + 1
  417.  
  418.  
  419.   mon.setCursorPos(x,y)
  420.   mon.write("MJ: " .. percentage("mj") .. "%")
  421.   greaterx()
  422.   endy = y + 2
  423.   endx = endx + 1
  424.   box(colors.white, startx, starty, endx, endy)
  425. end
  426.  
  427. -- MONITOR SCREEN PROGRAM --
  428.  
  429. function mon_screen()
  430.  
  431.   screen = 1
  432.   while remote_monitors[screen] ~= nil do
  433.     mon = remote_monitors[screen]
  434. --    print("drawing monitor")
  435.     mon.clear()
  436.     heading()
  437.     x = 2
  438.     y = 4
  439.     if settings["energy_stats"] then
  440. energy_stats(x, y)
  441. settings["energy_room"] = endx
  442. x = endx + 1
  443. end
  444.  
  445.     z = 1
  446.  
  447.     for num=1,reps_x do
  448.       y = 4
  449.  
  450.       for num=1,reps_y do
  451.         button_draw(z, x, y)
  452.         y = y + 2
  453.         z = z + 1
  454.       end
  455.       x = x + settings["button_width"] + 1
  456.     end
  457.     save()
  458.     screen = screen + 1
  459.   end
  460.   sleep(0.1)
  461. end
  462.  
  463. --FUNCTION BUTTON DETECT--
  464.  
  465. function select_button()
  466.   z = 1
  467.   button_pressed = nil
  468.  
  469.   repeat
  470.     if names[z] ~= nil then
  471.       if x > button_x1[z] and x < button_x2[z] and y == button_y1[z] then
  472.         return z
  473.         break
  474.       end
  475.     end
  476.     z = z + 1
  477.   until z > total_buttons
  478. end
  479.  
  480. function message_send()
  481.   if status[button_pressed] then
  482.     text = "deactivate"
  483.     status[button_pressed] = not status[button_pressed]
  484.   else
  485.     text = "activate"
  486.     status[button_pressed] = not status[button_pressed]
  487.   end
  488.  
  489.   rednet.send(ids[button_pressed], text)
  490.   print(names[button_pressed] .. " " .. ids[button_pressed] .. " " .. text)
  491.  
  492.   mon_screen()
  493. end
  494.  
  495. function offline()
  496.   status[1] = false
  497.   a = 2
  498.   while names[a] ~= nil do
  499.     if status[a] then
  500.       status[a] = false
  501.       text = "deactivate"
  502.       rednet.send(ids[a], text)
  503.     end
  504.     a = a + 1
  505.   end
  506.   mon_screen()
  507. end
  508.  
  509. -- SCREEN SAVER --
  510.  
  511. function image_draw()
  512.   screen = 1
  513.   image = {}
  514.   image = paintutils.loadImage("image")
  515.   while remote_monitors[screen] ~= nil do
  516.     print("image: " .. screen)
  517.     mon = remote_monitors[screen]
  518.     mon.clear()
  519.     oa_x, oa_y = mon.getSize()
  520.     term.redirect(mon)
  521.     x = (oa_x / 2) - settings["image_x"]
  522.     y = (oa_y / 2) - settings["image_y"]
  523.     paintutils.drawImage(image, x, y)
  524.     term.restore()
  525.     reset_text()
  526.     screen = screen + 1
  527.   end
  528. end
  529.  
  530. function screen_saver()
  531.   print("screen Saver")
  532.  
  533.   image_draw()
  534.   os.pullEvent("monitor_touch")
  535. end
  536.  
  537.  
  538. -- PROGRAM --
  539.  
  540. mon_screen()
  541. while true do
  542.   if settings["direct_power"] or settings["screen_saver"] then
  543.     os.startTimer(settings["refresh_rate"])
  544.   end
  545.  
  546.   event, id, x, y  = os.pullEvent()
  547.  
  548.   if event == "monitor_touch" then
  549.     button_pressed = select_button()
  550.     e = 1
  551.     if names[button_pressed] ~= nil then
  552.       if button_pressed ~= 1 then
  553.         if status[1] then
  554.           message_send()
  555.         end
  556.       else
  557.  
  558.         if status[1] then
  559.           offline()
  560.         else
  561.           status[1] = true
  562. --          startup_buttons
  563.         end
  564.       end
  565.     end
  566.  
  567.     mon_screen()
  568.  
  569.   elseif event == "rednet_message" then
  570.  
  571.     if id == settings["eu_id"] then
  572.       eu = textutils.unserialize (x)
  573.     elseif id == settings["mj_id"] then
  574.       mj = textutils.unserialize (x)
  575.     end
  576.  
  577.     mon_screen()
  578.  
  579.   elseif event == "timer" then
  580.     print("timer")
  581.     if settings["screen_saver"] then
  582.       e = e + 1
  583.       print("Timeout: " .. e)
  584.       if e == settings["timeout"] then
  585.         screen_saver()
  586.         e = 1
  587.       end
  588.     end
  589.     mon_screen()
  590.   end
  591. end
Advertisement
Add Comment
Please, Sign In to add comment