Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local ser = require("serialization")
- local network = require ("network")
- local fs = require("filesystem")
- local op = component.openprinter
- local extra = require("extralib")
- local VERSION = "1.0"
- local CONFIGFILE = "/etc/printserver.cfg"
- local SERVERNAME = "pserver1"
- local LOGFOLDER = "/var/log/"
- local LOGFILE = "printserver.log"
- local PORT = 9100
- local WARNPAPER = 10
- local WARNCOLOR = 5
- local WARNBLACK = 5
- local TIMERCOUNT = 0.1
- local activeJob = 1
- local queuedJobs = 0
- local queueTable = {}
- local timerEvent = nil
- function ensureLogFolderExists()
- if not fs.exists(LOGFOLDER) then
- fs.makeDirectory(LOGFOLDER)
- end
- end
- function logwrite(str)
- local file = io.open(LOGFOLDER .. "/" .. LOGFILE,"a")
- file:write(str)
- file:close()
- end
- function checkPrinter()
- if op.getPaperLevel() then
- if op.getPaperLevel() < WARNPAPER then
- logwrite(os.date() .. " Running out of paper\n")
- return false
- end
- else
- logwrite(os.date() .. " No paper.\n")
- return false
- end
- if op.getColorInkLevel() then
- if op.getColorInkLevel() < WARNCOLOR then
- logwrite(os.date() .. " Less then 5% color ink left\n")
- return false
- end
- else
- logwrite(os.date() .. " Out off color Ink.\n")
- return false
- end
- if op.getBlackInkLevel() then
- if op.getBlackInkLevel() < WARNBLACK then
- logwrite(os.date() .. " Less then 5% black ink left\n")
- return false
- end
- else
- logwrite(os.date() .. " Out off black Ink.\n")
- return false
- end
- return true
- end
- function timerCallback()
- if checkPrinter() == true then
- if activeJob <= queuedJobs then -- only if there are files waiting
- --print("Job ID : " .. queueTable[activeJob].id)
- --print("From : " .. queueTable[activeJob].client)
- --print("Status : " .. queueTable[activeJob].status)
- --print("Line : " .. queueTable[activeJob].pageLine .. " of " .. queueTable[activeJob].lines)
- --print("........................................................")
- if queueTable[activeJob].status == "queued" then
- queueTable[activeJob].status = "printing" -- set the status of the job to printing
- logwrite(os.date() .. " Starting print of job " .. activeJob .. "\n")
- end
- if queueTable[activeJob].pageLine ==19 then -- Page is full
- op.setTitle(queueTable[activeJob].client)
- op.writeln("<" .. queueTable[activeJob].page .. ">")
- queueTable[activeJob].page = queueTable[activeJob].page + 1 -- page counter for job one up
- queueTable[activeJob].pageLine = 1
- op.print()
- else
- op.writeln(queueTable[activeJob].data[queueTable[activeJob].activeLine])
- queueTable[activeJob].activeLine = queueTable[activeJob].activeLine + 1
- queueTable[activeJob].pageLine = queueTable[activeJob].pageLine + 1
- end
- if queueTable[activeJob].activeLine > queueTable[activeJob].lines then
- queueTable[activeJob].status = "finished"
- local i = queueTable[activeJob].pageLine
- for j=i, 19 do
- op.writeln("")
- end
- op.setTitle(queueTable[activeJob].client)
- op.writeln("<" .. queueTable[activeJob].page .. ">")
- op.print()
- logwrite(os.date() .. " Finshed print of job " .. activeJob .. "\n")
- activeJob = activeJob + 1
- end
- end
- end
- timerEvent = event.timer(TIMERCOUNT, timerCallback)
- end
- local function response(_, type, _a, _b, _c, _d)
- local channel = 0
- local remoteAddr = ""
- local port = 0
- if type == "connection" then
- channel = _a
- remoteAddr = _b
- port =_c
- logwrite(os.date() .. " Connection from " .. remoteAddr .. " ch " .. channel .. " port " .. port .. "\n")
- elseif type == "message" then
- channel = _a
- message = _b
- remoteAddr = _c
- port =_d
- local tmpTable = {}
- local msgTable = ser.unserialize(message)
- logwrite(os.date() .. " " .. remoteAddr .. " send message '" .. msgTable.command .. "' ch " .. channel .." port " .. port .. "\n")
- if msgTable.command == "info" then
- local msgTable ={}
- msgTable["command"] = "info"
- msgTable["version"] = VERSION
- msgTable["jobs"] = queuedJobs
- --print(ser.serialize(msgTable))
- network.tcp.send(channel, ser.serialize(msgTable))
- elseif msgTable.command == "print" then
- queuedJobs = queuedJobs + 1
- logwrite(os.date() .. " " .. remoteAddr .. " queueing job " ..queuedJobs .. " filename : " .. msgTable.filename .. "\n")
- for key,value in pairs(ser.unserialize(msgTable.data)) do
- done = false
- while done == false do
- if value:len() > 30 then
- table.insert(tmpTable,value:sub(1,30))
- value=">"..value:sub(31)
- else
- table.insert(tmpTable,value)
- done = true
- end
- end
- end
- local qTable = {}
- qTable["filename"] = msgTable.filename
- qTable["lines"] = #tmpTable
- qTable["activeLine"] = 1
- qTable["data"] = tmpTable
- qTable["pageLine"] = 1
- qTable["status"] = "queued" -- will be queued, printing or finished
- qTable["client"] = remoteAddr
- qTable["id"] = queuedJobs
- qTable["page"] = 1
- queueTable[queuedJobs] = qTable
- end
- elseif type == "close" then
- channel = _a
- logwrite(os.date() .. " Connection on channel " .. channel .. " closed\n")
- end
- end
- function loadConfig()
- local option = {}
- local configFile = io.open(CONFIGFILE, "r")
- while true do
- line = configFile:read()
- if line == nil then
- break
- else
- if line[1] ~= "#" then
- option = extra.splitString(line,"=") -- extralib sring split
- if option[1] == "LOGFILE" then
- LOGFILE = option[2]
- elseif option[1] == "LOGFOLDER" then
- LOGFOLDER = option[2]
- elseif option[1] == "SERVERNAME" then
- SERVERNAME = option[2]
- elseif option[1] == "PORT" then
- PORT = tonumber(option[2])
- elseif option[1] == "WARNPAPER" then
- WARNPAPER = tonumber(option[2])
- elseif option[1] == "WARNCOLOR" then
- WARNCOLOR = tonumber(option[2])
- elseif option[1] == "WARNBLACK" then
- WARNBLACK = tonumber(option[2])
- elseif option[1] == "TIMERCOUNT" then
- TIMERCOUNT = tonumber(option[2])
- end
- end
- end
- end
- configFile:close()
- end
- function start()
- loadConfig()
- ensureLogFolderExists()
- logwrite(os.date() .. " Starting print server (v" .. VERSION .. ") " .. SERVERNAME .. " at port " .. PORT .. "\n")
- if op == nil then
- logwrite(os.date() .. " No openprinter found!\n")
- logwrite(os.date() .. " Aborting!")
- return false
- end
- network.ip.bind(SERVERNAME)
- network.tcp.listen(PORT)
- event.listen("tcp", response)
- checkPrinter()
- op.clear()
- timerEvent = event.timer(TIMERCOUNT, timerCallback)
- end
- function stop()
- logwrite(os.date() .. " Stopping print server\n")
- network.tcp.unlisten(PORT)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement