Advertisement
Stiepen

printclient api

Sep 11th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. local printerid
  2. local cur_docid = 1
  3. local function packet(iType, id, cmd, data)
  4.   if type(data) == "table" then
  5.     data = textutils.serialize(data)
  6.   end
  7.   rednet.send(printerid, textutils.serialize({dataroot = {["type"] = iType, packetid = id, cmd = cmd, data = data}}))
  8. end
  9.  
  10. local function waitForRespose()
  11.   while true do
  12.     id, msg, disance = rednet.read(5)
  13.     if id == nil then return false, "Connection timed out" end
  14.     if id == printerid then
  15.       p = textutils.unserialize(msg)
  16.       if p.dataroot == nil then
  17.         return false, "Unknown Packet received. Printserver may be bugged"
  18.       end
  19.       p = p.dataroot
  20.       if p['type'] == 5 then
  21.         return false, p.data
  22.       elseif p['type'] == 6 then
  23.         return true, p.data
  24.       else
  25.         return false, "Unknown Packet received. Printserver may be bugged"
  26.       end
  27.     end
  28.   end
  29. end
  30.  
  31. function getCursorPos()
  32.   -- Not avaible yet
  33.   return nil
  34. end
  35.  
  36. function getPaperLevel()
  37.   -- Not avaible yet
  38.   return nil
  39. end
  40.  
  41. function setCursorPos(docid, x, y)
  42.   packet(3, docid, "setCursorPos", {x, y})
  43.   return waitForResponse()
  44. end
  45.  
  46. function newPage(docid)
  47.   packet(3, docid, "newPage", {})
  48.   return waitForResponse()
  49. end
  50.  
  51. function write(docid, text)
  52.   packet(3, docid, "write", {text})
  53.   return waitForResponse()
  54. end
  55.  
  56. function setPageTitle (docid, title)
  57.   packet(3, docid, "setPageTitle", {title})
  58.   return waitForResponse()
  59. end
  60.  
  61. function endPage (docid)
  62.   packet(3, docid, "endPage", {})
  63.   return waitForResponse()
  64. end
  65.  
  66. function startPrinting(printerid)
  67.   local docid = curdocid..os.time()..os.computerID()
  68.   curdocid = curdocid + 1
  69.   packet(1, docid, "", "")
  70.   s, m = waitForResponse()
  71.   if s then
  72.     return true, m, docid
  73.   else
  74.     return false, m
  75.   end
  76. end
  77.  
  78. function endPrinting(docid)
  79.   packet(2, docid, "", "")
  80.   return waitForResponse()
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement