Advertisement
osmarks

Modem Logger

Dec 14th, 2019
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local m = peripheral.find "modem"
  2. local o = peripheral.find "monitor"
  3.  
  4. o.setTextScale(0.5)
  5.  
  6. local w, h = o.getSize()
  7. local command_window = window.create(o, 1, h, w, 1)
  8. local outputs_window = window.create(o, 1, 1, w, h - 1)
  9.  
  10. local function exec_in_window(w, f)
  11.     local x, y = o.getCursorPos()
  12.     local last = term.redirect(w)
  13.     f()
  14.     term.redirect(last)
  15.     w.redraw()
  16.     o.setCursorPos(x, y)
  17. end
  18.  
  19. local function print_output(txt, color)
  20.     exec_in_window(outputs_window, function()
  21.         term.setTextColor(color or colors.white)
  22.         print(txt)
  23.     end)
  24. end
  25.  
  26. local function splitspace(str)
  27.     local tokens = {}
  28.     for token in string.gmatch(str, "[^%s]+") do
  29.         table.insert(tokens, token)
  30.     end
  31.     return tokens
  32. end
  33.  
  34. local function controls()
  35.     term.redirect(command_window)
  36.     term.setBackgroundColor(colors.white)
  37.     term.setTextColor(colors.black)
  38.     term.clear()
  39.     local hist = {}
  40.     while true do
  41.         write "> "
  42.         local msg = read(nil, hist)
  43.         table.insert(hist, msg)
  44.         local tokens = splitspace(msg)
  45.         local command = table.remove(tokens, 1)
  46.         if command == "open" then
  47.             local chan = tonumber(tokens[1])
  48.             m.open(chan)
  49.             print_output(("Opened %d"):format(chan), colors.gray)
  50.         elseif command == "close" then
  51.             local chan = tonumber(tokens[1])
  52.             m.close(chan)
  53.             print_output(("Closed %d"):format(chan), colors.gray)
  54.         else
  55.             print_output("Command invalid", colors.gray)
  56.         end
  57.     end
  58. end
  59.  
  60. local function compact_serialize(x)
  61.     local t = type(x)
  62.     if t == "number" then
  63.         return tostring(x)
  64.     elseif t == "string" then
  65.         return textutils.serialise(x)
  66.     elseif t == "table" then
  67.         local out = "{ "
  68.         for k, v in pairs(x) do
  69.             out = out .. string.format("[%s]=%s, ", compact_serialize(k), compact_serialize(v))
  70.         end
  71.         return out .. "}"
  72.     elseif t == "boolean" then
  73.         return tostring(x)
  74.     else
  75.         error("Unsupported type " .. t)
  76.     end
  77. end
  78.  
  79. local function safe_serialize(m)
  80.     local ok, res = pcall(compact_serialize, m)
  81.     if ok then return res
  82.     else return ("[UNSERIALIZABLE %s: %s]"):format(tostring(m), res) end
  83. end
  84.  
  85. local function tostring_with_default(x)
  86.     if not x then return "[UNKNOWN]"
  87.     else return tostring(x) end
  88. end
  89.  
  90. local function receive()
  91.     while true do
  92.         local _, _, channel, reply_channel, message, distance = os.pullEvent "modem_message"
  93.         print_output(("%d \16 %d | %s"):format(channel, reply_channel, tostring_with_default(distance)))
  94.         print_output(safe_serialize(message), colors.lightGray)
  95.     end
  96. end
  97.  
  98. parallel.waitForAny(controls, receive)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement