Savage_Me55iah

Savage Monitor v1.4

Dec 2nd, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.16 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au--
  2. --Version 1.4--
  3. --Needs corresponding "Savage Client v1.4" script @ http://pastebin.com/vbdYtpra--
  4. --type "monitor client" to auto download--
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --save this program as startup or monitor and type in startup: shell.run("monitor")--
  8. --example client computer: id-5 label-power
  9. --to add example client type "montior add 5 power"
  10. --to remove example client type "monitor remove 5 power"
  11. --to show settings of buttons type "monitor settings"
  12. --to adjust the settings of script type "edit settings", changing them here won't work once this program has been run once
  13. --to reset everything type "monitor reset"
  14. --buttons will not work without the online button on, turning the online button will shut off all other buttons
  15. --connect the monitors using network cables and wired modems
  16.  
  17.  
  18. local tArgs = { ... }
  19. local command = tostring(tArgs[1]) or false
  20. local name1 = tostring(tArgs[3]) or false
  21. local id = tonumber(tArgs[2]) or false
  22. local name2 = tostring(tArgs[5]) or false
  23. local id2 = tonumber(tArgs[4]) or false
  24.  
  25. --TABLES--
  26. print("tables")
  27.  
  28. names = {}
  29. ids = {}
  30. status = {}
  31.  
  32. function add_button(command, id, on_off)
  33.   num = 1
  34.   while names[num] ~= nil do
  35.     num = num + 1
  36.   end
  37.  
  38.   names[num] = command
  39.   ids[num] = id
  40.   status[num] = on_off
  41. end
  42.  
  43. add_button("online", 0, true)
  44.  
  45. local load_save_loc = {
  46.   ["load_save_loc"] = "load_save_loc",
  47.   ["settings"] = "settings",
  48.   ["names"] = "names",
  49.   ["ids"] = "ids",
  50.   ["status"] = "status",
  51.   ["eu"] = "eu",
  52.   ["mj"] = "mj",
  53. }
  54.  
  55. local settings = {
  56.   ["mon_side"] = "top",
  57.   ["wireless_side"] = "left",
  58.   ["remote_modem"] = "right",
  59.   ["heading color"] = colors.yellow,
  60.   ["heading background"] = colors.black,
  61.   ["heading"] = "Savage Monitor v1.4",
  62.   ["active color"] = colors.green,
  63.   ["inactive color"] = colors.red,
  64.   ["id"] = os.getComputerID(),
  65.   ["label"] = "Server",
  66.   ["refresh_rate"] = 10,
  67.   ["text_scale"] = 1,
  68.   ["button_width"] = 5,
  69.   ["energy_stats"] = false,
  70.   ["open_peripherals"] = false,
  71.   ["direct_power"] = false,
  72.   ["energy_room"] = 0,
  73.   ["eu_id"] = 1000,
  74.   ["mj_id"] = 1001,
  75.   ["screen_saver"] = true,
  76.   ["timeout"] = 50,
  77.   ["image_x"] = 27,
  78.   ["image_y"] = 9,
  79.   ["client"] = "vbdYtpra"
  80. }
  81.  
  82. local button_x1 = {
  83.   [1] = 2
  84. }
  85.  
  86. local button_x2 = {
  87.   [1] = 12
  88. }
  89.  
  90. local button_y1 = {
  91.   [1] = 4
  92. }
  93.  
  94. local eu = {
  95.   ["max"] = 0,
  96.   ["current"] = 0,
  97.   ["percent"] = 0
  98. }
  99.  
  100. local mj = {
  101.   ["max"] = 0,
  102.   ["current"] = 0,
  103.   ["percent"] = 0
  104. }
  105.  
  106. --LOAD & SAVE FUNCTIONS--
  107.  
  108. function load()
  109.   print("Loading...")
  110.   local fileHandle = fs.open (load_save_loc["load_save_loc"], 'r')
  111.   load_save_loc = textutils.unserialize (fileHandle.readAll())
  112.   fileHandle.close()
  113.  
  114.   local fileHandle = fs.open (load_save_loc["settings"], 'r')
  115.   settings = textutils.unserialize (fileHandle.readAll())
  116.   fileHandle.close()
  117.  
  118.   local fileHandle = fs.open (load_save_loc["names"], 'r')
  119.   names = textutils.unserialize (fileHandle.readAll())
  120.   fileHandle.close()
  121.  
  122.   local fileHandle = fs.open (load_save_loc["ids"], 'r')
  123.   ids = textutils.unserialize (fileHandle.readAll())
  124.   fileHandle.close()
  125.  
  126.   local fileHandle = fs.open (load_save_loc["status"], 'r')
  127.   status = textutils.unserialize (fileHandle.readAll())
  128.   fileHandle.close()
  129.  
  130.   local fileHandle = fs.open (load_save_loc["eu"], 'r')
  131.   eu = textutils.unserialize (fileHandle.readAll())
  132.   fileHandle.close()
  133.  
  134.   local fileHandle = fs.open (load_save_loc["mj"], 'r')
  135.   mj = textutils.unserialize (fileHandle.readAll())
  136.   fileHandle.close()
  137. end
  138.  
  139. function save()
  140.   print("Saving...")
  141.   local fileHandle = fs.open (load_save_loc["load_save_loc"], 'w')
  142.   fileHandle.write (textutils.serialize (load_save_loc))
  143.   fileHandle.close()
  144.  
  145.   local fileHandle = fs.open (load_save_loc["settings"], 'w')
  146.   fileHandle.write (textutils.serialize (settings))
  147.   fileHandle.close()
  148.  
  149.   local fileHandle = fs.open (load_save_loc["names"], 'w')
  150.   fileHandle.write (textutils.serialize (names))
  151.   fileHandle.close()
  152.  
  153.   local fileHandle = fs.open (load_save_loc["ids"], 'w')
  154.   fileHandle.write (textutils.serialize (ids))
  155.   fileHandle.close()
  156.  
  157.   local fileHandle = fs.open (load_save_loc["status"], 'w')
  158.   fileHandle.write (textutils.serialize (status))
  159.   fileHandle.close()
  160.  
  161.   local fileHandle = fs.open (load_save_loc["eu"], 'w')
  162.   fileHandle.write (textutils.serialize (eu))
  163.   fileHandle.close()
  164.  
  165.   local fileHandle = fs.open (load_save_loc["mj"], 'w')
  166.   fileHandle.write (textutils.serialize (mj))
  167.   fileHandle.close()
  168. end
  169.  
  170.  
  171. --PERLIMINARY WORKINGS--
  172.  
  173. if not fs.exists("image") or command == "reset" then
  174.   save()
  175.  
  176. --  fs.delete("startup")
  177. --  outputFile = io.open("startup", "a")
  178. --  text = tostring(shell.getRunningProgram())
  179. --  outputFile:write("shell.run(" .. text .. ")")
  180. --  outputFile:close()
  181.  
  182.   fs.delete("image")
  183.   outputFile = io.open("image", "w")
  184.   outputFile:write("eeeeeeeeeeeeeeeeeeeeeeeeeee")
  185.   outputFile:write("\n")
  186.   outputFile:write("eeeeeffffffffeefffffefffffe")
  187.   outputFile:write("\n")
  188.   outputFile:write("eeeeff0000f0feef000ffff00fe")
  189.   outputFile:write("\n")
  190.   outputFile:write("eeeef0fffffffeef0f00f00f0fe")
  191.   outputFile:write("\n")
  192.   outputFile:write("eeeeff00ffeeeeef0ff000ff0fe")
  193.   outputFile:write("\n")
  194.   outputFile:write("efffffff0feeeeefffff0fffffe")
  195.   outputFile:write("\n")
  196.   outputFile:write("ef0f0000ffeeeeef0fefffef0fe")
  197.   outputFile:write("\n")
  198.   outputFile:write("efffffffffeeeeefffeeeeefffe")
  199.   outputFile:write("\n")
  200.   outputFile:write("eeeeeeeeeeeeeeeeeeeeeeeeeee")
  201.   outputFile:close()
  202. else
  203.   load()
  204. end
  205.  
  206. function freespace()
  207.   a = "locating free space..."
  208.   print(a)
  209.   x = 1
  210.   repeat
  211.     x = x + 1
  212.   until names[x] == nil
  213.   freespace = x
  214.   a = "free space located at: "
  215.   print(a .. freespace)
  216. end
  217.  
  218. function target()
  219.   a = "locating target..."
  220.   print(a)
  221.   x = 1
  222.   repeat
  223.     x = x + 1
  224.   until names[x] == name1 or names[x] == nil
  225.   if names[x] == name1 then
  226.     target = x
  227.     a = "target located at: "
  228.     print(a .. target)
  229.   else
  230.     print("target button not found")
  231.     target = "not found"
  232.   end
  233. end
  234.  
  235. function add_button()
  236.   a = "adding..."
  237.   print(a)
  238.   freespace()
  239.   names[freespace] = name1
  240.   ids[freespace] = id
  241.   status[freespace] = false
  242.   a = "add successful"
  243.   print(a)
  244. end
  245.  
  246. function remove_button()
  247.   a = "removing..."
  248.   print(a)
  249.   target()
  250.   if target ~= "not found" then
  251.     freespace()
  252.     source = freespace - 1
  253.     names[target] = names[source]
  254.     ids[target] = ids[source]
  255.     status[target] = status[source]
  256.     names[source] = nil
  257.     ids[source] = nil
  258.     status[source] = nil
  259.     a = "removal successful"
  260.     print(a)
  261.   end
  262. end
  263.  
  264. function change_button()
  265.   print("changing " .. name1 .. id .. " to " .. name2 .. id2)
  266.   remove_button()
  267.   name1 = name2
  268.   id = id2
  269.   add_button()
  270.   print("change successful")
  271. end
  272.  
  273. function show_settings()
  274.   x = 2
  275.   while names[x] ~= nil do
  276.     print(x .. ": " .. names[x] .. " " .. ids[x])
  277.     x = x + 1
  278.   end
  279.   os.pullEvent()
  280. end
  281.  
  282. function save2()
  283.   print("confirm save? Y/N : ")
  284.   saving = read()
  285.   saving = tostring(saving)
  286.   if saving == "y" then
  287.     save()
  288.   end
  289. end
  290.  
  291. if command == "add" then
  292.   add_button()
  293.   save2()
  294. elseif command == "remove" then
  295.   remove_button()
  296.   save2()
  297. --elseif command == "change" then
  298. --  change_button()
  299. --  save2()
  300. elseif command == "settings" then
  301.   show_settings()
  302. elseif command == "client" then
  303.   shell.run("pastebin get " .. variables["client"] .. " client")
  304. end
  305.  
  306. function find_peripheral(t, e)
  307.   e = e or nil
  308.   sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
  309.   peripheral_found = false
  310.   for num=1, 6 do
  311.     type = peripheral.getType(sides[num])
  312.     if type == t then
  313.       if e == "wireless" then
  314.         if peripheral.call(sides[num], "isWireless") then
  315.           peripheral_side = sides[num]
  316.           peripheral_found = true
  317.         end
  318.       elseif e == "remote" then
  319.         if not peripheral.call(sides[num], "isWireless") then
  320.           peripheral_side = sides[num]
  321.           peripheral_found = true
  322.         end
  323.       elseif e == nil then
  324.         peripheral_side = sides[num]
  325.         peripheral_found = true
  326.       end
  327.     end
  328.   end
  329.   if peripheral_found ~= true then
  330.     error("No " .. t .. " attached to the computer", 0)
  331.   end
  332. end
  333.  
  334. os.setComputerLabel(settings["label"])
  335.  
  336. print("Connecting Modem...")
  337. find_peripheral("modem", "remote")
  338. mod = peripheral.wrap(peripheral_side)
  339. --mod = peripheral.wrap(settings["remote_modem"])
  340. print("Modem Connected")
  341.  
  342. local remote_peripherals = mod.getNamesRemote()
  343. remote_types = {}
  344. remote_monitors = {}
  345. remote_rec = {}
  346. remote_eu = {}
  347. a = 1
  348. b = 1
  349. c = 1
  350. d = 1
  351. while remote_peripherals[a] ~= nil do
  352.   remote_types[a] = mod.getTypeRemote(remote_peripherals[a])
  353.   if remote_types[a] == "monitor" then
  354.     remote_monitors[b] = peripheral.wrap(remote_peripherals[a])
  355.     b = b + 1
  356.   end
  357.   if settings["open_peripherals"] and settings["energy_stats"] and settings["direct_power"] then
  358.     if remote_types[a] == "redstone_energy_cell" then
  359.       remote_rec[c] = peripheral.wrap(remote_peripherals[a])
  360.       c = c + 1
  361.     end
  362.     if remote_types[a] == "batbox" then
  363.       remote_eu[d] = peripheral.wrap(remote_peripherals[a])
  364.       d = d + 1
  365.     end
  366.   end
  367.   a = a + 1
  368. end
  369.  
  370. --print("Connecting Monitor...")
  371. --findPeripheral( "monitor", e )
  372. --mon = peripheral.wrap(peripheral_side)
  373. --mon = peripheral.wrap(settings["mon_side"])
  374. --print("Connected")
  375.  
  376. print("Connecting Wireless...")
  377. find_peripheral("modem", "wireless")
  378. rednet.open(peripheral_side)
  379. print("Connected")
  380.  
  381. print("calculating...")
  382.  
  383. mon = remote_monitors[1]
  384. mon.setTextScale(settings["text_scale"])
  385. oa_x, oa_y = mon.getSize()
  386.  
  387. if settings["energy_stats"] and settings["open_peripherals"] then
  388.   oa_x = oa_x - 10
  389. end
  390.  
  391. reps_x = oa_x / (settings["button_width"] + 1)
  392. reps_x = math.floor(reps_x)
  393. reps_y = oa_y / 2
  394. reps_y = reps_y - 1
  395. reps_y = math.floor(reps_y)
  396. total_buttons = reps_x * reps_y
  397.  
  398. print(oa_x .. "," .. oa_y .. " - " .. reps_x .. " x " .. reps_y .. " - " .. total_buttons)
  399.  
  400. e = 1
  401.  
  402. --FUNCTIONS DRAWING MONITOR SCREEN--
  403.  
  404. print("Drawing mon screen")
  405.  
  406. function reset_text()
  407.   print("Reset text")
  408.   mon.setTextColor(colors.white)
  409.   mon.setBackgroundColor(colors.black)
  410. end
  411.  
  412. function heading()
  413.   print("heading")
  414.   mon.setCursorPos(2,2)
  415.   mon.setTextColor(settings["heading color"])
  416.   mon.setBackgroundColor(settings["heading background"])
  417.   mon.write(settings["heading"])
  418.   reset_text()
  419. end
  420.  
  421. function button_draw(number, xpos, ypos)
  422.   mon.setCursorPos(xpos, ypos)
  423.   button_x1[number], button_y1[number] = mon.getCursorPos()
  424.   if names[number] ~= nil then
  425.  
  426.     if status[number] then
  427.       mon.setBackgroundColor(settings["active color"])
  428.     else
  429.       mon.setBackgroundColor(settings["inactive color"])
  430.     end
  431.  
  432.     text = names[number]:upper()
  433.     text_length = string.len(text)
  434.     text_length = text_length + 2
  435.     if text_length > settings["button_width"] then
  436.       settings["button_width"] = text_length
  437.     end
  438.  
  439.     mon.write(" " .. text .. " ")
  440.  
  441.     currpos_x, currpos_y = mon.getCursorPos()
  442.     while currpos_x ~= (settings["button_width"] + xpos) do
  443.       mon.write(" ")
  444.       currpos_x, currpos_y = mon.getCursorPos()
  445.     end
  446.     button_x2[z], y = mon.getCursorPos()
  447.  
  448.     reset_text()
  449.     print("Adding button " .. number .. ": " .. names[number])
  450.   end
  451. end
  452.  
  453. -- ENERGY FUNCTIONS --
  454.  
  455. function box(color, left, top, right, bot)
  456.   term.redirect(mon)
  457.   paintutils.drawLine(right, top, left, top, color)
  458.   paintutils.drawLine(left, top, left, bot, color)
  459.   paintutils.drawLine(right, bot, left, bot, color)
  460.   paintutils.drawLine(right, top, right, bot, color)
  461.   term.restore()
  462. end
  463.  
  464. function decimal(number)
  465.   text = nil
  466.   num = nil
  467.   if number > 999 then
  468.     num = number / 1000
  469.     text = "K"
  470.     if num > 999 then
  471.       num = num / 1000
  472.       text = "M"
  473.       if num > 999 then
  474.       num = num / 1000
  475.       text = "B"
  476.       end
  477.     end
  478.   num = math.floor(num)
  479.   end
  480.   a = num or 0
  481.   text = text or 0
  482.   text = a .. text
  483. end
  484.  
  485. function energy_compile()
  486.   a = 1
  487.  
  488.   mj["max"] = 0
  489.   mj["current"] = 0
  490.   mj["percent"] = 0
  491.  
  492.   while remote_rec[a] ~= nil do
  493.     mj["max"] = mj["max"] + remote_rec[a].getMaxEnergyStored()
  494.     mj["current"] = mj["current"] + remote_rec[a].getEnergyStored()
  495.     a = a + 1
  496.   end
  497.  
  498.   a = 1
  499.  
  500.   eu["max"] = 0
  501.   eu["current"] = 0
  502.   eu["percent"] = 0
  503.  
  504.   while remote_eu[a] ~= nil do
  505.     eu["max"] = eu["max"] + remote_eu[a].getCapacity()
  506.     eu["current"] = eu["current"] + remote_eu[a].getStored()
  507.     a = a + 1
  508.   end
  509. end
  510.  
  511.  
  512. function percentage(type)
  513.   if type == "mj" then
  514.     mj["percent"] = mj["current"] / mj["max"] * 100
  515.     text = math.floor(mj["percent"])
  516.   elseif type == "eu" then
  517.     eu["percent"] = eu["current"] / eu["max"] * 100
  518.     text = math.floor(eu["percent"])
  519.   end
  520. end
  521.  
  522. function greaterx()
  523.   currx = mon.getCursorPos()
  524.   if currx > endx then
  525.     endx = currx
  526.   end
  527. end
  528.  
  529. function energy_stats(startx, starty)
  530.   if settings["direct_power"] then
  531.     energy_compile()
  532.   end
  533.  
  534.   x = startx + 2
  535.   y = starty + 2
  536.  
  537.   mon.setTextColor(colors.white)
  538.  
  539.   mon.setCursorPos(x,y)
  540.   if eu["current"] ~= nil then
  541.     decimal(eu["current"])
  542.   end
  543.   mon.write("EU: " .. text)
  544.   endx = mon.getCursorPos()
  545.   y = y + 1
  546.  
  547.   mon.setCursorPos(x,y)
  548.   decimal(eu["max"])
  549.   mon.write("EU: " .. text)
  550.   greaterx()
  551.   y = y + 1
  552.  
  553.   percentage("eu")
  554.   mon.setCursorPos(x,y)
  555.   mon.write("EU: " .. text .. "%")
  556.   greaterx()
  557.   y = y + 2
  558.  
  559.   decimal(mj["current"])
  560.   mon.setCursorPos(x,y)
  561.   mon.write("MJ: " .. text)
  562.   greaterx()
  563.   y = y + 1
  564.  
  565.   decimal(mj["max"])
  566.   mon.setCursorPos(x,y)
  567.   mon.write("MJ: " .. text)
  568.   greaterx()
  569.   y = y + 1
  570.  
  571.   percentage("mj")
  572.   mon.setCursorPos(x,y)
  573.   mon.write("MJ: " .. text .. "%")
  574.   greaterx()
  575.   endy = y + 2
  576.   endx = endx + 1
  577.   box(colors.white, startx, starty, endx, endy)
  578. end
  579.  
  580. -- MONITOR SCREEN PROGRAM --
  581.  
  582. function mon_screen()
  583.  
  584.   screen = 1
  585.   while remote_monitors[screen] ~= nil do
  586.     mon = remote_monitors[screen]
  587.  
  588.     print("drawing monitor")
  589.  
  590.     mon.clear()
  591.  
  592.     heading()
  593.  
  594.     x = 2
  595.     y = 4
  596.  
  597.     if settings["energy_stats"] then
  598.       energy_stats(x, y)
  599.       settings["energy_room"] = endx
  600.       x = endx + 1
  601.     end
  602.  
  603.     z = 1
  604.  
  605.     for num=1,reps_x do
  606.       y = 4
  607.  
  608.       for num=1,reps_y do
  609.         button_draw(z, x, y)
  610.         y = y + 2
  611.         z = z + 1
  612.       end
  613.  
  614.       x = x + settings["button_width"] + 1
  615.     end
  616.     save()
  617.     screen = screen + 1
  618.   end
  619. end
  620.  
  621. --FUNCTION BUTTON DETECT--
  622.  
  623. function select_button()
  624.   z = 1
  625.   button_pressed = nil
  626.  
  627.   repeat
  628.     if names[z] ~= nil then
  629.       if x > button_x1[z] and x < button_x2[z] and y == button_y1[z] then
  630.         button_pressed = z
  631.       end
  632.     end
  633.     z = z + 1
  634.   until button_pressed ~= nil or z > total_buttons
  635. end
  636.  
  637. function message_send()
  638.   if status[button_pressed] then
  639.     text = "deactivate"
  640.     status[button_pressed] = false
  641.   else
  642.     text = "activate"
  643.     status[button_pressed] = true
  644.   end
  645.  
  646.   rednet.send(ids[button_pressed], text)
  647.   print(names[button_pressed] .. " " .. ids[button_pressed] .. " " .. text)
  648.  
  649.   mon_screen()
  650. end
  651.  
  652. function offline()
  653.   status[1] = false
  654.   a = 2
  655.   while names[a] ~= nil do
  656.     if status[a] then
  657.       status[a] = false
  658.       text = "deactivate"
  659.       rednet.send(ids[a], text)
  660.     end
  661.     a = a + 1
  662.   end
  663.   mon_screen()
  664. end
  665.  
  666. -- SCREEN SAVER --
  667.  
  668. function image_draw()
  669.   screen = 1
  670.   image = {}
  671.   image = paintutils.loadImage("image")
  672.   while remote_monitors[screen] ~= nil do
  673.     print("image: " .. screen)
  674.     mon = remote_monitors[screen]
  675.     mon.clear()
  676.     oa_x, oa_y = mon.getSize()
  677.     term.redirect(mon)
  678.     x = oa_x - settings["image_x"]
  679.     x = math.random(1, x)
  680.     y = oa_y - settings["image_y"]
  681.     y = math.random(1, y)
  682.     paintutils.drawImage(image, x, y)
  683.     term.restore()
  684.     reset_text()
  685.     screen = screen + 1
  686.   end
  687. end
  688.  
  689. function screen_saver()
  690.   print("screen Saver")
  691.   while event ~= "monitor_touch" do
  692.     os.startTimer(settings["refresh_rate"])
  693.     image_draw()
  694.     event, id, x, y  = os.pullEvent()
  695.   end
  696. end
  697.  
  698.  
  699. -- PROGRAM --
  700.  
  701. mon_screen()
  702. while true do
  703.   if settings["direct_power"] or settings["screen_saver"] then
  704.     os.startTimer(settings["refresh_rate"])
  705.   end
  706.  
  707.   event, id, x, y  = os.pullEvent()
  708.  
  709.   if event == "monitor_touch" then
  710.     select_button()
  711.     e = 1
  712.     if names[button_pressed] ~= nil then
  713.       if button_pressed ~= 1 then
  714.         if status[1] then
  715.           message_send()
  716.         end
  717.       else
  718.  
  719.         if status[1] then
  720.           offline()
  721.         else
  722.           status[1] = true
  723.         end
  724.       end
  725.     end
  726.  
  727.     mon_screen()
  728.  
  729.   elseif event == "rednet_message" then
  730.  
  731.     if id == settings["eu_id"] then
  732.       eu = textutils.unserialize (x)
  733.     elseif id == settings["mj_id"] then
  734.       mj = textutils.unserialize (x)
  735.     end
  736.  
  737.     mon_screen()
  738.  
  739.   elseif event == "timer" then
  740.     print("timer")
  741.     if settings["screen_saver"] then
  742.       e = e + 1
  743.       print("Timeout: " .. e)
  744.       if e == settings["timeout"] then
  745.         screen_saver()
  746.         e = 1
  747.       end
  748.     end
  749.     mon_screen()
  750.   end
  751. end
Advertisement
Add Comment
Please, Sign In to add comment