2017-04-11 12:00:34 +02:00
|
|
|
#!/usr/bin/lua
|
2017-06-12 12:05:57 +02:00
|
|
|
|
|
|
|
local function log(message)
|
2017-06-13 13:04:09 +02:00
|
|
|
os.execute("logger " .. message)
|
2017-06-12 12:05:57 +02:00
|
|
|
print(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (table.getn(arg) == 0) then
|
2017-06-12 17:20:29 +02:00
|
|
|
print("Usage: ./print-fetch {printerSocket} {remoteURL} {id} [startGcode] [endGCode]")
|
2017-06-12 12:05:57 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
log("starting gcode fetch program")
|
|
|
|
|
2017-04-11 12:00:34 +02:00
|
|
|
package.cpath = package.cpath .. '/usr/lib/lua/?.so'
|
|
|
|
JSON = (loadfile "/usr/share/lua/wifibox/util/JSON.lua")()
|
|
|
|
|
|
|
|
local p3d = require("print3d")
|
|
|
|
|
|
|
|
local printer = p3d.getPrinter(arg[1])
|
|
|
|
|
2017-04-11 12:50:49 +02:00
|
|
|
local remote = arg[2]
|
2017-04-11 12:00:34 +02:00
|
|
|
|
|
|
|
local finished = false
|
|
|
|
|
2017-04-11 17:04:57 +02:00
|
|
|
local id = arg[3]
|
|
|
|
|
2017-06-12 12:05:57 +02:00
|
|
|
log("gcode file id: " .. id)
|
|
|
|
|
2017-04-11 17:04:57 +02:00
|
|
|
local info = JSON:decode(io.popen("wget -qO - " .. remote .. "/info/" .. id):read("*a"))
|
2017-04-11 12:00:34 +02:00
|
|
|
|
|
|
|
local current_line = 0
|
|
|
|
local total_lines = tonumber(info["lines"])
|
|
|
|
local started = false
|
|
|
|
|
2017-06-12 12:05:57 +02:00
|
|
|
log("total lines: " .. total_lines)
|
|
|
|
|
2017-06-13 13:04:09 +02:00
|
|
|
local startCode = nil
|
|
|
|
local endCode = nil
|
2017-06-12 17:20:29 +02:00
|
|
|
|
|
|
|
function countlines(file)
|
|
|
|
return tonumber(io.popen("wc -l < " .. file):read('*a'))
|
|
|
|
end
|
|
|
|
|
|
|
|
function readGCodeArg(argi)
|
|
|
|
local gcodeFile = arg[argi]
|
|
|
|
total_lines = total_lines + countlines(gcodeFile)
|
|
|
|
return io.open(gcodeFile):read('*a')
|
|
|
|
end
|
|
|
|
|
|
|
|
if table.getn(arg) >= 5 then
|
|
|
|
startCode = readGCodeArg(4)
|
|
|
|
endCode = readGCodeArg(5)
|
|
|
|
end
|
|
|
|
|
2017-06-13 13:04:09 +02:00
|
|
|
if startCode ~= nil then
|
|
|
|
log("appending start gcode")
|
|
|
|
printer:appendGcode(startCode)
|
|
|
|
end
|
2017-06-12 17:20:29 +02:00
|
|
|
|
2017-04-11 12:00:34 +02:00
|
|
|
while(not finished)
|
|
|
|
do
|
2017-04-11 17:04:57 +02:00
|
|
|
local f = io.popen("wget -qO - " .. remote .. "/fetch/" .. id .. "/" .. current_line)
|
2017-04-11 12:00:34 +02:00
|
|
|
local line = f:read()
|
|
|
|
while line ~= nil do
|
2017-06-12 12:05:57 +02:00
|
|
|
printer:appendGcode(line, total_lines)
|
2017-04-11 12:00:34 +02:00
|
|
|
current_line = current_line + 1
|
|
|
|
line = f:read()
|
|
|
|
end
|
|
|
|
|
|
|
|
if not started then
|
|
|
|
started = true
|
|
|
|
print("send print start command")
|
|
|
|
printer:startPrint()
|
|
|
|
end
|
|
|
|
|
2017-06-13 13:04:09 +02:00
|
|
|
if current_line >= total_lines then
|
|
|
|
log("finished fetching gcode")
|
|
|
|
if endCode ~= nil then
|
|
|
|
log("appending end gcode")
|
|
|
|
printer:appendGcode(endCode)
|
|
|
|
end
|
|
|
|
finished = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-04-11 12:00:34 +02:00
|
|
|
local accepts_new_gcode = false
|
|
|
|
|
|
|
|
while (not accepts_new_gcode)
|
|
|
|
do
|
|
|
|
local current,buffer,total,bufferSize,maxBufferSize = printer:getProgress()
|
|
|
|
local percentageBufferSize = bufferSize / maxBufferSize
|
|
|
|
|
|
|
|
if percentageBufferSize < 0.8 then
|
|
|
|
print("buffer below 80% capacity, sending new gcode")
|
|
|
|
accepts_new_gcode = true
|
|
|
|
else
|
|
|
|
print("buffer above 80% capacity")
|
|
|
|
os.execute("sleep 10")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|