Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Script by Savage_Me55iah of WUGcraft--
- local version, pastebinCode = "v2.1", "LS4mrJQm"
- --Needs corresponding "Savage Client v1.7" script @ http://pastebin.com/nBw1rV8A--
- --INSTRUCTIONS ON USE--
- --save this program as "monitor" and type in startup: sleep(1) shell.run("monitor")--
- --to adjust the settings of script type "edit config", changing them here won't work once this program has been run once
- --to reset everything delete files: config, names, status, ids, relays
- --buttons will not work without the online button on, turning the online button off will shut off all other buttons
- --all monitors should be the same size, things will get awkward if not
- --the control tab is where you add and remove buttons, relays will not work for the moment
- --TO DO LIST--
- --make a "update" button under the controls tab
- --make a "restart" and "terminate" button under the controls tab
- --make a "logs" button under the controls tab
- --setup the relays (setting up more than 1 server around base)
- --setup the screen saver
- --CHANGELOG--
- --2.1 = update functionality
- --Contact Savage_Me55iah @ CraftAU.com.au or Feenix-Rising.enjin.com or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
- -- Logging for debugging --
- local log_file = "LOG"
- local button_log = "BUTTON_LOG"
- local save_direct = "saves"
- do
- if fs.exists(tostring(save_direct.."/"..log_file)) then fs.delete(tostring(save_direct.."/"..log_file)) end
- if fs.exists(tostring(save_direct.."/"..button_log)) then fs.delete(tostring(save_direct.."/"..button_log)) end
- log_add = function(...)
- local params = {...}
- local text = tostring(params[1])
- local filehandler = fs.open(tostring(save_direct.."/"..log_file), 'a')
- if params[2] then text = "--"..params[1] end
- filehandler.write(text)
- filehandler.write("\n")
- filehandler.close()
- return true
- end
- log_button = function(...)
- local params = {...}
- local text = tostring(params[1])
- local filehandler = fs.open(tostring(save_direct.."/"..button_log), 'a')
- if params[2] then text = "--"..params[1] end
- filehandler.write(text)
- filehandler.write("\n")
- filehandler.close()
- return true
- end
- local tArgs = {...}
- if "update" == tostring(tArgs[1]) then
- log_add("updating")
- shell.run("pastebin get "..pastebinCode.." update")
- if fs.exists("update") then
- fs.delete(shell.getRunningProgram())
- fs.copy("update", shell.getRunningProgram())
- fs.delete("update")
- log_add("update success")
- else
- log_add("update failed")
- end
- sleep(2)
- os.reboot()
- end
- end
- -- Label --
- if not os.getComputerLabel() then
- term.clear() term.setCursorPos(1,1)
- log_add("no computer label")
- print("Enter computer label: ")
- os.setComputerLabel(read())
- log_add("computer label added: "..os.getComputerLabel())
- term.clear()
- end
- -- Settings & Saving/Loading --
- local settings_file = "config"
- local settings_template = [[-- adjust these configuration options as necessary.
- -- IMPORTANT!, API won't load if there are spaces in the values
- COMPUTER_LABEL = "${label}"
- COMPUTER_ID = ${id}
- HEADING = "${heading}"
- -- the relay determines if this server computer relays to the other server computers
- RELAY = "${relay}"
- OUTPUT_TO_MONITOR = "${mon_output}"
- SCREENSAVER = "${screensaver}"
- DISPLAY_ENERGY = "${display_energy}"
- SCREENSAVER_IMAGE = "${image}"
- IMAGE_X, IMAGE_Y = ${image_x}, ${image_y}
- SCREENSAVER_TIMEOUT = ${timeout}
- SCREEN_REFRESH_RATE = ${refresh_rate}
- TEXT_SCALE = ${text_scale}
- ]]
- local function interpolate(s, params)
- return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
- end
- do
- save_settings = function()
- local filehandler = fs.open(tostring(save_direct.."/"..settings_file), "w")
- settings = {
- label = COMPUTER_LABEL or os.getComputerLabel(),
- id = os.getComputerID(),
- heading = HEADING or "Savage_Monitor_"..version,
- relay = RELAY or "false",
- mon_output = OUTPUT_TO_MONITOR or "false",
- display_energy = DISPLAY_ENERGY or "false",
- screensaver = SCREENSAVER or "false",
- image_x = IMAGE_X or 27,
- image_y = IMAGE_Y or 9,
- timeout = SCREENSAVER_TIMEOUT or 50,
- refresh_rate = SCREEN_REFRESH_RATE or 300,
- text_scale = TEXT_SCALE or 1,
- }
- filehandler.write(interpolate(settings_template, settings))
- filehandler.close()
- log_add("settings saved")
- end
- end
- if fs.exists(tostring(save_direct.."/"..settings_file)) == false then
- log_add("Settings file not found", true)
- log_add("Generating Settings")
- save_settings()
- return
- else
- os.unloadAPI(tostring(save_direct.."/"..settings_file))
- if os.loadAPI(tostring(save_direct.."/"..settings_file)) == false then log_add("Could not load the settings file!", true) end
- CONFIG = nil
- for k, v in pairs(_G) do
- if k == settings_file then CONFIG = v break end
- end
- if CONFIG == nil then log_add("CONFIG came up nil", true) end
- end
- do
- load_one = function(command)
- if fs.exists(tostring(save_direct.."/"..command) then
- local fileHandler = fs.open(save_direct.."/"..command, 'r')
- return textutils.unserialize(fileHandler.readAll())
- end
- end
- save_one = function(command1, command2)
- local fileHandler = fs.open(tostring(save_direct.."/"..command1), 'w')
- fileHandler.write(textutils.serialize(command2))
- fileHandler.close()
- log_add("Saving: "..command1)
- end
- load_all = function()
- log_add("Load all")
- names = load_one("names")
- ids = load_one("ids")
- status = load_one("status")
- relays = load_one("relays")
- log_add("Load successful")
- end
- save_all = function()
- log_add("Save all")
- save_one("names", names)
- save_one("ids", ids)
- save_one("status", status)
- save_one("relays", relays)
- log_add("Save successful")
- end
- end
- -- TABLES --
- names = {}
- ids = {}
- status = {}
- relays = {}
- sides = {[1] = "front", [2] = "back", [3] = "left", [4] = "right", [5] = "top", [6] = "bottom"}
- peripheral_list = {[1] = term}
- do
- button_add = function(...)
- local params = {...}
- local num = table.maxn(names) + 1
- names[num], ids[num], status[num] = params[1], params[2], params[3]
- local text = "Adding button #"..num..": "..params[1].." "..params[2].." "..tostring(params[3])
- log_add(text)
- end
- install = function()
- button_add("scroll l", 0, false) -- 1
- button_add("main", 0, true) -- 2
- button_add("control", 0, false) -- 3
- button_add("scroll r", 0, false) -- 4
- button_add("button add", 0, false) -- 5
- button_add("button remove", 0, false) -- 6
- button_add("relay add", 0, false) -- 7
- button_add("relay remove", 0, false) -- 8
- button_add("update", 0, false) -- 9
- button_add("online", 0, true) -- 10
- save_all()
- load_all()
- end
- end
- if fs.exists(tostring(save_direct.."/names")) then
- load_all()
- if names[1] ~= "scroll l" then install() end
- else
- install()
- end
- function imagewrite(params)
- outputFile:write("\n")
- outputFile:write(params)
- end
- local image_table = {[1] = "eeeeeeeeeeeeeeeeeeeeeeeeeee",
- [2] = "eeeeeffffffffeefffffefffffe",
- [3] = "eeeeff0000f0feef000ffff00fe",
- [4] = "eeeef0fffffffeef0f00f00f0fe",
- [5] = "eeeeff00ffeeeeef0ff000ff0fe",
- [6] = "efffffff0feeeeefffff0fffffe",
- [7] = "ef0f0000ffeeeeef0fefffef0fe",
- [8] = "efffffffffeeeeefffeeeeefffe",
- [9] = "eeeeeeeeeeeeeeeeeeeeeeeeeee"
- }
- if not fs.exists(tostring(save_direct.."/image")) then
- outputFile = io.open(tostring(save_direct.."/image"), "w")
- log_add("Writing Image file")
- outputFile:write(image_table[1])
- for num = 2,table.maxn(image_table) do imagewrite(image_table[num]) end
- num = nil
- outputFile:close()
- end
- do
- button_length = function()
- local length = 1
- for k,v in ipairs(names) do
- if tonumber(string.len(v) + 3) > length and tonumber(k) >= 9 then length = string.len(v) end
- end
- return length
- end
- end
- -- PERIPHERALS --
- do
- peripherals_find = function()
- log_add("Finding Peripherals Direct")
- peripheral_list = {[1] = term}
- local num = 2
- for k,v in ipairs(sides) do
- p_type = peripheral.getType(v) or nil
- if p_type ~= nil then peripheral_list[num] = v num = num + 1 log_add("Found Peripheral: "..v.." "..p_type) end
- end
- log_add("Find Peripherals Direct Complete") log_add("Checking for Remote")
- local remote = nil
- for k,v in ipairs(peripheral_list) do
- if peripheral.getType(tostring(v)) == "modem" then
- if not peripheral.call(v, "isWireless") then remote = peripheral.wrap(v) log_add("Remote found: "..tostring(v)) end
- end
- end
- if remote ~= nil then
- log_add("Finding Peripherals Remote")
- peripheral_list = {}
- peripheral_list = remote.getNamesRemote()
- peripheral_list[(table.maxn(peripheral_list) + 1)] = peripheral_list[1]
- peripheral_list[1] = term
- local num = (table.maxn(peripheral_list) + 1)
- for k,v in ipairs(sides) do
- p_type = peripheral.getType(v) or nil
- if p_type ~= nil then peripheral_list[num] = v num = num + 1 end
- end
- else
- log_add("Remote not found", true)
- end
- end
- end
- -- PERIPHERAL SORTER --
- function decimal(number)
- log_add("Converting #: "..tostring(number))
- if type(number) == "number" then
- local text, num = ".", number
- if num >= 1000 then text = "K" end
- if num >= 1000000 then text = "M" end
- if num >= 1000000000 then text = "B" end
- if num >= 1000000000000 then text = "T" end
- while num >= 1000 do
- num = num / 1000
- end
- if text ~= "." then
- text = tostring(math.floor(num)..text) or 0
- else
- text = tostring(math.floor(num)) or 0
- end
- log_add("# Conversion complete: "..text)
- return text
- else
- return number
- end
- end
- do
- peripherals_capacitor = function(...)
- local params = {...}
- log_add("Getting Capacitor details: "..tostring(params[1]))
- local answer = {[1] = false}
- local command = peripheral.wrap(params[1])
- local p_methods = peripheral.getMethods(tostring(params[1]))
- for k,v in ipairs(p_methods) do
- if v == "getMaxEnergyStored" then answer[1] = true end
- end
- if answer[1] then
- log_add("Method <getMaxEnergyStored> applicable: "..tostring(params[1]))
- answer["capacity"] = command.getMaxEnergyStored("north") or 0
- answer["stored"] = command.getEnergyStored("north") or 0
- answer["percentage"] = math.floor((answer["stored"] / answer["capacity"])).."%" or "0%"
- log_add(tostring(params[1]).." Details: "..answer["capacity"].." "..answer["stored"].." "..answer["percentage"])
- else
- log_add("Method <getMaxEnergyStored> not applicable: "..tostring(params[1]), true)
- end
- return answer
- end
- peripherals_compile = function()
- local answer = {[1] = 0, [2] = 0, [3] = 0}
- local details = {}
- log_add("Compiling Capacitors data")
- for k,v in ipairs(capacitors) do
- details = peripherals_capacitor(v)
- answer[2] = answer[2] + details["capacity"] or 0
- answer[1] = answer[1] + details["stored"] or 0
- end
- answer[3] = tostring(math.floor((answer[1] / answer[2]) * 100)) .. "%"
- log_add("Compile Results: "..answer[2].." "..answer[1].." "..answer[3])
- return answer
- end
- peripherals_applied_energ = function(command)
- local params = {[1] = command or nil}
- log_add("Getting AE details: "..tostring(params[1]))
- local answer, applicable = {}, false
- if tostring(params[1]) == "nil" then params[1] = appl_energ[1] end
- if tostring(params[1]) ~= "nil" then
- local p_methods = peripheral.getMethods(params[1])
- for k,v in pairs(p_methods) do
- if v == "getTotalBytes" then applicable = true end
- end
- if applicable then
- log_add("Method <getTotalBytes> applicable: "..tostring(params[1]))
- answer[2] = peripheral.call(params[1], "getTotalBytes") or 0
- answer[1] = (answer[2] - peripheral.call(params[1], "getFreeBytes")) or 0
- answer[3] = tostring(math.floor((answer[1] / answer[2]) * 100)) .. "%"
- answer[5] = peripheral.call(params[1], "getTotalItemTypes") or 0
- answer[4] = (answer[5] - peripheral.call(params[1], "getRemainingItemTypes")) or 0
- answer[6] = tostring(math.floor((answer[4] / answer[5]) * 100)) .. "%"
- log_add(tostring(params[1]).." Details: "..answer[1].." "..answer[2].." "..answer[3])
- log_add(tostring(params[1]).." Details: "..answer[4].." "..answer[5].." "..answer[6])
- else
- log_add("Method <getTotalBytes> not applicable: "..tostring(params[1]), true)
- end
- end
- return answer
- end
- peripherals_sort = function()
- log_add("Sorting peripherals")
- local mon, cap, ae = 2, 1, 1
- screens = {[1] = term}
- capacitors = {}
- appl_energ = {}
- for num=2,table.maxn(peripheral_list) do
- log_add("Sorting Peripheral: "..tostring(peripheral_list[num]))
- local test1, test2 = {}, {}
- test1 = peripherals_capacitor(peripheral_list[num])
- test2 = peripherals_applied_energ(peripheral_list[num])
- if peripheral.getType(peripheral_list[num]) == "monitor" then
- screens[mon] = peripheral_list[num] log_add("Adding Monitor: "..peripheral_list[num]) mon = mon + 1
- elseif test1["capacity"] ~= nil then
- capacitors[cap] = peripheral_list[num] log_add("Adding Capacitor: "..peripheral_list[num]) cap = cap + 1
- elseif test2[2] ~= nil then
- appl_energ[ae] = peripheral_list[num] log_add("Adding AE: "..peripheral_list[num]) ae = ae + 1
- elseif peripheral.getType(peripheral_list[num]) == "modem" then
- if peripheral.call(peripheral_list[num], "isWireless") then wireless = peripheral_list[num] log_add("Found Wireless Modem: "..tostring(peripheral_list[num])) end
- end
- end
- 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
- end
- end
- -- SCREENS --
- do
- screen_change_colors = function(...)
- local params = {...}
- local color1, color2 = params[2] or colors.white, params[3] or colors.black
- params[1].setTextColor(color1)
- params[1].setBackgroundColor(color2)
- log_add("Changing colors to: "..color1.." "..color2)
- end
- screen_text = function(...)
- local params = {...}
- params[1].setCursorPos(params[3], params[4])
- screen_change_colors(params[1], (params[5] or nil), (params[6] or nil))
- log_add("Writting on: "..tostring(params[1]).." "..tostring(params[2]).." "..tostring(params[3]).." "..tostring(params[4]))
- params[1].write(params[2]:upper())
- screen_change_colors(params[1])
- end
- screen_drawbutton = function(command, text, num, x, y)
- local params = {[1] = command, [2] = text, [3] = num, [4] = x, [5] = y}
- local answer, log = {}, log
- answer[num] = {}
- log_add("Drawing Button Start")
- local text = " "..string.gsub(tostring(params[2]), "_", "%s").." "
- while string.len(text) < button_length() and num >= 9 do text = text.." " end
- log_add("Drawing Button: "..text.." "..string.len(text))
- answer[num]["x1"], answer[num]["y"] = x, y
- if status[num] then color1 = colors.green else color1 = colors.red end
- screen_text(params[1], text, params[4], params[5], colors.white, color1)
- answer[num]["x2"] = params[1].getCursorPos()
- if params[1] == term then
- term_button[num] = {}
- term_button[num]["x1"] = answer[num]["x1"]
- term_button[num]["y"] = answer[num]["y"]
- term_button[num]["x2"] = answer[num]["x2"]
- 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"]))
- else
- mon_button[num] = {}
- mon_button[num]["x1"] = answer[num]["x1"]
- mon_button[num]["y"] = answer[num]["y"]
- mon_button[num]["x2"] = answer[num]["x2"]
- 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"]))
- end
- if log ~= true then log_button("Failed to Draw Button: "..tostring(num), true) end
- end
- screen_box = function(...)
- local params = {...}
- term.redirect(params[1])
- paintutils.drawLine(params[5], params[4], params[3], params[4], params[2])
- paintutils.drawLine(params[3], params[4], params[3], params[6], params[2])
- paintutils.drawLine(params[5], params[6], params[3], params[6], params[2])
- paintutils.drawLine(params[5], params[4], params[5], params[6], params[2])
- term.restore()
- end
- screen_energy = function(...)
- local params = {...}
- log_add("Drawing Energy")
- local x, y = 2, 4
- local text1, text2 = {}, {}
- text1 = peripherals_compile()
- text2 = peripherals_applied_energ()
- for k,v in pairs(text1) do
- log_add("Drawing RF: "..tostring(k).." "..tostring(v))
- if type(v) == "number" then v = decimal(v) end
- v = "rf: "..tostring(v)
- screen_text(params[1], v, x, y)
- y = y + 1
- end
- y = y + 1
- for k,v in pairs(text2) do
- log_add("Drawing AE: "..tostring(k).." "..tostring(v))
- if type(v) == "number" then v = decimal(v) end
- v = "ae: "..tostring(v)
- screen_text(params[1], v, x, y)
- y = y + 1
- end
- log_add("Drawing Energy: Complete")
- end
- screen_bar = function(...)
- local params = {...}
- log_add("Drawing Top Bar")
- local x, y = 1, 1
- params[1].setCursorPos(2,1)
- for num=1,4 do
- x, y = params[1].getCursorPos()
- screen_drawbutton(params[1], tostring(names[num]), tonumber(num), x, y)
- end
- x, y = 2, 2
- screen_text(params[1], (" "..string.gsub(tostring(CONFIG.HEADING), "_", " ").." "), x, y, colors.white, colors.blue)
- log_add("Drawing Top Bar: Complete")
- end
- screen_main = function(...)
- local params = {...}
- log_add("Drawing Screen Main: "..tostring(current_page))
- start = 10
- if current_page == nil then current_page = 1 end
- if capacitors[1] ~= nil or appl_energ[1] ~= nil then
- screen_energy(params[1])
- end
- local x, y, rows = term_x1, 4, 1
- if params[1] == term then
- page_length = term_rows
- rows, border_x1, border_y1, border_x2, border_y2 = term_rows, term_x1, term_y1, term_x2, term_y2
- else
- page_length = mon_rows
- rows, border_x1, border_y1, border_x2, border_y2 = mon_rows, mon_x1, mon_y1, mon_x2, mon_y2
- end
- if current_page > 1 then start = (start + ((current_page - 1) * page_length)) end
- for num1=1,table.maxn(names) do
- y = 4
- for num2=1,rows do
- if x >= border_x1 and (x + button_length()) <= border_x2 and y <= border_y2 then
- if names[start] ~= nil then
- screen_drawbutton(params[1], names[start], start, x, y)
- end
- end
- start, y = start + 1, y + 2
- end
- x = x + button_length() + 1
- end
- log_add("Drawing Screen Main: Complete "..tostring(current_page))
- end
- screen_control = function(...)
- local params = {...}
- log_add("Drawing Screen Control")
- local x, y = 2, 4
- for num=5,9 do screen_drawbutton(params[1], names[num], num, x, y) y = y + 2 end
- x, y = 2, 2
- screen_text(params[1], "Label: "..os.getComputerLabel()..", ID: "..os.getComputerID(), x, y)
- log_add("Drawing Screen Control: Complete")
- end
- screen_drawall = function(...)
- local params = {...}
- mon_button = {[1] = {}}
- term_button = {[1] = {}}
- if current_screen == nil then current_screen = "main" end
- for num=1,table.maxn(screens) do
- log_add("Draw All: Drawing Screen: "..tostring(screens[num]))
- if screens[num] == term then mon = screens[num] else mon = peripheral.wrap(screens[num]) end
- mon.clear()
- if num == 1 then
- term_x, term_y = mon.getSize()
- term_x, term_y = term_x - 2, term_y - 4
- term_x1, term_y1, term_x2, term_y2 = 2, 4, term_x + 1, term_y + 2
- if capacitors[1] ~= nil or appl_energ[1] ~= nil then display_energy = true term_x, term_x1 = term_x - 9, term_x1 + 9 end
- term_rows, term_cols = math.floor(term_y / 2), math.floor(term_x / button_length())
- term_total = term_rows * term_cols
- 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)
- elseif num == 2 then
- mon.setTextScale(tonumber(CONFIG.TEXT_SCALE))
- mon_x, mon_y = mon.getSize()
- mon_x, mon_y = mon_x - 2, mon_y - 4
- mon_x1, mon_y1, mon_x2, mon_y2 = 2, 4, mon_x + 1, mon_y + 2
- if capacitors[1] ~= nil or appl_energ[1] ~= nil then display_energy = true mon_x, mon_x1 = mon_x - 9, mon_x1 + 9 end
- mon_rows, mon_cols = math.floor(mon_y / 2), math.floor(mon_x / button_length())
- mon_total = mon_rows * mon_cols
- 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)
- end
- screen_bar(mon)
- if peripheral.getType(tostring(screens[num])) == "monitor" then mon.setTextScale(tonumber(CONFIG.TEXT_SCALE)) end
- if current_screen == "main" then
- screen_main(mon)
- elseif current_screen == "control" then
- screen_control(mon)
- end
- log_add("Draw All: Drawing Screen: "..tostring(screens[num])..": Complete")
- end
- end
- -- RELAYS --
- relay_send = function(command)
- if command then
- for num = 2,table.maxn(relays) do
- rednet.send(relays[num], textutils.serialize(names).." "..textutils.serialize(ids).." "..textutils.serialize(status).." "..textutils.serialize(relays))
- log_button("Sending Relay to: "..tostring(relays[num]))
- end
- else
- rednet.send(relays[1], textutils.serialize(names).." "..textutils.serialize(ids).." "..textutils.serialize(status).." "..textutils.serialize(relays))
- log_button("Sending Relay to: "..tostring(relays[num]))
- end
- end
- relay_receive = function(...)
- local params = {...}
- log_button("Relay Recieved from: "..tostring(id))
- local split_msg, command = {}, false
- for k,v in ipairs(relays) do
- if params[1] == v then command = true end
- end
- if command then
- for w in string.gmatch(params[2], "%s+") do split_msg[#split_msg + 1] = w log_button(tostring(w)) end
- if table.maxn(split_msg) == 4 then
- names, ids, status, relays = textutils.unserialize(split_msg[1]), textutils.unserialize(split_msg[2]), textutils.unserialize(split_msg[3]), textutils.unserialize(split_msg[4])
- else
- log_button("Relay Recieved from: "..tostring(id), true)
- end
- end
- log_button("Relay Recieved from: "..tostring(id).." Complete")
- end
- -- BUTTONS --
- button_detect = function(command, x, y)
- local params = {[1] = command, [2] = x, [3] = y}
- log_button("Detecting buttoning @: "..tostring(command).." "..tostring(x).." "..tostring(y))
- local number, found = 0, false
- if params[1] == "mouse_click" then
- for k,v in pairs(term_button) do
- log_button("Checking: "..tostring(k).." "..tostring(term_button[k]["x1"]).." "..tostring(term_button[k]["y"]).." "..tostring(term_button[k]["x2"]))
- if params[2] >= term_button[k]["x1"] and params[2] <= term_button[k]["x2"] and params[3] == term_button[k]["y"] then
- number, found = k, true
- log_button("Button found: "..tostring(k))
- end
- end
- elseif params[1] == "monitor_touch" then
- for k,v in pairs(mon_button) do
- log_button("Checking: "..tostring(k).." "..tostring(mon_button[k]["x1"]).." "..tostring(mon_button[k]["y"]).." "..tostring(mon_button[k]["x2"]))
- if params[2] >= mon_button[k]["x1"] and params[2] <= mon_button[k]["x2"] and params[3] == mon_button[k]["y"] then
- number, found = k, true
- log_button("Button found: "..tostring(k))
- end
- end
- end
- if found == false then log_button("Failed to find button", true) end
- return number
- end
- button_switch = function(...)
- local params = {...}
- local text, num = nil, params[2]
- if params[1] then
- text, status[num] = "deactivate", false
- else
- if status[10] then
- text, status[num] = "activate", true
- end
- end
- log_button("Button switch to: "..tostring(text).." "..tostring(status[num]).." "..tostring(ids[num]))
- if tostring(text) ~= "nil" then
- rednet.send(ids[num], text)
- relay_send(CONFIG.RELAY)
- end
- end
- button_online = function(...)
- local params = {...}
- status[10] = not status[10]
- log_button("Button Online changing to: "..tostring(status[10]))
- if status[10] ~= true then
- for k,v in ipairs(status) do
- if k >= 11 then button_switch(true, k) end
- end
- end
- end
- button_barselect = function(...)
- local params = {...}
- for num=1,9 do
- status[num] = false
- end
- if params[1] ~= nil then
- status[params[1]] = true
- if params[1] == 1 or params[1] == 4 then
- status[2] = true
- end
- end
- log_button("Button Bar Select changing to: "..tostring(names[num]))
- end
- -- CONTROL --
- control_addbutton = function()
- term.clear() log_button("Adding Button...")
- screen_bar(term)
- local answer = {}
- while type(answer[1]) ~= "string" do
- screen_text(term, "enter name: ", 2, 4)
- answer[1] = string.upper(tostring(read())) or nil
- end
- while type(answer[2]) ~= "number" do
- screen_text(term, "enter client id: ", 2, 5)
- answer[2] = tonumber(read()) or nil
- end
- button_add(answer[1], answer[2], false)
- while answer[3] ~= "TRUE" and answer[3] ~= "FALSE" do
- screen_text(term, "adding button: "..answer[1].." "..answer[2], 2, 7)
- screen_text(term, "confirm to save?: <true/false>", 2, 8)
- term.setCursorPos(2, 9)
- answer[3] = string.upper(tostring(read())) or nil
- end
- log_button("Adding Button: "..tostring(answer[1]).." "..tostring(answer[2]).." "..tostring(answer[3]))
- button_barselect()
- if answer[3] == "TRUE" then save_all() end
- os.reboot()
- end
- control_removebutton = function
- term.clear() log_button("Removing Button...")
- screen_bar(term)
- local answer = {}
- while type(answer[1]) ~= "string" do
- screen_text(term, "enter name: ", 2, 4)
- answer[1] = string.upper(tostring(read())) or nil
- end
- for k,v in ipairs(names) do
- if v == answer[1] then answer[2] = k end
- end
- if answer[2] ~= nil then
- answer[4], answer[5] = names[answer[2]], ids[answer[2]]
- names[answer[2]], status[answer[2]], ids[answer[2]] = names[table.maxn(names)], status[table.maxn(status)], ids[table.maxn(ids)]
- names[table.maxn(names)], status[table.maxn(status)], ids[table.maxn(ids)] = nil, nil, nil
- end
- while answer[3] ~= "TRUE" and answer[3] ~= "FALSE" do
- screen_text(term, "removing button: "..answer[4].." "..answer[5], 2, 6)
- screen_text(term, "confirm to save?: <true/false>", 2, 7)
- term.setCursorPos(2, 8)
- answer[3] = string.upper(tostring(read())) or nil
- end
- log_button("Removing Button: "..tostring(answer[1]).." "..tostring(answer[2]).." "..tostring(answer[3]))
- button_barselect()
- if answer[3] == "TRUE" then save_all() end
- os.reboot()
- end
- control_addrelay = function()
- term.clear() log_button("Adding Relay...")
- screen_bar(term)
- local answer = {}
- while type(answer[1]) ~= "number" do
- screen_text(term, "if not main server > only enter in main server id...", 2, 4)
- screen_text(term, "enter relay id: ", 2, 5)
- answer[1] = tonumber(read()) or nil
- end
- relays[table.maxn(relays) + 1] = answer[1]
- while answer[2] ~= "TRUE" and answer[2] ~= "FALSE" do
- screen_text(term, "add relay: "..answer[1], 2, 6)
- screen_text(term, "confirm to save?: <true/false>", 2, 7)
- term.setCursorPos(2, 8)
- answer[2] = string.upper(tostring(read())) or nil
- end
- log_button("Adding Relay: "..tostring(answer[1]).." "..tostring(answer[2]))
- button_barselect()
- if answer[2] == "TRUE" then save_all() end
- os.reboot()
- end
- control_removerelay = function()
- term.clear() log_button("Removing Relay...")
- screen_bar(term)
- local answer = {}
- while type(answer[1]) ~= "number" do
- screen_text(term, "enter relay id: ", 2, 4)
- answer[1] = tonumber(read()) or nil
- end
- for k,v in ipairs(names) do
- if v == answer[1] then answer[2] = k end
- end
- if answer[2] ~= nil then
- answer[4] = relays[answer[2]]
- relays[answer[2]] = relays[table.maxn(ids)]
- relays[table.maxn(ids)] = nil
- end
- while answer[3] ~= "TRUE" and answer[3] ~= "FALSE" do
- screen_text(term, "removing relay: "..answer[2], 2, 6)
- screen_text(term, "confirm to save?: <true/false>", 2, 7)
- term.setCursorPos(2, 8)
- answer[3] = string.upper(tostring(read())) or nil
- end
- log_button("Removing Relay: "..tostring(answer[1]).." "..tostring(answer[3]))
- button_barselect()
- if answer[3] == "TRUE" then save_all() end
- os.reboot()
- end
- control_update = function()
- log_add("updating")
- shell.run("pastebin get "..pastebinCode.." update")
- if fs.exists("update") then
- fs.delete(shell.getRunningProgram())
- fs.copy("update", shell.getRunningProgram())
- fs.delete("update")
- log_add("update success")
- else
- log_add("update failed")
- end
- sleep(2)
- os.reboot()
- end
- end
- -- SCREEN SAVER: TO BE IMPLEMENTED--
- -- PROGRAM --
- peripherals_find()
- peripherals_sort()
- button_barselect(2)
- screen_drawall()
- if CONFIG.RELAY then relays[1] = os.getComputerID() end
- local event, id, x, y = nil, nil, nil, nil
- while true do
- if event == "monitor_touch" or event == "mouse_click" then
- local button_num = button_detect(event, x, y)
- if button_num >= 1 and button_num <= 9 then
- button_barselect(button_num)
- if status[1] then if current_page ~= 1 then current_page = current_page - 1 end button_barselect(2) end
- if status[2] then current_screen = "main" end
- if status[3] then current_screen = "control" end
- if status[4] then current_page = current_page + 1 button_barselect(2) end
- if status[5] then control_addbutton() end
- if status[6] then control_removebutton() end
- if status[7] then control_addrelay() end
- if status[8] then control_removerelay() end
- if status[9] then control_update() end
- peripherals_find() peripherals_sort() screen_drawall()
- elseif button_num == 10 then
- button_online()
- peripherals_find() peripherals_sort() screen_drawall()
- elseif button_num >= 11 then
- button_switch(status[button_num], button_num)
- peripherals_find() peripherals_sort() screen_drawall()
- end
- button_num = nil
- -- elseif event == "rednet_message" then
- -- relay_receive(x)
- -- if CONFIG.RELAY then relay_send(CONFIG.RELAY) end
- -- peripherals_find() peripherals_sort() screen_drawall()
- -- elseif event == "timer" then
- -- peripherals_find() peripherals_sort() screen_drawall()
- end
- event, id, x, y = nil, nil, nil, nil
- save_all()
- -- os.startTimer(tonumber(CONFIG.SCREEN_REFRESH_RATE))
- event, id, x, y = os.pullEvent()
- log_button("Event Detected: "..tostring(event).." "..tostring(id).." "..tostring(x).." "..tostring(y))
- end
Advertisement
Add Comment
Please, Sign In to add comment