Advertisement
D_Puppy

OC print server

Jun 6th, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.77 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local ser = require("serialization")
  4. local network = require ("network")
  5. local fs = require("filesystem")
  6. local op = component.openprinter
  7. local extra = require("extralib")
  8.  
  9. local VERSION = "1.0"
  10.  
  11. local CONFIGFILE = "/etc/printserver.cfg"
  12. local SERVERNAME = "pserver1"
  13. local LOGFOLDER = "/var/log/"
  14. local LOGFILE = "printserver.log"
  15. local PORT = 9100
  16. local WARNPAPER = 10
  17. local WARNCOLOR = 5
  18. local WARNBLACK = 5
  19. local TIMERCOUNT = 0.1
  20.  
  21. local activeJob = 1
  22. local queuedJobs = 0
  23. local queueTable = {}
  24.  
  25. local timerEvent = nil
  26.  
  27. function ensureLogFolderExists()
  28.     if not fs.exists(LOGFOLDER) then
  29.         fs.makeDirectory(LOGFOLDER)
  30.     end
  31. end
  32.  
  33. function logwrite(str)
  34.   local file = io.open(LOGFOLDER .. "/" .. LOGFILE,"a")
  35.   file:write(str)
  36.   file:close()
  37. end
  38.  
  39. function checkPrinter()
  40.     if op.getPaperLevel() then
  41.       if op.getPaperLevel() < WARNPAPER then
  42.     logwrite(os.date() .. " Running out of paper\n")
  43.     return false
  44.       end
  45.     else
  46.     logwrite(os.date() .. " No paper.\n")
  47.     return false
  48.     end
  49.     if op.getColorInkLevel() then
  50.       if op.getColorInkLevel() < WARNCOLOR then
  51.     logwrite(os.date() .. " Less then 5% color ink left\n")
  52.     return false
  53.       end
  54.     else
  55.       logwrite(os.date() .. " Out off color Ink.\n")
  56.       return false
  57.     end
  58.     if op.getBlackInkLevel() then
  59.       if op.getBlackInkLevel() < WARNBLACK then
  60.     logwrite(os.date() .. " Less then 5% black ink left\n")
  61.     return false
  62.       end
  63.     else
  64.       logwrite(os.date() .. " Out off black Ink.\n")
  65.       return false
  66.     end
  67.     return true
  68. end
  69.  
  70. function timerCallback()
  71.   if checkPrinter() == true then
  72.     if activeJob <= queuedJobs then     -- only if there are files waiting
  73.     --print("Job ID : " .. queueTable[activeJob].id)
  74.     --print("From   : " .. queueTable[activeJob].client)
  75.     --print("Status : " .. queueTable[activeJob].status)
  76.     --print("Line   : " .. queueTable[activeJob].pageLine .. " of " .. queueTable[activeJob].lines)
  77.     --print("........................................................")
  78.       if queueTable[activeJob].status == "queued" then
  79.     queueTable[activeJob].status = "printing"           -- set the status of the job to printing
  80.     logwrite(os.date() .. " Starting print of job " .. activeJob .. "\n")
  81.       end
  82.       if queueTable[activeJob].pageLine ==19 then           -- Page is full
  83.     op.setTitle(queueTable[activeJob].client)
  84.     op.writeln("<" .. queueTable[activeJob].page .. ">")
  85.     queueTable[activeJob].page = queueTable[activeJob].page + 1 -- page counter for job one up
  86.     queueTable[activeJob].pageLine = 1
  87.     op.print()
  88.       else
  89.     op.writeln(queueTable[activeJob].data[queueTable[activeJob].activeLine])
  90.     queueTable[activeJob].activeLine = queueTable[activeJob].activeLine + 1
  91.     queueTable[activeJob].pageLine = queueTable[activeJob].pageLine + 1
  92.       end
  93.       if queueTable[activeJob].activeLine > queueTable[activeJob].lines then
  94.     queueTable[activeJob].status = "finished"
  95.     local i = queueTable[activeJob].pageLine
  96.     for j=i, 19 do
  97.       op.writeln("")
  98.     end
  99.     op.setTitle(queueTable[activeJob].client)
  100.     op.writeln("<" .. queueTable[activeJob].page .. ">")
  101.     op.print()
  102.     logwrite(os.date() .. " Finshed print of job " .. activeJob .. "\n")
  103.     activeJob = activeJob + 1
  104.       end
  105.     end
  106.   end
  107.   timerEvent = event.timer(TIMERCOUNT, timerCallback)
  108. end
  109.  
  110. local function response(_, type, _a, _b, _c, _d)
  111.   local channel = 0
  112.   local remoteAddr = ""
  113.   local port = 0
  114.   if type == "connection" then
  115.     channel = _a
  116.     remoteAddr = _b
  117.     port =_c
  118.     logwrite(os.date() .. " Connection from " .. remoteAddr .. " ch " .. channel .. " port " .. port .. "\n")
  119.   elseif type == "message" then
  120.     channel = _a
  121.     message = _b
  122.     remoteAddr = _c
  123.     port =_d
  124.     local tmpTable = {}
  125.     local msgTable = ser.unserialize(message)
  126.     logwrite(os.date() .. " " .. remoteAddr .. " send message '" .. msgTable.command .. "' ch " .. channel .." port " .. port .. "\n")
  127.     if msgTable.command == "info" then
  128.       local msgTable ={}
  129.       msgTable["command"] = "info"
  130.       msgTable["version"] = VERSION
  131.       msgTable["jobs"] = queuedJobs
  132.       --print(ser.serialize(msgTable))
  133.       network.tcp.send(channel, ser.serialize(msgTable))
  134.      
  135.     elseif msgTable.command == "print" then
  136.       queuedJobs = queuedJobs + 1
  137.       logwrite(os.date() .. " " .. remoteAddr .. " queueing job " ..queuedJobs .. " filename : " .. msgTable.filename .. "\n")
  138.       for key,value in pairs(ser.unserialize(msgTable.data)) do
  139.     done = false
  140.     while done == false do
  141.       if value:len() > 30 then
  142.         table.insert(tmpTable,value:sub(1,30))
  143.         value=">"..value:sub(31)
  144.       else
  145.         table.insert(tmpTable,value)
  146.         done = true
  147.       end
  148.     end
  149.       end
  150.       local qTable = {}
  151.      
  152.       qTable["filename"] = msgTable.filename
  153.       qTable["lines"] = #tmpTable
  154.       qTable["activeLine"] = 1
  155.       qTable["data"] = tmpTable
  156.       qTable["pageLine"] = 1
  157.       qTable["status"] = "queued"       -- will be queued, printing or finished
  158.       qTable["client"] = remoteAddr
  159.       qTable["id"] = queuedJobs
  160.       qTable["page"] = 1
  161.      
  162.       queueTable[queuedJobs] = qTable
  163.     end
  164.    
  165.   elseif type == "close" then
  166.     channel = _a
  167.     logwrite(os.date() .. " Connection on channel " .. channel .. " closed\n")
  168.   end
  169. end
  170.  
  171. function loadConfig()
  172.   local option = {}
  173.   local configFile = io.open(CONFIGFILE, "r")
  174.   while true do
  175.     line = configFile:read()
  176.     if line == nil then
  177.       break
  178.     else
  179.       if line[1] ~= "#" then
  180.     option = extra.splitString(line,"=")    -- extralib sring split
  181.     if option[1] == "LOGFILE" then
  182.       LOGFILE = option[2]
  183.     elseif option[1] == "LOGFOLDER" then
  184.       LOGFOLDER = option[2]
  185.     elseif option[1] == "SERVERNAME" then
  186.       SERVERNAME = option[2]
  187.     elseif option[1] == "PORT" then
  188.       PORT = tonumber(option[2])
  189.     elseif option[1] == "WARNPAPER" then
  190.       WARNPAPER = tonumber(option[2])
  191.     elseif option[1] == "WARNCOLOR" then
  192.       WARNCOLOR = tonumber(option[2])
  193.     elseif option[1] == "WARNBLACK" then
  194.       WARNBLACK = tonumber(option[2])
  195.     elseif option[1] == "TIMERCOUNT" then
  196.       TIMERCOUNT = tonumber(option[2])
  197.     end
  198.       end
  199.     end
  200. end
  201.  
  202.   configFile:close()
  203. end
  204.  
  205.  
  206. function start()
  207.   loadConfig()
  208.   ensureLogFolderExists()
  209.   logwrite(os.date() .. " Starting print server (v" .. VERSION .. ") " .. SERVERNAME .. " at port " .. PORT .. "\n")
  210.   if op == nil then
  211.     logwrite(os.date() .. " No openprinter found!\n")
  212.     logwrite(os.date() .. " Aborting!")
  213.     return false
  214.   end
  215.   network.ip.bind(SERVERNAME)
  216.   network.tcp.listen(PORT)
  217.   event.listen("tcp", response)
  218.   checkPrinter()
  219.   op.clear()
  220.   timerEvent = event.timer(TIMERCOUNT, timerCallback)
  221. end
  222.  
  223. function stop()
  224.   logwrite(os.date() .. " Stopping print server\n")
  225.   network.tcp.unlisten(PORT)
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement