Savage_Me55iah

Savage Monitor V2.0

Jul 15th, 2014
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.88 KB | None | 0 0
  1. --Script by Savage_Me55iah of WUGcraft--
  2. local version, pastebinCode = "v2.1", "LS4mrJQm"
  3.  
  4. --Needs corresponding "Savage Client v1.7" script @ http://pastebin.com/nBw1rV8A--
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --save this program as "monitor" and type in startup: sleep(1) shell.run("monitor")--
  8. --to adjust the settings of script type "edit config", changing them here won't work once this program has been run once
  9. --to reset everything delete files: config, names, status, ids, relays
  10. --buttons will not work without the online button on, turning the online button off will shut off all other buttons
  11. --all monitors should be the same size, things will get awkward if not
  12. --the control tab is where you add and remove buttons, relays will not work for the moment
  13.  
  14. --TO DO LIST--
  15. --make a "update" button under the controls tab
  16. --make a "restart" and "terminate" button under the controls tab
  17. --make a "logs" button under the controls tab
  18. --setup the relays (setting up more than 1 server around base)
  19. --setup the screen saver
  20.  
  21. --CHANGELOG--
  22. --2.1 = update functionality
  23.  
  24. --Contact Savage_Me55iah @ CraftAU.com.au or Feenix-Rising.enjin.com or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  25.  
  26. -- Logging for debugging --
  27.  
  28. local log_file = "LOG"
  29. local button_log = "BUTTON_LOG"
  30. local save_direct = "saves"
  31.  
  32. do
  33.   if fs.exists(tostring(save_direct.."/"..log_file)) then fs.delete(tostring(save_direct.."/"..log_file)) end
  34.   if fs.exists(tostring(save_direct.."/"..button_log)) then fs.delete(tostring(save_direct.."/"..button_log)) end
  35.   log_add = function(...)
  36.     local params = {...}
  37.     local text = tostring(params[1])
  38.     local filehandler = fs.open(tostring(save_direct.."/"..log_file), 'a')
  39.     if params[2] then text = "--"..params[1] end
  40.     filehandler.write(text)
  41.     filehandler.write("\n")
  42.     filehandler.close()
  43.     return true
  44.   end
  45.   log_button = function(...)
  46.     local params = {...}
  47.     local text = tostring(params[1])
  48.     local filehandler = fs.open(tostring(save_direct.."/"..button_log), 'a')
  49.     if params[2] then text = "--"..params[1] end
  50.     filehandler.write(text)
  51.     filehandler.write("\n")
  52.     filehandler.close()
  53.     return true
  54.   end
  55.  
  56. local tArgs = {...}
  57.  
  58.   if "update" == tostring(tArgs[1]) then
  59.     log_add("updating")
  60.     shell.run("pastebin get "..pastebinCode.." update")
  61.     if fs.exists("update") then
  62.         fs.delete(shell.getRunningProgram())
  63.         fs.copy("update", shell.getRunningProgram())
  64.         fs.delete("update")
  65.         log_add("update success")
  66.     else
  67.         log_add("update failed")
  68.     end
  69.     sleep(2)
  70.     os.reboot()
  71.   end
  72. end
  73.  
  74. -- Label --
  75.  
  76. if not os.getComputerLabel() then
  77.   term.clear() term.setCursorPos(1,1)
  78.   log_add("no computer label")
  79.   print("Enter computer label: ")
  80.   os.setComputerLabel(read())
  81.   log_add("computer label added: "..os.getComputerLabel())
  82.   term.clear()
  83. end
  84.  
  85. -- Settings & Saving/Loading --
  86.  
  87. local settings_file = "config"
  88.  
  89. local settings_template = [[-- adjust these configuration options as necessary.
  90. -- IMPORTANT!, API won't load if there are spaces in the values
  91. COMPUTER_LABEL = "${label}"
  92. COMPUTER_ID = ${id}
  93.  
  94. HEADING = "${heading}"
  95.  
  96. -- the relay determines if this server computer relays to the other server computers
  97. RELAY = "${relay}"
  98.  
  99. OUTPUT_TO_MONITOR = "${mon_output}"
  100. SCREENSAVER = "${screensaver}"
  101. DISPLAY_ENERGY = "${display_energy}"
  102.  
  103. SCREENSAVER_IMAGE = "${image}"
  104. IMAGE_X, IMAGE_Y = ${image_x}, ${image_y}
  105. SCREENSAVER_TIMEOUT = ${timeout}
  106. SCREEN_REFRESH_RATE = ${refresh_rate}
  107. TEXT_SCALE = ${text_scale}
  108. ]]
  109.  
  110. local function interpolate(s, params)
  111.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  112. end
  113.  
  114. do  
  115.   save_settings = function()
  116.   local filehandler = fs.open(tostring(save_direct.."/"..settings_file), "w")
  117.    
  118.   settings = {
  119.         label = COMPUTER_LABEL or os.getComputerLabel(),
  120.         id = os.getComputerID(),
  121.         heading = HEADING or "Savage_Monitor_"..version,
  122.         relay = RELAY or "false",
  123.         mon_output = OUTPUT_TO_MONITOR or "false",
  124.         display_energy = DISPLAY_ENERGY or "false",
  125.         screensaver = SCREENSAVER or "false",
  126.         image_x = IMAGE_X or 27,
  127.         image_y = IMAGE_Y or 9,
  128.         timeout = SCREENSAVER_TIMEOUT or 50,
  129.         refresh_rate = SCREEN_REFRESH_RATE or 300,
  130.         text_scale = TEXT_SCALE or 1,
  131. }
  132.  
  133.   filehandler.write(interpolate(settings_template, settings))
  134.   filehandler.close()
  135.   log_add("settings saved")
  136.   end
  137. end
  138.  
  139.  
  140. if fs.exists(tostring(save_direct.."/"..settings_file)) == false then
  141.     log_add("Settings file not found", true)
  142.     log_add("Generating Settings")
  143.     save_settings()
  144.     return
  145.   else
  146.     os.unloadAPI(tostring(save_direct.."/"..settings_file))
  147.  
  148.     if os.loadAPI(tostring(save_direct.."/"..settings_file)) == false then log_add("Could not load the settings file!", true) end
  149.  
  150.     CONFIG = nil
  151.  
  152.     for k, v in pairs(_G) do
  153.         if k == settings_file then CONFIG = v break end
  154.     end
  155.  
  156.     if CONFIG == nil then log_add("CONFIG came up nil", true) end
  157. end
  158.  
  159.  
  160. do
  161.   load_one = function(command)
  162.       if fs.exists(tostring(save_direct.."/"..command) then
  163.         local fileHandler = fs.open(save_direct.."/"..command, 'r')
  164.         return textutils.unserialize(fileHandler.readAll())
  165.       end
  166.   end
  167.  
  168.   save_one = function(command1, command2)
  169.       local fileHandler = fs.open(tostring(save_direct.."/"..command1), 'w')
  170.       fileHandler.write(textutils.serialize(command2))
  171.       fileHandler.close()
  172.       log_add("Saving: "..command1)
  173.   end
  174.  
  175.   load_all = function()
  176.       log_add("Load all")
  177.       names = load_one("names")
  178.       ids = load_one("ids")
  179.       status = load_one("status")
  180.       relays = load_one("relays")
  181.       log_add("Load successful")
  182.   end
  183.  
  184.   save_all = function()
  185.       log_add("Save all")
  186.       save_one("names", names)
  187.       save_one("ids", ids)
  188.       save_one("status", status)
  189.       save_one("relays", relays)
  190.       log_add("Save successful")
  191.   end
  192. end
  193.  
  194. -- TABLES --
  195.  
  196. names = {}
  197. ids = {}
  198. status = {}
  199. relays = {}
  200. sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
  201. peripheral_list = {[1] = term}
  202.  
  203. do
  204.   button_add = function(...)
  205.     local params = {...}
  206.     local num = table.maxn(names) + 1
  207.     names[num], ids[num], status[num] = params[1], params[2], params[3]
  208.     local text = "Adding button #"..num..": "..params[1].." "..params[2].." "..tostring(params[3])
  209.     log_add(text)
  210.   end
  211.  
  212.  
  213. install = function()
  214.   button_add("scroll l", 0, false)      -- 1
  215.   button_add("main", 0, true)           -- 2
  216.   button_add("control", 0, false)       -- 3
  217.   button_add("scroll r", 0, false)      -- 4
  218.   button_add("button add", 0, false)    -- 5
  219.   button_add("button remove", 0, false) -- 6
  220.   button_add("relay add", 0, false)     -- 7
  221.   button_add("relay remove", 0, false)  -- 8
  222.   button_add("update", 0, false)        -- 9
  223.   button_add("online", 0, true)         -- 10
  224.   save_all()
  225.   load_all()
  226. end
  227. end
  228.  
  229. if fs.exists(tostring(save_direct.."/names")) then
  230.   load_all()
  231.   if names[1] ~= "scroll l" then install() end
  232. else
  233.   install()
  234. end
  235.  
  236. function imagewrite(params)
  237.   outputFile:write("\n")
  238.   outputFile:write(params)
  239. end
  240.  
  241. local image_table = {[1] = "eeeeeeeeeeeeeeeeeeeeeeeeeee",
  242.   [2] = "eeeeeffffffffeefffffefffffe",
  243.   [3] = "eeeeff0000f0feef000ffff00fe",
  244.   [4] = "eeeef0fffffffeef0f00f00f0fe",
  245.   [5] = "eeeeff00ffeeeeef0ff000ff0fe",
  246.   [6] = "efffffff0feeeeefffff0fffffe",
  247.   [7] = "ef0f0000ffeeeeef0fefffef0fe",
  248.   [8] = "efffffffffeeeeefffeeeeefffe",
  249.   [9] = "eeeeeeeeeeeeeeeeeeeeeeeeeee"
  250. }
  251.  
  252. if not fs.exists(tostring(save_direct.."/image")) then
  253.   outputFile = io.open(tostring(save_direct.."/image"), "w")
  254.   log_add("Writing Image file")
  255.   outputFile:write(image_table[1])
  256.   for num = 2,table.maxn(image_table) do imagewrite(image_table[num]) end
  257.   num = nil
  258.   outputFile:close()
  259. end
  260.  
  261. do
  262.   button_length = function()
  263.     local length = 1
  264.     for k,v in ipairs(names) do
  265.       if tonumber(string.len(v) + 3) > length and tonumber(k) >= 9 then length = string.len(v) end
  266.     end
  267.     return length
  268.   end
  269. end
  270.  
  271. -- PERIPHERALS --
  272.  
  273. do
  274.   peripherals_find = function()
  275.     log_add("Finding Peripherals Direct")
  276.     peripheral_list = {[1] = term}
  277.     local num = 2
  278.     for k,v in ipairs(sides) do
  279.       p_type = peripheral.getType(v) or nil
  280.       if p_type ~= nil then peripheral_list[num] = v num = num + 1 log_add("Found Peripheral: "..v.." "..p_type) end
  281.     end
  282.     log_add("Find Peripherals Direct Complete") log_add("Checking for Remote")
  283.     local remote = nil
  284.     for k,v in ipairs(peripheral_list) do
  285.       if peripheral.getType(tostring(v)) == "modem" then
  286.         if not peripheral.call(v, "isWireless") then remote = peripheral.wrap(v) log_add("Remote found: "..tostring(v)) end
  287.       end
  288.     end
  289.  
  290.     if remote ~= nil then
  291.       log_add("Finding Peripherals Remote")
  292.       peripheral_list = {}
  293.       peripheral_list = remote.getNamesRemote()
  294.       peripheral_list[(table.maxn(peripheral_list) + 1)] = peripheral_list[1]
  295.       peripheral_list[1] = term
  296.       local num = (table.maxn(peripheral_list) + 1)
  297.       for k,v in ipairs(sides) do
  298.         p_type = peripheral.getType(v) or nil
  299.         if p_type ~= nil then peripheral_list[num] = v num = num + 1 end
  300.       end
  301.     else
  302.       log_add("Remote not found", true)
  303.     end
  304.   end
  305. end
  306.  
  307. -- PERIPHERAL SORTER --
  308.  
  309. function decimal(number)
  310.   log_add("Converting #: "..tostring(number))
  311.   if type(number) == "number" then
  312.     local text, num = ".", number
  313.       if num >= 1000 then text = "K" end
  314.       if num >= 1000000 then text = "M" end
  315.       if num >= 1000000000 then text = "B" end
  316.       if num >= 1000000000000 then text = "T" end
  317.     while num >= 1000 do
  318.       num = num / 1000
  319.     end
  320.     if text ~= "." then
  321.       text = tostring(math.floor(num)..text) or 0
  322.     else
  323.       text = tostring(math.floor(num)) or 0
  324.     end
  325.     log_add("# Conversion complete: "..text)
  326.     return text
  327.   else
  328.     return number
  329.   end
  330. end
  331.  
  332. do
  333.   peripherals_capacitor = function(...)
  334.     local params = {...}
  335.     log_add("Getting Capacitor details: "..tostring(params[1]))
  336.     local answer = {[1] = false}
  337.     local command = peripheral.wrap(params[1])
  338.     local p_methods = peripheral.getMethods(tostring(params[1]))
  339.     for k,v in ipairs(p_methods) do
  340.       if v == "getMaxEnergyStored" then answer[1] = true end
  341.     end
  342.     if answer[1] then
  343.       log_add("Method <getMaxEnergyStored> applicable: "..tostring(params[1]))
  344.       answer["capacity"] = command.getMaxEnergyStored("north") or 0
  345.       answer["stored"] = command.getEnergyStored("north") or 0
  346.       answer["percentage"] = math.floor((answer["stored"] / answer["capacity"])).."%" or "0%"
  347.       log_add(tostring(params[1]).." Details: "..answer["capacity"].." "..answer["stored"].." "..answer["percentage"])
  348.     else
  349.       log_add("Method <getMaxEnergyStored> not applicable: "..tostring(params[1]), true)
  350.     end
  351.     return answer
  352.   end
  353.  
  354.   peripherals_compile = function()
  355.     local answer = {[1] = 0, [2] = 0, [3] = 0}
  356.     local details = {}
  357.     log_add("Compiling Capacitors data")
  358.     for k,v in ipairs(capacitors) do
  359.       details = peripherals_capacitor(v)
  360.       answer[2] = answer[2] + details["capacity"] or 0
  361.       answer[1] = answer[1] + details["stored"] or 0
  362.     end
  363.     answer[3] = tostring(math.floor((answer[1] / answer[2]) * 100)) .. "%"
  364.     log_add("Compile Results: "..answer[2].." "..answer[1].." "..answer[3])
  365.     return answer
  366.   end
  367.  
  368.   peripherals_applied_energ = function(command)
  369.     local params = {[1] = command or nil}
  370.     log_add("Getting AE details: "..tostring(params[1]))
  371.     local answer, applicable = {}, false
  372.     if tostring(params[1]) == "nil" then params[1] = appl_energ[1] end
  373.     if tostring(params[1]) ~= "nil" then
  374.     local p_methods = peripheral.getMethods(params[1])
  375.     for k,v in pairs(p_methods) do
  376.       if v == "getTotalBytes" then applicable = true end
  377.     end
  378.     if applicable then
  379.       log_add("Method <getTotalBytes> applicable: "..tostring(params[1]))
  380.       answer[2] = peripheral.call(params[1], "getTotalBytes") or 0
  381.       answer[1] = (answer[2] - peripheral.call(params[1], "getFreeBytes")) or 0
  382.       answer[3] = tostring(math.floor((answer[1] / answer[2]) * 100)) .. "%"
  383.       answer[5] = peripheral.call(params[1], "getTotalItemTypes") or 0
  384.       answer[4] = (answer[5] - peripheral.call(params[1], "getRemainingItemTypes")) or 0
  385.       answer[6] = tostring(math.floor((answer[4] / answer[5]) * 100)) .. "%"
  386.       log_add(tostring(params[1]).." Details: "..answer[1].." "..answer[2].." "..answer[3])
  387.       log_add(tostring(params[1]).." Details: "..answer[4].." "..answer[5].." "..answer[6])
  388.     else
  389.       log_add("Method <getTotalBytes> not applicable: "..tostring(params[1]), true)
  390.     end
  391.     end
  392.     return answer
  393.   end
  394.  
  395.   peripherals_sort = function()
  396.     log_add("Sorting peripherals")
  397.     local mon, cap, ae = 2, 1, 1
  398.     screens = {[1] = term}
  399.     capacitors = {}
  400.     appl_energ = {}
  401.  
  402.     for num=2,table.maxn(peripheral_list) do
  403.       log_add("Sorting Peripheral: "..tostring(peripheral_list[num]))
  404.       local test1, test2 = {}, {}
  405.       test1 = peripherals_capacitor(peripheral_list[num])
  406.       test2 = peripherals_applied_energ(peripheral_list[num])
  407.    
  408.       if peripheral.getType(peripheral_list[num]) == "monitor" then
  409.         screens[mon] = peripheral_list[num] log_add("Adding Monitor: "..peripheral_list[num]) mon = mon + 1
  410.       elseif test1["capacity"] ~= nil then
  411.         capacitors[cap] = peripheral_list[num] log_add("Adding Capacitor: "..peripheral_list[num]) cap = cap + 1
  412.       elseif test2[2] ~= nil then
  413.         appl_energ[ae] = peripheral_list[num] log_add("Adding AE: "..peripheral_list[num]) ae = ae + 1
  414.       elseif peripheral.getType(peripheral_list[num]) == "modem" then
  415.         if peripheral.call(peripheral_list[num], "isWireless") then wireless = peripheral_list[num] log_add("Found Wireless Modem: "..tostring(peripheral_list[num])) end
  416.       end
  417.     end
  418.     if wireless ~= nil then rednet.open(wireless) log_add("Wireless added") else log_add("Failure to add Wireless Modem: Program Terminated", true) print("Failure to find Wireless Modem: Program Terminated") shell.exit() end
  419.   end
  420. end
  421.  
  422. -- SCREENS --
  423.  
  424. do
  425.   screen_change_colors = function(...)
  426.     local params = {...}
  427.     local color1, color2 = params[2] or colors.white, params[3] or colors.black
  428.     params[1].setTextColor(color1)
  429.     params[1].setBackgroundColor(color2)
  430.     log_add("Changing colors to: "..color1.." "..color2)
  431.   end
  432.  
  433.   screen_text = function(...)
  434.     local params = {...}
  435.     params[1].setCursorPos(params[3], params[4])
  436.     screen_change_colors(params[1], (params[5] or nil), (params[6] or nil))
  437.     log_add("Writting on: "..tostring(params[1]).." "..tostring(params[2]).." "..tostring(params[3]).." "..tostring(params[4]))
  438.     params[1].write(params[2]:upper())
  439.     screen_change_colors(params[1])
  440.   end
  441.  
  442.   screen_drawbutton = function(command, text, num, x, y)
  443.     local params = {[1] = command, [2] = text, [3] = num, [4] = x, [5] = y}
  444.     local answer, log = {}, log
  445.     answer[num] = {}
  446.     log_add("Drawing Button Start")
  447.     local text = " "..string.gsub(tostring(params[2]), "_", "%s").." "
  448.     while string.len(text) < button_length() and num >= 9 do text = text.." " end
  449.     log_add("Drawing Button: "..text.." "..string.len(text))
  450.     answer[num]["x1"], answer[num]["y"] = x, y
  451.     if status[num] then color1 = colors.green else color1 = colors.red end
  452.     screen_text(params[1], text, params[4], params[5], colors.white, color1)
  453.     answer[num]["x2"] = params[1].getCursorPos()
  454.     if params[1] == term then
  455.       term_button[num] = {}
  456.       term_button[num]["x1"] = answer[num]["x1"]
  457.       term_button[num]["y"] = answer[num]["y"]
  458.       term_button[num]["x2"] = answer[num]["x2"]
  459.       log = log_add("Making Button: "..tostring(params[1]).." "..text.." "..tostring(num).." "..tostring(status[num]).." "..tostring(term_button[num]["x1"]).." "..tostring(term_button[num]["y"]).." "..tostring(term_button[num]["x2"]))
  460.     else
  461.       mon_button[num] = {}
  462.       mon_button[num]["x1"] = answer[num]["x1"]
  463.       mon_button[num]["y"] = answer[num]["y"]
  464.       mon_button[num]["x2"] = answer[num]["x2"]
  465.       log = log_add("Making Button: "..tostring(params[1]).." "..text.." "..tostring(num).." "..tostring(status[num]).." "..tostring(mon_button[num]["x1"]).." "..tostring(mon_button[num]["y"]).." "..tostring(mon_button[num]["x2"]))
  466.     end
  467.     if log ~= true then log_button("Failed to Draw Button: "..tostring(num), true) end
  468.   end
  469.  
  470.   screen_box = function(...)
  471.     local params = {...}
  472.     term.redirect(params[1])
  473.     paintutils.drawLine(params[5], params[4], params[3], params[4], params[2])
  474.     paintutils.drawLine(params[3], params[4], params[3], params[6], params[2])
  475.     paintutils.drawLine(params[5], params[6], params[3], params[6], params[2])
  476.     paintutils.drawLine(params[5], params[4], params[5], params[6], params[2])
  477.     term.restore()
  478.   end
  479.  
  480.   screen_energy = function(...)
  481.       local params = {...}
  482.       log_add("Drawing Energy")
  483.       local x, y = 2, 4
  484.       local text1, text2 = {}, {}
  485.       text1 = peripherals_compile()
  486.       text2 = peripherals_applied_energ()
  487.       for k,v in pairs(text1) do
  488.             log_add("Drawing RF: "..tostring(k).." "..tostring(v))
  489.         if type(v) == "number" then v = decimal(v) end
  490.         v = "rf: "..tostring(v)
  491.         screen_text(params[1], v, x, y)
  492.         y = y + 1
  493.           end
  494.           y = y + 1
  495.       for k,v in pairs(text2) do
  496.             log_add("Drawing AE: "..tostring(k).." "..tostring(v))
  497.         if type(v) == "number" then v = decimal(v) end
  498.         v = "ae: "..tostring(v)
  499.         screen_text(params[1], v, x, y)
  500.         y = y + 1
  501.           end
  502.       log_add("Drawing Energy: Complete")
  503.   end
  504.  
  505.   screen_bar = function(...)
  506.     local params = {...}
  507.     log_add("Drawing Top Bar")
  508.     local x, y = 1, 1
  509.     params[1].setCursorPos(2,1)
  510.     for num=1,4 do
  511.       x, y = params[1].getCursorPos()
  512.       screen_drawbutton(params[1], tostring(names[num]), tonumber(num), x, y)
  513.     end
  514.     x, y = 2, 2
  515.     screen_text(params[1], (" "..string.gsub(tostring(CONFIG.HEADING), "_", " ").." "), x, y, colors.white, colors.blue)
  516.     log_add("Drawing Top Bar: Complete")
  517.   end
  518.    
  519.   screen_main = function(...)
  520.     local params = {...}
  521.     log_add("Drawing Screen Main: "..tostring(current_page))
  522.     start = 10
  523.     if current_page == nil then current_page = 1 end
  524.     if capacitors[1] ~= nil or appl_energ[1] ~= nil then
  525.       screen_energy(params[1])
  526.     end
  527.     local x, y, rows = term_x1, 4, 1
  528.     if params[1] == term then
  529.       page_length = term_rows
  530.       rows, border_x1, border_y1, border_x2, border_y2 = term_rows, term_x1, term_y1, term_x2, term_y2
  531.     else
  532.       page_length = mon_rows
  533.       rows, border_x1, border_y1, border_x2, border_y2 = mon_rows, mon_x1, mon_y1, mon_x2, mon_y2
  534.     end
  535.     if current_page > 1 then start = (start + ((current_page - 1) * page_length)) end
  536.     for num1=1,table.maxn(names) do
  537.       y = 4
  538.       for num2=1,rows do
  539.         if x >= border_x1 and (x + button_length()) <= border_x2 and y <= border_y2 then
  540.           if names[start] ~= nil then
  541.             screen_drawbutton(params[1], names[start], start, x, y)
  542.           end
  543.         end
  544.         start, y = start + 1, y + 2
  545.       end
  546.       x = x + button_length() + 1
  547.     end
  548.     log_add("Drawing Screen Main: Complete "..tostring(current_page))
  549.   end
  550.  
  551.   screen_control = function(...)
  552.     local params = {...}
  553.     log_add("Drawing Screen Control")
  554.     local x, y = 2, 4
  555.     for num=5,9 do screen_drawbutton(params[1], names[num], num, x, y) y = y + 2 end
  556.     x, y = 2, 2
  557.     screen_text(params[1], "Label: "..os.getComputerLabel()..", ID: "..os.getComputerID(), x, y)
  558.     log_add("Drawing Screen Control: Complete")
  559.   end
  560.    
  561.   screen_drawall = function(...)
  562.     local params = {...}
  563.     mon_button = {[1] = {}}
  564.     term_button = {[1] = {}}
  565.     if current_screen == nil then current_screen = "main" end
  566.     for num=1,table.maxn(screens) do
  567.       log_add("Draw All: Drawing Screen: "..tostring(screens[num]))
  568.       if screens[num] == term then mon = screens[num] else mon = peripheral.wrap(screens[num]) end
  569.       mon.clear()
  570.       if num == 1 then
  571.         term_x, term_y = mon.getSize()
  572.         term_x, term_y = term_x - 2, term_y - 4
  573.         term_x1, term_y1, term_x2, term_y2 = 2, 4, term_x + 1, term_y + 2
  574.         if capacitors[1] ~= nil or appl_energ[1] ~= nil then display_energy = true term_x, term_x1 = term_x - 9, term_x1 + 9 end
  575.         term_rows, term_cols = math.floor(term_y / 2), math.floor(term_x / button_length())
  576.         term_total = term_rows * term_cols
  577.         log_add("Term Details log: "..term_x.." "..term_y) log_add("Term Details log: "..term_x1.." "..term_y1.." "..term_x2.." "..term_y2) log_add("Term Details log: "..term_rows.." "..term_cols.." "..term_total)
  578.       elseif num == 2 then
  579.         mon.setTextScale(tonumber(CONFIG.TEXT_SCALE))
  580.         mon_x, mon_y = mon.getSize()
  581.         mon_x, mon_y = mon_x - 2, mon_y - 4
  582.         mon_x1, mon_y1, mon_x2, mon_y2 = 2, 4, mon_x + 1, mon_y + 2
  583.         if capacitors[1] ~= nil or appl_energ[1] ~= nil then display_energy = true mon_x, mon_x1 = mon_x - 9, mon_x1 + 9 end
  584.         mon_rows, mon_cols = math.floor(mon_y / 2), math.floor(mon_x / button_length())
  585.         mon_total = mon_rows * mon_cols
  586.         log_add("Mon Details log: "..mon_x.." "..mon_y) log_add("Mon Details log: "..mon_x1.." "..mon_y1.." "..mon_x2.." "..mon_y2) log_add("Mon Details log: "..mon_rows.." "..mon_cols.." "..mon_total)
  587.       end
  588.       screen_bar(mon)
  589.       if peripheral.getType(tostring(screens[num])) == "monitor" then mon.setTextScale(tonumber(CONFIG.TEXT_SCALE)) end
  590.       if current_screen == "main" then
  591.         screen_main(mon)
  592.       elseif current_screen == "control" then
  593.         screen_control(mon)
  594.       end
  595.       log_add("Draw All: Drawing Screen: "..tostring(screens[num])..": Complete")
  596.     end
  597.   end
  598.  
  599. -- RELAYS --
  600.  
  601.   relay_send =  function(command)
  602.     if command then
  603.       for num = 2,table.maxn(relays) do
  604.         rednet.send(relays[num], textutils.serialize(names).." "..textutils.serialize(ids).." "..textutils.serialize(status).." "..textutils.serialize(relays))
  605.         log_button("Sending Relay to: "..tostring(relays[num]))
  606.       end
  607.     else
  608.       rednet.send(relays[1], textutils.serialize(names).." "..textutils.serialize(ids).." "..textutils.serialize(status).." "..textutils.serialize(relays))
  609.       log_button("Sending Relay to: "..tostring(relays[num]))
  610.     end
  611.   end
  612.  
  613.   relay_receive = function(...)
  614.     local params = {...}
  615.     log_button("Relay Recieved from: "..tostring(id))
  616.     local split_msg, command = {}, false
  617.     for k,v in ipairs(relays) do
  618.       if params[1] == v then command = true end
  619.     end
  620.     if command then
  621.       for w in string.gmatch(params[2], "%s+") do split_msg[#split_msg + 1] = w log_button(tostring(w)) end
  622.       if table.maxn(split_msg) == 4 then
  623.         names, ids, status, relays = textutils.unserialize(split_msg[1]), textutils.unserialize(split_msg[2]), textutils.unserialize(split_msg[3]), textutils.unserialize(split_msg[4])
  624.       else
  625.         log_button("Relay Recieved from: "..tostring(id), true)
  626.       end
  627.     end
  628.     log_button("Relay Recieved from: "..tostring(id).." Complete")
  629.   end
  630.  
  631. -- BUTTONS --
  632.  
  633.   button_detect = function(command, x, y)
  634.     local params = {[1] = command, [2] = x, [3] = y}
  635.     log_button("Detecting buttoning @: "..tostring(command).." "..tostring(x).." "..tostring(y))
  636.     local number, found = 0, false
  637.     if params[1] == "mouse_click" then
  638.       for k,v in pairs(term_button) do
  639.         log_button("Checking: "..tostring(k).." "..tostring(term_button[k]["x1"]).." "..tostring(term_button[k]["y"]).." "..tostring(term_button[k]["x2"]))
  640.         if params[2] >= term_button[k]["x1"] and params[2] <= term_button[k]["x2"] and params[3] == term_button[k]["y"] then
  641.           number, found = k, true
  642.           log_button("Button found: "..tostring(k))
  643.         end
  644.       end
  645.     elseif params[1] == "monitor_touch" then
  646.       for k,v in pairs(mon_button) do
  647.         log_button("Checking: "..tostring(k).." "..tostring(mon_button[k]["x1"]).." "..tostring(mon_button[k]["y"]).." "..tostring(mon_button[k]["x2"]))
  648.         if params[2] >= mon_button[k]["x1"] and params[2] <= mon_button[k]["x2"] and params[3] == mon_button[k]["y"] then
  649.           number, found = k, true
  650.           log_button("Button found: "..tostring(k))
  651.         end
  652.       end
  653.     end
  654.     if found == false then log_button("Failed to find button", true) end
  655.     return number
  656.   end
  657.  
  658.   button_switch = function(...)
  659.     local params = {...}
  660.     local text, num = nil, params[2]
  661.     if params[1] then
  662.       text, status[num] = "deactivate", false
  663.     else
  664.       if status[10] then
  665.         text, status[num] = "activate", true
  666.       end
  667.     end
  668.     log_button("Button switch to: "..tostring(text).." "..tostring(status[num]).." "..tostring(ids[num]))
  669.     if tostring(text) ~= "nil" then
  670.       rednet.send(ids[num], text)
  671.       relay_send(CONFIG.RELAY)
  672.     end
  673.   end
  674.  
  675.   button_online = function(...)
  676.     local params = {...}
  677.     status[10] = not status[10]
  678.     log_button("Button Online changing to: "..tostring(status[10]))
  679.     if status[10] ~= true then
  680.       for k,v in ipairs(status) do
  681.         if k >= 11 then button_switch(true, k) end
  682.       end
  683.     end
  684.   end
  685.  
  686.   button_barselect = function(...)
  687.     local params = {...}
  688.     for num=1,9 do
  689.       status[num] = false
  690.     end
  691.     if params[1] ~= nil then
  692.       status[params[1]] = true
  693.       if params[1] == 1 or params[1] == 4 then
  694.         status[2] = true
  695.       end
  696.     end
  697.     log_button("Button Bar Select changing to: "..tostring(names[num]))
  698.   end
  699.  
  700. -- CONTROL --
  701.  
  702.   control_addbutton = function()
  703.     term.clear() log_button("Adding Button...")
  704.     screen_bar(term)
  705.     local answer = {}
  706.     while type(answer[1]) ~= "string" do
  707.       screen_text(term, "enter name: ", 2, 4)
  708.       answer[1] = string.upper(tostring(read())) or nil
  709.     end
  710.     while type(answer[2]) ~= "number" do
  711.       screen_text(term, "enter client id: ", 2, 5)
  712.       answer[2] = tonumber(read()) or nil
  713.     end
  714.     button_add(answer[1], answer[2], false)
  715.     while answer[3] ~= "TRUE" and answer[3] ~= "FALSE" do
  716.       screen_text(term, "adding button: "..answer[1].." "..answer[2], 2, 7)
  717.       screen_text(term, "confirm to save?: <true/false>", 2, 8)
  718.       term.setCursorPos(2, 9)
  719.       answer[3] = string.upper(tostring(read())) or nil
  720.     end
  721.     log_button("Adding Button: "..tostring(answer[1]).." "..tostring(answer[2]).." "..tostring(answer[3]))
  722.     button_barselect()
  723.     if answer[3] == "TRUE" then save_all() end
  724.     os.reboot()
  725.   end
  726.  
  727.   control_removebutton = function
  728.     term.clear() log_button("Removing Button...")
  729.     screen_bar(term)
  730.     local answer = {}
  731.     while type(answer[1]) ~= "string" do
  732.       screen_text(term, "enter name: ", 2, 4)
  733.       answer[1] = string.upper(tostring(read())) or nil
  734.     end
  735.     for k,v in ipairs(names) do
  736.       if v == answer[1] then answer[2] = k end
  737.     end
  738.     if answer[2] ~= nil then
  739.       answer[4], answer[5] = names[answer[2]], ids[answer[2]]
  740.       names[answer[2]], status[answer[2]], ids[answer[2]] = names[table.maxn(names)], status[table.maxn(status)], ids[table.maxn(ids)]
  741.       names[table.maxn(names)], status[table.maxn(status)], ids[table.maxn(ids)] = nil, nil, nil
  742.     end
  743.     while answer[3] ~= "TRUE" and answer[3] ~= "FALSE" do
  744.       screen_text(term, "removing button: "..answer[4].." "..answer[5], 2, 6)
  745.       screen_text(term, "confirm to save?: <true/false>", 2, 7)
  746.       term.setCursorPos(2, 8)
  747.       answer[3] = string.upper(tostring(read())) or nil
  748.     end
  749.     log_button("Removing Button: "..tostring(answer[1]).." "..tostring(answer[2]).." "..tostring(answer[3]))
  750.     button_barselect()
  751.     if answer[3] == "TRUE" then save_all() end
  752.     os.reboot()
  753.   end
  754.  
  755.   control_addrelay = function()
  756.     term.clear() log_button("Adding Relay...")
  757.     screen_bar(term)
  758.     local answer = {}
  759.     while type(answer[1]) ~= "number" do
  760.       screen_text(term, "if not main server > only enter in main server id...", 2, 4)
  761.       screen_text(term, "enter relay id: ", 2, 5)
  762.       answer[1] = tonumber(read()) or nil
  763.     end
  764.     relays[table.maxn(relays) + 1] = answer[1]
  765.     while answer[2] ~= "TRUE" and answer[2] ~= "FALSE" do
  766.       screen_text(term, "add relay: "..answer[1], 2, 6)
  767.       screen_text(term, "confirm to save?: <true/false>", 2, 7)
  768.       term.setCursorPos(2, 8)
  769.       answer[2] = string.upper(tostring(read())) or nil
  770.     end
  771.     log_button("Adding Relay: "..tostring(answer[1]).." "..tostring(answer[2]))
  772.     button_barselect()
  773.     if answer[2] == "TRUE" then save_all() end
  774.     os.reboot()
  775.   end
  776.  
  777.   control_removerelay = function()
  778.     term.clear() log_button("Removing Relay...")
  779.     screen_bar(term)
  780.     local answer = {}
  781.     while type(answer[1]) ~= "number" do
  782.       screen_text(term, "enter relay id: ", 2, 4)
  783.       answer[1] = tonumber(read()) or nil
  784.     end
  785.     for k,v in ipairs(names) do
  786.       if v == answer[1] then answer[2] = k end
  787.     end
  788.     if answer[2] ~= nil then
  789.       answer[4] = relays[answer[2]]
  790.       relays[answer[2]] = relays[table.maxn(ids)]
  791.       relays[table.maxn(ids)] = nil
  792.     end
  793.     while answer[3] ~= "TRUE" and answer[3] ~= "FALSE" do
  794.       screen_text(term, "removing relay: "..answer[2], 2, 6)
  795.       screen_text(term, "confirm to save?: <true/false>", 2, 7)
  796.       term.setCursorPos(2, 8)
  797.       answer[3] = string.upper(tostring(read())) or nil
  798.     end
  799.     log_button("Removing Relay: "..tostring(answer[1]).." "..tostring(answer[3]))
  800.     button_barselect()
  801.     if answer[3] == "TRUE" then save_all() end
  802.     os.reboot()
  803.   end
  804.  
  805.   control_update = function()
  806.     log_add("updating")
  807.     shell.run("pastebin get "..pastebinCode.." update")
  808.     if fs.exists("update") then
  809.         fs.delete(shell.getRunningProgram())
  810.         fs.copy("update", shell.getRunningProgram())
  811.         fs.delete("update")
  812.         log_add("update success")
  813.     else
  814.         log_add("update failed")
  815.     end
  816.     sleep(2)
  817.     os.reboot()
  818.   end
  819. end
  820.  
  821. -- SCREEN SAVER: TO BE IMPLEMENTED--
  822.  
  823.  
  824. -- PROGRAM --
  825.  
  826. peripherals_find()
  827. peripherals_sort()
  828. button_barselect(2)
  829. screen_drawall()
  830. if CONFIG.RELAY then relays[1] = os.getComputerID() end
  831. local event, id, x, y = nil, nil, nil, nil
  832. while true do
  833.  
  834.   if event == "monitor_touch" or event == "mouse_click" then
  835.  
  836.     local button_num = button_detect(event, x, y)
  837.     if button_num >= 1 and button_num <= 9 then
  838.       button_barselect(button_num)
  839.       if status[1] then if current_page ~= 1 then current_page = current_page - 1 end button_barselect(2) end
  840.       if status[2] then current_screen = "main" end
  841.       if status[3] then current_screen = "control" end
  842.       if status[4] then current_page = current_page + 1 button_barselect(2) end
  843.       if status[5] then control_addbutton() end
  844.       if status[6] then control_removebutton() end
  845.       if status[7] then control_addrelay() end
  846.       if status[8] then control_removerelay() end
  847.       if status[9] then control_update() end
  848.       peripherals_find() peripherals_sort() screen_drawall()
  849.     elseif button_num == 10 then
  850.       button_online()
  851.       peripherals_find() peripherals_sort() screen_drawall()
  852.     elseif button_num >= 11 then
  853.       button_switch(status[button_num], button_num)
  854.       peripherals_find() peripherals_sort() screen_drawall()
  855.     end
  856.     button_num = nil
  857.    
  858. --  elseif event == "rednet_message" then
  859. --    relay_receive(x)
  860. --    if CONFIG.RELAY then relay_send(CONFIG.RELAY) end
  861. --    peripherals_find() peripherals_sort() screen_drawall()
  862.    
  863. --  elseif event == "timer" then
  864. --    peripherals_find() peripherals_sort() screen_drawall()
  865.   end
  866.  
  867.   event, id, x, y = nil, nil, nil, nil
  868.   save_all()
  869. --  os.startTimer(tonumber(CONFIG.SCREEN_REFRESH_RATE))
  870.   event, id, x, y  = os.pullEvent()
  871.   log_button("Event Detected: "..tostring(event).." "..tostring(id).." "..tostring(x).." "..tostring(y))
  872. end
Advertisement
Add Comment
Please, Sign In to add comment