Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cm = require("component")
- local sr = require("serialization")
- local os = require("os")
- local ss = require("term")
- local buffer = require("doubleBuffering")
- local gui = require("GUI")
- local event = require("event")
- local con = cm.modem
- local gpu = cm.gpu
- local bg = 0xE4E3E3
- local header = 0x135D96
- local fontcolor = 0x3E5567
- local active = 0x0B44A4
- local success = 0x3AF230
- local failure = 0xBC0000
- local information = {
- ["UA"] = "Не выбран!",
- ["FILE"] = "Не выбран!",
- ["OWN"] = "Не обозначен!"
- }
- buffer.setResolution(100, 30)
- local mainContainer = gui.fullScreenContainer()
- local function exitProcedure()
- if information["UA"] ~= "Не выбран!" then
- con.send(information["UA"], 8080, "abort")
- end
- ss.clear()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.setResolution(160, 50)
- ss.clear()
- os.exit()
- end
- local function keyexit(eventID, ...)
- if code == 17 then exitProcedure() end
- end
- local function exitButton(func)
- mainContainer:addChild(gui.roundedButton(2, 1, 10, 3, bg, fontcolor, active, bg, "Выйти")).onTouch = func
- end
- local function setClearScreen(logotext)
- buffer.clear()
- mainContainer:deleteChildren()
- buffer.square(0, 0, 160, 50, bg, fontcolor, " ", 0.0)
- buffer.square(0, 0, 160, 5, header, fontcolor, " ", 0.0)
- buffer.text(43, 2, fontcolor, logotext, 0.0)
- end
- local function menu()
- local function backButton()
- setClearScreen("Open FTP")
- exitButton(exitProcedure)
- menu()
- end
- --Выбор адреса
- mainContainer:addChild(gui.roundedButton(71, 6, 15, 5, header, bg, active, fontcolor, "Выбор адреса")).onTouch = function()
- setClearScreen("Выбор Адреса")
- exitButton(backButton)
- con.open(8080)
- if con.isOpen(8080) then
- mainContainer:addChild(gui.label(41, 10, 20, 2, success, "Порт 8080 - Открыт"))
- mainContainer:addChild(gui.roundedButton(41, 20, 20, 3, header, bg, active, fontcolor, "Broadcast")).onTouch = function()
- setClearScreen("Отправка")
- exitButton(backButton)
- con.broadcast(8080, true)
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message == "acproc" then
- mainContainer:addChild(gui.label(31, 14, 20, 2, header, "Адрес цели - " .. from))
- mainContainer:addChild(gui.label(36, 15, 20, 2, header, "Дистанция до цели - " .. math.round(distance) .. " метров"))
- mainContainer:addChild(gui.roundedButton(41, 10, 20, 3, header, bg, active, fontcolor, "Принять адресата")).onTouch = function()
- con.send(from, port, "acc")
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message == "aprocced" then
- information["UA"] = from
- information["OWN"] = own
- gui.error("Ожидание готовности собеседника . . .")
- con.send(from, port, "AOK")
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message == "ready" then
- backButton()
- end
- end
- end
- else
- backButton()
- gui.error("Непредвиденная ошибка. Попробуйте снова")
- end
- end
- if information["UA"] ~= "Не выбран!" then
- backButton()
- gui.error("Уже выбрана цель! Если Вы хотите выбрать дургую цель, перезапустите программу.")
- end
- else
- mainContainer:addChild(gui.label(41, 10, 20, 2, failure, "Порт 8080 - Закрыт"))
- mainContainer:addChild(gui.label(37, 12, 20, 2, failure, "Перезапустите устройство и попробуйте снова"))
- end
- end
- --Выбор файла
- mainContainer:addChild(gui.roundedButton(71, 11, 15, 5, header, bg, active, fontcolor, "Выбор файла")).onTouch = function()
- setClearScreen("Выбор Файла")
- exitButton(backButton)
- mainContainer:addChild(gui.label(41, 6, 20, 1, header, "Выберете файл для отправки"))
- local filesystemChooser = mainContainer:addChild(gui.filesystemChooser(41, 15, 20, 3, 0xE1E1E1, 0x888888, 0x3C3C3C, 0x888888, nil, "Подтвердить", "Отменить", "Выбрать", "/"))
- filesystemChooser:setMode(gui.filesystemModes.open, gui.filesystemModes.file)
- filesystemChooser.onSubmit = function(path)
- information["FILE"] = path
- con.send(information["UA"], 8080, "selected")
- backButton()
- gui.error("Вы выбрали файл. Теперь нажмите кнопку 'Отправка' для того что бы начать отправку файла.")
- end
- if information["FILE"] ~= "Не выбран!" then
- gui.error("Вы уже выбрали файл! Если Вы хотите отправить что-то другое, начните все заного!")
- backButton()
- end
- end
- --Отправка файла
- mainContainer:addChild(gui.roundedButton(71, 16, 15, 5, header, bg, active, fontcolor, "Отправка")).onTouch = function()
- setClearScreen("Отправка")
- exitButton(backButton)
- if information["UA"] ~= "Не выбран!" and information["OWN"] ~= "Не обозначен!" and information["FILE"] ~= "Не выбран!" then
- mainContainer:addChild(gui.roundedButton(41, 10, 15, 5, header, bg, active, fontcolor, "Отправить!")).onTouch = function()
- local file = io.open(information["FILE"])
- local lines = {}
- for line in io.lines(information["FILE"]) do table.insert(lines, line) end
- con.send(information["UA"], 8080, false)
- con.send(information["UA"], 8080, sr.serialize(lines))
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message == "success" then
- os.sleep(5)
- exitProcedure()
- end
- end
- else
- backButton()
- gui.error("Не выбран файл/адресат! Попробуйте снова когда заполните данную информацию.")
- end
- end
- --Прием файла
- mainContainer:addChild(gui.roundedButton(71, 21, 15, 5, header, bg, active, fontcolor, "Приём")).onTouch = function()
- setClearScreen("Приём")
- exitButton(backButton)
- con.open(8080)
- if con.isOpen(8080) then
- mainContainer:addChild(gui.label(41, 10, 20, 2, success, "Порт 8080 - Открыт"))
- mainContainer:addChild(gui.roundedButton(37, 12, 20, 3, header, bg, active, fontcolor, "Начать прослушку порта")).onTouch = function()
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message then
- information["OWN"] = own
- con.send(from, port, "acproc")
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message == "acc" then
- setClearScreen("Получение данных от " .. from)
- exitButton(backButton)
- con.send(from, port, "aprocced")
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message == "AOK" then
- mainContainer:addChild(gui.label(41, 16, 20, 2, header, "Выберите путь сохранения получаемых файлов"))
- local filesystemChooser = mainContainer:addChild(gui.filesystemChooser(37, 17, 10, 3, 0xE1E1E1, 0x888888, 0x3C3C3C, 0x888888, "downloaded.lua", "Подтвердить", "Отменить", "Выбрать", "/"))
- filesystemChooser:setMode(gui.filesystemModes.save, gui.filesystemModes.directory)
- filesystemChooser.onSubmit = function(path)
- mainContainer:deleteChildren()
- buffer.clear()
- mainContainer:stopEventHandling()
- mainContainer:drawOnScreen(false)
- buffer.draw(false)
- ss.clear()
- gpu.setBackground(bg)
- gpu.setForeground(header)
- con.send(from, 8080, "ready")
- print("Переход в минималистичный режим . . . Выполнен")
- while true do
- local size = 0
- local current = 0
- local lines = {}
- local _, own, from, port, distance, message = event.pull("modem_message")
- if message == "selected" then
- gpu.setBackground(bg)
- gpu.setForeground(header)
- print("Отправитель выбрал файл . . .")
- end
- if message == false then
- local _, own, from, port, distance, message = event.pull("modem_message")
- local lines = sr.unserialize(message)
- file = io.open(path, "w")
- for i = 1, #lines do file:write(lines[i] .. "\n") end
- file:close()
- gpu.setForeground(success)
- print("Файл получен и сохранен по пути - " .. path)
- con.send(from, 8080, "success")
- os.sleep(5)
- exitProcedure()
- break
- end
- end
- end
- end
- else
- backButton()
- gui.error("Непредвиденная ошибка. Попробуйте снова")
- end
- end
- end
- if information["UA"] ~= "Не выбран!" then
- backButton()
- gui.error("Вы являетесь отправителем! Если Вы хотите принимать файлы - перезапустите программу.")
- end
- else
- mainContainer:addChild(gui.label(41, 10, 20, 2, failure, "Порт 8080 - Закрыт"))
- mainContainer:addChild(gui.label(37, 12, 20, 2, failure, "Перезапустите устройство и попробуйте снова"))
- end
- end
- --Все что было введено пользователем
- mainContainer:addChild(gui.label(2, 6, 20, 2, fontcolor, "Адрес цели: " .. information["UA"]))
- mainContainer:addChild(gui.label(2, 8, 20, 2, fontcolor, "Файл для отправки: " .. information["FILE"]))
- mainContainer:addChild(gui.label(2, 10, 20, 2, fontcolor, "Ваш адрес: " .. information["OWN"]))
- end
- event.listen("key_up", keyexit)
- setClearScreen("Open FTP")
- exitButton(exitProcedure)
- menu()
- buffer.draw(true)
- mainContainer:drawOnScreen(true)
- mainContainer:startEventHandling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement