osmarks

Printron Server

Aug 14th, 2018
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. local m = peripheral.find "modem"
  2. local o = peripheral.find "monitor"
  3. local p = peripheral.find "printer"
  4.  
  5. local log_channel, job_channel = 404, 1337
  6.  
  7. o.setCursorPos(1, 1)
  8. o.clear()
  9. o.setTextScale(0.5)
  10.  
  11. m.open(job_channel)
  12.  
  13. local function log(msg)
  14.     m.transmit(log_channel, job_channel, msg)
  15.     local last_term = term.redirect(o)
  16.     if type(msg) == "string" then print(msg) end
  17.     term.redirect(last_term)
  18. end
  19.  
  20. local function make_printer_terminal(printer)
  21.     local function stub() end
  22.  
  23.     local title = ""
  24.  
  25.     local printerTerminal = {
  26.         getCursorPos = printer.getCursorPos,
  27.         getSize = printer.getPageSize,
  28.         write = printer.write,
  29.         isColor = function() return false end,
  30.         isColour = function() return false end,
  31.         blit = stub,
  32.         clear = stub,
  33.         clearLine = stub,
  34.         setCursorBlink = stub,
  35.         setTextColor = stub,
  36.         setBackgroundColor = stub,
  37.         getBackgroundColor = function() return colors.white end,
  38.         getTextColor = function() return colors.black end
  39.     }
  40.    
  41.     function printerTerminal.setCursorPos(x, y)
  42.             local w, h = printer.getPageSize()
  43.             if y > (h + 1) then printerTerminal.scroll() y = y - h end
  44.             printer.setCursorPos(x, y)
  45.     end
  46.  
  47.     function printerTerminal.makePage()
  48.         while not printer.newPage() do
  49.             if printer.getPaperLevel() < 1 then
  50.                 log "Add more paper."
  51.             end
  52.             if printer.getInkLevel() < 1 then
  53.                 log "Add more ink."
  54.             end
  55.  
  56.             log "Hit any key on print server when done."
  57.             os.pullEvent "key"
  58.         end
  59.         printer.setPageTitle(title)
  60.     end
  61.  
  62.     function printerTerminal.finishPage()
  63.         log "Clear output tray."
  64.  
  65.         while not printer.endPage() do
  66.             sleep(1)
  67.         end
  68.     end
  69.  
  70.     function printerTerminal.scroll()
  71.         printerTerminal.finishPage()
  72.         printerTerminal.makePage()
  73.     end
  74.  
  75.     function printerTerminal.setPageTitle(t)
  76.         title = t
  77.     end
  78.  
  79.     return printerTerminal
  80. end
  81.  
  82. local queue = {}
  83.  
  84. local function do_printy_stuff()
  85.     local pterm = make_printer_terminal(p)
  86.     while true do
  87.         local job
  88.         repeat
  89.             job = table.remove(queue, 1)
  90.             sleep(1)
  91.         until job ~= nil
  92.  
  93.         job.title = job.title or "Untitled"
  94.  
  95.         term.redirect(pterm)
  96.         log { type = "job_running" }
  97.         pterm.makePage()
  98.         log(string.format("Printing %d chars for job %s.", #job.text, job.title))
  99.         pterm.setPageTitle(job.title)
  100.         print(job.text)
  101.         pterm.finishPage()
  102.         log "Printing complete."
  103.         log { type = "job_complete" }
  104.     end
  105. end
  106.  
  107. local function respond_to_commands()
  108.     while true do
  109.         local _, _, chan, reply_chan, message = os.pullEvent "modem_message"
  110.  
  111.         if chan == job_channel and reply_chan == log_channel and type(message) == "table" and message.text and message.title then
  112.             log { type = "job_starting" }
  113.             table.insert(queue, message)
  114.         else
  115.             print "Ignoring invalid message."
  116.         end
  117.     end
  118. end
  119.  
  120. local function handle_terminate()
  121.     os.pullEventRaw "terminate"
  122.     log "Print job manually terminated."
  123.     log { type = "job_complete" }
  124. end
  125.  
  126. parallel.waitForAny(respond_to_commands, do_printy_stuff, handle_terminate)
Add Comment
Please, Sign In to add comment