Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local port = 42
- local myName = 'Bob'
- local lofc = 'lc.txt' --list of connections
- local timer = (math.random(1, 7)/7)
- if #tArgs == 2 then
- port = tonumber(tArgs[1])
- myName = tostring(tArgs[2])
- elseif #tArgs > 2 then
- print('usage: cm <port> <MyName>')
- os.sleep(3)
- os.exit()
- end
- local component = require('component')
- local event = require('event')
- local term = require('term')
- local ser = require('serialization')
- local modem = component.modem
- local tList = {} --{'name', 'address', 'port', 'distance'}
- local tList1 = {}
- --сохраниение таблицы в файл
- local function saveList(tbl, fl)
- file = io.open(fl, 'w')
- file:write(ser.serialize(tbl))
- file:close()
- end
- --загрузка таблицы из файла
- local function loadList(fl)
- file = io.open(fl, 'r')
- if not file then
- file = io.open(fl, 'a')
- else
- return ser.unserialize(file:read('*a'))
- end
- file:close()
- end
- --сообщение своего id и поиск устройств
- local function beacon(addTime)
- while addTime > 0 do
- modem.broadcast(port, myName) --сообщаеи свое имя
- os.sleep(timer)
- modem.open(port)
- e, r, s, p, d, n = event.pull(timer, 'modem_message')
- if p == port then --если получили сигнал с нужного порта
- c = 1
- for i = 1, #tList do --проходимся по таблице адресов
- if s == tList[i][2] then --если нашли адрес из списка
- tList[i] = {n, s, p, math.floor(d)} --обновляем его данные
- else
- c = c+1
- end
- end
- if c > #tList then --если адреса нет в списке
- tList[#tList+1] = {n, s, p, math.floor(d)} --добавляем новй адрес
- end
- end
- modem.close(port)
- os.sleep(0.5)
- addTime = addTime-1
- end
- end
- if loadList(lofc) ~= nil then
- tList1 = loadList(lofc)
- tList = tList1
- end
- while true do
- term.clear()
- local y = 9
- term.setCursor(1, y) term.write('[№]')
- term.setCursor(5, y) term.write('[Name]')
- term.setCursor(15, y) term.write('[Address]')
- term.setCursor(25, y) term.write('[Port]')
- term.setCursor(32, y) term.write('[Distance]')
- for z = 1, #tList do
- term.setCursor(1, y+1+z) term.write('['..z..']') --номер
- term.setCursor(5, y+1+z) term.write('['..tList[z][1]..']') --имя
- term.setCursor(15, y+1+z) term.write('['..string.sub(tList[z][2], 1, 8)..']') --адрес
- term.setCursor(25, y+1+z) term.write('['..tList[z][3]..']') --порт
- term.setCursor(32, y+1+z) term.write('['..tList[z][4]..']') --расстояние
- end
- term.setCursor(1, 1)
- print('Введите [-r n], для поиска новых устройсв, где - n время ожидания в секундах')
- print('Введите [-s n], для сохранения адреса, где n - номер в списке')
- print('Введите [-d n], для для удаления адреса')
- print('Введите [-q], для выхода из программы')
- local arg = io.read()
- arg_o = string.sub(arg, 1, 2)
- if arg_o == '-s' then
- tList1[#tList1+1] = tList[tonumber(string.sub(arg, 4, string.len(arg)))] --копируем адрес из tList в tList1
- saveList(tList1, lofc) --сохраняем все адреса
- elseif arg_o == '-d' then
- table.remove(tList1, tonumber(string.sub(arg, 4, string.len(arg)))) --удляем адрес из tList1
- saveList(tList1, lofc) --сохраняем все адреса
- elseif arg_o == '-r' then
- t = tonumber(string.sub(arg, 4, string.len(arg)))
- if t ~= nil then
- beacon(t)
- else
- beacon(5)
- end
- elseif arg_o == '-q' then
- term.clear()
- os.exit()
- else
- print('ЕГГОГ!')
- os.sleep(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement