11#! /usr/bin/lua
22
33local function log (message )
4- os.execute (" logger " .. message )
5- print (message )
4+ os.execute (" logger " .. message )
5+ print (message )
66end
77
88if (table .getn (arg ) == 0 ) then
9- print (" Usage: ./print-fetch {printerSocket} {gcodeServerURL} {id} [startGcode] [endGCode]" )
10- return
9+ print (" Usage: ./print-fetch {printerSocket} {gcodeServerURL} {id} [startGcode] [endGCode]" )
10+ return
1111end
1212
1313log (" starting gcode fetch program" )
@@ -19,8 +19,8 @@ local p3d = require("print3d")
1919
2020local printer = p3d .getPrinter (arg [1 ])
2121if printer == nil then
22- log (" error connecting to printer" )
23- return
22+ log (" error connecting to printer" )
23+ return
2424end
2525
2626local gcodeServer = arg [2 ]
@@ -35,8 +35,8 @@ log("gcode server: " .. gcodeServer)
3535local info = JSON :decode (io.popen (" wget -qO - " .. gcodeServer .. " /info/" .. id ):read (" *a" ))
3636
3737if info == nil then
38- log (" could not retrieve file info" )
39- return
38+ log (" could not retrieve file info" )
39+ return
4040end
4141
4242local current_line = 0
@@ -49,64 +49,64 @@ local startCode = nil
4949local endCode = nil
5050
5151function countlines (file )
52- return tonumber (io.popen (" wc -l < " .. file ):read (' *a' ))
52+ return tonumber (io.popen (" wc -l < " .. file ):read (' *a' ))
5353end
5454
5555function readGCodeArg (argi )
56- local gcodeFile = arg [argi ]
57- return io.open (gcodeFile ):read (' *a' )
56+ local gcodeFile = arg [argi ]
57+ return io.open (gcodeFile ):read (' *a' )
5858end
5959
6060if table .getn (arg ) >= 5 then
61- startCode = readGCodeArg (4 )
62- endCode = readGCodeArg (5 )
61+ startCode = readGCodeArg (4 )
62+ endCode = readGCodeArg (5 )
6363end
6464
6565if startCode ~= nil then
66- log (" appending start gcode" )
67- printer :appendGcode (startCode )
66+ log (" appending start gcode" )
67+ printer :appendGcode (startCode )
6868end
6969
7070while (not finished )
7171do
72- local f = io.popen (" wget -qO - " .. gcodeServer .. " /fetch/" .. id .. " /" .. current_line )
73- local line = f :read ()
74- while line ~= nil do
75- printer :appendGcode (line , total_lines , { seq_number = - 1 , seq_total = - 1 , source = id })
76- current_line = current_line + 1
77- line = f :read ()
78- end
79-
80- if not started then
81- started = true
82- print (" send print start command" )
83- printer :startPrint ()
84- end
85-
86- if current_line >= total_lines then
87- log (" finished fetching gcode" )
88- if endCode ~= nil then
89- log (" appending end gcode" )
90- printer :appendGcode (endCode , total_lines , { seq_number = - 1 , seq_total = - 1 , source = id })
72+ local f = io.popen (" wget -qO - " .. gcodeServer .. " /fetch/" .. id .. " /" .. current_line )
73+ local line = f :read ()
74+ while line ~= nil do
75+ printer :appendGcode (line , total_lines , { seq_number = - 1 , seq_total = - 1 , source = id })
76+ current_line = current_line + 1
77+ line = f :read ()
78+ end
79+
80+ if not started then
81+ started = true
82+ print (" send print start command" )
83+ printer :startPrint ()
84+ end
85+
86+ if current_line >= total_lines then
87+ log (" finished fetching gcode" )
88+ if endCode ~= nil then
89+ log (" appending end gcode" )
90+ printer :appendGcode (endCode , total_lines , { seq_number = - 1 , seq_total = - 1 , source = id })
91+ end
92+ finished = true
93+ break
9194 end
92- finished = true
93- break
94- end
9595
9696
97- local accepts_new_gcode = false
97+ local accepts_new_gcode = false
9898
99- while (not accepts_new_gcode )
100- do
101- local current ,buffer ,total ,bufferSize ,maxBufferSize = printer :getProgress ()
102- local percentageBufferSize = bufferSize / maxBufferSize
99+ while (not accepts_new_gcode )
100+ do
101+ local current ,buffer ,total ,bufferSize ,maxBufferSize = printer :getProgress ()
102+ local percentageBufferSize = bufferSize / maxBufferSize
103103
104- if percentageBufferSize < 0.8 then
105- print (" buffer below 80% capacity, sending new gcode" )
106- accepts_new_gcode = true
107- else
108- print (" buffer above 80% capacity" )
109- os.execute (" sleep 10" )
104+ if percentageBufferSize < 0.8 then
105+ print (" buffer below 80% capacity, sending new gcode" )
106+ accepts_new_gcode = true
107+ else
108+ print (" buffer above 80% capacity" )
109+ os.execute (" sleep 10" )
110+ end
110111 end
111- end
112112end
0 commit comments